-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsendTokens.php
More file actions
183 lines (159 loc) · 6.04 KB
/
sendTokens.php
File metadata and controls
183 lines (159 loc) · 6.04 KB
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
<?php
require 'vendor/autoload.php';
if (($argc !== 3 && $argc !== 4) || ($argc === 4 && strlen($argv[3]) !== 18)) {
echo "Usage: php sendTokens.php <destinationAddress> <amountInWholeNumber> <optional:amountWith18Zeros>" . PHP_EOL;
echo "Example to send 2 tokens: php sendTokens.php 0xB3F0c9d503104163537Dd741D502117BBf6aF8f1 16" . PHP_EOL;
echo "Example to send 0.2 tokens: php sendTokens.php 0xB3F0c9d503104163537Dd741D502117BBf6aF8f1 0 200000000000000000" . PHP_EOL;
echo "Example to send 2.5 tokens: php sendTokens.php 0xB3F0c9d503104163537Dd741D502117BBf6aF8f1 2 500000000000000000" . PHP_EOL;
exit(1);
}
$destinationAddress = $argv[1];
$amountInWholeNumber = null;
if ($argc === 3) {
$amountInWholeNumber = intval($argv[2]) * (10 ** 18);
} else {
$amountInWholeNumber = intval($argv[2] . $argv[3]);
}
use Web3\Web3;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Contract;
use Web3\Utils;
use Web3p\EthereumTx\Transaction;
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__, '../.env');
$dotenv->load();
$infuraProjectId = $_ENV['INFURA_PROJECT_ID'];
$infuraProjectSecret = $_ENV['INFURA_PROJECT_SECRET'];
$contractAddress = $_ENV['TOKEN_CONTRACT_ADDRESS'];
$fromAccount = $_ENV['SOURCE_ACCOUNT_ADDRESS'];
$fromAccountPrivateKey = $_ENV['SOURCE_ACCOUNT_PRIVATE_KEY'];
$secondsToWaitForReceiptString = $_ENV['SECONDS_TO_WAIT_FOR_RECEIPT'];
$secondsToWaitForReceipt = intval($secondsToWaitForReceiptString);
$factorToMultiplyGasEstimateString = $_ENV['FACTOR_TO_MULTIPLY_GAS_ESTIMATE'];
$factorToMultiplyGasEstimate = intval($factorToMultiplyGasEstimateString);
$chainIds = [
'Mainnet' => 1,
'Ropsten' => 3
];
$infuraHosts = [
'Mainnet' => 'mainnet.infura.io',
'Ropsten' => 'ropsten.infura.io'
];
$chainId = $chainIds[$_ENV['CHAIN_NAME']];
$infuraHost = $infuraHosts[$_ENV['CHAIN_NAME']];
$abi = file_get_contents(__DIR__ . '/../resources/Erc777TokenAbiArray.json');
$contract = new Contract("https://:$infuraProjectSecret@$infuraHost/v3/$infuraProjectId", $abi);
$eth = $contract->eth;
$contract->at($contractAddress)->call('balanceOf', $fromAccount, [
'from' => $fromAccount
], function ($err, $results) use ($contract) {
if ($err !== null) {
echo $err->getMessage() . PHP_EOL;
}
if (isset($results)) {
foreach ($results as &$result) {
$bn = Utils::toBn($result);
echo 'BEFORE fromAccount balance ' . $bn->toString() . PHP_EOL;
}
}
});
$contract->at($contractAddress)->call('balanceOf', $destinationAddress, [
'from' => $destinationAddress
], function ($err, $results) use ($contract) {
if ($err !== null) {
echo $err->getMessage() . PHP_EOL;
}
if (isset($results)) {
foreach ($results as &$result) {
$bn = Utils::toBn($result);
echo 'BEFORE destinationAddress balance ' . $bn->toString() . PHP_EOL;
}
}
});
$rawTransactionData = '0x' . $contract->at($contractAddress)->getData('transfer', $destinationAddress, $amountInWholeNumber);
$transactionCount = null;
$eth->getTransactionCount($fromAccount, function ($err, $transactionCountResult) use(&$transactionCount) {
if($err) {
echo 'getTransactionCount error: ' . $err->getMessage() . PHP_EOL;
} else {
$transactionCount = $transactionCountResult;
}
});
echo "\$transactionCount=$transactionCount" . PHP_EOL;
$transactionParams = [
'nonce' => "0x" . dechex($transactionCount->toString()),
'from' => $fromAccount,
'to' => $contractAddress,
'gas' => '0x' . dechex(8000000),
'value' => '0x0',
'data' => $rawTransactionData
];
$estimatedGas = null;
$eth->estimateGas($transactionParams, function ($err, $gas) use (&$estimatedGas) {
if ($err) {
echo 'estimateGas error: ' . $err->getMessage() . PHP_EOL;
} else {
$estimatedGas = $gas;
}
});
echo "\$estimatedGas=$estimatedGas" . PHP_EOL;
$gasPriceMultiplied = hexdec(dechex($estimatedGas->toString())) * $factorToMultiplyGasEstimate;
echo "\$gasPriceMultiplied=$gasPriceMultiplied" . PHP_EOL;
$transactionParams['gasPrice'] = '0x' . dechex($gasPriceMultiplied);
$transactionParams['chainId'] = $chainId;
$tx = new Transaction($transactionParams);
$signedTx = '0x' . $tx->sign($fromAccountPrivateKey);
$txHash = null;
$eth->sendRawTransaction($signedTx, function ($err, $txResult) use (&$txHash) {
if($err) {
echo 'transaction error: ' . $err->getMessage() . PHP_EOL;
} else {
$txHash = $txResult;
}
});
echo "\$txHash=$txHash" . PHP_EOL;
$txReceipt = null;
echo "Waiting for transaction receipt";
for ($i=0; $i <= $secondsToWaitForReceipt; $i++) {
echo '.';
$eth->getTransactionReceipt($txHash, function ($err, $txReceiptResult) use(&$txReceipt) {
if($err) {
echo 'getTransactionReceipt error: ' . $err->getMessage() . PHP_EOL;
} else {
$txReceipt = $txReceiptResult;
}
});
if ($txReceipt) {
echo PHP_EOL;
break;
}
sleep(1);
}
$txStatus = $txReceipt->status;
echo "\$txStatus=$txStatus" . PHP_EOL;
$contract->at($contractAddress)->call('balanceOf', $fromAccount, [
'from' => $fromAccount
], function ($err, $results) use ($contract) {
if ($err !== null) {
echo $err->getMessage() . PHP_EOL;
}
if (isset($results)) {
foreach ($results as &$result) {
$bn = Utils::toBn($result);
echo 'AFTER fromAccount balance ' . $bn->toString() . PHP_EOL;
}
}
});
$contract->at($contractAddress)->call('balanceOf', $destinationAddress, [
'from' => $destinationAddress
], function ($err, $results) use ($contract) {
if ($err !== null) {
echo $err->getMessage() . PHP_EOL;
}
if (isset($results)) {
foreach ($results as &$result) {
$bn = Utils::toBn($result);
echo 'AFTER destinationAddress balance ' . $bn->toString() . PHP_EOL;
}
}
});