Skip to content

Commit

Permalink
feat(src): wait the transaction in commit
Browse files Browse the repository at this point in the history
Wait the transaction and get its receipt in commit.
  • Loading branch information
olgahaha committed Jun 15, 2023
1 parent 60219be commit 7143152
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/nit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export async function pull(assetCid: string, blockchainInfo) {
// }
//}

export async function commit(assetCid: string, commitData: string, blockchainInfo) {
export async function commit(assetCid: string, commitData: string, blockchainInfo, confirms: number = 1) {
const commitString = addActionNameInCommit(commitData);
let r;
if (blockchainInfo.gasPrice != null) {
Expand All @@ -324,7 +324,9 @@ export async function commit(assetCid: string, commitData: string, blockchainInf
} else {
r = await blockchainInfo.contract.commit(assetCid, commitString, { gasLimit: blockchainInfo.gasLimit });
}
return r;

// Wait for the transaction to be mined
return await r.wait(confirms);
}

export async function log(assetCid: string, blockchainInfo, fromIndex: number, toIndex: number = null) {
Expand Down
10 changes: 3 additions & 7 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,15 +609,11 @@ async function main() {
} else {
commitEventIndexCid = nit.assetCidMock;
}
const commitResult = await nit.commit(commitEventIndexCid, JSON.stringify(commitData), blockchain);

console.log(`Commit Tx: ${commitResult.hash}`);
console.log(`Commit Explorer: ${blockchain.explorerBaseUrl}/${commitResult.hash}`);

try {
// Wait for the transaction to be mined
const transactionReceipt = await commitResult.wait();
console.log(`Block Number: ${transactionReceipt.blockNumber}`);
const transactionReceipt = await nit.commit(commitEventIndexCid, JSON.stringify(commitData), blockchain);
console.log(`Commit Tx: ${transactionReceipt.transactionHash}`);
console.log(`Commit Explorer: ${blockchain.explorerBaseUrl}/${transactionReceipt.transactionHash}`);
} catch (error) {
console.error(`Transaction error: ${error}`);
}
Expand Down

0 comments on commit 7143152

Please sign in to comment.