Basically I want to be able to track smart contracts balance and unique transactions over a period of time such as , last 24hours, last hour and so.

What would be the best approach into doing this? How should I store the data I get in order to create my API, SQL/NoSQL db?

Thank you in advance!


You can get the Ether balance of a smart contract at any block using the Web3 JSON RPC which is exposed through Web3 libraries like Web3.js:


web3.eth.getBalance(address [, defaultBlock] [, callback])

As you can see, this function call supports a "block" input, which can be used to get the Ether balance of the specified address at a certain block.

Blocks are not created at perfect intervals, but you should be able to approximate the block you need by assuming the average block time is ~15 seconds.

You can then confirm you have the right block by getting the timestamp value from the block (web3.eth.getBlock()) and adjusting when needed.

This kind of logic is integrated in this JavaScript Application: ETH Balance Graph (Code)

To get the transaction count for the contract is more complicated because incoming transactions to a contract are not indexed by the network.

My suggestions would be to track the transactions which emit an Event.

You can get all the past events for a contract using:


myContract.getPastEvents(event[, options][, callback])

From that, you should get a pretty good idea of the activity of the smart contract over time. Especially if the contract is an ERC20 token, which must emit a Transfer event whenever tokens are transferred and should emit the same event when tokens are created.

However, to truly get all internal and external transactions, you would need to go through each block since the contract was created, and look for those transactions (as far as I know). This is what Etherscan does, and is not a trivial task.

  • If I want to keep track of a certain contract, something similar to a cron job could I use subscribe? Also, if a smart contract does not emit an event the only solution to track its transactions is to have them saved? – Demeterca Ionuţ-Alexandru Oct 30 '18 at 21:07
  • The most brute force thing you can do is track each block, look at every transaction in that block, and look for ones which call the contract you are interested in. This can be done starting with subscribe, which can notify you when new blocks are created. – Shawn Tabrizi Oct 30 '18 at 21:24
  • Is there any other way to track events in a more generalised way? Such as defining my own general event and use that for every smart contract I want to track? – Demeterca Ionuţ-Alexandru Oct 30 '18 at 21:42
  • You need to be the contract developer to define an event your contract emits. It is part of the contract code. If you are the contract developer, then yes, you can emit any arbitrary events you want at pretty much any time. – Shawn Tabrizi Oct 30 '18 at 22:06
  • I am just a third party looking to track other people contracts :\ – Demeterca Ionuţ-Alexandru Oct 31 '18 at 7:39

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .