0

I am trying to get the wallet balance for this address 0x389044F3ac7472060A0618116e3624A5f0f20F28 as of Aug 31 2022. In the past, I've been able to do so by getting the transaction history and subtracting transactions labelled as sent from incoming ones. This one presents a challenge as I am only able to access the last 100,000 transactions. I know there's got to be a more efficient way to do this.

Is there an API I could use to retrieve the entire transaction history (given its large volume)? Or even better would anyone know how to automate this entire process by retrieving the transaction history and then creating a script to add/subtract balances prior to Aug 31, 2022?

Would appreciate some help on this one. Been stuck for 3 days and I'm about to get chewed up at work if I do not figure out a way to solve this.

2 Answers 2

2

You could use Forge framework to fork the mainnet to a certain block height (check what block height was around Aug 31).

That way you can move up and down the history of the mainnet and see what balance that address had at each specific block.

It would look something like that:

pragma solidity ^0.8.0;

import "forge-std/Test.sol";

contract getAddressBalanceTest is Test {
 function getAddressBalance() public {
        vm.createSelectFork(vm.envString("ETH_RPC_URL"), YOUR_BLOCK_HEIGHT);
        address target = 0x389044F3ac7472060A0618116e3624A5f0f20F28;
        console.log("Target balance: ", target.balance);

Hope this helps!

3
  • Thanks a lot man. Will give this a try right now. Cheers! Oct 27, 2022 at 16:40
  • My pleasure! Let me know if this worked and mark my answer as correct so other people with the same problem can find it! Thanks Oct 27, 2022 at 17:45
  • Hey man, do you think you would be able to guide me on how I should set up the environment? I do not have a solid technical background, but I am willing to learn Oct 29, 2022 at 11:40
1

You need the archive node. Every provider has an option for the archive node, it is usually of an extra cost. But you can start your node, using geth or erigon

./erigon --datadir D:/Erigon --chain mainnet --private.api.addr=127.0.0.1:9090 --prune=hrtc --prune.h.older=400000 --prune.r.older=400000 --prune.t.older=400000 --prune.c.older=400000

Then you can query blockchains data at any block. When you call balanceOf method, this actually queries the nodes latest block, but you can specify any of the block numbers in the past. This is the RPC method, all clients should support it. If you use Node.js there is the lib for this: web3js#methods-mymethod-call

Or the example for TypeScript with 0xweb library, which wraps web3js and adds additional tools, like getting the Block Number by Date

npm i 0xweb -g
0xweb init
import { Config } from '@dequanto/Config';
import { ERC20 } from '@dequanto-contracts/openzeppelin/ERC20';
import { BlockDateResolver } from '@dequanto/blocks/BlockDateResolver';
import { EthWeb3Client } from '@dequanto/clients/EthWeb3Client';

async function example () {
    await Config.fetch();

    let client = new EthWeb3Client();
    let dateResolver = new BlockDateResolver(client);
    let blockNumber = await dateResolver.getBlockNumberFor(new Date('2022-10-27T00:00:00Z'));

    let erc20 = new ERC20('0x<TOKEN>', client);
    let balanceOf = erc20.forBlock(blockNumber).balanceOf('0x<USER>');
    console.log('Balance on the date:', balanceOf);
}
example();
3
  • Hey man, do I need to archive the node under both methods? Or just under RPC? I am not the most technical person. I have a background in finance and everything that I know computer-wise I've taught myself by taking free compsci courses and doing small projects Oct 29, 2022 at 11:35
  • You don't need TO archive node, you need THE archive node. This is the normal node, but it doesn't remove its state. So you can query (make RPC calls) it with any BlockNumber in the past. For example, Infura Node Provider has a checkbox for the node, when activating, the node will preserve the state, but it costs more.
    – tenbits
    Oct 29, 2022 at 11:52
  • So, if you have a list of tokens for the user, then you can make a single RPC call to balanceOf with a specific block number, and so you get the user's token balance at that date.
    – tenbits
    Oct 29, 2022 at 11:54

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.