Skip to content

Commit

Permalink
fix(sdk): allow higher value utxos and return change (#45)
Browse files Browse the repository at this point in the history
* refactor: allow higher (wrong) value utxos

* fix: consolidate out amount instead of parsing to int

* feat: return sell price, postage for buyers clarity
  • Loading branch information
iamcrazycoder authored Aug 16, 2023
1 parent f21e870 commit 39ebcc8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/sdk/src/inscription/instant-buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -166,7 +168,9 @@ export async function generateBuyerPsbt({
return {
hex: psbt.toHex(),
base64: psbt.toBase64(),
fee
fee,
postage,
sellPrice
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/transactions/OrdTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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.")
}
Expand Down

0 comments on commit 39ebcc8

Please sign in to comment.