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)
30 ether
toswapExactTokensForTokens
which is not a payable function