0
path = [TETHER, wAVAX, TIME]

contract_txn =  contract_TJ_ROUTER.functions.swapExactTokensForTokens( Web3.toWei(380.27, 'ether'), Web3.toWei(0, 'ether'), path , address_wallet, block['timestamp']+30 ).buildTransaction({
                                'type': '0x2',
                                'chainId': 31337,                            
                                'gas': 8000000,
                                'maxFeePerGas': 2 * block['baseFeePerGas'],  # required for dynamic fee transactions
                                'maxPriorityFeePerGas': 0,  # required for dynamic fee transactions
                                'nonce': nonce,
    })


signed = w3.eth.account.sign_transaction(contract_txn, privatekey)
tx_hash =w3.eth.send_raw_transaction(signed.rawTransaction)

Above is the transaction sign formatting I have been using. I am trying to swap Tether for TIME via wAVAX. (I am trying this transaction on Avalanche, an EVM chain).

I was able to successfully implement swapExactAVAXForTokens function to go from wAVAX to Tether. So I think I have a rough idea of how EIP-1559 works and calling functions works.

However, when running the block of code above, I get the following error:

 File "traderjoe_swap.py", line 169, in <module>
    tx_hash =w3.eth.send_raw_transaction(signed.rawTransaction)
  File "/home/blackwidow/.local/lib/python3.8/site-packages/web3/eth.py", line 698, in send_raw_transaction
    return self._send_raw_transaction(transaction)
  File "/home/blackwidow/.local/lib/python3.8/site-packages/web3/module.py", line 57, in caller
    result = w3.manager.request_blocking(method_str,
  File "/home/blackwidow/.local/lib/python3.8/site-packages/web3/manager.py", line 187, in request_blocking
    return self.formatted_response(response,
  File "/home/blackwidow/.local/lib/python3.8/site-packages/web3/manager.py", line 168, in formatted_response
    raise ValueError(response["error"])
ValueError: {'code': -32603, 'message': "Error: VM Exception while processing transaction: reverted with reason string 'TransferHelper: TRANSFER_FROM_FAILED'", 'data': {'txHash': '0x5c3531ff85064d8e8458881631d5b7ed60a8ff8160b7d09923c748682d8d496a'}}

I have looked at the chain explorer to see some example execution of swapExactTokensForTokens to make sure I had the right input arguments.

P.S. When trying to implement swapExactAVAXForTokens to go from wAVAX to Tether to TIME, the transaction does not throw an error and it does subtract the right amount of AVAX but I do not receive either Tether nor TIME in return.

1 Answer 1

0

TRANSFER_FROM_FAILED message means that you have not approved the contract to transfer your ERC20 token

you can check that by going to the DEX app in your browser, and finding the pair you're trying to trade, then see if it is approved or not - you might also want to check whether the "router" address is the same as the one you're using in your code

I've had similar issue with Uniswap V3 where I approved the contract (Uniswap) to transfer my ERC20 via their website. Everything worked fine, so I worked on the rest of the code, however while I was working, they upgraded their "router" (main contract) and thus changed it's address. When I deployed a new test token after and approved it via uniswap website, my TXs were failing and I had no idea why - I had to update uniswap router address in my code, because the old one did not have an approval to transfer my new tokens

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.