Skip to content

Commit

Permalink
refactor(sdk): create reusable fn to transform inscriptions (#49)
Browse files Browse the repository at this point in the history
refactor: create reusable fn to transform inscriptions

also, remove unused class members and constructor
  • Loading branch information
iamcrazycoder authored Aug 18, 2023
1 parent 532f233 commit 23e2fab
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions packages/sdk/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as bitcoin from "bitcoinjs-lib"

import { apiConfig } from "../config"
import { Network } from "../config/types"
import { Inscription } from "../inscription/types"
import { Transaction, UTXO, UTXOLimited } from "../transactions/types"
import { decodeObject } from "../utils"
Expand All @@ -19,11 +17,13 @@ import {
} from "./types"

export class OrditApi {
static readonly #config = apiConfig
#network: Network = "testnet"
static transformInscriptions(inscriptions: Inscription[] | undefined) {
if (!inscriptions) return []

constructor(network: Network) {
this.#network = network
return inscriptions.map((inscription) => {
inscription.meta = inscription.meta ? decodeObject(inscription.meta) : inscription.meta
return inscription
})
}

static async fetchUnspentUTXOs({
Expand Down Expand Up @@ -57,12 +57,7 @@ export class OrditApi {
const { spendableUTXOs, unspendableUTXOs } = utxos.reduce(
(acc, utxo) => {
if (utxo.inscriptions?.length && !utxo.safeToSpend) {
if (decodeMetadata) {
utxo.inscriptions = utxo.inscriptions.map((inscription) => {
inscription.meta = inscription.meta ? decodeObject(inscription.meta) : inscription.meta
return inscription
})
}
utxo.inscriptions = decodeMetadata ? this.transformInscriptions(utxo.inscriptions) : utxo.inscriptions

acc.unspendableUTXOs.push(utxo)
} else {
Expand Down Expand Up @@ -109,16 +104,10 @@ export class OrditApi {
rpc.id
)

if (tx && tx.vout.length && decodeMetadata) {
tx.vout = tx.vout.map((vout) => {
vout.inscriptions = vout.inscriptions.map((inscription) => {
inscription.meta = inscription.meta ? decodeObject(inscription.meta) : inscription.meta
return inscription
})

return vout
})
}
tx.vout = tx.vout.map((vout) => {
vout.inscriptions = decodeMetadata ? this.transformInscriptions(vout.inscriptions) : vout.inscriptions
return vout
})

return {
tx,
Expand All @@ -141,10 +130,7 @@ export class OrditApi {
)

if (decodeMetadata) {
inscriptions = inscriptions.map((inscription) => {
inscription.meta = inscription.meta ? decodeObject(inscription.meta) : inscription.meta
return inscription
})
inscriptions = this.transformInscriptions(inscriptions)
}

return inscriptions
Expand All @@ -155,7 +141,7 @@ export class OrditApi {
throw new Error("Invalid options provided.")
}

const inscription = await rpc[network].call<Inscription>(
let inscription = await rpc[network].call<Inscription>(
"GetInscription",
{
id,
Expand All @@ -164,7 +150,9 @@ export class OrditApi {
rpc.id
)

inscription.meta = inscription.meta && decodeMetadata ? decodeObject(inscription.meta) : inscription.meta
if (decodeMetadata) {
inscription = this.transformInscriptions([inscription])[0]
}

return inscription
}
Expand Down

0 comments on commit 23e2fab

Please sign in to comment.