Skip to content

Commit

Permalink
feat(fee): add instant trade buyer to accept general outputs (#113)
Browse files Browse the repository at this point in the history
* feat: add secondary fee to instant trade buyer txn

* refactor to accept general outputs

* revert unecessary file change

* update outputs index

* rename var
  • Loading branch information
JoshuaC817 authored Mar 11, 2024
1 parent e8a363f commit 1bd6860
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/sdk/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export const MAXIMUM_FEE = 500_000_000
export const MAXIMUM_SCRIPT_ELEMENT_SIZE = 520

// Maximum royalty percentage
export const MAXIMUM_ROYALTY_PERCENTAGE = 0.2
export const MAXIMUM_ROYALTY_PERCENTAGE = 0.2
23 changes: 16 additions & 7 deletions packages/sdk/src/instant-trade/InstantTradeBuyerTxBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Psbt } from "bitcoinjs-lib"
import reverseBuffer from "buffer-reverse"

import { decodePSBT, generateTxUniqueIdentifier, getScriptType, INSTANT_BUY_SELLER_INPUT_INDEX } from ".."
import { decodePSBT, generateTxUniqueIdentifier, getScriptType, INSTANT_BUY_SELLER_INPUT_INDEX, Output } from ".."
import { MINIMUM_AMOUNT_IN_SATS } from "../constants"
import { InjectableInput, InjectableOutput } from "../transactions/PSBTBuilder"
import { OrditSDKError } from "../utils/errors"
Expand All @@ -10,12 +10,14 @@ import InstantTradeBuilder, { InstantTradeBuilderArgOptions } from "./InstantTra
interface InstantTradeBuyerTxBuilderArgOptions extends InstantTradeBuilderArgOptions {
sellerPSBT: string
receiveAddress?: string
outputs?: Output[]
}

export default class InstantTradeBuyerTxBuilder extends InstantTradeBuilder {
private receiveAddress?: string
private sellerPSBT!: Psbt
private sellerAddress?: string
private incomingOutputs?: Output[]

constructor({
address,
Expand All @@ -24,18 +26,20 @@ export default class InstantTradeBuyerTxBuilder extends InstantTradeBuilder {
receiveAddress,
sellerPSBT,
feeRate,
datasource
datasource,
outputs
}: InstantTradeBuyerTxBuilderArgOptions) {
super({
address,
datasource,
network,
publicKey,
feeRate
feeRate,
})

this.receiveAddress = receiveAddress
this.decodeSellerPSBT(sellerPSBT)
this.incomingOutputs = outputs ?? []
}

private decodeSellerPSBT(hex: string) {
Expand Down Expand Up @@ -75,12 +79,10 @@ export default class InstantTradeBuyerTxBuilder extends InstantTradeBuilder {
}

private bindRefundableOutput() {
this.outputs = [
{
this.outputs = [{
address: this.address,
value: this.utxos.reduce((acc, curr, index) => (acc += [0, 1].includes(index) ? curr.sats : 0), 0)
}
]
}]
}

private bindInscriptionOutput() {
Expand All @@ -90,6 +92,12 @@ export default class InstantTradeBuyerTxBuilder extends InstantTradeBuilder {
})
}

private bindIncomingOutputs() {
if (!!this.incomingOutputs) {
this.outputs = [...this.outputs, ...this.incomingOutputs]
}
}

private mergePSBTs() {
const hash = reverseBuffer(this.sellerPSBT.txInputs[0].hash).toString("hex")
const index = this.sellerPSBT.txInputs[0].index
Expand Down Expand Up @@ -171,6 +179,7 @@ export default class InstantTradeBuyerTxBuilder extends InstantTradeBuilder {
this.decodeRoyalty()
this.bindRefundableOutput()
this.bindInscriptionOutput()
this.bindIncomingOutputs()
this.mergePSBTs()

await this.prepare()
Expand Down

0 comments on commit 1bd6860

Please sign in to comment.