From 3a30af5514277851c087efc5cef51222342818ac Mon Sep 17 00:00:00 2001 From: Sai Kranthi Date: Fri, 30 Jun 2023 15:11:52 +0530 Subject: [PATCH] feat(sdk): add inscription fetching logic --- packages/sdk/src/api/index.ts | 134 +++++++++++++++++++++++++++++++ packages/sdk/src/wallet/Ordit.ts | 23 +++++- 2 files changed, 156 insertions(+), 1 deletion(-) diff --git a/packages/sdk/src/api/index.ts b/packages/sdk/src/api/index.ts index 8a096743..f3c6ce98 100644 --- a/packages/sdk/src/api/index.ts +++ b/packages/sdk/src/api/index.ts @@ -30,9 +30,143 @@ export class OrditApi { throw new Error(error); } } + + static async fetchAllInscriptions({ address, network = "testnet" }: FetchInscriptionsOptions) { + if (!address) { + throw new Error("Invalid options provided."); + } + + const fullUri = `${this.#config.apis[network].batter}/utxo/unspents`; + const payload = { + address, + options: { + txhex: true, + notsafetospend: false, + allowedrarity: ["common"] + } + }; + + try { + const response = await _fetch(fullUri, { + method: "POST", + body: JSON.stringify(payload), + headers: { + Accept: "application/json", + "Content-Type": "application/json" + } + }); + + const data: UTXO = await response.json(); + + if (data && data.success && data.rdata && data.rdata.length) { + const inscriptions: InscriptionsEntity[] = []; + + data.rdata.forEach((utxo: RdataEntity) => { + if (utxo.inscriptions && utxo.inscriptions.length) { + inscriptions.push(...utxo.inscriptions); + } + }); + + return inscriptions; + } else { + throw new Error("No data found."); + } + } catch (error: any) { + throw new Error(error.message); + } + } + + static async fetchInscriptionDetails({ outpoint, network = "testnet" }: FetchInscriptionDetailsOptions) { + if (!outpoint) { + throw new Error("Invalid options provided."); + } + + const fullUri = `${this.#config.apis[network].batter}/utxo/inscriptions/${outpoint}`; + + try { + const response = await _fetch(fullUri); + + const data: InscriptionDetailsEntity = await response.json(); + + return data; + } catch (error: any) { + throw new Error(error.message); + } + } } export type FetchOptions = { data: any; network: Network; }; + +export type FetchInscriptionsOptions = { + address: string; + network?: Network; +}; + +export type FetchInscriptionDetailsOptions = { + outpoint: string; + network?: Network; +}; + +export interface UTXO { + success: boolean; + message: string; + rdata?: RdataEntity[] | null; +} +export interface RdataEntity { + n: number; + txHash: string; + blockHash: string; + blockN: number; + sats: number; + scriptPubKey: ScriptPubKey; + txid: string; + value: number; + ordinals?: OrdinalsEntity[] | null; + inscriptions?: InscriptionsEntity[] | null; + safeToSpend: boolean; + confirmation: number; +} +export interface ScriptPubKey { + asm: string; + desc: string; + hex: string; + address: string; + type: string; +} +export interface OrdinalsEntity { + number: number; + decimal: string; + degree: string; + name: string; + height: number; + cycle: number; + epoch: number; + period: number; + offset: number; + rarity: string; + output: string; + start: number; + size: number; +} +export interface InscriptionsEntity { + id: string; + outpoint: string; + owner: string; + fee: number; + height: number; + number: number; + sat: number; + timestamp: number; + media_type: string; + media_size: number; + media_content: string; +} + +export interface InscriptionDetailsEntity { + success: boolean; + message: string; + rdata?: InscriptionsEntity[] | null; +} diff --git a/packages/sdk/src/wallet/Ordit.ts b/packages/sdk/src/wallet/Ordit.ts index 2dbd7d96..eb69139f 100644 --- a/packages/sdk/src/wallet/Ordit.ts +++ b/packages/sdk/src/wallet/Ordit.ts @@ -186,8 +186,29 @@ export class Ordit { }; } + async getInscriptions() { + if (!this.selectedAddress) { + throw new Error("Wallet not fully initialized."); + } + + return OrditApi.fetchAllInscriptions({ + address: this.selectedAddress, + network: this.#network + }); + } + static inscription = { - new: (options: OrdTransactionOptions) => new OrdTransaction(options) + new: (options: OrdTransactionOptions) => new OrdTransaction(options), + getInscriptionDetails: (outpoint: string, network: Network = "testnet") => { + if (!outpoint) { + throw new Error("Outpoint is required."); + } + + return OrditApi.fetchInscriptionDetails({ + outpoint, + network + }); + } }; #initialize() {