2

I have been trying to write token swap scripts for Spookyswap on Fantom (so the transaction formatting should be pre EIP-1559) in python using web3.py.

I have forked Fantom via Hardhat for testing purposes.

The script below, using swapExactETHForTokens, works and results in swapping 30 FTM for MIM.

path = [wFTM, MIM]
contract_txn = contract_SPOOKY_ROUTER.functions.swapExactETHForTokens( 0, path , address_wallet, block['timestamp']+30 ).buildTransaction({
                            # 'type': '0x2',
                            'from': address_wallet,
                            'chainId':  31337,    # FTM 
                            'value' : Web3.toWei(30, 'ether'),
                            'gas': 250000,
                            'gasPrice': w3.eth.gas_price, #Web3.toWei('300', 'gwei'),
                            'nonce': nonce,
})

signed = w3.eth.account.sign_transaction(contract_txn, privatekey)
w3.eth.send_raw_transaction(signed.rawTransaction)
nonce += 1

However, I have not been able to get swapExactTokensForTokens to work despite the two functions supposedly having the same input arguments.

Below is my attempt to get swapExactTokensForTokens to work. (I have tinkered with the parameters and tried moving them around as well.)

Any help would be appreciated.

path = [MIM, fohm]
contract_txn = contract_SPOOKY_ROUTER.functions.swapExactTokensForTokens(num, 0,  path , address_wallet, block['timestamp']+30 ).buildTransaction({
                            # 'type': '0x2',
                            #'from': address_wallet,
                            'chainId':  31337,    # FTM 
                            'value' : Web3.toWei(30, 'ether'),
                            'gas': 250000,
                            'gasPrice': w3.eth.gas_price, #Web3.toWei('300', 'gwei'),
                            'nonce': nonce,
})

signed = w3.eth.account.sign_transaction(contract_txn, privatekey)
w3.eth.send_raw_transaction(signed.rawTransaction)
nonce += 1

The error message is usually the following:

raise ValueError(response["error"])
ValueError: {'code': -32603, 'message': 'Error: Transaction reverted without a reason string', 'data': {'txHash': '0xa32dfefb03e27bb82490e5b8a05957ab87e0097a0b7da997010c239a8a35a294'}}

or

ValueError: {'code': -32603, 'message': "Error: VM Exception while processing transaction: reverted with reason string 'UniswapV2Router: EXPIRED'", 'data': {'txHash': '0x1871c1afb2967895aad61440ec22d64400a1874582f9b45651a1067ac3ca7891'}}

I did approve all relevant contracts before running the blocks of code above

approval_wFTM = contract_wFTM.functions.approve(address_wallet, 2**256-1)
approval_fohm = contract_fohm.functions.approve(address_wallet, 2**256-1)
approval_MIM = contract_MIM.functions.approve(address_wallet, 2**256-1)
1
  • It seems that you're a sending value of 30 ether to swapExactTokensForTokens which is not a payable function
    – Adam Boudj
    Commented Mar 19, 2022 at 15:46

1 Answer 1

-1

I am just as stuck as you are, but i believe it's due to the stable: bool param that needs to be true whenever there is stable in the route, or false when not. I will look at this tomorrow

1

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.