1.0.1 • Published 5 months ago

@rosen-bridge/bitcoin-utxo-selection v1.0.1

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
5 months ago

@rosen-bridge/bitcoin-utxo-selection

Table of contents

Introduction

A TypeScript package for selecting Bitcoin UTXOs to cover required amounts of BTC and fees, built on top of the abstract box selection framework.

Installation

npm:

npm i @rosen-bridge/bitcoin-utxo-selection

yarn:

yarn add @rosen-bridge/bitcoin-utxo-selection

Usage

Here's a basic example of how to use this package:

import { BitcoinBoxSelection } from '@rosen-bridge/bitcoin-utxo-selection';
import { DummyLogger } from '@rosen-bridge/abstract-logger';

// Create a fee estimator
// An example fee estimator for a transaction with only native-segwit inputs and outputs and 3 fee ratio
const feeEstimator = BitcoinBoxSelection.generateFeeEstimator(
  1, // additional output count
  42, // tx base weight
  272, // input weight unit
  124, // output weight unit
  3, // fee ratio (in satoshis per byte)
  4, // discount factor
);

// Create the selection instance
const selector = new BitcoinBoxSelection(new DummyLogger());

// Define required assets
const requiredAssets = {
  nativeToken: 6000000n, // 0.06 BTC
  tokens: [],
};

// Define UTXOs
const utxos: BitcoinUtxo[] = [
  { txId: 'tx1', index: 0, value: 5000000 }, // 0.05 BTC
  { txId: 'tx2', index: 0, value: 3000000 }, // 0.03 BTC
  { txId: 'tx3', index: 0, value: 2000000 }, // 0.02 BTC
];

// Get covering boxes
const result = await selector.getCoveringBoxes(
  requiredAssets,
  [], // forbidden box IDs
  new Map(), // track map
  utxos.values(),
  294n, // min box value
  undefined, // max token count (does not matter on Bitcoin, but still cannot be 0)
  feeEstimator,
);

if (result.covered) {
  console.log('Selected UTXOs:', result.boxes);
  console.log('Additional assets:', result.additionalAssets);
} else {
  console.log('Could not cover requirements');
}
1.0.1

5 months ago

1.0.0

5 months ago

0.3.0

11 months ago

0.3.0-4e36261b

5 months ago

0.2.1

1 year ago

0.2.2

1 year ago

0.2.0

2 years ago

0.1.0

2 years ago