bbnn
bbnn

Reputation: 3612

How to get the data that etherscan provide in Uniswap trade transaction?

I tried to play around with web3.js, however it seems to be difficult to get the data that etherscan provides.

For example in this transaction: https://etherscan.io/tx/0x6ce8bbf5eca28a2a327b5f2b9f93ad5393c12bafe89f2cc379dd37ca9d1627b9

How can I get these data?

Upvotes: -1

Views: 964

Answers (1)

Petr Hejda
Petr Hejda

Reputation: 43581

Etherscan transaction detail page aggregates the information from multiple sources.

Some of the data is stored in the event logs. They are part of a transaction receipt, which is available only for transactions that have already been produced in a block. That's because event logs are generated during the actual execution of the transaction, which is performed by the block producer (miner on PoW, validator on PoS).

Docs: https://web3js.readthedocs.io/en/v1.10.0/web3-eth.html#gettransactionreceipt

Specifically the Transfer event logs are emitted when ERC-20 tokens are transferred. They are emitted by each ERC-20 token contract, which means that you can see the token contract address, and query it for additional info such as the token name.

Note that ERC-20 token name name is just a text field that's assigned value by the token developer and is not validated by the network. So there can be multiple tokens with the same name for example. And in this case, "Bitcoin" is just a name of the ERC-20 token that might represent the value of BTC on the Ethereum network, or it might not. But it's not the actual L1 Bitcoin.

As for the pair info and price, you can also query the pair contract. The Swap event log was emitted by a specific Uniswap V2 (fairly widely used decentralized exchange) pair contract that enables trading between those two specific ERC-20 tokens. But the functions that return pair info - or whether some information is even available - depend on the actual exchange contract. So actually the pair info on Uniswap V3 or dYdX or any other decentralized exchange might be retrieved differently.

const txReceipt = await web3.eth.getTransactionReceipt("0x6ce8bbf5eca28a2a327b5f2b9f93ad5393c12bafe89f2cc379dd37ca9d1627b9");
for (let log of txReceipt.logs) {
    switch (log.topics[0]) {
        case web3.utils.keccak256("Transfer(address,address,uint256)"):
            // TODO decode the 32byte hex representation to decimal numbers and addresses
            console.log(`Transfer ${log.data} tokens (token contract ${log.address}) from ${log.topics[1]} to ${log.topics[2]}`);
            break;
        case web3.utils.keccak256("Swap(address,uint256,uint256,uint256,uint256,address)"):
            console.log(`Swap on Uniswap V2 pair - pair contract address ${log.address}`);
            break;
    }
}

const pairContract = new web3.eth.Contract(ABI_JSON, CONTRACT_ADDRESS);
// Uniswap V2 doesn't return the price directly
// but since it's a liquidity pool
// you can simply divide their reserves of token0 by their reserves of token1
const reserves = await pairContract.methods.getReserves();

Upvotes: 0

Related Questions