I am using web3 to perform ethereum transactions. I have been able to perform the transaction, but I want to notify the user if the transaction is successful and has been mined. How would i do this in node.js?
I have tried to find ways to use a webhook on the reciever ethereum wallet in order to notify the server if a transaction on the blockchain has been mined. After that i would be able to notify the user.
My last option is to create a multi threaded loop on my server that checks if the transaction hash has been mined.
I am unsure if I should provide any code, as I don’t know if it will help.
You can use the web3.eth.getTransactionReceipt method to get a receipt for a transaction, or wait for an event generated by a smart contract when your transaction is being executed.
However, you should take into mind that public Ethereum may have forks and you should wait 5-6 blocks to make sure that the transaction will not be dropped along with the "side" fork.
Related
As currently, I'm working on fabric SDKs. I want to get all the past transactions of fabric on the client-side.
Example: I already have 1 installed chain code. On fabric, I called delete_user and edit_user methods. I want those all transaction on client side without storing in offchain DB.
Can anyone suggest Node SDK method for the same?
Have each transaction function emit a suitably named chaincode event (such as "deleteUser" and "editUser"). The chaincode event gets emitted be peers only when the transaction is successfully committed and updates the ledger. Your client application can listen for those chaincode events and take action on each chaincode event.
You can start listening for chaincode events from a specific block number to replay historic events. You may also want to use a "checkpointer" (or roll-your-own) mechanism for persisting the block number and transaction ID your listener last successfully processed so you can resume listening from exactly the same point after an application restart. This would allow you to process each chaincode event exactly once, with no duplication or missed events.
See these Fabric samples for examples:
https://github.com/hyperledger/fabric-samples/tree/main/asset-transfer-events
https://github.com/hyperledger/fabric-samples/tree/main/off_chain_data
Currently ,in fabric gateway API when we submit the transaction using APIs provided by TransactionImpl ,inside commitTransaction method of TranscatioImpl it creates commitHandler on network and transaction Id and after sending the transaction to channel,it waits for the response to come back by calling commitHandler.waitForevents .Is there any way I can do this in asynchronous way ,i.e. after submitting the transaction to orderer, I want to proceed submitting next transaction and write a separate event listener which would listen on the events coming back from fabric and take some action on them as and when they recieve.
You don't have to wait on submitTransaction if you don't want to, for example suppose you want to submit a number of transactions and wait for them all to commit then you can just collect the promises from submitTransaction and wait on them at a later point in time. If you don't care when your transaction is committed then you can specify the gateway commit strategy as null which means it won't even bother to listen for transaction committed events. see https://hyperledger.github.io/fabric-sdk-node/release-1.4/tutorial-transaction-commit-events.html
for more info
If you want to do your own event handling you can add contract/block/transaction event listeners yourself
see https://hyperledger.github.io/fabric-sdk-node/release-1.4/tutorial-listening-to-events.html
for more details
I am using the hyper-ledger fabric network with 2 organisation and chain-code also installed on the network its working. I came across submitTransaction and EvaluateTransaction in fabric node js. what is difference between them, what i observer is.
When i initiate contract.submittransaction the submitTransaction from the fabric-node even though if i call the query method from the chain-code its create the new block and chain-code query method don't have put-state
If i use contract.evaluateTransaction the evaluate transaction from the fabric-node to query method its does not create any transaction.
The difference between submitTransaction vs evaluateTransaction is that submitTransaction takes the proposal results returned from invoking the smart contract and submits them to the orderer and waits for the transaction to be committed. This means that the proposal results will be ordered and delivered to the peers for validation and committed to the blockchain.
It is irrespective of what the smart contract transaction does, however the general pattern is that submitTransaction is used for transactions that change the world state and evaluateTransaction is used for transactions that only query the world state (or query key history).
However it's perfectly reasonable for example to want to record querying of the world state onto the ledger and so you would use submitTransaction on a smart contract function that doesn't modify the world state in order to do this.
contract.submittransaction execute the transaction against the chain code that's why it creates the new block. while contract.evaluateTransaction only query the state database that's why don't create the new block.
To my knowledge, there isn't a way to do timed transaction in Hyperledger Fabric.
Consider the use case using the marbles example. Say I want to transfer a marble 600 seconds after I received it. Does the Fabric SDK provide anyway for me to get the unix timestamp of the event when I received my marble then send another transaction to a queue that will happen exactly 600 seconds later by calculating the timestamp + 600?
As you are talking about the time when the Marbles are actually received, then in my opinion you have to write some code on both sides. i.e, both at client and the chaincode sides.
I am not sure how to do the same with only SDK/Client side code.
If you are willing to write something in your transaction processing logic, there is a method ChaincodeStubInterface.GetTxTimestamp() in the github.com/hyperledger/fabric/core/chaincode/shim package to get the time when the transaction is processed by the Fabric.
You could return the same to your SDK, and then to your external calling program. And then compute the +600 seconds and send the next transaction.
No, there is no way to automate a timed transaction from within the chaincode and it would be bad practice to try handle it there. If you try to create a timestamp from within the chaincode, it is guaranteed that all peers processing the transaction proposal will return different values as they do not all being processing the proposal at the same exact instance. Since the results sets will return non-deterministic, the transaction will always fail when it enters the validation phase. If you try to use stub.GetTxTimestamp() you will only be returning the timestamp that the client itself sent up, as per the docs.
The best way to do this is with pure sdk code. After acquiring requisite transaction proposals and sending the transaction for ordering, listen for a transaction commitment event.
Upon receiving notification of transaction commitment, you can queue up another transaction to be sent endorsement and commitment 600 seconds later. The specifics of how will differ from sdk to sdk, but all sdk's support notification of transaction commitment.
Currently I have setup ripple mainnet server and provide availability of XRP trading, If any user submits the transaction in his account from the external wallet, How I know which transaction performed?
Currently, I have used WebSocket socket and subscribe all account to the listener, So when any transaction comes in subscribed account then it will catch the transaction. But the issue is that when my xrp server is down or listeners missed any transaction then how we fetch incoming transaction later.
When you get your next transaction.
You check that account's PreviousTxnLgrSeq and see if that matches with the latest tx on your end. You can find that from the account_info API method.
Or you can query account_tx to see if the latest tx you got on your end matches with just previous tx. And if those don't match,... you've got the answer above in the account_tx method.
One downside to this practice is that you have to wait until someone deposits again to that particular account.
To avoid such circumstances, you can track every accounts' latest tx ledger sequence on your end. So if you've missed some ledgers you would know which accounts are lagging behind and check if those accounts have transactions that you've missed using account_tx method with the ledger_index_min set to the latest tx ledger sequence and ledger_index_max set to -1.
In other words this means check this account's transactions since i've last checked until the latest ledger... anyway I hope you get the idea. Godspeed! ;)