Skip to content

Commit

Permalink
chore: use interface (type) for coinselection algorithymns
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-sanderson committed Oct 29, 2024
1 parent 41e1bb2 commit 5707a4d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
21 changes: 8 additions & 13 deletions packages/utxo-lib/src/coinselect/inputs/accumulative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@ import {
finalize,
ZERO,
} from '../coinselectUtils';
import {
CoinSelectOptions,
CoinSelectInput,
CoinSelectOutput,
CoinSelectResult,
} from '../../types';
import { CoinSelectInput, CoinSelectResult, CoinSelectAlgorithm } from '../../types';

// add inputs until we reach or surpass the target value (or deplete)
// worst-case: O(n)
export function accumulative(
utxos0: CoinSelectInput[],
outputs: CoinSelectOutput[],
feeRate: number,
options: CoinSelectOptions,
): CoinSelectResult {
export const accumulative: CoinSelectAlgorithm = (
utxos0,
outputs,
feeRate,
options,
): CoinSelectResult => {
let inAccum = ZERO;
const inputs: CoinSelectInput[] = [];
const outAccum = sumOrNaN(outputs);
Expand Down Expand Up @@ -80,4 +75,4 @@ export function accumulative(
const fee = getFee(inputs, outputs, feeRate, options);

return { fee };
}
};
16 changes: 3 additions & 13 deletions packages/utxo-lib/src/coinselect/inputs/bnb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ import {
ZERO,
OUTPUT_SCRIPT_LENGTH,
} from '../coinselectUtils';
import {
CoinSelectInput,
CoinSelectOutput,
CoinSelectOptions,
CoinSelectResult,
} from '../../types';
import { CoinSelectInput, CoinSelectResult, CoinSelectAlgorithm } from '../../types';

const MAX_TRIES = 1000000;

Expand Down Expand Up @@ -132,12 +127,7 @@ function search(
* https://github.com/bitcoin/bitcoin/blob/b2ec0326fd76e64a6d0d7e4745506b29f60d0be5/src/wallet/coinselection.cpp
*/

export function bnb(
utxos: CoinSelectInput[],
outputs: CoinSelectOutput[],
feeRate: number,
options: CoinSelectOptions,
): CoinSelectResult {
export const bnb: CoinSelectAlgorithm = (utxos, outputs, feeRate, options): CoinSelectResult => {
if (options.baseFee) return { fee: 0 }; // TEMP: disable bnb algorithm for DOGE
if (utxos.find(u => u.required)) return { fee: 0 }; // TODO: enable bnb algorithm if required utxos are defined

Expand Down Expand Up @@ -200,4 +190,4 @@ export function bnb(
}

return { fee: 0 };
}
};

0 comments on commit 5707a4d

Please sign in to comment.