I have a problem, this transaction with SushiRouter does not work, and it also writes an error ImportError: cannot import name 'encode' from 'eth_abi' (C:\Users\PC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\eth_abi_init_.py)

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

1 Answer 1


You say your error is cannot import name 'encode' from 'eth_abi', but in your code you use eth_abi.encode_single which was deprecated so as to not use different code for decoding single vs multiple. You should use eth_abi.encode as of v4.

Looking at the docs, this should help:

    encoded_eth = eth_abi.encode(['address'], [eth_address])
    encoded_weth = eth_abi.encode(['address'], [weth_address])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.