1

I need to sign two different transactions:

token_contract = web3.eth.contract(address=web3.to_checksum_address(TOKEN_ADDRESS), abi=json.loads(erc20_abi))
approve_trx = token_contract.functions.approve(getenv("ROUTER_ADDRESS"), my_out).build_transaction({
    'nonce': web3.eth.get_transaction_count(wallet.address),
    'from': wallet.address,
    'maxFeePerGas': int(trx["maxFeePerGas"], 16) + web3.to_wei(getenv("BRIBE_GWEI"), "gwei") if "maxFeePerGas" in trx else web3.to_wei(getenv("BRIBE_GWEI"), "gwei"),
    'maxPriorityFeePerGas': int(trx["maxPriorityFeePerGas"], 16) + web3.to_wei(getenv("BRIBE_GWEI"), "gwei") if "maxPriorityFeePerGas" in trx else web3.to_wei(getenv("BRIBE_GWEI"), "gwei")
})

and

sell_trx = router_contract.functions.swapExactTokensForETH(my_out, 0, [TOKEN_ADDR, WETH_ADDR], wallet.address, int(time.time()) + 30).build_transaction({
      'nonce': web3.eth.get_transaction_count(wallet.address),
      'from': wallet.address,
      'maxFeePerGas': int(trx["maxFeePerGas"], 16) + web3.to_wei(getenv("BRIBE_GWEI"), "gwei") if "maxFeePerGas" in trx else web3.to_wei(getenv("BRIBE_GWEI"), "gwei"),
      'maxPriorityFeePerGas': int(trx["maxPriorityFeePerGas"], 16) + web3.to_wei(getenv("BRIBE_GWEI"), "gwei") if "maxPriorityFeePerGas" in trx else web3.to_wei(getenv("BRIBE_GWEI"), "gwei")
})

and then I want to sign them with:

signed_approve = web3.eth.account.sign_transaction(approve_trx, getenv("PRIV_KEY"))
signed_sell = web3.eth.account.sign_transaction(sell_trx, getenv("PRIV_KEY"))

However, build_transaction of the sell trx raises the error: execution reverted: TransferHelper: TRANSFER_FROM_FAILED

This is correct since I did not sent the first transaction yet, but is there a way to sing both instead of send the approve and wait for it?

1 Answer 1

0

If I understand your issue correctly, signing both the approve and swap transactions simultaneously using the web3 library appears to be impossible. Nevertheless, there are two alternative approaches you can explore:

#1: Smart Contract with Swap Functionality

Create a smart contract featuring a function designed for swapping. This function should inherently include both the approve and swap functionalities within it (ERC821 may help).

#2: Single Approval with a Substantial Token Amount

Basically, you just need to approve only one time with a significantly large token allowance. Once this approval is done, other approvals become unnecessary as the existing allowance is already ample for subsequent swap transactions.

1
  • OK thank you for the info! Commented Dec 23, 2023 at 17:19

Your Answer

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

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