-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtx.php
69 lines (64 loc) · 2.36 KB
/
tx.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
<?php
require('./exampleBase.php');
require('./utils.php');
use Web3\Utils;
use Web3\Contract;
use Web3p\EthereumTx\Transaction;
use Web3p\EthereumTx\EIP1559Transaction;
$mainAccount = $testAddress2;
$ownAccount = $testAddress;
$ownBalance = getBalance($eth, $ownAccount);
// send 0.01 bnb back to main account
$nonce = getNonce($eth, $ownAccount);
$value = Utils::toWei('0.01', 'ether');
$transaction = new Transaction([
'nonce' => '0x' . $nonce->toHex(),
'to' => $mainAccount,
'gas' => '0xfd240',
'gasPrice' => '0x' . Utils::toWei('5', 'gwei')->toHex(),
'value' => '0x' . $value->toHex(),
'chainId' => $chainId
]);
$transaction->sign($testPrivateKey);
$txHash = '';
$eth->sendRawTransaction('0x' . $transaction->serialize(), function ($err, $transaction) use ($eth, $mainAccount, $ownAccount, &$txHash) {
if ($err !== null) {
echo 'Error: ' . $err->getMessage();
return;
}
echo 'Tx hash: ' . $transaction . PHP_EOL;
$txHash = $transaction;
});
$transaction = confirmTx($eth, $txHash);
if (!$transaction) {
throw new Error('Transaction was not confirmed.');
}
echo "Transaction has been confirmed. " . " transaction hash: " . $txHash . " block number: " . $transaction->blockNumber . PHP_EOL;
// BSC didn't support EIP1559 yet
// $nonce = $nonce->add(Utils::toBn(1));
// $value = Utils::toWei('0.01', 'ether');
// $transaction = new EIP1559Transaction([
// 'nonce' => '0x' . $nonce->toHex(),
// 'to' => $mainAccount,
// 'gas' => '0xfd240',
// 'gasPrice' => '0x' . Utils::toWei('5', 'gwei')->toHex(),
// 'value' => '0x' . $value->toHex(),
// 'chainId' => $chainId,
// 'maxPriorityFeePerGas' => '0x1',
// 'maxFeePerGas' => Utils::toWei('5', 'gwei')->toHex(),
// ]);
// $transaction->sign($testPrivateKey);
// $txHash = '';
// $eth->sendRawTransaction('0x' . $transaction->serialize(), function ($err, $transaction) use ($eth, $mainAccount, $ownAccount, &$txHash) {
// if ($err !== null) {
// echo 'Error: ' . $err->getMessage();
// return;
// }
// echo 'Tx hash: ' . $transaction . PHP_EOL;
// $txHash = $transaction;
// });
// $transaction = confirmTx($eth, $txHash);
// if (!$transaction) {
// throw new Error('Transaction was not confirmed.');
// }
// echo "Transaction has been confirmed. " . " transaction hash: " . $txHash . " block number: " . $transaction->blockNumber . PHP_EOL;