-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsc-api-fetch.php
executable file
·266 lines (235 loc) · 9.29 KB
/
bsc-api-fetch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/*
Plugin Name: BSCScan simple token stats
Plugin URI: https://github.com/sjmc11/bscscan-wp-plugin
Description: Periodically fetch token stats from bscscan api and update ACF values. Requires ACF Pro.
Author: JC
Version: 0.0.1
Author URI: https://github.com/sjmc11/
*/
/**
* This plugin relies on ACF to provide meta fields, an admin page, db entries etc..
* In future this could be integrated directly.
* This serves to cover the core functionality of fetching from the bscscan API every x hours
*/
class bscScan {
// public static $tokenDecimal;
// public static $tokenAddress;
//
//
// function __construct() {
// bscScan::$tokenDecimal = $GLOBALS['tokenDecimal'];
// bscScan::$tokenAddress = $GLOBALS['tokenAddress'];
// bscScan::$tokenAddress = "0x5xbeea03923266ca8af5ad3821b2j150b33a25a5";
// }
static function handleActivate() {
// Bail if ACF is not activated
if (!is_plugin_active('advanced-custom-fields/acf.php')) {
die('This plugin requires Advanced Custom fields to be active.');
}
// Bail if activating from network, or bulk
if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
die('Does not support remote activate or multi-site');
}
// Setup schedule
if (! wp_next_scheduled ( 'bsc_fetch_event' )) {
wp_schedule_event(time(), 'hourly', 'bsc_fetch_event');
}
if(get_field('bscscan_api_key', 'option') && get_field('bscscan_token_address', 'option') && get_field('bscscan_burn_address', 'option')){
self::fetchTokenStats();
}
}
static function handleDeactivate() {
// Destroy schedule
$timestamp = wp_next_scheduled("bsc_fetch_event");
wp_unschedule_event( $timestamp,"bsc_fetch_event");
}
/**
* Add ACF admin page
* @return void
*/
static function registerAcfAdminPage(){
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'BSC Token Stats',
'menu_title' => 'BSC Token Stats',
'menu_slug' => 'bscscan-stats',
'capability' => 'edit_posts',
'redirect' => false,
'icon_url' => 'dashicons-chart-pie',
'position' => 34
));
}
if( function_exists('acf_add_local_field_group') ){
acf_add_local_field_group(array (
'key' => 'bscscan_stats',
'title' => 'BSC Token Stats',
'fields' => array (
array (
'key' => 'total_supply',
'label' => 'Total supply',
'name' => 'total_supply',
'type' => 'number',
'required' => 0,
'wrapper' => array (
'width' => '33',
),
'placeholder' => '',
'readonly' => 0,
),
array (
'key' => 'total_burned',
'label' => 'Total burned',
'name' => 'total_burned',
'type' => 'number',
'required' => 0,
'wrapper' => array (
'width' => '33',
),
'placeholder' => '',
'readonly' => 0,
),
array (
'key' => 'circ_supply',
'label' => 'Circulating supply',
'name' => 'circ_supply',
'type' => 'number',
'required' => 0,
'wrapper' => array (
'width' => '33',
),
'placeholder' => '',
'readonly' => 0,
),
array (
'key' => 'bscscan_api_key',
'label' => 'BscScan API key',
'name' => 'bscscan_api_key',
'type' => 'text',
'required' => 1,
'wrapper' => array (
'width' => '33',
),
'placeholder' => '',
'readonly' => 0,
),
array (
'key' => 'bscscan_token_address',
'label' => 'BscScan Token Address',
'name' => 'bscscan_token_address',
'type' => 'text',
'required' => 1,
'wrapper' => array (
'width' => '33',
),
'placeholder' => '',
'readonly' => 0,
),
array (
'key' => 'bscscan_burn_address',
'label' => 'BscScan Token Burn Address',
'name' => 'bscscan_burn_address',
'type' => 'text',
'required' => 1,
'wrapper' => array (
'width' => '33',
),
'placeholder' => '',
'default' => '0x000000000000000000000000000000000000dead',
'readonly' => 0,
),
),
'location' => array (
array (
array (
'param' => 'options_page',
'operator' => '==',
'value' => 'bscscan-stats',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
));
}
}
/**
* @return void
*/
static function fetchTokenStats(){
self::fetchCircSupply();
self::fetchBurnAmount();
}
/**
* Fetch circulating supply
* @return string|void
*/
private static function fetchCircSupply(){
$tokenDecimal = 9;
$tokenAddress = get_field('bscscan_token_address', 'option');
$apiUrl = "https://api.bscscan.com/api?module=stats&action=tokensupply&contractaddress=" . $tokenAddress . "&apikey=" . get_field('bscscan_api_key', 'option');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $apiUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "cURL Error #:" . $err;
} else {
$bscanResponse = json_decode($response, true);
update_field('total_supply', $bscanResponse['result'] / (10 ** $tokenDecimal), 'option');
}
}
/**
* Fetch burn address amount
* @return string|void
*/
private static function fetchBurnAmount(){
$tokenDecimal = 9;
$tokenAddress = get_field('bscscan_token_address', 'option');
$burnAddress = get_field('bscscan_burn_address', 'option');
$apiUrl = "https://api.bscscan.com/api?module=account&action=tokenbalance&contractaddress=" . $tokenAddress . "&address=" . $burnAddress . "&apikey=" . get_field('bscscan_api_key', 'option');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $apiUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "cURL Error #:" . $err;
} else {
$bscanResponse = json_decode($response, true);
update_field('total_burned', $bscanResponse['result'] / (10 ** $tokenDecimal), 'option');
update_field('circ_supply', get_field('total_supply', 'option') - get_field('total_burned', 'option'), 'option');
}
}
}
register_activation_hook( __FILE__, array( 'bscScan', 'handleActivate' ) );
register_deactivation_hook( __FILE__, array( 'bscScan', 'handleDeactivate' ) );
add_action( 'acf/init', array( 'bscScan', 'registerAcfAdminPage'));
add_action('bsc_fetch_event', array( 'bscScan', 'fetchTokenStats'));
// Uncomment to fetch stats on refresh
//add_action('init', array( 'bscScan', 'fetchTokenStats'));