1

I'm trying to get all transactions that occured to an address within a specified time range. The transactions shall include all ethers as well as token transactions done to/from an ethereum address.

I tried writing the following code. The minTimeStamp is the time from which I need to get transactions till current time.

Take a quick peak to that:

async function getT(minTimestamp) {
minTimestamp = 1592834400;

const transactions = [];
let blockNum, block;

await web3.eth.getBlockNumber().then(r => {
    blockNum = r;
});

await web3.eth.getBlock(blockNum).then(r => {
    block = r;
});

while (block.timestamp >= minTimestamp) {
    await new Promise(async(next) => {
        console.log(`[*] Searching block ${blockNum}`);
        if (block && block.transactions) {
            for (let txHash of block.transactions) {
                await new Promise(async(next2) => {
                    let tx = await web3.eth.getTransaction(txHash);
                    if (tx.to != null) {
                        if ('0x92376311Ce465c54b26D18C50f8a90734bFB1172'.toLowerCase() === tx.to.toLowerCase()) {
                            console.log(`[+] transaction found on block ${blockNum}`);
                            console.log({
                                address: tx.from,
                                value: web3.utils.fromWei(tx.value, 'ether'),
                                timestamp: new Date()
                            });
                        }
                    }
                    if (tx.from != null) {
                        if ('0x92376311Ce465c54b26D18C50f8a90734bFB1172'.toLowerCase() === tx.from.toLowerCase()) {
                            console.log(`[+] transaction found on block ${blockNum}`);
                            console.log({
                                address: tx.to,
                                value: web3.utils.fromWei(tx.value, 'ether'),
                                timestamp: new Date()
                            });
                        }
                    }
                    next2();
                });
            }
        }

        transactions.push.apply(transactions, block.transactions);

        --blockNum;

        await web3.eth.getBlock(blockNum).then(r => {
            block = r;
        });

        next();
    });
}

console.log('Retreived', transactions.length, 'transactions');

}

It works fine although very slow, but I am not able to get token transactions from this code.

Can anyone suggest how I can tweak the code to get that?

Also, if there is any better approach I will appreciate the suggestions.

Thanks, looking forward Fariha

  • You might be better off using something like EthSlurp (ethslurp.com) than rolling your own. – Chan-Ho Suh Jun 25 at 21:28
2
+25

Token transfers

The challenge with token transfers is that you can't detect them by monitoring the "receiving" address. Tokens are not explicitly transferred to an address - they are only assigned to an address inside the token contract. So whenever someone transfers tokens to address A he actually sends a transaction to the token contract at address T, to function transfer. The transfer function then simply updates its internal ledger to mark address A as having more tokens.

So to monitor token transfers to address A you have to actually monitor the token contract. But, to make matters more difficult, that only monitors that one token. So basically you would have to monitor every single token contract to know whether some of them assigns (transfers) tokens to address A.

Fortunately (ERC20) token contracts are supposed to emit Transfer event when they transfer tokens so you can add a generic filter to read those events and check their to parameter. Here are some examples: Monitor token transfers

Performance

With your current implementation you are halting execution of the code while waiting for every single Promise to resolve. So you should be able to improve the performance by adding these Promises to an array and dealing with their results when they come in. That way you would not need to wait for the actual resolving of the previous promise before starting creating/executing the next promise.

The actual implementation is not really a question suited for this forum as it's basically a JavaScript question, but here you can find some examples: https://www.codingame.com/playgrounds/347/javascript-promises-mastering-the-asynchronous/how-to-join-promises

| improve this answer | |
  • Hi Lauri, thanks for the answer. Can u guide me making the code more fast and optimistic? – Fariha Abbasi Jun 25 at 10:33
  • I'd appreciate if u re-edit the code with good performance you mentioned – Fariha Abbasi Jun 25 at 10:33
  • I'm not very fluent in web3js and there are possibly other ways to improve the performance, I just gave you one option. And the actual implementation is not really a question for this forum as it has nothing to do with Ethereum but about JavaScript - it's just about handling Javascript Promises. But edited now answer to add a bit more details. – Lauri Peltonen Jun 25 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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