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.
-
you can use etherscan io api– AVATARCommented 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.– mzaidiCommented Oct 31, 2018 at 11:40
-
you can use nonce for that– AVATARCommented Oct 31, 2018 at 11:43
-
I dont think you are understanding my question– mzaidiCommented Oct 31, 2018 at 11:48
-
you want the number of transactions 'from' the contract or 'to' the contract?– JaimeCommented Oct 31, 2018 at 11:52
4 Answers
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:
- The contract do not generate an event for every transaction.
- The contract generates more than one event per transaction.
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:
- Real Ethereum transaction included into a block with separate hash, receipt etc.
- So called “internal” transaction, initiated by a smart contract, that does not have hash nor receipt.
For “transaction of a contract” possible meanings are:
- Transaction sent to this contract.
- Transaction sent by this contract (internal transaction).
- Transaction that created this contract.
- Transaction that destroyed this contract.
- 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.
- Transaction that accessed this contract without executing its byte code, i.e. read ether balance of this contract or byte code of it.
- 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.
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)
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
I have also documented my search for explorers - https://dspyt.com/exploring-ethereum