How to make client code (dApp) pause until transaction has been confirmed.
-
Hi, i am using connex to sign and commit a transaction to the blockchain. Then immediately after, I am reading that data from the blockchain.
is there a way to make my dApp wait until the transaction has been confirmed?
here is the code I have tried:
...const method = connex.thor.account(CONTRACT_ADDRESS).method(abi);
const clause = method.asClause(newItemCount, owner, approvers, ipfsHash);
let signingService = connex.vendor.sign('tx');
let result = await signingService.request(
[
{
comment: 'Batch Creating Items',
...clause
}
]
);
const transaction = connex.thor.transaction(result.txid);
const txDetail = await transaction.get();
let receipt = await transaction.getReceipt();...
const method = connex.thor.account(CONTRACT_ADDRESS).method(abi);
// THIS FAILS if transaction has not yet been confirmed!!!!
let result = await method.call(index);
let tokenId = result.decoded['0'];
return tokenId;immediately after I write to my contract, I need to read what I just wrote. However, I cannot read until the transaction has been confirmed. I need to know how to make my client code wait until the transaction has been confirmed before reading. How do I do that?
-
@Thors_girl Thank You!!!
-
You can write a loop to wait until you get non-null receipt. In the loop, using
await ticker.next()
to efficiently wait for block-chain activity. the code snippet is https://gist.github.com/qianbin/d11d12182e2b72bbafef2ca07a071520
-
const ticker = connex.thor.ticker()
let receipt = null
do {
await ticker.next()
// need to catch exception
receipt = await transaction.getReceipt()
} while(!receipt)