-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathswap.php
178 lines (159 loc) · 5.34 KB
/
swap.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
<?php
require('./exampleBase.php');
require('./utils.php');
use Web3\Utils;
use Web3\Contract;
use Web3p\EthereumTx\Transaction;
// get chain id
$chainId = getChainId($web3->net);
$contract = new Contract($web3->provider, $uniV2Json->abi);
$ownAccount = $testAddress;
echo 'Start to swap eth to tokens' . PHP_EOL;
// swap eth to token
$contract = $contract->at($testUNIRouterAddress);
$amountIn = Utils::toWei('0.01', 'ether');
$amountOut;
$path = [
'0xb4fbf271143f4fbf7b91a5ded31805e42b2208d6',
'0x1f9840a85d5af5bf1d1762f925bdaddc4201f984'
];
// get swap amounts
$getAmountsOutRes = getUniV2AmountsOut($contract, $amountIn, $path, [
'from' => $testAddress
]);
echo 'Expect token output: ' . $getAmountsOutRes[1]->toString() . PHP_EOL;
$amountOut = $getAmountsOutRes[1];
$estimatedGas;
$gasPrice = '0x' . Utils::toWei('50', 'gwei')->toHex();
$contract->estimateGas('swapExactETHForTokens', $amountOut, $path, $testAddress, 1700000000, [
'from' => $testAddress,
'value' => '0x' . $amountIn->toHex()
], function ($err, $result) use (&$estimatedGas) {
if ($err !== null) {
throw $err;
}
$estimatedGas = $result->multiply(Utils::toBn(2));
});
$data = $contract->getData('swapExactETHForTokens', $amountOut, $path, $testAddress, 1700000000);
$nonce = getNonce($eth, $ownAccount);
$transaction = new Transaction([
'nonce' => '0x' . $nonce->toHex(),
'gas' => '0x' . $estimatedGas->toHex(),
'gasPrice' => $gasPrice,
'data' => '0x' . $data,
'value' => '0x' . $amountIn->toHex(),
'chainId' => $chainId,
'to' => $testUNIRouterAddress
]);
$transaction->sign($testPrivateKey);
$txHash = '';
$eth->sendRawTransaction('0x' . $transaction->serialize(), function ($err, $transaction) use ($eth, $ownAccount, &$txHash) {
if ($err !== null) {
echo 'Error: ' . $err->getMessage();
return;
}
echo 'Swap eth 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 eth" . PHP_EOL;
$token = new Contract($web3->provider, $erc20Json->abi);
$token = $token->at($path[1]);
// swap amountIn and amountOut
$tmp = $amountIn;
$amountIn = $amountOut;
$amountOut = $tmp;
$allowance;
$token->call('allowance', $testAddress, $testUNIRouterAddress, [
'from' => $testAddress
], function ($err, $result) use ($path, &$amountOut, &$allowance) {
if ($err !== null) {
throw $err;
}
if ($result && count($result) > 0) {
$allowance = $result[0];
}
});
// approve
if ($allowance->compare($amountIn) < 0) {
$estimatedApproveGas;
$token->estimateGas('approve', $testUNIRouterAddress, $amountIn, [
'from' => $testAddress,
], function ($err, $result) use (&$estimatedApproveGas) {
if ($err !== null) {
throw $err;
}
$estimatedApproveGas = $result->multiply(Utils::toBn(2));
});
$data = $token->getData('approve', $testUNIRouterAddress, $amountIn);
$nonce = $nonce->add(Utils::toBn(1));
$transaction = new Transaction([
'nonce' => '0x' . $nonce->toHex(),
'gas' => '0x' . $estimatedApproveGas->toHex(),
'gasPrice' => $gasPrice,
'data' => '0x' . $data,
'chainId' => $chainId,
'to' => $path[1]
]);
$transaction->sign($testPrivateKey);
$txHash = '';
$eth->sendRawTransaction('0x' . $transaction->serialize(), function ($err, $transaction) use ($eth, $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]
];
// get swap amounts
$getAmountsOutRes = getUniV2AmountsOut($contract, $amountIn, $newPath, [
'from' => $testAddress
]);
echo 'Expect token output: ' . $getAmountsOutRes[1]->toString() . PHP_EOL;
$amountOut = $getAmountsOutRes[1];
$contract->estimateGas('swapExactTokensForETH', $amountIn, $amountOut, $newPath, $testAddress, 1700000000, [
'from' => $testAddress
], function ($err, $result) use (&$estimatedGas) {
if ($err !== null) {
throw $err;
}
$estimatedGas = $result->multiply(Utils::toBn(2));
});
$data = $contract->getData('swapExactTokensForETH', $amountIn, $amountOut, $newPath, $testAddress, 1700000000);
$nonce = $nonce->add(Utils::toBn(1));
$transaction = new Transaction([
'nonce' => '0x' . $nonce->toHex(),
'gas' => '0x' . $estimatedGas->toHex(),
'gasPrice' => $gasPrice,
'data' => '0x' . $data,
'chainId' => $chainId,
'to' => $testUNIRouterAddress
]);
$transaction->sign($testPrivateKey);
$txHash = '';
$eth->sendRawTransaction('0x' . $transaction->serialize(), function ($err, $transaction) use ($eth, $ownAccount, &$txHash) {
if ($err !== null) {
echo 'Error: ' . $err->getMessage();
return;
}
echo 'Swap tokens to eth 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 eth!" . PHP_EOL;