From 39ebcc8a0e0d673fa6e0a530b149d23de244928f Mon Sep 17 00:00:00 2001 From: Nishant Ghodke <64554492+iamcrazycoder@users.noreply.github.com> Date: Wed, 16 Aug 2023 14:18:40 +0530 Subject: [PATCH] fix(sdk): allow higher value utxos and return change (#45) * refactor: allow higher (wrong) value utxos * fix: consolidate out amount instead of parsing to int * feat: return sell price, postage for buyers clarity --- packages/sdk/src/inscription/instant-buy.ts | 6 +++++- packages/sdk/src/transactions/OrdTransaction.ts | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/sdk/src/inscription/instant-buy.ts b/packages/sdk/src/inscription/instant-buy.ts index 2e1c9738..a8682441 100644 --- a/packages/sdk/src/inscription/instant-buy.ts +++ b/packages/sdk/src/inscription/instant-buy.ts @@ -121,6 +121,8 @@ export async function generateBuyerPsbt({ // seller psbt merge const decodedSellerPsbt = bitcoin.Psbt.fromHex(sellerPsbt, { network: networkObj }) + const sellPrice = (decodedSellerPsbt.data.globalMap.unsignedTx as any).tx.outs[0].value - postage + // inputs ;(psbt.data.globalMap.unsignedTx as any).tx.ins[2] = (decodedSellerPsbt.data.globalMap.unsignedTx as any).tx.ins[0] psbt.data.inputs[2] = decodedSellerPsbt.data.inputs[0] @@ -166,7 +168,9 @@ export async function generateBuyerPsbt({ return { hex: psbt.toHex(), base64: psbt.toBase64(), - fee + fee, + postage, + sellPrice } } diff --git a/packages/sdk/src/transactions/OrdTransaction.ts b/packages/sdk/src/transactions/OrdTransaction.ts index f432df4c..944b113e 100644 --- a/packages/sdk/src/transactions/OrdTransaction.ts +++ b/packages/sdk/src/transactions/OrdTransaction.ts @@ -303,7 +303,7 @@ export class OrdTransaction { throw new Error("No commit address found. Please generate a commit address.") } - const outAmount = this.#outs.reduce((acc, cur) => (acc = +cur.value), 0) + const outAmount = this.#outs.reduce((acc, cur) => (acc += cur.value), 0) const amount = this.postage + this.#feeForWitnessData! + outAmount const utxos = await OrditApi.fetchSpendables({ @@ -313,7 +313,7 @@ export class OrdTransaction { type: this.#safeMode === "on" ? "spendable" : "all" }) - const suitableUTXO = utxos.find((utxo) => utxo.value === amount) + const suitableUTXO = utxos.find((utxo) => utxo.value >= amount) if (!suitableUTXO) { throw new Error("No suitable unspent found for reveal.") }