-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.php
74 lines (68 loc) · 1.87 KB
/
utils.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
<?php
function getBalance($eth, $account) {
$balance = 0;
$eth->getBalance($account, function ($err, $rawBalance) use (&$balance) {
if ($err !== null) {
throw $err;
}
$balance = $rawBalance;
});
return $balance;
}
function getNonce($eth, $account) {
$nonce = 0;
$eth->getTransactionCount($account, function ($err, $count) use (&$nonce) {
if ($err !== null) {
throw $err;
}
$nonce = $count;
});
return $nonce;
}
function getTransactionReceipt($eth, $txHash) {
$tx;
$eth->getTransactionReceipt($txHash, function ($err, $transaction) use (&$tx) {
if ($err !== null) {
throw $err;
}
$tx = $transaction;
});
return $tx;
}
function getChainId($net) {
$version;
$net->version(function ($err, $ver) use (&$version) {
if ($err !== null) {
throw $err;
}
$version = $ver;
});
return $version;
}
function confirmTx($eth, $txHash) {
$transaction = null;
while (!$transaction) {
$transaction = getTransactionReceipt($eth, $txHash);
if ($transaction) {
return $transaction;
} else {
echo "Sleep one second and wait transaction to be confirmed" . PHP_EOL;
sleep(1);
}
}
}
// getUniV2AmountsOut
function getUniV2AmountsOut ($contract, $amountIn, $path, $txOptions) {
$amountOut = null;
$contract->call('getAmountsOut', $amountIn, $path, $txOptions, function ($err, $result) use ($path, &$amountOut) {
if ($err !== null) {
throw $err;
}
if ($result && isset($result['amounts']) && count($result['amounts']) == count($path)) {
$amountOut = $result['amounts'];
} else {
throw new Error('failed to call getAmountsOut');
}
});
return $amountOut;
}