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.
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.