let's take an example I send calimID = 123 and got a transaction hash = 0x123... when I try to click on claimID in front end I should be linked to transaction hash... how could I do that?
-
You can use a smart contract for required mapping– AniketApr 18, 2017 at 12:27
-
I think you would get an answer much more quickly if you clarified this question. It's very unclear what you're asking for. Do you want to link to an external blockchain scraper like etherscan? Or do you want to retrieve the transaction details and display it in your front end? Not at all clear what "should be linked to transaction hash" means.– Thomas Jay RushApr 18, 2017 at 16:55
2 Answers
In the function in the contract, call a log event. Let's imagine you have a function like this (examples untested, may not work or even compile, it's just to give you the general idea...):
uint256 nextClaimID;
function claim() {
uint256 myClaimID = nextClaimID;
// Do something with the claim ID, whatever it is that your claim involves
nextClaimID++;
}
In your contract, you define an event:
event LogClaim(
uint256 indexed claim_id
// You may also want to add some other fields you want to retrieve about a claim
);
Then add it to your claim
function:
function claim() {
uint256 myClaimID = lastClaimID;
// Do something with the claim ID
emit LogClaim(myClaimID);
nextClaimID++;
}
Now in your front-end, you can make a filter for the log related to that claim ID:
var START_BLOCK = 123456; // The earliest block number your claim might have happened in.
var your_claim_id = 98765; // The claim you're interested in
var claim_filter = rc.LogNewClaim({claim_id:your_claim_id}, {fromBlock: START_BLOCK, toBlock:'latest'});
To just grab everything from the past with that ID. It should include a field called transactionHash
, which is your txid.
claim_filter.get(function(error, logs) {
for (var i=0; i<logs.length; i++) {
console.log('Your claim was created in txid', logs[i].transactionHash);
}
});
The document you need to study is the JavaScript-API.
Here we can find various getters under web3.eth.get...
.
web3.eth.getBlock()
and web3.eth.getTransaction()
are probably the main ones you want to be looking at.
However, you need to be more specific in your question. It appears that your frontend design could be better thought out so as not to rely on the transaction data itself but the state data held within the contract and the events that are logged to the chain.
Trying to track transactions based on state data, implies complex chain explorers and searches, not really what you want to get into just for a DAPP.