-
Notifications
You must be signed in to change notification settings - Fork 4
/
swap_tokens.php
143 lines (127 loc) · 4.2 KB
/
swap_tokens.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
<?php
require('./exampleBase.php');
require('./utils.php');
use Web3\Utils;
use Web3\Contract;
use Web3p\EthereumTx\Transaction;
$contract = new Contract($web3->provider, $uniV2Json->abi);
$ownAccount = $testAddress;
$ownBalance;
// get chain id
$chainId = getChainId($web3->net);
echo 'Start to swap tokens' . PHP_EOL;
// swap usdc to dai on polygon
$contract = $contract->at($testUNIRouterAddress);
$path = [
'0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174',
'0x8f3cf7ad23cd3cadbd9735aff958023239c6a063'
];
$amountIn = Utils::toWei('0.01', 'mwei');
$amountOut = Utils::toWei('9.6', 'ether');
$nonce = getNonce($eth, $ownAccount);
// checkout balance and approved allowance
$token = new Contract($web3->provider, $erc20Json->abi);
$token = $token->at($path[0]);
$allowance;
$token->call('balanceOf', $testAddress, [
'from' => $testAddress
], function ($err, $result) use ($path, &$amountOut, &$ownBalance) {
if ($err !== null) {
throw $err;
}
if ($result && count($result) > 0) {
$ownBalance = $result[0];
}
});
if ($ownBalance->compare($amountIn) < 0) {
throw new Error('Balance not enough');
}
$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];
}
});
$gasPrice = '0x' . Utils::toWei('50', 'gwei')->toHex();
// 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);
$transaction = new Transaction([
'nonce' => '0x' . $nonce->toHex(),
'gas' => '0x' . $estimatedApproveGas->toHex(),
'gasPrice' => $gasPrice,
'data' => '0x' . $data,
'chainId' => $chainId,
'to' => $path[0]
]);
$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.');
}
$nonce = $nonce->add(Utils::toBn(1));
}
// get swap amounts
$getAmountsOutRes = getUniV2AmountsOut($contract, $amountIn, $path, [
'from' => $testAddress
]);
echo 'Expect token output: ' . $getAmountsOutRes[1]->toString() . PHP_EOL;
$amountOut = $getAmountsOutRes[1];
$estimatedGas;
$contract->estimateGas('swapExactTokensForTokens', $amountIn, $amountOut, $path, $testAddress, 1700000000, [
'from' => $testAddress
], function ($err, $result) use (&$estimatedGas) {
if ($err !== null) {
throw $err;
}
if ($result) {
echo 'Estimate gas: ' . $result->toString() . PHP_EOL;
}
$estimatedGas = $result->multiply(Utils::toBn(2));
});
$data = $contract->getData('swapExactTokensForTokens', $amountIn, $amountOut, $path, $testAddress, 1700000000);
$nonce = getNonce($eth, $ownAccount);
$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) {
throw $err;
}
echo 'Swap 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" . PHP_EOL;