0

I Am trying to make a swap on uniswap using its routerV3's exactInputSingle function like this:

from web3 import Web3, HTTPProvider
from web3.gas_strategies.time_based import fast_gas_price_strategy #♦, medium_gas_price_strategy, slow_gas_price_strategy
from web3.middleware import geth_poa_middleware
import json
from time import time

w3 = Web3(HTTPProvider(rpc))

w3.eth.set_gas_price_strategy(fast_gas_price_strategy)
w3.middleware_onion.inject(geth_poa_middleware, layer=0)

router_address = '0xE592427A0AEce92De3Edee1F18E0157C05861564'
router_abi = json.loads(...)
erc20_abi = json.loads(...)

wallet_address = 'XXX'
private_key = 'XXX'

router_contract = w3.eth.contract(address=universal_router_address, abi=router_abi)

weth_address = w3.to_checksum_address('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2')
usdc_address = w3.to_checksum_address('0xdac17f958d2ee523a2206206994597c13d831ec7')

weth_contract = w3.eth.contract(address=weth_address, abi=erc20_abi)
usdc_contract = w3.eth.contract(address=usdc_address, abi=erc20_abi)

print(weth_contract.functions.balanceOf(wallet_address).call() / 10**18) # 0.2392

amountIn = int(0.004*10**18)

ExactInputSingleParams = (
    weth_address,
    usdc_address,
    10000,
    wallet_address,
    int(time())+10*60
    amountIn,
    1,
    0
)

func = router_contract.functions.exactInputSingle(ExactInputSingleParams)

params = {
    "from": wallet_address,
    "value": amountIn,
    'gasPrice': w3.eth.gas_price,
    'gas': 53000,
    "nonce": w3.eth.get_transaction_count(wallet_address),
    'chainId': w3.eth.chain_id,
}

txn = func.build_transaction(params)
signed_tx = w3.eth.account.sign_transaction(txn, private_key)

txn_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)

But that transaction keeps failling. Any idea why?

0

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.