我有一个问题,这个带有SushiRouter的事务不能工作,它还写了一个错误ImportError:无法从'eth_abi‘ImportError导入名称'encode’。
sushi_abi = json.load(open('router_sushi_abi.json'))
sushi_arbitrum_address = arb_w3.to_checksum_address('0x9c6522117e2ed1fe5bdb72bb0ed5e3f2bde7dbe0')
#sushi_optimism_address = opt_w3.to_checksum_address('0xB0D502E938ed5f4df2E681fE6E419ff29631d62b')
sushi_arbitrum_contract = arb_w3.eth.contract(address=sushi_arbitrum_address, abi=sushi_abi)
def swapRouter(wallet, amount, tokenout):
account = arb_w3.eth.account.from_key(wallet)
address = account.address
nonce = arb_w3.eth.get_transaction_count(address)
value = arb_w3.to_wei(amount, 'ether')
#min_amount = value - (value * 5) // 1000
encoded_eth = eth_abi.encode_single('address', eth_address)
encoded_weth = eth_abi.encode_single('address', weth_address)
encoded_token = eth_abi.encode_single('address', tokenout)
route = encoded_eth + encoded_weth + encoded_token
approve = arb_w3.to_checksum_address('0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE')
swap_txn = sushi_arbitrum_contract.functions.processRoute(eth_adress,value,tokenout, 0 ,address,route).build_transaction({
'from': address,
'value': value,
'gasPrice': arb_w3.eth.gas_price,
'nonce': arb_w3.eth.get_transaction_count(address),
})
signed_swap_txn = arb_w3.eth.account.sign_transaction(swap_txn, wallet)
swap_txn_hash = arb_w3.eth.send_raw_transaction(signed_swap_txn.rawTransaction)
print(f"SUSHI SWAP | TRANS ACCEPTED: https://arbiscan.io/tx/{swap_txn_hash.hex()}")发布于 2023-05-02 11:57:52
您说您的错误是cannot import name 'encode' from 'eth_abi',但是在您的代码中您使用的是不推荐的eth_abi.encode_single,这样就不会使用不同的代码来解码单个还是多个。您应该使用eth_abi.encode作为v4。
看看文档,这应该会有帮助:
encoded_eth = eth_abi.encode(['address'], [eth_address])
encoded_weth = eth_abi.encode(['address'], [weth_address])https://ethereum.stackexchange.com/questions/149659
复制相似问题