From cd426af5ce6bdc62840064ccc5d7070be61a7857 Mon Sep 17 00:00:00 2001 From: Dust Date: Thu, 7 Nov 2024 14:32:36 -0300 Subject: [PATCH] CU-86a5geecu - BS Lib - Improve wait transaction functionality --- .../CU-86a5geecu_2024-11-07-17-25.json | 10 +++++++ packages/blockchain-service/src/functions.ts | 27 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 common/changes/@cityofzion/blockchain-service/CU-86a5geecu_2024-11-07-17-25.json diff --git a/common/changes/@cityofzion/blockchain-service/CU-86a5geecu_2024-11-07-17-25.json b/common/changes/@cityofzion/blockchain-service/CU-86a5geecu_2024-11-07-17-25.json new file mode 100644 index 0000000..b6b61b9 --- /dev/null +++ b/common/changes/@cityofzion/blockchain-service/CU-86a5geecu_2024-11-07-17-25.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@cityofzion/blockchain-service", + "comment": "Add a new function to wait for a specific transaction by an account", + "type": "minor" + } + ], + "packageName": "@cityofzion/blockchain-service" +} \ No newline at end of file diff --git a/packages/blockchain-service/src/functions.ts b/packages/blockchain-service/src/functions.ts index 80b4a60..43e39cb 100644 --- a/packages/blockchain-service/src/functions.ts +++ b/packages/blockchain-service/src/functions.ts @@ -72,6 +72,33 @@ export async function waitForTransaction( return false } +export async function waitForAccountTransaction( + service: BlockchainService, + txId: string, + account: Account +): Promise { + const maxAttempts = 10 + + let attempts = 1 + + do { + await wait(60000) + + try { + const response = await service.blockchainDataService.getTransactionsByAddress({ address: account.address }) + const isTransactionConfirmed = response.transactions.some(transaction => transaction.hash === txId) + + if (isTransactionConfirmed) return true + } catch { + // Empty block + } + + attempts++ + } while (attempts < maxAttempts) + + return false +} + export async function fetchAccountsForBlockchainServices( blockchainServices: BlockchainService[], getAccountCallback: (service: BlockchainService, index: number) => Promise>