ergo_lib_wasm/
contract.rs

1//! Contract, for easier ErgoTree generation
2use ergo_lib::chain;
3use wasm_bindgen::prelude::*;
4
5use crate::address::Address;
6use crate::ergo_tree::ErgoTree;
7use crate::error_conversion::to_js;
8
9/// Defines the contract(script) that will be guarding box contents
10#[wasm_bindgen]
11#[derive(PartialEq, Eq, Debug, Clone)]
12pub struct Contract(chain::contract::Contract);
13
14#[wasm_bindgen]
15impl Contract {
16    /// Create new contract from ErgoTree
17    pub fn new(ergo_tree: ErgoTree) -> Contract {
18        Contract(chain::contract::Contract::new(ergo_tree.into()))
19    }
20
21    /// create new contract that allow spending of the guarded box by a given recipient ([`Address`])
22    pub fn pay_to_address(recipient: &Address) -> Result<Contract, JsValue> {
23        chain::contract::Contract::pay_to_address(&recipient.clone().into())
24            .map_err(to_js)
25            .map(Contract)
26    }
27
28    /// Compiles a contract from ErgoScript source code
29    #[cfg(feature = "compiler")]
30    pub fn compile(source: &str) -> Result<Contract, JsValue> {
31        chain::contract::Contract::compile(
32            source,
33            ergo_lib::ergoscript_compiler::script_env::ScriptEnv::new(),
34        )
35        .map_err(|e| JsValue::from_str(e.pretty_desc(source).as_str()))
36        .map(Contract)
37    }
38
39    /// Get the ErgoTree of the contract
40    pub fn ergo_tree(&self) -> ErgoTree {
41        self.0.ergo_tree().into()
42    }
43}
44
45impl From<Contract> for chain::contract::Contract {
46    fn from(c: Contract) -> Self {
47        c.0
48    }
49}