From 96916caea6a278dd38ae511e48a7986f6f7c2561 Mon Sep 17 00:00:00 2001 From: inaie ignacio Date: Wed, 26 Jul 2023 17:11:20 +0800 Subject: [PATCH 1/4] fix: work in progress --- src/transaction.spec.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/transaction.spec.ts b/src/transaction.spec.ts index 72a27d6b..1374e471 100644 --- a/src/transaction.spec.ts +++ b/src/transaction.spec.ts @@ -1,5 +1,6 @@ import { describe, test } from '@jest/globals' import algosdk, { makeBasicAccountTransactionSigner } from 'algosdk' +import AlgodClient from 'algosdk/dist/types/client/v2/algod/algod' import invariant from 'tiny-invariant' import * as algokit from './' import { algorandFixture } from './testing' @@ -64,6 +65,26 @@ describe('transaction', () => { ) }) + test('waiting transaction', async () => { + const algod = new AlgodClient('', 'https://testnet-api.algonode.cloud/', '') + const testAccount = algosdk.generateAccount() + + await algokit.transferAlgos( + { + from: testAccount, + to: testAccount.addr, + amount: algokit.algos(5), + note: 'Transfer 5 ALGOs', + }, + algod, + ) + + const txn = await getTestTransaction() + const signedTransaction = await algokit.signTransaction(txn, testAccount) + await algod.sendRawTransaction(signedTransaction).do() + await algokit.waitForConfirmation(txn.txID(), 2 ?? 5, algod) + }) + test('Transaction cap is ignored if higher than fee', async () => { const { algod, testAccount } = localnet.context const txn = await getTestTransaction() From c82d708c91215e7be0e746d8fb2b9c49fa71b984 Mon Sep 17 00:00:00 2001 From: Altynbek Orumbayev Date: Wed, 26 Jul 2023 12:11:34 +0200 Subject: [PATCH 2/4] fix: wip --- src/transaction.spec.ts | 19 +++++-------------- src/transaction.ts | 29 +++++++++++++++++------------ 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/transaction.spec.ts b/src/transaction.spec.ts index 1374e471..257a881f 100644 --- a/src/transaction.spec.ts +++ b/src/transaction.spec.ts @@ -1,6 +1,5 @@ import { describe, test } from '@jest/globals' import algosdk, { makeBasicAccountTransactionSigner } from 'algosdk' -import AlgodClient from 'algosdk/dist/types/client/v2/algod/algod' import invariant from 'tiny-invariant' import * as algokit from './' import { algorandFixture } from './testing' @@ -66,22 +65,14 @@ describe('transaction', () => { }) test('waiting transaction', async () => { - const algod = new AlgodClient('', 'https://testnet-api.algonode.cloud/', '') - const testAccount = algosdk.generateAccount() - - await algokit.transferAlgos( - { - from: testAccount, - to: testAccount.addr, - amount: algokit.algos(5), - note: 'Transfer 5 ALGOs', - }, - algod, - ) - + const { algod, testAccount } = localnet.context const txn = await getTestTransaction() const signedTransaction = await algokit.signTransaction(txn, testAccount) await algod.sendRawTransaction(signedTransaction).do() + + // const mock = jest.mock("algosdk") // Put the path to algod in algosdk module or just algosdk + // let mockPendingTransactionInfo = mock() + await algokit.waitForConfirmation(txn.txID(), 2 ?? 5, algod) }) diff --git a/src/transaction.ts b/src/transaction.ts index 189614af..31e8a5cf 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -356,20 +356,25 @@ export const waitForConfirmation = async function ( const startRound = BigInt(status.lastRound) + 1n let currentRound = startRound while (currentRound < startRound + BigInt(maxRoundsToWait)) { - const pendingInfo = modelsv2.PendingTransactionResponse.from_obj_for_encoding( - await algod.pendingTransactionInformation(transactionId).do(), - ) - if (pendingInfo !== undefined) { - const confirmedRound = pendingInfo.confirmedRound - if (confirmedRound && confirmedRound > 0) { - return pendingInfo - } else { - const poolError = pendingInfo.poolError - if (poolError != null && poolError.length > 0) { - // If there was a pool error, then the transaction has been rejected! - throw new Error(`Transaction ${transactionId} was rejected; pool error: ${poolError}`) + try { + const pendingInfo = modelsv2.PendingTransactionResponse.from_obj_for_encoding( + await algod.pendingTransactionInformation(transactionId).do(), + ) + if (pendingInfo !== undefined) { + const confirmedRound = pendingInfo.confirmedRound + if (confirmedRound && confirmedRound > 0) { + return pendingInfo + } else { + const poolError = pendingInfo.poolError + if (poolError != null && poolError.length > 0) { + // If there was a pool error, then the transaction has been rejected! + throw new Error(`Transaction ${transactionId} was rejected; pool error: ${poolError}`) + } } } + } catch (e) { + currentRound++ + continue } await algod.statusAfterBlock(toNumber(currentRound)).do() From 95f4c12beb731eaa4b0ec65670713745d4786fc5 Mon Sep 17 00:00:00 2001 From: inaie ignacio Date: Fri, 28 Jul 2023 17:24:24 +0800 Subject: [PATCH 3/4] fix: retry pendingTransactionInformation if gets a httperror --- src/transaction.spec.ts | 29 +++++++++++++++++------------ src/transaction.ts | 9 ++++++--- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/transaction.spec.ts b/src/transaction.spec.ts index 257a881f..b93110d8 100644 --- a/src/transaction.spec.ts +++ b/src/transaction.spec.ts @@ -64,18 +64,6 @@ describe('transaction', () => { ) }) - test('waiting transaction', async () => { - const { algod, testAccount } = localnet.context - const txn = await getTestTransaction() - const signedTransaction = await algokit.signTransaction(txn, testAccount) - await algod.sendRawTransaction(signedTransaction).do() - - // const mock = jest.mock("algosdk") // Put the path to algod in algosdk module or just algosdk - // let mockPendingTransactionInfo = mock() - - await algokit.waitForConfirmation(txn.txID(), 2 ?? 5, algod) - }) - test('Transaction cap is ignored if higher than fee', async () => { const { algod, testAccount } = localnet.context const txn = await getTestTransaction() @@ -293,6 +281,17 @@ describe('transaction', () => { algod, ) }) + + test('Transaction wait for confirmation http error', async () => { + const { algod } = localnet.context + const txn = await getTestTransaction() + try { + await algokit.waitForConfirmation(txn.txID(), 5, algod) + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } catch (e: any) { + expect(e.message).toEqual(`Transaction ${txn.txID()} not confirmed after 5 rounds`) + } + }) }) describe('transaction node encoder', () => { @@ -345,3 +344,9 @@ describe('transaction node encoder', () => { `) }) }) +function throwError(error: { + status: number + message: string +}): import('algosdk/dist/types/client/v2/algod/pendingTransactionInformation').default { + throw new Error('Function not implemented.') +} diff --git a/src/transaction.ts b/src/transaction.ts index 31e8a5cf..e8d4d0fc 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -372,9 +372,12 @@ export const waitForConfirmation = async function ( } } } - } catch (e) { - currentRound++ - continue + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } catch (e: any) { + if (e.name === 'URLTokenBaseHTTPError') { + currentRound++ + continue + } } await algod.statusAfterBlock(toNumber(currentRound)).do() From b61d4721a208c0d8fc69d207263dea47cacf0b18 Mon Sep 17 00:00:00 2001 From: inaie ignacio Date: Mon, 31 Jul 2023 11:37:30 +0800 Subject: [PATCH 4/4] fix: pr review --- docs/code/modules/index.md | 8 ++++---- src/transaction.spec.ts | 11 ++--------- src/transaction.ts | 4 ++-- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/docs/code/modules/index.md b/docs/code/modules/index.md index 25e1151c..82e871ac 100644 --- a/docs/code/modules/index.md +++ b/docs/code/modules/index.md @@ -169,7 +169,7 @@ the estimated rate. #### Defined in -[src/transaction.ts:389](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/transaction.ts#L389) +[src/transaction.ts:397](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/transaction.ts#L397) ___ @@ -225,7 +225,7 @@ Allows for control of fees on a `Transaction` or `SuggestedParams` object #### Defined in -[src/transaction.ts:412](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/transaction.ts#L412) +[src/transaction.ts:420](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/transaction.ts#L420) ___ @@ -1153,7 +1153,7 @@ The array of transactions with signers #### Defined in -[src/transaction.ts:444](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/transaction.ts#L444) +[src/transaction.ts:452](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/transaction.ts#L452) ___ @@ -1452,7 +1452,7 @@ The suggested transaction parameters #### Defined in -[src/transaction.ts:435](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/transaction.ts#L435) +[src/transaction.ts:443](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/transaction.ts#L443) ___ diff --git a/src/transaction.spec.ts b/src/transaction.spec.ts index b93110d8..320bebb5 100644 --- a/src/transaction.spec.ts +++ b/src/transaction.spec.ts @@ -287,9 +287,8 @@ describe('transaction', () => { const txn = await getTestTransaction() try { await algokit.waitForConfirmation(txn.txID(), 5, algod) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - } catch (e: any) { - expect(e.message).toEqual(`Transaction ${txn.txID()} not confirmed after 5 rounds`) + } catch (e: unknown) { + expect((e as Error).message).toEqual(`Transaction ${txn.txID()} not confirmed after 5 rounds`) } }) }) @@ -344,9 +343,3 @@ describe('transaction node encoder', () => { `) }) }) -function throwError(error: { - status: number - message: string -}): import('algosdk/dist/types/client/v2/algod/pendingTransactionInformation').default { - throw new Error('Function not implemented.') -} diff --git a/src/transaction.ts b/src/transaction.ts index e8d4d0fc..d2c8cebb 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -373,8 +373,8 @@ export const waitForConfirmation = async function ( } } // eslint-disable-next-line @typescript-eslint/no-explicit-any - } catch (e: any) { - if (e.name === 'URLTokenBaseHTTPError') { + } catch (e: unknown) { + if ((e as Error).name === 'URLTokenBaseHTTPError') { currentRound++ continue }