Skip to content

Commit

Permalink
Ordinal transaction functions (#201)
Browse files Browse the repository at this point in the history
* added functions for sign ordinal tx and get fee

* fix test

* eslist disable for throwing error

* named params, added function to get correct utxo
  • Loading branch information
abdulhaseeb4239 authored Aug 9, 2023
1 parent b76adc3 commit 9d12451
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion transactions/btc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as btc from '@scure/btc-signer';
import BigNumber from 'bignumber.js';
import BitcoinEsploraApiProvider from '../api/esplora/esploraAPiProvider';
import { fetchBtcFeeRate } from '../api/xverse';
import { BtcFeeResponse, ErrorCodes, NetworkType, ResponseError, UTXO } from '../types';
import { BtcFeeResponse, ErrorCodes, Inscription, NetworkType, ResponseError, UTXO } from '../types';
import { getBtcPrivateKey, getBtcTaprootPrivateKey } from '../wallet';
import { selectOptimalUtxos } from './btc.utils';
import { BitcoinNetwork, getBtcNetwork } from './btcNetwork';
Expand Down Expand Up @@ -407,6 +407,11 @@ export function filterUtxos(allUtxos: UTXO[], filterUtxoSet: UTXO[]) {
);
}

// get correct ordinal utxo to send from address utxos
export function getOrdinalUtxo(addressUtxos: UTXO[], ordinal: Inscription): UTXO | undefined {
return addressUtxos.find((utxo) => `${utxo.txid}:${utxo.vout}` === ordinal.output);
}

// Used to calculate fees for setting low/high fee settings
// Should replace this function
export async function getBtcFeesForOrdinalSend(
Expand Down Expand Up @@ -471,6 +476,32 @@ export async function getBtcFeesForOrdinalSend(
}
}

export async function getBtcFeesForOrdinalTransaction(feeParams: {
recipientAddress: string;
btcAddress: string;
ordinalsAddress: string;
network: NetworkType;
ordinal: Inscription;
isRecover?: boolean;
feeMode?: string;
feeRateInput?: string;
}): Promise<{ fee: BigNumber; selectedFeeRate?: BigNumber }> {
const { recipientAddress, btcAddress, ordinalsAddress, network, ordinal, isRecover, feeMode, feeRateInput } =
feeParams;
const btcClient = new BitcoinEsploraApiProvider({
network,
});
const address = isRecover ? btcAddress : ordinalsAddress;
const addressUtxos = await btcClient.getUnspentUtxos(address);
const ordUtxo = getOrdinalUtxo(addressUtxos, ordinal);
if (!ordUtxo) {
// TODO: Throw error and not just the code
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new ResponseError(ErrorCodes.OrdinalUtxoNotfound).statusCode;
}
return getBtcFeesForOrdinalSend(recipientAddress, ordUtxo, btcAddress, network, addressUtxos, feeMode, feeRateInput);
}

// Used to calculate fees for setting low/high fee settings
// Should replace this function
export async function getBtcFeesForNonOrdinalBtcSend(
Expand Down Expand Up @@ -844,6 +875,42 @@ export async function signOrdinalSendTransaction(
return signedBtcTx;
}

export async function signOrdinalTransaction(ordinalTxParams: {
recipientAddress: string;
btcAddress: string;
ordinalsAddress: string;
accountIndex: number;
seedPhrase: string;
network: NetworkType;
ordinal: Inscription;
fee?: BigNumber;
isRecover?: boolean;
}): Promise<SignedBtcTx> {
const { recipientAddress, btcAddress, ordinalsAddress, accountIndex, seedPhrase, network, ordinal, fee, isRecover } =
ordinalTxParams;
const btcClient = new BitcoinEsploraApiProvider({
network,
});
const address = isRecover ? btcAddress : ordinalsAddress;
const addressUtxos = await btcClient.getUnspentUtxos(address);
const ordUtxo = getOrdinalUtxo(addressUtxos, ordinal);
if (!ordUtxo) {
// TODO: Throw error and not just the code
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new ResponseError(ErrorCodes.OrdinalUtxoNotfound).statusCode;
}
return signOrdinalSendTransaction(
recipientAddress,
ordUtxo,
btcAddress,
accountIndex,
seedPhrase,
network,
addressUtxos,
fee,
);
}

export async function signNonOrdinalBtcSendTransaction(
recipientAddress: string,
nonOrdinalUtxos: Array<UTXO>,
Expand Down

0 comments on commit 9d12451

Please sign in to comment.