0

I am trying to use web3 to swap tokens in uniswap. My swap ( ETH -> Token) is working correctly, but for some reason the reverse swap ( Token -> ETH) does not seem to work. I am not able to find the issue as it does not throw any error. However, my tokens are withdrawn but I dont get any ETH back.

Transactions in question:

  1. I first buy DAI from ETH. It works perfectly
  2. I then approve the DAI to be sold.
  3. I then try to swap DAI back to ETH. The transaction succeeds but I dont see the ETH in my account.

Here is my code for token to ETH conversion


MIN_ERC20_ABI = json.loads(
    """
    [{"constant": true, "inputs": [], "name": "name", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "approve", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transferFrom", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "_owner", "type": "address" } ], "name": "balanceOf", "outputs": [ { "name": "balance", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "symbol", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transfer", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "_owner", "type": "address" }, { "name": "_spender", "type": "address" } ], "name": "allowance", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "payable": true, "stateMutability": "payable", "type": "fallback" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "owner", "type": "address" }, { "indexed": true, "name": "spender", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Approval", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Transfer", "type": "event"}]
    """
)

        web3 = Web3(Web3.HTTPProvider("https_provider"))

        sell_tx_params = {
            "nonce": web3.eth.get_transaction_count(metamask_address),
            "from": metamask_address,
            "maxPriorityFeePerGas": web3.eth.max_priority_fee,
            "chainId": 1,
            "maxFeePerGas": 100 * 10**10,
        }

        token_contract = web3.eth.contract(address='token_address', abi=MIN_ERC20_ABI)
        amount_to_sell = token_contract.functions.balanceOf(metamask_address).call()
        min_amount_out = 0  # for testing only
        router_contract = uniswap_router_contract here.

        sell_tx = router_contract.functions.swapExactTokensForETHSupportingFeeOnTransferTokens(
                amount_to_sell,  # amount to sell
                min_amount_out,  # min amount out
                [web3.to_checksum_address(token_address),web3.to_checksum_address(WETH_address)],
                metamask_address,
                int(time.time()) + 180,  # deadline now + 180 sec
            ).build_transaction(sell_tx_params)
        

        signed_sell_tx = web3.eth.account.sign_transaction(sell_tx, wallet_private_key)

        tx_hash = web3.eth.send_raw_transaction(signed_sell_tx.rawTransaction)
        
        tx_receipt = web3.eth.wait_for_transaction_receipt(tx_hash)

        if tx_receipt and tx_receipt["status"] == 1:
            return True

Blockquote

2 Answers 2

0

The pool you are interacting with a DAI-WETH LP, so your account receives WETH as a result of the swap.

1
  • I checked if I received WETH by importing WETH in my metamask account. It says 0 WETH available.
    – user130198
    Jan 16 at 21:57
0

Tx details provided say you got the ETH, right under "Interacted With (To):" you will see WETH going to the router, and then router sending ETH.

Since this TX resulted in native ETH balance increase, you can also see the balance increase under "State" to verify.

State increase for address 0xf62213...37038044 => 0.016987237508355765 ETH

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.