Skip to content

Commit

Permalink
feat(sdk): add inscription fetching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kranthicodes committed Jun 30, 2023
1 parent da21ce3 commit 3a30af5
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 1 deletion.
134 changes: 134 additions & 0 deletions packages/sdk/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
23 changes: 22 additions & 1 deletion packages/sdk/src/wallet/Ordit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 3a30af5

Please sign in to comment.