1

I'm trying to get the amount that I recieved in WETH during swap on Uniswap(can be hardcoded WETH). On the code below, I took random address that did swap, got his transactions and then decoded it with the "abi-decoder" library, when he is swapping WETH -> Other asset, it is not a problem, I can see it in the amountOut || transaction.value || amoutOutMin line. But when the swap is any asset to WETH the amountIn is represented as the "any asset" amount and I don't know how to convert or any other way to get the WETH amount that I received.

Here is Transaction for example and the output that I see in the code at the "decodedInput" variable:

    {
  name: "swapExactTokensForETHSupportingFeeOnTransferTokens",
  params: [
    {
      name: "amountIn",
      value: "1112191000000000",
      type: "uint256",
    },
    {
      name: "amountOutMin",
      value: "0",
      type: "uint256",
    },
    {
      name: "path",
      value: [
        "0xae7a14225b11fe39d6ef00c3388bddaeedbdbad6",
        "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
      ],
      type: "address[]",
    },
    {
      name: "to",
      value: "0xe86826abb64f09dc48637869c948a44d1b1bdfbb",
      type: "address",
    },
    {
      name: "deadline",
      value: "1691688949",
      type: "uint256",
    },
  ],
}

And this is the code (the main of it that gets the amountIn, amountOut etc.. :

const abiDecoder = require("abi-decoder");
wallets = ["0xe86826abb64f09dc48637869c948a44d1b1bdfbb"] //TODO: temp for testing, must delete 
for (const wallet of wallets) {
    const transactionsOfWallet = await getTransactions(wallet, 0, latestBlock);
    for (const transaction of transactionsOfWallet) {
        const decodedInput = abiDecoder.decodeMethod(transaction.input);
        decodedInput.params.forEach(item => {
            params[item.name] = item.value;
        });
        const { amountIn, amountOut, amoutOutMin, path } = params;
        const amountToSent = parseFloat(web3.utils.fromWei(amountOut || transaction.value || amoutOutMin, 'ether'));
        const amountToEthRecieved = parseFloat(web3vb.utils.fromWei(amountIn, 'ether')); // <---- amountIn I'm getting in terms of other asset but not as WETH and I don't know how to convert.
    }
}

Thank you!

1 Answer 1

0

It is most likley in cases where you are swapping to eth as opposed to weth, and so the token is being pulled to the router before being unwrapped and you are receiving eth through a final internal call.

You can use the events emitted by the pool(s) to process the swap volume.

>>> POOL_ADDRESS = '0x8B62C43c11ABeE86C6a682177049971283593009'
>>> POOL_ABI = '''
... [{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type ":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed" :false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"},{"indexed":true,"internalType":"address","name":"to","type":"address"}],"name":"Burn","type": "event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name ":"amount1","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount0In","type":"uin t256"},{"indexed":false,"internalType":"uint256","name":"amount1In","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount0Out","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1Out","type":"u int256"},{"indexed":true,"internalType":"address","name":"to","type":"address"}],"name":"Swap","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint112","name":"reserve0","type":"uint112"},{"indexed":false,"i nternalType":"uint112","name":"reserve1","type":"uint112"}],"name":"Sync","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","nam e":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[],"name":"DOMAIN_SEPARATOR","outputs":[{"internalType":"bytes32","name":"","type":"bytes32 "}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"MINIMUM_LIQUIDITY","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"PERMIT_TYPEHASH"," outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":" allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"value","ty pe":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balanceOf","outputs" :[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"}],"name":"burn","outputs":[{"internalType":"uint256","name":"amount0", "type":"uint256"},{"internalType":"uint256","name":"amount1","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutabili ty":"view","type":"function"},{"inputs":[],"name":"factory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getReserves","outputs":[{"internalType":"uint1 12","name":"_reserve0","type":"uint112"},{"internalType":"uint112","name":"_reserve1","type":"uint112"},{"internalType":"uint32","name":"_blockTimestampLast","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[{"inte rnalType":"address","name":"_token0","type":"address"},{"internalType":"address","name":"_token1","type":"address"}],"name":"initialize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"kLast","outputs" :[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"}],"name":"mint","outputs":[{"internalType":"uint256","name":"liquidity ","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"add ress","name":"","type":"address"}],"name":"nonces","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"inte rnalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint8","name":"v","type":"uint8"},{"internal Type":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"permit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"price0CumulativeLast","outputs":[{"i nternalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"price1CumulativeLast","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type": "function"},{"inputs":[{"internalType":"address","name":"to","type":"address"}],"name":"skim","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount0Out","type":"uint256"},{"int ernalType":"uint256","name":"amount1Out","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"swap","outputs":[],"stateMutability":"nonpayable","type":"f unction"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"sync","outputs":[],"stateMutability":"nonpayable","type":"function"}, {"inputs":[],"name":"token0","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"token1","outputs":[{"internalType":"address","name":"","type":"address"}],"st ateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","ty pe":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"addres s","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"sta teMutability":"nonpayable","type":"function"}]
... '''
>>>
>>> pool = w3.eth.contract(address=POOL_ADDRESS, abi=POOL_ABI)
>>> SWAP_TOPIC = w3.solidity_keccak(['string'],['Swap(address,uint256,uint256,uint256,uint256,address)']).hex()
>>>
>>> logs = w3.eth.get_logs({'fromBlock': 17886007, 'toBlock': 17886007, 'address': pool.address})
>>>
>>> for log in logs:
...   for topic in log['topics']:
...     if SWAP_TOPIC == topic.hex():
...       swap = pool.events.Swap().process_log(log)['args']
...       # process swap volume
...       amount0In, amount1In, amount0Out, amount1Out = swap['amount0In'], swap['amount1In'], swap['amount0Out'], swap['amount1Out']
...       print(f'[+] OUTPUT: amount0In -> {amount0In} | amount1In -> {amount1In} | amount0Out -> {amount0Out} | amount1Out -> {amount1Out}')
...
[+] OUTPUT: amount0In -> 1056581450000000 | amount1In -> 0 | amount0Out -> 0 | amount1Out -> 915322751579735498
[+] OUTPUT: amount0In -> 491783716436899 | amount1In -> 0 | amount0Out -> 0 | amount1Out -> 365687058658809892
[+] OUTPUT: amount0In -> 0 | amount1In -> 45826359740773267 | amount0Out -> 63772864840325 | amount1Out -> 0
>>>

Top result is yours:

>>> 915322751579735498 / (10 ** 18)
0.9153227515797355

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.