0

First of all, sorry for my English.

Using web3.py I managed to execute a swap from an "X" coin to WETH from a UniswapV3 pool. However, I miss an error when I try to execute a swap from WETH to an "X" coin. I don't know what I'm failing at, could you help me?

This is the code that I have:

def transactionUniswap(account_id, amountIn, amountOutMin, pairs):
    try:
        # Connection to ETH
        w3 = connectToWeb3()
        # Get all data
        amountIn = w3.toWei(amountIn, "ether")
        amountOutMin = w3.toWei(amountOutMin, "ether")
        contractUniswap = getContract(w3)
        account = getAccount(w3, account_id)
        gasPrice = getGasPrice(w3)
        nonce = getNonce(w3, account_id)
        privateKey = getPrivateKey(account_id)

        abi = get_abi()

        import time
        # Calculate timestamp
        current_ts = int(time.time()) + (30*60)

        params_function = {
            'tokenIn':pairs[0],
            'tokenOut':pairs[1],
            'fee':3000,
            'recipient':account.address,
            'deadline':current_ts,
            'amountIn':amountIn,
            'amountOutMinimum':amountOutMin,
            'sqrtPriceLimitX96':0
        }

        # Create the transaction
        calls = []
        swap = contractUniswap.encodeABI( fn_name = 'exactInputSingle', args=[params_function])


        # checks if token to swap is WETH
        value = 0 if pairs[0] != '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' else amountIn
        

        txArgs = {
            "to":contractUniswap.address,
            "from": account.address,
            "data":swap,
            "gas":200000,
            "gasPrice":int(gasPrice),
            "nonce":nonce,
            "value":value
        }
        
        # Sign the transaction
        signed_tx = w3.eth.account.signTransaction(txArgs, privateKey)

        send_transaction = w3.eth.sendRawTransaction(signed_tx.rawTransaction)



        return True
    

    except Exception as e:
        raise e
        return e
    

And here you can check the hash, with the input data and more: https://etherscan.io/tx/0xc495901215f34af3b92bdd69df0686fe8ae8e7a5f14937862faf5690c2f07099

SOLUTION

At the end, I found a library called uniswap-python that makes the swap really fast and easy, with only few lines of code:

from uniswap import Uniswap
        uniswap = Uniswap(address=YOUR_ADDRESS_ACCOUNT, private_key=_YOUR_ACCOUNT_PRIVATE_KEY, version=CAN_BE_3_2_1, provider=YOUR_NODE)

5
  • I looked at the tx, your X token raised an error ""ERC20: transfer amount exceeds allowance". So you need to approve the router first.
    – 0xSanson
    Feb 4, 2023 at 17:56
  • How exactly? Making a multicall? Or how? Thanks you so much for your answer Feb 6, 2023 at 8:11
  • You need to make a transaction before the swap, with to=token.address and as data the ABI-encoded approve(router.address, max_uint256).
    – 0xSanson
    Feb 6, 2023 at 11:23
  • @MiguelPayá Please, add the solution as an answer. So the question can be marked as resolved.
    – Ismael
    Feb 9, 2023 at 16:04
  • @Ismael done! Thanks you Mar 22, 2023 at 7:51

1 Answer 1

2

SOLUTION

At the end, I found a library called uniswap-python that makes the swap really fast and easy, with only few lines of code:

from uniswap import Uniswap
        uniswap = Uniswap(address=YOUR_ADDRESS_ACCOUNT, private_key=_YOUR_ACCOUNT_PRIVATE_KEY, version=CAN_BE_3_2_1, provider=YOUR_NODE)

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.