use ergo_lib::ergotree_ir::chain;
use ergo_lib::wallet;
use ergo_lib::wallet::box_selector::BoxSelector;
use wasm_bindgen::prelude::*;
use crate::box_coll::ErgoBoxes;
use crate::ergo_box::BoxValue;
use crate::ergo_box::ErgoBoxAssetsDataList;
use crate::error_conversion::to_js;
use crate::token::Tokens;
use bounded_vec::BoundedVec;
extern crate derive_more;
use derive_more::{From, Into};
#[wasm_bindgen]
#[derive(PartialEq, Eq, Debug, Clone, From, Into)]
pub struct BoxSelection(
wallet::box_selector::BoxSelection<ergo_lib::ergotree_ir::chain::ergo_box::ErgoBox>,
);
#[wasm_bindgen]
impl BoxSelection {
#[wasm_bindgen(constructor)]
pub fn new(boxes: &ErgoBoxes, change: &ErgoBoxAssetsDataList) -> Result<BoxSelection, JsValue> {
Ok(BoxSelection(wallet::box_selector::BoxSelection::<
ergo_lib::ergotree_ir::chain::ergo_box::ErgoBox,
> {
boxes: BoundedVec::from_vec(boxes.clone().into()).map_err(to_js)?,
change_boxes: change.clone().into(),
}))
}
pub fn boxes(&self) -> ErgoBoxes {
self.0.boxes.as_vec().clone().into()
}
pub fn change(&self) -> ErgoBoxAssetsDataList {
self.0.change_boxes.clone().into()
}
}
#[wasm_bindgen]
pub struct SimpleBoxSelector(wallet::box_selector::SimpleBoxSelector);
#[wasm_bindgen]
impl SimpleBoxSelector {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
SimpleBoxSelector(wallet::box_selector::SimpleBoxSelector::new())
}
pub fn select(
&self,
inputs: &ErgoBoxes,
target_balance: &BoxValue,
target_tokens: &Tokens,
) -> Result<BoxSelection, JsValue> {
self.0
.select(
inputs.clone().into(),
target_balance.clone().into(),
target_tokens
.clone()
.0
.into_iter()
.map(|t| t.into())
.collect::<Vec<chain::token::Token>>()
.as_slice(),
)
.map_err(to_js)
.map(BoxSelection)
}
}