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()
|