Getting failed transactions with and infura

Hi there,
I’m getting failed transactions(reverted) when I try to buy tokens from uniswap v2 using the swapExactEthForToken method in my build transaction with I have added my code below.
I have tried this on ropsten, rinkeby and even mainnet.
I have test ETH on both test networks also.

Any help will be greatly appreciated.

from web3 import Web3
import json
import config
import time
import pprint

infura_url = config.INFURA_URL_TESTNET
web3 = Web3(Web3.HTTPProvider(infura_url))


uniswap address and abi

uniswap_router = ‘0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D’
uniswap_factory = web3.toChecksumAddress(‘0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f’)
uniswapABI = ‘I pasted the uniswap v2 abi here’
uniswap_factory_abi = json.loads(uniswapABI)

sender_address = “0x39DC57cf562736D62bE25EFC1B37ce43dEaf51Bc”

balance = web3.eth.get_balance(sender_address)
eth_bal = web3.fromWei(balance, ‘ether’)
print('My Test Eth Balance is: ', eth_bal)

tokenToBuy = web3.toChecksumAddress(“0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984”)
spend = web3.toChecksumAddress(“0xc778417E063141139Fce010982780140Aa0cD5Ab”) # wbnb contract

Setup the Uniswap contract

contract = web3.eth.contract(address=uniswap_factory, abi=uniswap_factory_abi)

nonce = web3.eth.get_transaction_count(sender_address)

start = time.time()

uniswapv2_txn = contract.functions.swapExactETHForTokens(
10000000000, # set to 0, or specify minimum amount of tokeny you want to receive - consider decimals!!!
[spend, tokenToBuy],
(int(time.time()) + 100000)
‘from’: sender_address,
‘value’: web3.toWei(0.01, ‘ether’),
‘gas’: 2000000,
‘nonce’: nonce,

signed_txn = web3.eth.account.sign_transaction(uniswapv2_txn, private_key=config.private)
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)

Hi @ekokotasamuel70 and welcome to our Infura community :smiley:

Please note that in order to get the transaction processed you have to add test ETH for the gas fee to be paid. You can add free ETH to your testnet wallet from here ->

1 Like

Hi @Flaviu,

I already have a ropsten balance. I have edited my initial code in the question above to print out my test eth balance.

Here is the output I got along with the transaction hash.

My Test Eth Balance is: 0.554408414852423556
Maybe I am missing something in my code or infura setup? I really appreciate you looking into this for me.

And oh… Thanks for the warm welcome to the community. :blush:
I hope to contribute as much as I can.

Hi @Leiya_Kenney, can you possibly help me with this also?
I know it may not be an infura problem but any help will be greatly appreciated.

Hey @ekokotasamuel70, can you send us the exact error message you’re getting when the transaction gets reverted? Hoping that might help point us to where the issue might be stemming from.

Hi @Leiya_Kenney, here’s the screenshot of one failed transaction on ropsten testnet.

Hi @Leiya_Kenney and @Flaviu

Glad to inform you that I’ve found the error in my code and fixed it. I was building my contract wrongly.
I apparently used the uniswap_factory address in building my contract, instead of using the uniswap router address.(Rookie mistake, I guess. )
I also used the contract address of WETH in my spend variable.

It now works fine.

1 Like

Hi @ekokotasamuel70

I’m happy that you managed to resolve your issue. If you have any other questions please let us know.