Skip to content

Commit

Permalink
add JSDoc to qi hdwallet methods
Browse files Browse the repository at this point in the history
  • Loading branch information
alejoacosta74 authored and rileystephens28 committed Nov 27, 2024
1 parent cc7194a commit 45845e2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
22 changes: 20 additions & 2 deletions src/transaction/abstract-coinselector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export type SelectedCoinsResult = {
* @category Transaction
* @abstract
*/
export interface CoinSelectionConfig {
target?: bigint;
fee?: bigint;
includeLocked?: boolean;
// Any future parameters can be added here
}

export abstract class AbstractCoinSelector {
public availableUTXOs: UTXO[];
public totalInputValue: bigint = BigInt(0);
Expand Down Expand Up @@ -65,10 +72,10 @@ export abstract class AbstractCoinSelector {
* and change outputs.
*
* @abstract
* @param {SpendTarget} target - The target address and value to spend.
* @param {CoinSelectionConfig} config - The configuration for coin selection.
* @returns {SelectedCoinsResult} The selected UTXOs and outputs.
*/
abstract performSelection(target: bigint, fee: bigint): SelectedCoinsResult;
abstract performSelection(config: CoinSelectionConfig): SelectedCoinsResult;

/**
* Validates the provided UTXO instance. In order to be valid for coin selection, the UTXO must have a valid address
Expand All @@ -95,4 +102,15 @@ export abstract class AbstractCoinSelector {
throw new Error('UTXO index is required');
}
}

/**
* Validates the available UTXOs.
*
* @throws Will throw an error if there are no available UTXOs.
*/
protected validateUTXOs() {
if (this.availableUTXOs.length === 0) {
throw new Error('No UTXOs available');
}
}
}
17 changes: 4 additions & 13 deletions src/transaction/coinselector-fewest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// import { bigIntAbs } from '../utils/maths.js';
import { AbstractCoinSelector, SelectedCoinsResult } from './abstract-coinselector.js';
import { AbstractCoinSelector, CoinSelectionConfig, SelectedCoinsResult } from './abstract-coinselector.js';
import { UTXO, denominate, denominations } from './utxo.js';

/**
Expand All @@ -21,7 +21,9 @@ export class FewestCoinSelector extends AbstractCoinSelector {
* @param {bigint} fee - The fee amount to include in the selection.
* @returns {SelectedCoinsResult} The selected UTXOs and outputs.
*/
performSelection(target: bigint, fee: bigint = BigInt(0)): SelectedCoinsResult {
performSelection(config: CoinSelectionConfig): SelectedCoinsResult {
const { target = BigInt(0), fee = BigInt(0) } = config;

if (target <= BigInt(0)) {
throw new Error('Target amount must be greater than 0');
}
Expand Down Expand Up @@ -353,15 +355,4 @@ export class FewestCoinSelector extends AbstractCoinSelector {
return diff > BigInt(0) ? 1 : diff < BigInt(0) ? -1 : 0;
});
}

/**
* Validates the available UTXOs.
*
* @throws Will throw an error if there are no available UTXOs.
*/
private validateUTXOs() {
if (this.availableUTXOs.length === 0) {
throw new Error('No UTXOs available');
}
}
}
25 changes: 23 additions & 2 deletions src/wallet/qi-hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ export class QiHDWallet extends AbstractHDWallet<QiAddressInfo> {
const fewestCoinSelector = new FewestCoinSelector(unlockedUTXOs);

const spendTarget: bigint = amount;
let selection = fewestCoinSelector.performSelection(spendTarget);
let selection = fewestCoinSelector.performSelection({ target: spendTarget });

// 3. Generate as many unused addresses as required to populate the spend outputs
const sendAddresses = await getDestinationAddresses(selection.spendOutputs.length);
Expand Down Expand Up @@ -702,7 +702,7 @@ export class QiHDWallet extends AbstractHDWallet<QiAddressInfo> {
finalFee = await this.provider.estimateFeeForQi(feeEstimationTx);

// Get new selection with updated fee 2x
selection = fewestCoinSelector.performSelection(spendTarget, finalFee * 3n);
selection = fewestCoinSelector.performSelection({ target: spendTarget, fee: finalFee * 3n });

// Determine if new addresses are needed for the change outputs
const changeAddressesNeeded = selection.changeOutputs.length - changeAddresses.length;
Expand Down Expand Up @@ -862,6 +862,17 @@ export class QiHDWallet extends AbstractHDWallet<QiAddressInfo> {
return this.prepareAndSendTransaction(amount, originZone, getDestinationAddresses);
}

/**
* Prepares a transaction with the specified parameters.
*
* @private
* @param {SelectedCoinsResult} selection - The selected coins result.
* @param {string[]} inputPubKeys - The public keys of the inputs.
* @param {string[]} sendAddresses - The addresses to send to.
* @param {string[]} changeAddresses - The addresses to change to.
* @param {number} chainId - The chain ID.
* @returns {Promise<QiTransaction>} A promise that resolves to the prepared transaction.
*/
private async prepareTransaction(
selection: SelectedCoinsResult,
inputPubKeys: string[],
Expand Down Expand Up @@ -895,6 +906,16 @@ export class QiHDWallet extends AbstractHDWallet<QiAddressInfo> {
return tx;
}

/**
* Prepares a fee estimation transaction with the specified parameters.
*
* @private
* @param {SelectedCoinsResult} selection - The selected coins result.
* @param {string[]} inputPubKeys - The public keys of the inputs.
* @param {string[]} sendAddresses - The addresses to send to.
* @param {string[]} changeAddresses - The addresses to change to.
* @returns {QiPerformActionTransaction} The prepared transaction.
*/
private prepareFeeEstimationTransaction(
selection: SelectedCoinsResult,
inputPubKeys: string[],
Expand Down

0 comments on commit 45845e2

Please sign in to comment.