0%

solidity | 通过合约进行 pancakeswap 交易

这里通过 bsctestpancakeswap 进行测试。

首先添加一下 pancakeswap 的流动性,自己创建一个币,然后添加流动性。

这里选择的 token

然后和 BNB 添加流动性

目标就是,通过合约进行 token 兑换 BNB 的操作。

合约的代码如下

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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

// 合约调用 pancakeswap
// 合约转出任意代币

interface functionContract {
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function balanceOf(address owner) external view returns (uint);
function allowance(address owner, address spender) external view returns (uint);
function approve(address spender, uint value) external returns (bool);
}


contract MultiTrade {

constructor() {
}

function tokenSwap(uint32 count, address[] calldata cexAddress, uint[] calldata amountIns, uint[] calldata amountOutMins, address[][] memory paths, address[] calldata tos, uint deadlines) public {
uint amountIn = amountIns[0];
for (uint i = 0; i < count; i++) {
address swapAddress = cexAddress[i];
address[] memory path = paths[i];
address base = path[0];
address token = path[path.length - 1];
address to = tos[i];
uint amountOut = amountOutMins[i];
uint deadline = deadlines;
functionContract routerCex = functionContract(swapAddress);
functionContract baseToken = functionContract(base);
functionContract tokenContract = functionContract(token);
if(baseToken.allowance(address(this),swapAddress) == 0){
baseToken.approve(swapAddress,type(uint256).max);
}
routerCex.swapExactTokensForTokens(amountIn,amountOut,path,to,deadline);
amountIn = tokenContract.balanceOf(address(this));
}
}
}

部署的合约地址在

互动脚本

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
from web3 import Web3

from Constant.Strategy.StrategyConstant import StrategyType
import time

abi = """[
{
"inputs": [
{
"internalType": "uint32",
"name": "count",
"type": "uint32"
},
{
"internalType": "address[]",
"name": "cexAddress",
"type": "address[]"
},
{
"internalType": "uint256[]",
"name": "amountIns",
"type": "uint256[]"
},
{
"internalType": "uint256[]",
"name": "amountOutMins",
"type": "uint256[]"
},
{
"internalType": "address[][]",
"name": "paths",
"type": "address[][]"
},
{
"internalType": "address[]",
"name": "tos",
"type": "address[]"
},
{
"internalType": "uint256",
"name": "deadlines",
"type": "uint256"
}
],
"name": "tokenSwap",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
}
]"""


class Multiswap:
name = StrategyType.MULTISWAP.value

def __init__(self, strategyEngine):
self.web3 = Web3(Web3.HTTPProvider('https://data-seed-prebsc-1-s1.binance.org:8545/'))
self.strategyEngine = strategyEngine
self.multiswap = self.web3.eth.contract(
address=Web3.toChecksumAddress("0xddb73917773c5ef46952245e0EDdCF150eb8b889"), abi=abi)

def hello_world(self):
nonce = self.web3.eth.getTransactionCount("0xD1B260aba3bA6f3D3099725b1b1C7cdD12581974")
transaction = self.multiswap.functions.tokenSwap(
1,
# pancakeswap bsc testnet router 地址
[Web3.toChecksumAddress("0x9ac64cc6e4415144c455bd8e4837fea55603e5c3")],
[3000000000000000],
[0],
[[
# token 在 bsc testnet 的地址
Web3.toChecksumAddress("0xd3b21a0197de488ca3b7fd9f7eb6f9ce6ae4ebfa"),
# wbnb 在 bsc testnet 的地址
Web3.toChecksumAddress("0xae13d989dac2f0debff460ac112a837c89baa7cd")
]],
[
Web3.toChecksumAddress("0xD1B260aba3bA6f3D3099725b1b1C7cdD12581974")
],
int(time.time()) + 1000
).buildTransaction({
'gas': 24991838,
'gasPrice': self.web3.toWei('10', 'gwei'),
'nonce': nonce,
})
signed_tx = self.web3.eth.account.signTransaction(transaction,
"private")
txn_hash = self.web3.eth.sendRawTransaction(signed_tx.rawTransaction)
print('txn_hash:', Web3.toHex(txn_hash))


if __name__ == '__main__':
l = Multiswap(None)
l.hello_world()
请我喝杯咖啡吧~