Skip to content

Commit

Permalink
Merge pull request #112 from CityOfZion/CU-86a5geecu
Browse files Browse the repository at this point in the history
CU-86a5geecu - BS Lib - Improve wait transaction functionality
  • Loading branch information
thiagocbalducci authored Nov 7, 2024
2 parents 70fc092 + cd426af commit 1c2cc43
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 1c2cc43

Please sign in to comment.