1

I have been looking for a practical solution to get the full transaction history for my contract. There does not seem to be an easy solution for this without relying on external resources. For example this question mentions etherscan, a separate database or running my own node, but I would prefer a solution where I can directly get the history from the blockchain using web3.

My contract will not have a huge list of transactions (< 100), so I was thinking of storing each transaction hash inside the contract. What would be the downsides of this approach?

redrobot
  • 113
  • 3

1 Answers1

1

Off-chain access

Technically the entire transaction history does exist in the blockchain. If you have a full-node or if you use some external service, you should be able to query this data. However, this transaction history is not available on-chain unless you use an oracle service.

On-chain access

To access this data on-chain, you'll have to manually store this data. The question becomes: should you store this data on-chain? Generally it is advised to avoid this, since blockchains are not the most efficient database.

However, there are times that you should store certain data on-chain. In my opinion, the best way to determine whether certain data should be stored on-chain or not is to determine if it will be needed to verify or to process certain transactions.

If the data is simply for user interface and is not required in the logic of the contract, then you should consider avoiding storing the data directly on-chain. You will always have indirect access to the data off-chain:

  • Transaction history can be obtained from blockchain.
  • Indices (indexes) and various transformations of data can be computed on-the-fly off-chain from any on-chain state or events.

However, if an index or history of data is critical for the logic of a contract, it may be appropriate to store that index on-chain, since that would be more efficient than computing it on-the-fly.

TL;DR

Extra data needed to verify or process transactions on-chain should be stored as state data, where inefficient or impossible to derive this data from existing state data. All other data should be computed and potentially cached off-chain.

Your Situation

Your intention to store this transaction history is solely for the purpose of easy off-chain access. It is not needed for any contract logic. Therefore, it would be bad practice to store this on-chain.

The Etherscan API might be what you are looking for for off-chain queries.

For other data indexing, consider services such as The Graph.

David Callanan
  • 792
  • 6
  • 20
  • It would purely be for my own convenience, so yes, bad practice. I am now looking into emitting and monitoring my own events, probably supplemented with an external api to verify my off-chain history on a regular basis. – redrobot Jul 03 '21 at 20:01
  • @redrobot Interesting, good luck! – David Callanan Jul 04 '21 at 17:06