-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAct.php
203 lines (177 loc) · 4.51 KB
/
Act.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
<?php
/**
*
*/
use JsonRPC\Client;
class Act {
var $client;
function __construct($rpc_server) {
if (!$this->client) {
$this->client = new Client($rpc_server);
$this->client->authentication(ACT_RPC_USERNAME, ACT_RPC_PASSWORD);
}
}
private function exec($procedure, $param = []) {
try {
$res = $this->client->execute($procedure, $param);
return $res;
} catch (JsonRPC\Exception\ConnectionFailureException $e) {
$this->_log($e->getMessage());
return false;
} catch (JsonRPC\Exception\ResponseException $e) {
$this->_log($e->getMessage());
return false;
}
}
/**
* @desc 获取当前区块编号
*/
function getBlockNumber() {
$result = $this->exec('blockchain_get_block_count');
return hexdec($result);
}
/**
* @desc 根据编号获取块内容
*/
function getBlockByNumber($block_number) {
if (!$block_number) {
return false;
}
$result = $this->exec('blockchain_get_block', [$block_number]);
return $result;
}
function getEvent($block_number, $trx_id) {
$result = $this->exec('blockchain_get_events', [$block_number, $trx_id]);
return $result;
}
/**
* @desc 获取当前网络内容
*/
function getInfo() {
$result = $this->exec('get_info');
return $result;
}
/**
* @desc 新建钱包
*/
function createWallet($name, $password) {
$result = $this->exec('wallet_create', ['wallet_name' => $name, 'password' => $password]);
return $result;
}
function walletInfo() {
$result = $this->exec('wallet_get_info');
return $result;
}
/**
* @desc 打开钱包
*/
function openWallet($name) {
$result = $this->exec('wallet_open', [$name]);
return $result;
}
/**
* @desc 解锁钱包
*/
function unlockWallet($timeout, $password) {
$result = $this->exec('wallet_unlock', [$timeout, $password]);
return $result;
}
/**
* @desc 获取某个账户的交易记录
*/
function getAccountTransaction($name) {
$this->openWallet(ACT_WALLET_USERNAME);
$result = $this->exec('wallet_account_transaction_history', [$name]);
return $result;
}
/**
* @desc 获取交易费
*/
function getTransactionFee() {
$this->openWallet(ACT_WALLET_USERNAME);
$result = $this->exec('wallet_get_transaction_fee');
return $result;
}
/**
* @desc 发起交易信息
*/
function transferToAddress($from_account_name, $to_address, $amount, $message = '') {
$this->openWallet(ACT_WALLET_USERNAME);
$this->unlockWallet(120, ACT_WALLET_PASSWORD);
$result = $this->exec('wallet_transfer_to_address', [
$amount, 'ACT', $from_account_name, $to_address, $message,
]);
return $result;
}
/**
* @desc创建账户
*/
function createAccount($username) {
$this->openWallet(ACT_WALLET_USERNAME);
$this->unlockWallet(120, ACT_WALLET_PASSWORD);
$result = $this->exec('wallet_account_create', [$username]);
return $result;
}
/**
* @desc 获取交易信息
*/
function getTransaction($transaction_id) {
$result = $this->exec('wallet_get_transaction', [$transaction_id]);
return $result;
}
/**
* @desc 获取某个账户的余额
*/
function getAccountBalance($account) {
$this->openWallet(ACT_WALLET_USERNAME);
$result = $this->exec('wallet_account_balance', [$account]);
return $result;
}
/**
* @desc获取交易的结果ID
*/
function getContractResult($trx_id) {
$result = $this->exec('blockchain_get_contract_result', [$trx_id]);
return $result;
}
/**
* @desc 调用合约
*/
function callContract($contract_address, $account_name, $function_name, $param, $asset_symbol, $cost_ceil = 1) {
$result = $this->exec('call_contract', [$contract_address, $account_name, $function_name, $param, $asset_symbol, $cost_ceil]);
return $result;
}
function transferBin($from_account_name, $to_address, $amount) {
$result = $this->callContract(BIN_CONTRACT_ADDRESS, $from_account_name, 'transfer_to', $to_address . '|' . $amount, 'ACT', 1);
return $result;
}
/**
* @desc 获取交易信息
*/
function blockchainTransaction($trx_id) {
if (!$trx_id) {
return false;
}
$result = $this->exec('blockchain_get_transaction', [$trx_id]);
return $result;
}
/**
* @desc 合约交易信息
*/
function prettyTransaction($trx_id) {
$result = $this->exec('blockchain_get_pretty_contract_transaction', [$trx_id]);
return $result;
}
/**
* @desc 内部数值转换成ACT
*/
function toAct($number) {
return $number / 100000;
}
function _log($msg) {
$err = date('Y-m-d H:i:s') . "\n";
$err .= $msg . "\n";
$err .= "---------------------------\n";
error_log($err, 3, '/home/log/act_rpc.log');
}
}