Skip to content

Commit

Permalink
fix: bug fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcrazycoder committed Jul 28, 2023
1 parent effaf28 commit 8247a01
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
20 changes: 9 additions & 11 deletions packages/sdk/src/inscription/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ export async function mintFromCollection(options: MintFromCollectionOptions) {
throw new Error("Invalid options supplied.");
}

let colTxId = null;
let colVOut = null;
const [colTxId, colVOut] = options.collectionOutpoint.split(":").map((v, i) => {
if(i === 0) return v

try {
[colTxId, colVOut] = options.collectionOutpoint.split(":");
} catch (error) {
throw new Error(error);
}
const value = parseInt(v)
return isNaN(value) || !value ? false: value
}) as [string, number | false]

if (!colTxId || !colVOut) {
throw new Error("Invalid collection outpoint supplied.");
Expand All @@ -56,13 +54,13 @@ export async function mintFromCollection(options: MintFromCollectionOptions) {
throw new Error("Failed to get raw transaction for id: " + colTxId);
}

const colMeta = tx.rdata.vout[colVOut].inscriptions[0].meta;
const colMeta = tx.vout[colVOut].inscriptions[0].meta;

let validInscription = false;

for (let i = 0; i < colMeta.insc.length; i++) {
for (let i = 0; i < colMeta?.insc.length; i++) {
if (
colMeta.insc[i].iid == options.inscriptionIid &&
colMeta?.insc[i].iid == options.inscriptionIid &&
colMeta.publ[options.publisherIndex] &&
options.nonce < colMeta.insc[i].lim
) {
Expand All @@ -80,7 +78,7 @@ export async function mintFromCollection(options: MintFromCollectionOptions) {
ty: "insc",
col: options.collectionOutpoint,
iid: options.inscriptionIid,
publ: colMeta.publ[options.publisherIndex],
publ: colMeta?.publ[options.publisherIndex],
nonce: options.nonce,
traits: options.traits
};
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/inscription/instant-buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function generateBuyerPsbt({
throw new Error("Failed to get raw transaction for id: " + ordTxId);
}

const output = tx.rdata && tx.rdata.vout[ordOutNumber];
const output = tx && tx.vout[ordOutNumber];

if (!output) {
throw new Error("Outpoint not found.");
Expand Down Expand Up @@ -180,7 +180,7 @@ export async function generateBuyerPsbt({
const { rawTx } = await OrditApi.fetchTx({ txId: utxo.txid, network, hex: true })

if (format !== "p2tr") {
for (const output in rawTx.outs) {
for (const output in rawTx?.outs) {
try {
rawTx.setWitness(parseInt(output), []);
} catch {}
Expand All @@ -190,7 +190,7 @@ export async function generateBuyerPsbt({
const input: any = {
hash: utxo.txid,
index: utxo.n,
nonWitnessUtxo: rawTx.toBuffer(),
nonWitnessUtxo: rawTx?.toBuffer(),
sequence: 0xfffffffd // Needs to be at least 2 below max int value to be RBF
};

Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/transactions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Inscription, Ordinal } from "../inscription/types"
export type Vout = {
value: number
n: number
ordinals: Ordinal
inscriptions: Inscription
ordinals: Ordinal[]
inscriptions: Inscription[]
spent: string | false
scriptPubKey: {
asm: string
Expand Down
17 changes: 13 additions & 4 deletions packages/sdk/src/wallet/Ordit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from "..";
import { OrditApi } from "../api";
import { Network } from "../config/types";
import { Inscription } from "../inscription/types";
import { OrdTransaction, OrdTransactionOptions } from "../transactions";

bitcoin.initEccLib(ecc);
Expand Down Expand Up @@ -268,20 +269,28 @@ export class Ordit {
throw new Error("Wallet not fully initialized.");
}

return OrditApi.fetchAllInscriptions({
const { unspendableUTXOs }= await OrditApi.fetchUnspentUTXOs({
address: this.selectedAddress,
network: this.#network
});
})

return unspendableUTXOs.reduce((acc, curr) => {
if(curr.inscriptions) {
acc.push(...curr.inscriptions)
}

return acc
}, [] as Inscription[])
}

static inscription = {
new: (options: OrdTransactionOptions) => new OrdTransaction(options),
getInscriptionDetails: (outpoint: string, network: Network = "testnet") => {
fetchInscriptions: (outpoint: string, network: Network = "testnet") => {
if (!outpoint) {
throw new Error("Outpoint is required.");
}

return OrditApi.fetchInscriptionDetails({
return OrditApi.fetchInscriptions({
outpoint,
network
});
Expand Down

0 comments on commit 8247a01

Please sign in to comment.