1

I want to determine the number of transactions of a contract in a certain period. My question is that in the transaction receipt, if we want to say that contract A has x number of transactions will these be only the transactions sent from this contract or they will be also the one sent to it, i.e. the sum of transaction numbers having this contract address in the to and from fields in the transaction receipt.

9
  • you can use etherscan io api
    – AVATAR
    Commented Oct 31, 2018 at 11:38
  • I dont want to find the total number of transactions of a contract, rather want to find them in a certain period for which i am utilizing getting transaction information from individual blocks.
    – mzaidi
    Commented Oct 31, 2018 at 11:40
  • you can use nonce for that
    – AVATAR
    Commented Oct 31, 2018 at 11:43
  • I dont think you are understanding my question
    – mzaidi
    Commented Oct 31, 2018 at 11:48
  • you want the number of transactions 'from' the contract or 'to' the contract?
    – Jaime
    Commented Oct 31, 2018 at 11:52

4 Answers 4

1

If the contract is emiting logs for each transaction received then you just need to look for the logs emitted by the contract.

You can do this with etherescan: https://etherscan.io/apis#logs

For instance: https://api.etherscan.io/api?module=logs&action=getLogs &fromBlock=379224 &toBlock=latest &address=0x33990122638b9132ca29c723bdf037f1a891a70c &apikey=YourApiKeyToken

will return a list of all the logs generated by the contract (presume a single event per transaction)

Notice that you can decide what from what interval (in blocks) to look for.

The only caveats possible are:

  1. The contract do not generate an event for every transaction.
  2. The contract generates more than one event per transaction.
1

This depends on what do you mean by ”transaction” and what do you mean by “transaction of a contract”.

For transaction there could be at least two meanings:

  1. Real Ethereum transaction included into a block with separate hash, receipt etc.
  2. So called “internal” transaction, initiated by a smart contract, that does not have hash nor receipt.

For “transaction of a contract” possible meanings are:

  1. Transaction sent to this contract.
  2. Transaction sent by this contract (internal transaction).
  3. Transaction that created this contract.
  4. Transaction that destroyed this contract.
  5. Transaction that destroyed some other contracts sending remaining ether from the balance of destroyed contract to this contract without executing this contract's byte code.
  6. Transaction that accessed this contract without executing its byte code, i.e. read ether balance of this contract or byte code of it.
  7. Transaction that performed delegate call to this contract.

You need to use different approaches to detect and count different kinds of transaction related to the contract in different ways. In some cases Web3 API will be enough, but in the other cases, you will need to trace transactions all use etherescan.io API.

1

If you have a geth node running; you can use this script to calculate the number of transaction of a contract in a given block range.

function countNumberOfContractTxs(contractAddress, blockStart, blockEnd) {
  var incominngCount = 0;
  var outgoingCount = 0;
  contractAddress = contractAddress.toLowerCase()
  if(!blockEnd) blockEnd = eth.getBlock('latest').number
  for(var blockNum=blockStart; blockNum<blockEnd; blockNum++) {
    var txs = eth.getBlock(blockNum).transactions;
    if(!txs.length) continue;
    for(var j=0; j<txs.length; j++) {
      var tx = txs[j]
      var txDetail = eth.getTransactionReceipt(tx)
      if(txDetail.status == '0x0') continue;
      if(txDetail.from.toLowerCase() == contractAddress) outgoingCount++
      if(txDetail.to.toLowerCase() == contractAddress) incominngCount++
    }
  }
  console.log(incominngCount, outgoingCount)
}

Just paste this function in geth console and call this like a nornal contract function. Last param is optional and the script checks only for successful txs. countNumberOfContractTxs(<contractAddress>, startBlock, endBlock)

0

Not sure if there is a decentralised solution but etherscan unfortunately remains the most utilised tool by developers by far.

Definitely consider api by blockscout: https://optimism.blockscout.com/api-docs

enter image description here

I have also documented my search for explorers - https://dspyt.com/exploring-ethereum

Your Answer

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

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