From 465b64cdc58e9a4077e87c57266e3fbdde567205 Mon Sep 17 00:00:00 2001 From: selankon Date: Mon, 15 Jul 2024 11:59:01 +0200 Subject: [PATCH] Implements txByIndex --- src/api/chain.ts | 13 +++++++++++++ test/api/chain.test.ts | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/api/chain.ts b/src/api/chain.ts index d0a8b5ff..baea1e14 100644 --- a/src/api/chain.ts +++ b/src/api/chain.ts @@ -9,6 +9,7 @@ enum ChainAPIMethods { COSTS = '/chain/info/electionPriceFactors', CIRCUITS = '/chain/info/circuit', TX_INFO = '/chain/transactions/reference', + TX_INFO_BY_INDEX = '/chain/transactions/reference/index/{index}', TX_INFO_BLOCK = '/chain/transactions/{blockHeight}/{txIndex}', SUBMIT_TX = '/chain/transactions', TX_LIST = '/chain/transactions/page', @@ -467,6 +468,18 @@ export abstract class ChainAPI extends API { .catch(this.isApiError); } + public static txByIndex(url: string, index: number): Promise { + return axios + .get(url + ChainAPIMethods.TX_INFO_BY_INDEX.replace('{index}', String(index))) + .then((response) => { + if (response.status === 204) { + throw new ErrTransactionNotFound(); + } + return response.data; + }) + .catch(this.isApiError); + } + /** * Fetches information about a transaction by its containing block an index on the block. * diff --git a/test/api/chain.test.ts b/test/api/chain.test.ts index 860eb34c..1eebab24 100644 --- a/test/api/chain.test.ts +++ b/test/api/chain.test.ts @@ -13,4 +13,9 @@ describe('Chain API tests', () => { await ChainAPI.txInfo(URL, '0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF'); }).rejects.toThrow(ErrTransactionNotFound); }, 5000); + it('should throw when asking for a non existent transaction by index', async () => { + await expect(async () => { + await ChainAPI.txByIndex(URL, 0); + }).rejects.toThrow(ErrTransactionNotFound); + }, 5000); });