-
Notifications
You must be signed in to change notification settings - Fork 10
/
balance.php
128 lines (84 loc) · 2.54 KB
/
balance.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
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//require_once 'globals.php';
function getBalance($address,$network) {
//$CurrentNetwork=$network;
// echo $network;
if ($network=="Bitcoin") {
//echo "Bitcoin:".$address;
$url = "https://api.blockcypher.com/v1/btc/main/addrs/$address";
$response = file_get_contents($url);
$data = json_decode($response, true);
if (isset($data['balance'])) {
$balance = $data['balance'] / 100000000; // Convert from satoshis to BTC
// echo $balance;
return $balance;
} else {
echo 'bitcoin Error: ' ;
//return null;
return 0;
}
}else{
/*
$rpcUrl="";
if ($network=="Ethereum") {
$rpcUrl="https://eth.drpc.org";
}else if ($network=="BinanceBNB") {
$rpcUrl="https://bsc-dataseed1.binance.org:443";
}*/
$Networks = array(
"Ethereum" => "https://eth.drpc.org",
"BinanceBNB" => "https://bsc-dataseed1.binance.org:443",
"PolygonMatic" => "https://polygon.llamarpc.com",
"AvalancheAvax" => "https://api.avax.network/ext/bc/C/rpc",
"CronosCRO" => "https://evm.cronos.org",
"FantomFTM" => "https://rpc.ankr.com/fantom"
);
$rpcUrl = $Networks[$network];
// Create a JSON-RPC request to get the balance of the address
$request = json_encode([
'jsonrpc' => '2.0',
'id' => 1,
'method' => 'eth_getBalance',
'params' => [
$address,
'latest' // You can use 'latest' to get the latest balance
]
]);
// Initialize cURL session
$ch = curl_init($rpcUrl);
// Set cURL options
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($request)
]);
// Execute the cURL request
$response = curl_exec($ch);
// Check for cURL errors
if (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch);
exit;
}
// Close cURL session
curl_close($ch);
// Decode the JSON response
$responseData = json_decode($response, true);
// Check if the response is valid
if (isset($responseData['result'])) {
// The balance is returned in Wei, you can convert it to MATIC or other units as needed
$balanceWei = hexdec($responseData['result']);
$balance = $balanceWei / 1e18; // Convert Wei to MATIC
// echo "Balance of $address : $balance<br>";
return $balance;
} else {
echo "Error: Unable to fetch balance<br>";
return 0;
}
}
}
?>