Skip to content

Commit

Permalink
refactor(sdk): performance improvements (#48)
Browse files Browse the repository at this point in the history
* perf: access collection inscription from tx response

Co-authored-by: KevinK <[email protected]>

* feat: extend publish/mint fn to accept RBF flag

* refactor(rpc): return detailed error message

* refactor: set `enableRBF` optional in createPsbt fn

---------

Co-authored-by: KevinK <[email protected]>
  • Loading branch information
iamcrazycoder and kevzzsk authored Aug 18, 2023
1 parent 1403ec9 commit 532f233
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/sdk/src/api/jsonrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class JsonRpc {
if (response.status === 200) {
const json = await response.json()
if (json.error) {
throw new Error(json.error.message)
throw new Error(json.error.data || json.error.message)
}
return json.result
}
Expand Down
17 changes: 7 additions & 10 deletions packages/sdk/src/inscription/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ export async function mintFromCollection(options: MintFromCollectionOptions) {
throw new Error("Failed to get raw transaction for id: " + colTxId)
}

const colMeta = tx.vout[colVOut].inscriptions[0].meta
const collection = tx.vout[colVOut].inscriptions[0]
if (!collection) {
throw new Error("Invalid collection")
}

const colMeta = collection.meta
let validInscription = false

for (let i = 0; i < colMeta?.insc.length; i++) {
Expand All @@ -70,15 +74,6 @@ export async function mintFromCollection(options: MintFromCollectionOptions) {
throw new Error("Invalid inscription iid supplied.")
}

const [collection] = await OrditApi.fetchInscriptions({
outpoint: options.collectionOutpoint,
network: options.network
})

if (!collection) {
throw new Error("Invalid collection")
}

const meta: any = {
p: "vord",
v: 1,
Expand Down Expand Up @@ -136,6 +131,7 @@ export type PublishCollectionOptions = Pick<GetWalletOptions, "safeMode"> & {
publicKey: string
outs?: Outputs
encodeMetadata?: boolean
enableRBF?: boolean
}

export type CollectionInscription = {
Expand All @@ -161,6 +157,7 @@ export type MintFromCollectionOptions = Pick<GetWalletOptions, "safeMode"> & {
outs?: Outputs
traits?: any
encodeMetadata?: boolean
enableRBF?: boolean
}

type Outputs = Array<{ address: string; value: number }>
6 changes: 5 additions & 1 deletion packages/sdk/src/transactions/OrdTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class OrdTransaction {
#outs: Outputs = []
#safeMode: OnOffUnion
#encodeMetadata: boolean
#enableRBF: boolean

constructor({
feeRate = 10,
Expand All @@ -50,6 +51,7 @@ export class OrdTransaction {
publicKey,
outs = [],
encodeMetadata = false,
enableRBF = true,
...otherOptions
}: OrdTransactionOptions) {
if (!publicKey || !otherOptions.changeAddress || !otherOptions.destination || !otherOptions.mediaContent) {
Expand All @@ -68,6 +70,7 @@ export class OrdTransaction {
this.#outs = outs
this.#safeMode = !otherOptions.safeMode ? "on" : otherOptions.safeMode
this.#encodeMetadata = encodeMetadata
this.#enableRBF = enableRBF

const xKey = getAddressesFromPublicKey(publicKey, network, "p2tr")[0].xkey

Expand Down Expand Up @@ -108,7 +111,7 @@ export class OrdTransaction {
const psbt = new bitcoin.Psbt({ network: networkObj })

psbt.addInput({
sequence: 0xfffffffd, // Needs to be at least 2 below max int value to be RBF
sequence: this.#enableRBF ? 0xfffffffd : 0xffffffff,
hash: this.#suitableUnspent.txid,
index: parseInt(this.#suitableUnspent.n),
tapInternalKey: Buffer.from(this.#xKey, "hex"),
Expand Down Expand Up @@ -337,6 +340,7 @@ export type OrdTransactionOptions = Pick<GetWalletOptions, "safeMode"> & {
publicKey: string
outs?: Outputs
encodeMetadata?: boolean
enableRBF?: boolean
}

type Outputs = Array<{ address: string; value: number }>
2 changes: 1 addition & 1 deletion packages/sdk/src/transactions/psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export type CreatePsbtOptions = {
address: string
cardinals: number
}[]
enableRBF: boolean
enableRBF?: boolean
pubKey: string
network: Network
safeMode?: OnOffUnion
Expand Down

0 comments on commit 532f233

Please sign in to comment.