-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblock.php
91 lines (79 loc) · 2.36 KB
/
block.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
<?php
header('Access-Control-Allow-Origin: *');
include './checkAdmin.php';
include './Webhook.php';
require_once 'libs/jsonRPCClient.php';
$gethRPC = new jsonRPCClient('http://127.0.0.1:8545');
if(isset($_REQUEST['hash'])){
header('Content-Type: application/json');
echo blockByHash($_REQUEST['hash'],$gethRPC);
} else if(isset($_REQUEST['block']) && isset($_REQUEST['index'])){
header('Content-Type: application/json');
echo getTransactionInBlock($_REQUEST['block'],$_REQUEST['index'],$gethRPC);
} else if(isset($_REQUEST['block'])){
header('Content-Type: application/json');
echo blockByNumber($gethRPC,$_REQUEST['block']);
} else {
header('Content-Type: application/json');
echo blockNumber($gethRPC);
}
function getRPCResponse($result){
if(isset($result['result'])){
return $result['result'];
} else {
throw new Exception($result['error']['message']);
}
}
function getDefaultResponse(){
$data['error'] = false;
$data['msg'] = "";
$data['data'] = "";
return $data;
}
function blockNumber($gethRPC){
$data = getDefaultResponse();
try {
$ret = getRPCResponse($gethRPC->eth_blockNumber());
$data['data'] = array("blockNumber"=>hexdec($ret));
}
catch (exception $e) {
$data['error'] = true;
$data['msg'] = $e->getMessage();
}
return json_encode($data);
}
function blockByNumber($gethRPC,$blcnb){
$data = getDefaultResponse();
try {
$ret = getRPCResponse($gethRPC->eth_getBlockByNumber($blcnb,true));
$data["data"]=$ret;
}
catch (exception $e) {
$data['error'] = true;
$data['msg'] = $e->getMessage();
}
return json_encode($data);
}
function blockByHash($gethRPC,$blchash){
$data = getDefaultResponse();
try {
$ret = getRPCResponse($gethRPC->eth_getBlockByHash($blcnb,true));
$data["data"]=$ret;
}
catch (exception $e) {
$data['error'] = true;
$data['msg'] = $e->getMessage();
}
return json_encode($data);
}
function getTransactionInBlock($block,$index,$gethRPC) {
$data = getDefaultResponse();
try {
$data = getRPCResponse($gethRPC->eth_getTransactionByBlockNumberAndIndex($block, $index),"pending");
}
catch (exception $e) {
$data['error'] = true;
$data['msg'] = $e->getMessage();
}
return json_encode($data);
}