ergo_lib/chain/
contract.rs

1//! Ergo contract
2
3use ergotree_ir::chain::address::Address;
4use ergotree_ir::ergo_tree::ErgoTree;
5use ergotree_ir::serialization::SigmaParsingError;
6
7/// High-level wrapper for ErgoTree
8#[derive(PartialEq, Eq, Debug, Clone)]
9pub struct Contract {
10    ergo_tree: ErgoTree,
11}
12
13impl Contract {
14    /// create new contract from ErgoTree
15    pub fn new(ergo_tree: ErgoTree) -> Contract {
16        Contract { ergo_tree }
17    }
18
19    /// create new contract that allow spending for a given Address
20    pub fn pay_to_address(address: &Address) -> Result<Contract, SigmaParsingError> {
21        Ok(Contract::new(address.script()?))
22    }
23
24    /// get ErgoTree for this contract
25    pub fn ergo_tree(&self) -> ErgoTree {
26        self.ergo_tree.clone()
27    }
28
29    /// Compiles a contract from ErgoScript source code
30    #[cfg(feature = "compiler")]
31    pub fn compile(
32        source: &str,
33        env: ergoscript_compiler::script_env::ScriptEnv,
34    ) -> Result<Contract, ergoscript_compiler::compiler::CompileError> {
35        let ergo_tree = ergoscript_compiler::compiler::compile(source, env)?;
36        Ok(Contract { ergo_tree })
37    }
38}
39
40#[cfg(test)]
41#[allow(clippy::unwrap_used)]
42mod tests {
43    use super::*;
44
45    #[cfg(feature = "compiler")]
46    #[test]
47    fn compile() {
48        let contract =
49            Contract::compile("HEIGHT", ergoscript_compiler::script_env::ScriptEnv::new()).unwrap();
50        dbg!(&contract);
51    }
52}