Im trying to swap WETH -> USDC in the Sepolia Testnet using Uniswap Router02 contract.

Firstly I minted 0.5 ETH from the Faucet then created a Python script that uses Brownie (had to add the Sepolia testnet by hand, here are the connection details):

├─id: sepolia
├─chainid: 11155111
├─explorer: https://api-sepolia.etherscan.io/api
└─host: https://sepolia.infura.io/v3/[INFURA_API_KEY]

Next is the Python script, note that I had to import the ERC20 ABI locally to load the USDC contract since the code in the explorer is not verified.

import time
import json
from brownie import *
import os

def load_config():
    config_file_path = 'config.json'  # Adjust the path as needed

    with open(config_file_path, 'r') as config_file:
        config_data = json.load(config_file)

    return config_data

def erc20_config():
    config_file_path = 'erc20_abi.json'  # Adjust the path as needed

    with open(config_file_path, 'r') as config_file:
        config_data = json.load(config_file)

    return config_data

# Load configuration
config = load_config()
erc20 = erc20_config()
user = accounts.load('test_account')

user_balance_before = weth.balanceOf(user.address) / 10**weth.decimals()
print(f"User WETH balance before: {user_balance_before} ETH")

User WETH balance before: 0.019779979849524138 ETH

# Define contract addresses on Goerli
router_address = '0xC532a74256D3Db42D0Bf7a0400fEFDbad7694008'
weth_address = '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9'
usdc_address = '0xf08A50178dfcDe18524640EA6618a1f965821715'
router = Contract.from_explorer(router_address)
weth = Contract.from_explorer(weth_address)
#usdc = Contract.from_explorer(usdc_address)
usdc = Contract.from_abi(name="USDC Token",address=usdc_address, abi=erc20)

weth_balance_before = weth.balanceOf(user.address) / 10**weth.decimals()
usdc_balance_before = usdc.balanceOf(user.address) / 10**usdc.decimals()

print(f"User WETH balance before: {weth_balance_before} WETH")
print(f"User USDC balance before: {usdc_balance_before} USDC")
weth.approve(router.address, 499830654504108713, {'from': user})

User WETH balance before: 0.019779979849524138 WETH
User USDC balance before: 0.0 USDC
Transaction sent: 0x59ec909dc5ade115dd345f51f51d27427739794defdce6cf560a0b5b4375ab75
Gas price: 1.761260845 gwei Gas limit: 29135 Nonce: 4
WETH.approve confirmed Block: 5335731 Gas used: 26144 (89.73%)

So the actual picture of this account is:

  • 499830654504108713 ## WETH RouterV2 ALLOWANCE

  • 19779979849524137 ## Test-account WETH balance

  • 6593326616508046 ## Trade size

If I'm not mistaken the amountIn should be enough and the amountOut is literally whatever (0.000001 USDC) but when I try to make the transaction:

router.swapExactTokensForTokens(6593326616508046,1, [weth.address, usdc.address], user.address, 1000*int(time.time()+30), {'from':user.address})

I get the following error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/Javier/opt/anaconda3/lib/python3.8/site-packages/brownie/network/contract.py", line 1861, in __call__
    return self.transact(*args)
  File "/Users/Javier/opt/anaconda3/lib/python3.8/site-packages/brownie/network/contract.py", line 1734, in transact
    return tx["from"].transfer(
  File "/Users/Javier/opt/anaconda3/lib/python3.8/site-packages/brownie/network/account.py", line 644, in transfer
    receipt, exc = self._make_transaction(
  File "/Users/Javier/opt/anaconda3/lib/python3.8/site-packages/brownie/network/account.py", line 727, in _make_transaction
    raise VirtualMachineError(e) from None
  File "/Users/Javier/opt/anaconda3/lib/python3.8/site-packages/brownie/exceptions.py", line 93, in __init__
    raise ValueError(str(exc)) from None
ValueError: Gas estimation failed: 'execution reverted'. This transaction will likely revert. If you wish to broadcast, you must set the gas limit manually.

I've tried playing around with the parameters but have not been able to make the transaction go through. Could someone help me understand what am i doing wrong?


Your Answer

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

Browse other questions tagged or ask your own question.