Skip to content

Commit

Permalink
CU-86a5geecu - BS Lib - Improve wait transaction functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
dustr94 committed Nov 7, 2024
1 parent 70fc092 commit cd426af
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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"
}
27 changes: 27 additions & 0 deletions packages/blockchain-service/src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,33 @@ export async function waitForTransaction<BSName extends string = string>(
return false
}

export async function waitForAccountTransaction<BSName extends string = string>(
service: BlockchainService<BSName>,
txId: string,
account: Account<BSName>
): Promise<boolean> {
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<BSName extends string = string>(
blockchainServices: BlockchainService<BSName>[],
getAccountCallback: (service: BlockchainService<BSName>, index: number) => Promise<Account<BSName>>
Expand Down

0 comments on commit cd426af

Please sign in to comment.