diff --git a/modules/chronik-client/README.md b/modules/chronik-client/README.md --- a/modules/chronik-client/README.md +++ b/modules/chronik-client/README.md @@ -95,3 +95,4 @@ 0.17.0 - Add support for token proto to endpoints that return `Tx_InNode` to `ChronikClientNode` 0.18.0 - Add support for websocket connections to `ChronikClientNode` 0.19.0 - Add support for token data in tx inputs and outputs to `ChronikClientNode` +0.20.0 - Add support for `tokenId` endpoints to `ChronikClientNode` diff --git a/modules/chronik-client/package-lock.json b/modules/chronik-client/package-lock.json --- a/modules/chronik-client/package-lock.json +++ b/modules/chronik-client/package-lock.json @@ -1,12 +1,12 @@ { "name": "chronik-client", - "version": "0.19.0", + "version": "0.20.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "chronik-client", - "version": "0.19.0", + "version": "0.20.0", "license": "MIT", "dependencies": { "@types/ws": "^8.2.1", diff --git a/modules/chronik-client/package.json b/modules/chronik-client/package.json --- a/modules/chronik-client/package.json +++ b/modules/chronik-client/package.json @@ -1,6 +1,6 @@ { "name": "chronik-client", - "version": "0.19.0", + "version": "0.20.0", "description": "A client for accessing the Chronik Indexer API", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/modules/chronik-client/src/ChronikClientNode.ts b/modules/chronik-client/src/ChronikClientNode.ts --- a/modules/chronik-client/src/ChronikClientNode.ts +++ b/modules/chronik-client/src/ChronikClientNode.ts @@ -142,6 +142,11 @@ return convertToRawTx(rawTx); } + /** Create object that allows fetching info about a given token */ + public tokenId(tokenId: string): TokenIdEndpoint { + return new TokenIdEndpoint(this._proxyInterface, tokenId); + } + /** Create object that allows fetching script history or UTXOs. */ public script( scriptType: ScriptType_InNode, @@ -216,6 +221,91 @@ } } +/** Allows fetching script history and UTXOs. */ +export class TokenIdEndpoint { + private _proxyInterface: FailoverProxy; + private _tokenId: string; + + constructor(proxyInterface: FailoverProxy, tokenId: string) { + this._proxyInterface = proxyInterface; + this._tokenId = tokenId; + } + + /** + * Fetches the tx history of this tokenId, in anti-chronological order. + * @param page Page index of the tx history. + * @param pageSize Number of txs per page. + */ + public async history( + page = 0, // Get the first page if unspecified + pageSize = 25, // Must be less than 200, let server handle error as server setting could change + ): Promise<TxHistoryPage_InNode> { + const data = await this._proxyInterface.get( + `/token-id/${this._tokenId}/history?page=${page}&page_size=${pageSize}`, + ); + const historyPage = proto.TxHistoryPage.decode(data); + return { + txs: historyPage.txs.map(convertToTx), + numPages: historyPage.numPages, + numTxs: historyPage.numTxs, + }; + } + + /** + * Fetches the confirmed tx history of this tokenId, in anti-chronological order. + * @param page Page index of the tx history. + * @param pageSize Number of txs per page. + */ + public async confirmedTxs( + page = 0, // Get the first page if unspecified + pageSize = 25, // Must be less than 200, let server handle error as server setting could change + ): Promise<TxHistoryPage_InNode> { + const data = await this._proxyInterface.get( + `/token-id/${this._tokenId}/confirmed-txs?page=${page}&page_size=${pageSize}`, + ); + const historyPage = proto.TxHistoryPage.decode(data); + return { + txs: historyPage.txs.map(convertToTx), + numPages: historyPage.numPages, + numTxs: historyPage.numTxs, + }; + } + + /** + * Fetches the unconfirmed tx history of this tokenId, in anti-chronological order. + * @param page Page index of the tx history. + * @param pageSize Number of txs per page. + */ + public async unconfirmedTxs( + page = 0, // Get the first page if unspecified + pageSize = 25, // Must be less than 200, let server handle error as server setting could change + ): Promise<TxHistoryPage_InNode> { + const data = await this._proxyInterface.get( + `/token-id/${this._tokenId}/unconfirmed-txs?page=${page}&page_size=${pageSize}`, + ); + const historyPage = proto.TxHistoryPage.decode(data); + return { + txs: historyPage.txs.map(convertToTx), + numPages: historyPage.numPages, + numTxs: historyPage.numTxs, + }; + } + + /** + * Fetches the current UTXO set for this tokenId. + */ + public async utxos(): Promise<TokenIdUtxos> { + const data = await this._proxyInterface.get( + `/token-id/${this._tokenId}/utxos`, + ); + const utxos = proto.Utxos.decode(data); + return { + tokenId: this._tokenId, + utxos: utxos.utxos.map(convertToUtxo), + }; + } +} + /** Config for a WebSocket connection to Chronik. */ export interface WsConfig_InNode { /** Fired when a message is sent from the WebSocket. */ @@ -1151,3 +1241,11 @@ type: 'Error'; msg: string; } + +/** List of UTXOs */ +export interface TokenIdUtxos { + /** TokenId used to fetch these utxos */ + tokenId: string; + /** UTXOs */ + utxos: Utxo_InNode[]; +}