-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patheth.rpc.php
276 lines (245 loc) · 7.3 KB
/
eth.rpc.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
267
268
269
270
271
272
273
274
275
276
<?php
class EnryEWT_ETH {
// for product
private static $address = "http://1.2.3.4";
private static $port = 8888;
// for private test
// private static $address = "http://localhost";
// private static $port = 8545;
// Warning! don't modify this parmeretes.
private static $v = "2.0";
private static $h = 1e18;
private static $hs = "1000000000000000000";
// get net version
public static function getNetVersion()
{
$data = self::generateRequestData("net_version");
return self::request(json_encode($data));
}
public static function blockNumber()
{
$data = self::generateRequestData("eth_blockNumber");
return self::request(json_encode($data));
}
public static function newAccount($pwd = 'yh02sz55y8h8szy629h2s0z')
{
$params = [$pwd];
$data = self::generateRequestData("personal_newAccount", $params);
return self::request(json_encode($data));
}
//eth的账号余额
public static function getBalance($addr, $tag = "latest")
{
self::validateEthAddress($addr);
$params = [$addr, $tag];
$data = self::generateRequestData("eth_getBalance", $params);
return self::request(json_encode($data));
}
//查询所有地址
public static function accounts()
{
$data = self::generateRequestData("eth_accounts");
return self::request(json_encode($data));
}
public static function unlockAccounts($addr, $pwd, $duration = 60)
{
$params = [$addr, $pwd, $duration];
$data = self::generateRequestData("personal_unlockAccount", $params);
return self::request(json_encode($data));
}
//查询子地址时给出的是1
public static function lockAccounts($addr)
{
$params = [$addr];
$data = self::generateRequestData("personal_lockAccount", $params);
return self::request(json_encode($data));
}
public static function filterchanges($filterid)
{
$params = [$filterid];
$data = self::generateRequestData("eth_getFilterLogs", $params);
return self::request(json_encode($data));
}
public static function newFilter($address)
{
return self::_newFilter($address);
}
public static function _newFilter($address, $fromBlock = 0, $toBlock = 0, $topics = [])
{
$params = [
"address" => $address,
];
if ($fromBlock > 0) {
$params['fromBlock'] = $fromBlock;
}
if ($toBlock > 0) {
$params['toBlock'] = $toBlock;
}
if (!empty($topics)) {
$params['topics'] = $topics;
}
$data = self::generateRequestData("eth_newFilter", [(object) $params]);
//var_dump(json_encode($data));
return self::request(json_encode($data));
}
// transfer
public static function transaction($from, $to, $value, $pwd){
self::validateEthAddress($from);
self::validateEthAddress($to);
$unlockInfo = json_decode(self::unlockAccounts($from, $pwd));
if (!isset($unlockInfo->result) || !$unlockInfo->result ){
return $unlockInfo;
}
$transferInfo = json_decode(self::sendTransaction($from, $to, $value));
self::lockAccounts($from);
return $transferInfo;
}
// manual transfer, unlock first, then sendTransfer, and lock account last;
public static function sendTransaction($from, $to, $value)
{
return self::_sendTransaction($from, $to, $value);
}
// send transaction full pars
public static function _sendTransaction($from, $to, $value, $gas = 0, $gasPrice = 0, $data = "", $nonce = 0)
{
$params = [
"from" => $from,
"to" => $to,
"value" => self::toHexWei($value),
];
if ($gas > 0) {
$params['gas'] = $gas;
}
if ($gasPrice > 0) {
$params['gasPrice'] = $gasPrice;
}
if (strlen($data) > 0) {
$params['data'] = $data;
}
if ($nonce > 0) {
$params['nonce'] = $nonce;
}
$data = self::generateRequestData("eth_sendTransaction", [(object) $params]);
//echo "<pre>";
//print_r(json_encode($data));
return self::request(json_encode($data));
}
public static function getTransactionByHash($hash)
{
self::validateEthAddress($hash);
$params = [$hash];
$data = self::generateRequestData("eth_getTransactionByHash", $params);
return self::request(json_encode($data));
}
public static function toDec($hexAmount)
{
return hexdec($hexAmount);
}
//hash查询结果中的value值 转换为10进制
public static function toEth($hexAmount)
{
return self::toDec($hexAmount) / self::$h;
}
public static function toWei($eth)
{
return $eth * self::$h;
}
public static function toHex($eth)
{
return "0x" . dechex($eth);
}
public static function toHexWei($eth)
{
//return "0x" . dechex(self::toWei($eth));
return "0x" . base_convert(self::calc($eth, self::$hs, "mul"), 10, 16);
}
private static function validateEthAddress($addr)
{
if (strlen($addr) < 10 || substr($addr, 0, 2) != "0x") {
echo "Invalid address:". $addr;
exit();
}
}
private static function generateRequestData($method, $params = [])
{
$data = [
"jsonrpc" => self::$v,
"method" => $method,
"params" => $params,
"id" => mt_rand(1, 100000000),
];
return $data;
}
private static function request($post_data)
{
if (strlen(self::$address) <= 0 || self::$port <= 0) {
echo "eth client address or port error";
exit();
}
$url = self::$address . ":" . self::$port;
return self::post($url, $post_data);
}
// for big number
private static function calc($m, $n, $x)
{
$errors = array(
'被除数不能为零',
'负数没有平方根'
);
switch ($x) {
case 'add':
$t = bcadd($m, $n);
break;
case 'sub':
$t = bcsub($m, $n);
break;
case 'mul':
$t = bcmul($m, $n);
break;
case 'div':
if ($n != 0) {
$t = bcdiv($m, $n);
} else {
return $errors[0];
}
break;
case 'pow':
$t = bcpow($m, $n);
break;
case 'mod':
if ($n != 0) {
$t = bcmod($m, $n);
} else {
return $errors[0];
}
break;
case 'sqrt':
if ($m >= 0) {
$t = bcsqrt($m);
} else {
return $errors[1];
}
break;
}
$t = preg_replace("/\..*0+$/", '', $t);
return $t;
}
// curl for request
private static function post($url, $post_data = '', $timeout = 10)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
if ($post_data != '') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, false);
$file_contents = curl_exec($ch);
curl_close($ch);
return $file_contents;
}
}
?>