forked from web3p/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswap.php
More file actions
141 lines (130 loc) · 4.43 KB
/
swap.php
File metadata and controls
141 lines (130 loc) · 4.43 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
<?php
require('./exampleBase.php');
require('./utils.php');
use Web3\Utils;
use Web3\Contract;
use Web3p\EthereumTx\Transaction;
$contract = new Contract($web3->provider, $testUNIAbi);
$ownAccount = $testAddress;
$ownBalance = getBalance($eth, $ownAccount);
// transfer some ether to test account
$value = Utils::toWei('10', 'ether');
echo 'Start to swap bnb to tokens' . PHP_EOL;
// swap bnb to token
$contract = $contract->at($testUNIRouterAddress);
$path = [
'0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c',
'0x758d08864fb6cce3062667225ca10b8f00496cc2'
];
$amountIn = '0x' . Utils::toWei('0.001', 'ether')->toHex();
$contract->call('swapExactETHForTokens', $amountIn, $path, $testAddress, 1700000000, [
'value' => $amountIn
], function ($err, $result) use ($path) {
if ($err !== null) {
throw $err;
}
if ($result && isset($result['amounts']) && count($result['amounts']) == count($path)) {
echo 'Expect token output: ' . $result['amounts'][1]->toString() . PHP_EOL;
}
});
$estimatedGas = '0x' . Utils::toWei('200', 'kwei')->toHex();
$gasPrice = '0x' . Utils::toWei('5', 'gwei')->toHex();
$contract->estimateGas('swapExactETHForTokens', $amountIn, $path, $testAddress, 1700000000, [
'from' => $testAddress,
'value' => $amountIn
], function ($err, $result) use (&$estimatedGas) {
if ($err !== null) {
throw $err;
}
$estimatedGas = '0x' . $result->toHex();
});
$data = $contract->getData('swapExactETHForTokens', $amountIn, $path, $testAddress, 1700000000);
$nonce = getNonce($eth, $ownAccount);
$transaction = new Transaction([
'nonce' => '0x' . $nonce->toHex(),
'gas' => $estimatedGas,
'gasPrice' => $gasPrice,
'data' => '0x' . $data,
'value' => $amountIn,
'chainId' => $chainId,
'to' => $testUNIRouterAddress
]);
$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 'Swap bnb to tokens tx hash: ' . $transaction . PHP_EOL;
$txHash = $transaction;
});
$transaction = confirmTx($eth, $txHash);
if (!$transaction) {
throw new Error('Transaction was not confirmed.');
}
echo "Transaction was confirmed, let's swap back to bnb" . PHP_EOL;
$token = new Contract($web3->provider, $testAbi);
$token = $token->at($path[1]);
$data = $token->getData('approve', $testUNIRouterAddress, $amountIn);
$nonce = $nonce->add(Utils::toBn(1));
$transaction = new Transaction([
'nonce' => '0x' . $nonce->toHex(),
'gas' => $estimatedGas,
'gasPrice' => $gasPrice,
'data' => '0x' . $data,
'chainId' => $chainId,
'to' => $path[1]
]);
$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 'Approve tx hash: ' . $transaction . PHP_EOL;
$txHash = $transaction;
});
$transaction = confirmTx($eth, $txHash);
if (!$transaction) {
throw new Error('Transaction was not confirmed.');
}
// swap back
$newPath = [
$path[1],
$path[0]
];
$contract->estimateGas('swapExactTokensForETH', $amountIn, 0, $newPath, $testAddress, 1700000000, [
'from' => $testAddress
], function ($err, $result) use (&$estimatedGas) {
if ($err !== null) {
throw $err;
}
$estimatedGas = '0x' . $result->toHex();
});
$data = $contract->getData('swapExactTokensForETH', $amountIn, 0, $newPath, $testAddress, 1700000000);
$nonce = $nonce->add(Utils::toBn(1));
$transaction = new Transaction([
'nonce' => '0x' . $nonce->toHex(),
'gas' => $estimatedGas,
'gasPrice' => $gasPrice,
'data' => '0x' . $data,
'chainId' => $chainId,
'to' => $testUNIRouterAddress
]);
$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 'Swap tokens to bnb tx hash: ' . $transaction . PHP_EOL;
$txHash = $transaction;
});
$transaction = confirmTx($eth, $txHash);
if (!$transaction) {
throw new Error('Transaction was not confirmed.');
}
echo "Congratulation! The tokens did swap back to bnb!" . PHP_EOL;