From 60219bed1ef89c79729aa4e31049d2c4ae73d206 Mon Sep 17 00:00:00 2001 From: Olga Shen Date: Wed, 14 Jun 2023 16:14:36 +0800 Subject: [PATCH 1/2] fix(run.ts): commit transaction not included Fix the issue where the transaction was successfully committed and a transaction hash was obtained, but the transaction was never included in a block. Add a wait mechanism to ensure the transaction is mined or log the error reason if it fails. --- src/run.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/run.ts b/src/run.ts index 5347e37..065459e 100644 --- a/src/run.ts +++ b/src/run.ts @@ -614,6 +614,14 @@ async function main() { 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}`); + } catch (error) { + console.error(`Transaction error: ${error}`); + } + // Reset stage await setWorkingAssetCid(""); From 71431526782eab93a831251384e3c1c0b32c5b9f Mon Sep 17 00:00:00 2001 From: Olga Shen Date: Thu, 15 Jun 2023 16:11:33 +0800 Subject: [PATCH 2/2] feat(src): wait the transaction in commit Wait the transaction and get its receipt in commit. --- src/nit.ts | 6 ++++-- src/run.ts | 10 +++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/nit.ts b/src/nit.ts index 9ec3d38..207d549 100644 --- a/src/nit.ts +++ b/src/nit.ts @@ -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) { @@ -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) { diff --git a/src/run.ts b/src/run.ts index 065459e..1825c43 100644 --- a/src/run.ts +++ b/src/run.ts @@ -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}`); }