0

If I do something like

web3.eth.sendTransaction({
    from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
    value:50,
    to:'0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BA1'
}, function(error, hash){ 
    //API call to backend and save history
});

The callback would not be called if the user refreshes browser. How do I make sure that I still get notified of the transaction even when the user refreshes browser?

2
  • You mean the user refreshes the browser before the transaction get mined ? Apr 2, 2018 at 8:48
  • @MedMansour yes precisely
    – forJ
    Apr 2, 2018 at 8:48

1 Answer 1

2

I think, there is a bit of confusions. Let me explained:

Sending a transaction

When you execute this code:

web3.eth.sendTransaction({
    from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
    value:50,
    to:'0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BA1'
}, function(error, hash){ 
    //API call to backend and save history
});

You get the transaction hash (unique identifier) instantly. That means the transaction is broadcast to the network but isn't mined yet.

When the transaction is mined

Then, if you want to get notify of a mined transaction, you have to call web3.eth.getTransactionReceipt(hash [, callback]) (doc).

But you don't know when the transaction will be mined as it depends of the miners (usually based on the gas you're willing to pay). It can take few seconds or a few hours sometimes.

Some framework, like Truffle are doing this job for you, when you send a transaction, they are polling the node with getTransactionReceipt to be able to notify you when it's mined.

If the user closes the browser before it's mined

Back to your question. Between the time your user receives the transaction hash and the time it's mined, everything can happen (browser crash, close the browser, etc.).

So if you still want to notify the user when he opens again your dApp, I would suggest to use the browser local storage.

// Send the transaction
web3.eth.sendTransaction({
    from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
    value:50,
    to:'0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BA1'
}, function(error, hash){ 
    //Save the transaction hash in the local storage
    var pendingTransactions = JSON.parse(localStorage.getItem("pendingTransactions"));
    pendingTransactions.push(hash);
    localStorage.setItem("pendingTransactions", JSON.stringify(pendingTransactions));

});

When the user opens the dApp, you can start a scheduler to unstack the pending transactions and notify your user.

var pendingTransactions = JSON.parse(localStorage.getItem("pendingTransactions"));

pendingTransactions.forEach(function(value, index) {
    // Get the transaction hash
    web3.eth.getTransactionReceipt(value, function(err, receipt) {
         if(!err) {
             // Send notification to the user receipt
             console.log(receipt);

             // Remove from the pending transactions
             pendingTransactions.splice(index, 1);
             localStorage.setItem("pendingTransactions", JSON.stringify(pendingTransactions));
         }
    })
});

I haven't tested this code but I hope you will understand the principle.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.