[Ethereum] How to get information about last 5-10 transactions in ethereum blockchain

go-ethereumtransactionsweb3js

I want to retrieve information on last 5-10 transaction hash created by a specific function in my smart contract. Is there any way I can get historical transaction hash ?

Best Answer

Finding all (or the last 5) transactions corresponding with a particular function call translates to: find transactions going to a specific address and then check the first bytes of the callData to match the function signature. The function signature is calculated as e.g. bytes3(sha3("myFunction(uint256,string,address)")). This is what you have to look for.

The usual libraries (e.g. web3.js) or APIs dont provide this out of the box. But you could go with e.g. this etherscan API http://api.etherscan.io/api?module=account&action=txlist&address=0x807Aa96410f7cfF5614fD8DB6CbFa82d86B7029d&sort=asc

Let's suppose you store the returned JSON object in ob, then you could extract the tx hashes of the transactions which led to a call of the function with the signature 0xdf78c1dd via

ob.result.forEach(function(element){
if(element.input.startsWith("0xdf78c1dd"))
  console.log("txHash: " + element.hash);
})
Related Topic