0

I'm trying to fetch most recent DAI transfers for this address 0x6b175474e89094c44da98b954eedeac495271d0f, and retrieve information about them such as address, etc. I feel like I'm missing understanding of fundamentals in order to do it properly.

Steps I had taken

I connected to metamask and added web3-provider to be able to retrieve library from context.

I don't understand how to retrieve DAI blocks only. What I did was:

  • Retrieved ABI for the smart contract under this address from https://etherscan.io/address/0x6b175474e89094c44da98b954eedeac495271d0f#code
  • Created contract with contractAddress, abi and ethers lib
  • Retrieved latest block number
  • Retrieved blocks using iterator and decremented iterator after every loop run

Actual code:

const contractAddress = '0x6b175474e89094c44da98b954eedeac495271d0f';
const contract = new ethers.Contract(contractAddress, ABI, library);
const recent = await contract.provider.getBlockNumber();
const next = await contract.provider.getBlock(recent);
const next2 = await contract.provider.getBlock(recent - 1);

Example of what I received:

{
    "hash": "0xf898356b6eb8f5ad645ba3a9b869a8d63bbc3f1fc02e6be41ba582520d0d5d68",
    "parentHash": "0xa3e23363bfb22b68a2d100d013e4819a4e1e2488a09de89b9e2a29bb07d44ca3",
    "number": 15824974,
    "timestamp": 1666699379,
    "nonce": "0x0000000000000000",
    "difficulty": 0,
    "gasLimit": {
        "type": "BigNumber",
        "hex": "0x01c9c380"
    },
    "gasUsed": {
        "type": "BigNumber",
        "hex": "0xb6b595"
    },
    "miner": "0x0000000000000000000000000000000000000001",
    "extraData": "0x",
    "transactions": [
        "0xc37d0e9bb0c86e5e891d078a3fb9a9f9a28dbc15682baf0873f970db1ca0fbd8",
        "0x56ff2f53eef9371a08a96ab1ed26f9d92c70bbd6ca11dbfad61bd716f9b47874",
        "0xdc5a1b7cb11d87de17ebf40dfaf6a0936a9d919ad0dce838d749ba37f3699078",
        "0x1682989ee993aa9d0a91e5e88dc36d0d01b7396210730bfb0d3121b1b6020ff9",
        "0xac563722220fa76fa6aa2ae9ec4c81e39c2ed7ab23015c161bda363cee698504",
        "0x22100e064a0dbba6fb058ee09cddd7d3566216f7cad6426c71577d1d1bd75da4",
        "0x8fe87a212109cc5d15e13813076fc063b7c6020b3974b860f11eb85136f5e2cc",
        "0x1a469786f20d9a92f83d9f108fbb1f77d9ae8839f15f8fcbfadc8f8fd617edab",
        "0x730452d559d46c869f9d52a85cb133d858b73adeaaeeb2c154f6bd9dbc9e6f65",
        "0x3a809e9baa35fd0c55e71c4937c84fe06a819ed5448e2b2e1b552ca983e48cb9",
        "0xfcb670ea41b107ffc72e166ee8c02eb0102b1cde12b7c5ce06002edfccb9c9b1"
    ],
    "baseFeePerGas": {
        "type": "BigNumber",
        "hex": "0x0335b6a98d"
    },
    "_difficulty": {
        "type": "BigNumber",
        "hex": "0x00"
    }
}

My assumption was that If I were to copy the hash from the response I'd be able to view the transaction in etherscan, but I was wrong. Following link returns no match https://etherscan.io/tx/0xf898356b6eb8f5ad645ba3a9b869a8d63bbc3f1fc02e6be41ba582520d0d5d68

Does the code I wrote get blocks related to DAI only? Or am I missing something? Why doesn't the tx appear in etherscan? How do I filter transactions basing on sender? To put it in other words - how do I mimick etherscan view for displaying ERC-20 token? I would really appreciate any kind of enlightening!

1 Answer 1

0

That is the hash of block number 15824974. You're dumping the content of the block. Blocks are batches of transactions with a hash of the previous block in the chain. This links blocks together (in a chain) because hashes are cryptographically derived from the block data.

You can see the block on etherscan via this URL https://etherscan.io/block/15824974, but you can also see the same thing on this link https://etherscan.io/block/0xf898356b6eb8f5ad645ba3a9b869a8d63bbc3f1fc02e6be41ba582520d0d5d68 (Note the same hash in the URL as you have)

It's not that simple to get transaction history. It's not indexed by the blockchain, not by a standard node, "out-of-the-box". As of today, the size of ethereum blockchain is around 950 GB. For many cases, you can probably rely on Etherscan.

In order to get the transactions you'd have to use Etherscan as a provider in ethers and call the getHistory method. Please note that not all providers support the getHistory method.

The following code snippet will list the transactions to the address you specified in the last 10 blocks. (There might be no transactions when you query it so just tweak the numbers)

const ethers = require("ethers");

let address = "0x6b175474e89094c44da98b954eedeac495271d0f";
let provider = new ethers.providers.EtherscanProvider();

async function main() {
  const currentBlock = await provider.getBlockNumber()
  
  const startBlock = currentBlock - 10;
  const endBlock = currentBlock;

  let history = await provider.getHistory(address, startBlock, endBlock);

  history.forEach((tx) => {
      console.log(tx);
  })
}

main();

For more information please head over to ethers documentation about the EtherscanProvider - https://docs.ethers.io/v5/api/providers/api-providers/#EtherscanProvider

If you'd like to further filter the transactions (e.g. particular token) then you need an appropriate provider to do that OR do some manual filtering in javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.