Skip to content

Commit

Permalink
refactor: update refundable utxos picking logic
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcrazycoder committed Aug 14, 2023
1 parent a976575 commit ee64237
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions packages/sdk/src/inscription/instant-buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,25 @@ export async function generateBuyerPsbt({

postage = convertBTCToSatoshis(output.value)

const { totalUTXOs, spendableUTXOs } = await OrditApi.fetchUnspentUTXOs({ address: address.address!, network })
if (!totalUTXOs) {
throw new Error("No UTXOs found.")
}

const psbt = new bitcoin.Psbt({ network: networkObj })
const refundableUTXOs = []

// find refundableUTXOs utxos
for (let i = 0; i < spendableUTXOs.length; i++) {
const utxo = spendableUTXOs[i]

if (utxo.sats >= MINIMUM_AMOUNT_IN_SATS) {
refundableUTXOs.push(utxo)
}
}
const utxos = (
await OrditApi.fetchUnspentUTXOs({
address: address.address!,
network,
sort: "asc" // sort by ascending order to use low amount utxos as refundable utxos
})
).spendableUTXOs.filter((utxo) => utxo.sats >= MINIMUM_AMOUNT_IN_SATS)

if (refundableUTXOs.length < 2 || !spendableUTXOs.length) {
throw new Error("No suitable UTXOs found.")
// 3 = 2 refundables + 1 to cover for purchase
if (utxos.length < 3) {
throw new Error("No suitable UTXOs found")
}

const psbt = new bitcoin.Psbt({ network: networkObj })
let totalInput = 0

const witnessScripts: Buffer[] = []
const usedUTXOTxIds: string[] = []
for (let i = 0; i < 2; i++) {
const refundableUTXOs = [utxos[0]].concat(utxos[1])
for (let i = 0; i < refundableUTXOs.length; i++) {
const refundableUTXO = refundableUTXOs[i]
if (usedUTXOTxIds.includes(refundableUTXO.txid)) continue

Expand Down Expand Up @@ -133,8 +126,8 @@ export async function generateBuyerPsbt({
;(psbt.data.globalMap.unsignedTx as any).tx.outs[2] = (decodedSellerPsbt.data.globalMap.unsignedTx as any).tx.outs[0]
psbt.data.outputs[2] = decodedSellerPsbt.data.outputs[0]

for (let i = 0; i < spendableUTXOs.length; i++) {
const utxo = spendableUTXOs[i]
for (let i = 0; i < utxos.length; i++) {
const utxo = utxos[i]
if (usedUTXOTxIds.includes(utxo.txid)) continue

const input = await processInput({ utxo, pubKey: publicKey, network })
Expand All @@ -161,7 +154,7 @@ export async function generateBuyerPsbt({
throw new Error("Insufficient funds to buy this inscription")
}

if (changeValue > 580) {
if (changeValue > MINIMUM_AMOUNT_IN_SATS) {
psbt.addOutput({
address: address.address!,
value: changeValue
Expand Down

0 comments on commit ee64237

Please sign in to comment.