-
Notifications
You must be signed in to change notification settings - Fork 46
/
tx_builder.rs
61 lines (56 loc) · 2.12 KB
/
tx_builder.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! Unsigned transaction builder
use ergo_lib::{chain, wallet};
use wasm_bindgen::prelude::*;
use crate::data_input::DataInputs;
use crate::{
address::Address,
box_coll::{ErgoBoxCandidates, ErgoBoxes},
box_selector::BoxSelector,
ergo_box::BoxValue,
transaction::UnsignedTransaction,
};
/// Unsigned transaction builder
#[wasm_bindgen]
pub struct TxBuilder(wallet::tx_builder::TxBuilder<chain::ergo_box::ErgoBox>);
#[wasm_bindgen]
impl TxBuilder {
/// Creates new TxBuilder
/// `box_selector` - input box selection algorithm to choose inputs from `boxes_to_spend`,
/// `boxes_to_spend` - spendable boxes,
/// `output_candidates` - output boxes to be "created" in this transaction,
/// `current_height` - chain height that will be used in additionally created boxes (change, miner's fee, etc.),
/// `fee_amount` - miner's fee,
/// `change_address` - change (inputs - outputs) will be sent to this address,
/// `min_change_value` - minimal value of the change to be sent to `change_address`, value less than that
/// will be given to miners,
pub fn new(
box_selector: BoxSelector,
inputs: &ErgoBoxes,
output_candidates: &ErgoBoxCandidates,
current_height: u32,
fee_amount: &BoxValue,
change_address: &Address,
min_change_value: &BoxValue,
) -> TxBuilder {
TxBuilder(ergo_lib::wallet::tx_builder::TxBuilder::new(
box_selector.inner::<chain::ergo_box::ErgoBox>(),
inputs.clone().into(),
output_candidates.clone().into(),
current_height,
fee_amount.clone().into(),
change_address.clone().into(),
min_change_value.clone().into(),
))
}
/// Set transaction's data inputs
pub fn set_data_inputs(&mut self, data_inputs: &DataInputs) {
self.0.set_data_inputs(data_inputs.into())
}
/// Build the unsigned transaction
pub fn build(self) -> Result<UnsignedTransaction, JsValue> {
self.0
.build()
.map_err(|e| JsValue::from_str(&format!("{}", e)))
.map(UnsignedTransaction::from)
}
}