Skip to content

Commit

Permalink
Implements txByIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
selankon committed Jul 15, 2024
1 parent 3fbbe94 commit 465b64c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/api/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -467,6 +468,18 @@ export abstract class ChainAPI extends API {
.catch(this.isApiError);
}

public static txByIndex(url: string, index: number): Promise<IChainTxReference> {
return axios
.get<IChainTxReference>(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.
*
Expand Down
5 changes: 5 additions & 0 deletions test/api/chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 465b64c

Please sign in to comment.