0

I'm using the below code to try and make a swap on Sepolia. I've got ETH in my wallet and have found a token that has had liquidity added. Depending what I set the minOutput to I get 'Fail with error 'UniswapV2Router: EXCESSIVE_INPUT_AMOUNT' or 'Fail with error 'UniswapV2Library: INSUFFICIENT_OUTPUT_AMOUNT' errors. Would someone please be able to suggest what I'm doing wrong? I have referred to this page for troubleshooting but to no avail: Failed token purchase transactions on uniswap with web3.py

Thank you in advance,I have included a tx receipt from the terminal output. I notice it has 'Contract Address: None' in there, could that be the issue? Sample etherscan tx: https://sepolia.etherscan.io/tx/0xf34fdd35db67ebec8854f8216e5c303c60ae68cbfd2c1a666425d123c693ac12

Transaction Hash: AttributeDict({'blockHash': HexBytes('0xa65d59036c7c39a97c770c90a63b50cef93d5863571750cde23dbce478400052'), 'blockNumber': 4969362, 'contractAddress': None, 'cumulativeGasUsed': 7384983, 'effectiveGasPrice': 2614217107, 'from': '0x1a6711833C06d9D98B76515CCc3a8a81C3308df3', 'gasUsed': 30812, 'logs': [], 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), 'status': 0, 'to': '0xC532a74256D3Db42D0Bf7a0400fEFDbad7694008', 'transactionHash': HexBytes('0xf34fdd35db67ebec8854f8216e5c303c60ae68cbfd2c1a666425d123c693ac12'), 'transactionIndex': 58, 'type': 0})
from web3 import Web3
from decimal import Decimal
import time


w3 = Web3(Web3.HTTPProvider('https://eth-sepolia.g.alchemy.com/v2/MY-KEY'))
if not w3.is_connected():
    print("Not connecting t node.")
    exit()


private_key = "PRIVATE_KEY"
wallet_address = "0x1a6711833C06d9D98B76515CCc3a8a81C3308df3"
uniswap_router_address = "0xC532a74256D3Db42D0Bf7a0400fEFDbad7694008"
token_to_buy = "0x0b71A7DfBd9a2F249895D6f963206a3Fc56aeC16" #testBot found on sepolia with liq added
amount_to_buy = 5

uniswap_v2_router_abi = [COPY PASTED ABI FROM ROUTER CONTRACT]
uniswap_router_contract = w3.eth.contract(address=uniswap_router_address, abi=uniswap_v2_router_abi)


gas_price = w3.eth.gas_price
gas_limit = 2000000
nonce = w3.eth.get_transaction_count(wallet_address)
print(f"The nonce is: {nonce}")


weth = w3.to_checksum_address("0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9") #weth ca

transaction = uniswap_router_contract.functions.swapETHForExactTokens(
    0, 
    [weth, token_to_buy],
    wallet_address,
    int(time.time() + 120)  # uint256
).build_transaction({
    'from': wallet_address,
    'gas': gas_limit,
    'gasPrice': gas_price,
    'nonce': nonce,
})


signed_transaction = w3.eth.account.sign_transaction(transaction, private_key)
print(signed_transaction)

1 Answer 1

2

Because you use function swapETHForExactTokens, thus, you have to define "value" when build_transaction to send ETH with transaction. That why in the transaction hash you can see 0 at value as shown in the picture below.

enter image description here

You can fix the code to this and it will work fine

transaction = uniswap_router_contract.functions.swapETHForExactTokens(
    0, 
    [weth, token_to_buy],
    wallet_address,
    int(time.time() + 120)  # uint256
).build_transaction({
    'from': wallet_address,
    'gas': gas_limit,
    'value': w3.toWei(amount_to_buy, 'ether'), // add this line
    'gasPrice': gas_price,
    'nonce': nonce,
})

Hope it helps.

2
  • Thanks @tqdpham96 that fixed the value part! Strange I couldn't find that in other examples on SE. I now get the insufficient output amount error, but this I believe may be due to the pool itself. Would you agree? sepolia.etherscan.io/tx/… Thanks!
    – stooks
    Dec 27, 2023 at 22:28
  • FYI for others. Couldn't for the life of me make this work, switched router and tokens out for for Goerli testnet and it works now. Must be some quirk with sepolia, maybe low liquidity pools etc.
    – stooks
    Dec 28, 2023 at 10:06

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.