Skip to content

Commit

Permalink
[enhancement] Readme inclusion, update and Docs.rs imp
Browse files Browse the repository at this point in the history
Signed-off-by: neoz666 <neoz.blockchain@gmail.com>
  • Loading branch information
NeoZ666 committed Feb 10, 2025
1 parent 0c68071 commit f7548fd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,37 @@ Bitcoin specific example is given [here](./examples/bitcoin_crate/).
An example usage is given below

```rust
let utxos: Vec<UTXO> = vec![<utxo1>, <utxo2>, ..., <utxon>]; // List of the available UTXOs
let output_groups: Vec<OutputGroup> = utxos.iter().map(|utxo| convert_utxo_to_output(utxo)).collect();
use rust_coinselect::{
types::{CoinSelectionOpt, ExcessStrategy, OutputGroup},
selectcoin::select_coin,
};

// List of the available UTXOs
// let utxos: Vec<UTXO> = vec![<utxo1>, <utxo2>, ..., <utxon>];

let output_groups = vec![
OutputGroup { value: 1_000_000, weight: 100, input_count: 1, creation_sequence: None },
OutputGroup { value: 2_000_000, weight: 100, input_count: 1, creation_sequence: None },
];

let options = CoinSelectionOpt {
target_value: 4_000_000u64,
target_fee_rate: 0.5f32,
target_value: 1_500_000u64,
target_feerate: 0.5f32,
long_term_feerate: Some(0.3f32),
min_absolute_fee: 1000u64,
base_weight: 72u64,
change_weight: 18u64,
change_cost: 250u64,
cost_per_input: 300u64,
cost_per_output: 250u64,
avg_input_weight: 300u64,
avg_output_weight: 250u64,
min_change_value: 1_000u64,
excess_strategy: ExcessStrategy::ToChange,
};

let selection_output = select_coin(&output_groups, options);
println!("Estimated waste = {}", selection_output.waste);
println!("Indexes of the selected utxos = {}", selection_output.selected_inputs);
if let Ok(selection_output) = select_coin(&output_groups, &options) {
println!("Indexes of the selected utxos = {:?}", selection_output.selected_inputs);
}

let selected_utxos: Vec<UTXO> = selection_output.iter().map(|index| utxos[index]).collect();
```

The `convert_utxo_to_output` logic should be implemented by the user for the respective blockchain protocol.
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#![doc = include_str!("../README.md")]
#![doc = include_str!("../README.md")]

/// Collection of coin selection algorithms including Knapsack, Branch and Bound (BNB), First-In First-Out (FIFO), Single-Random-Draw (SRD), and Lowest Larger
pub mod algorithms;
/// Wrapper API that runs all coin selection algorithms in parallel and returns the result with lowest waste
pub mod selectcoin;
/// Core types and structs used throughout the library including OutputGroup and CoinSelectionOpt
pub mod types;
/// Helper functions with tests for fee calculation, weight computation, and waste metrics
pub mod utils;

0 comments on commit f7548fd

Please sign in to comment.