Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ordinal transaction functions #201

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { BitcoinNetwork, getBtcNetwork } from './btcNetwork';

Expand Down Expand Up @@ -405,6 +405,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 @@ -469,6 +474,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 @@ -789,6 +820,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