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
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! ErgoBoxCandidate builder
use ergo_lib::chain;
use wasm_bindgen::prelude::*;

use crate::ast::Constant;
use crate::contract::Contract;
use crate::token::Token;
use crate::token::TokenAmount;
use crate::token::TokenId;

use super::BoxValue;
use super::ErgoBoxCandidate;
use super::NonMandatoryRegisterId;

/// ErgoBoxCandidate builder
#[wasm_bindgen]
pub struct ErgoBoxCandidateBuilder(chain::ergo_box::box_builder::ErgoBoxCandidateBuilder);

#[wasm_bindgen]
impl ErgoBoxCandidateBuilder {
    /// Create builder with required box parameters:
    /// `value` - amount of money associated with the box
    /// `contract` - guarding contract([`Contract`]), which should be evaluated to true in order
    /// to open(spend) this box
    /// `creation_height` - height when a transaction containing the box is created.
    /// It should not exceed height of the block, containing the transaction with this box.
    #[wasm_bindgen(constructor)]
    pub fn new(value: &BoxValue, contract: &Contract, creation_height: u32) -> Self {
        ErgoBoxCandidateBuilder(chain::ergo_box::box_builder::ErgoBoxCandidateBuilder::new(
            value.clone().into(),
            chain::contract::Contract::from(contract.clone()).ergo_tree(),
            creation_height,
        ))
    }

    /// Set minimal value (per byte of the serialized box size)
    pub fn set_min_box_value_per_byte(&mut self, new_min_value_per_byte: u32) {
        self.0.set_min_box_value_per_byte(new_min_value_per_byte);
    }

    /// Get minimal value (per byte of the serialized box size)
    pub fn min_box_value_per_byte(&self) -> u32 {
        self.0.min_box_value_per_byte()
    }

    /// Set new box value
    pub fn set_value(&mut self, new_value: BoxValue) {
        self.0.set_value(new_value.into());
    }

    /// Get box value
    pub fn value(&self) -> BoxValue {
        (*self.0.value()).into()
    }

    /// Calculate serialized box size(in bytes)
    pub fn calc_box_size_bytes(&self) -> Result<usize, JsValue> {
        self.0
            .calc_box_size_bytes()
            .map_err(|e| JsValue::from_str(&format!("{}", e)))
    }

    /// Calculate minimal box value for the current box serialized size(in bytes)
    pub fn calc_min_box_value(&self) -> Result<BoxValue, JsValue> {
        self.0
            .calc_min_box_value()
            .map(BoxValue::from)
            .map_err(|e| JsValue::from_str(&format!("{}", e)))
    }

    /// Set register with a given id (R4-R9) to the given value
    pub fn set_register_value(&mut self, register_id: NonMandatoryRegisterId, value: &Constant) {
        self.0
            .set_register_value(register_id.into(), value.clone().into());
    }

    /// Returns register value for the given register id (R4-R9), or None if the register is empty
    pub fn register_value(&self, register_id: NonMandatoryRegisterId) -> Option<Constant> {
        self.0
            .register_value(&register_id.into())
            .cloned()
            .map(Constant::from)
    }

    /// Delete register value(make register empty) for the given register id (R4-R9)
    pub fn delete_register_value(&mut self, register_id: NonMandatoryRegisterId) {
        self.0.delete_register_value(&register_id.into());
    }

    /// Mint token, as defined in https://github.com/ergoplatform/eips/blob/master/eip-0004.md
    /// `token` - token id(box id of the first input box in transaction) and token amount,
    /// `token_name` - token name (will be encoded in R4),
    /// `token_desc` - token description (will be encoded in R5),
    /// `num_decimals` - number of decimals (will be encoded in R6)
    pub fn mint_token(
        &mut self,
        token: &Token,
        token_name: String,
        token_desc: String,
        num_decimals: usize,
    ) {
        self.0
            .mint_token(token.clone().into(), token_name, token_desc, num_decimals);
    }

    /// Add given token id and token amount
    pub fn add_token(&mut self, token_id: &TokenId, amount: &TokenAmount) {
        self.0.add_token(chain::token::Token {
            token_id: token_id.clone().into(),
            amount: amount.clone().into(),
        });
    }

    /// Build the box candidate
    pub fn build(&self) -> Result<ErgoBoxCandidate, JsValue> {
        self.0
            .clone()
            .build()
            .map_err(|e| JsValue::from_str(&format!("{}", e)))
            .map(ErgoBoxCandidate)
    }
}