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
//! Contract, for easier ErgoTree generation
use ergo_lib_c_core::{
    address::ConstAddressPtr,
    contract::*,
    ergo_tree::{ConstErgoTreePtr, ErgoTreePtr},
    Error, ErrorPtr,
};
use paste::paste;

use std::{ffi::CStr, os::raw::c_char};

use crate::delete_ptr;

/// Create new contract from ErgoTree
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_contract_new(
    ergo_tree_ptr: ConstErgoTreePtr,
    contract_out: *mut ContractPtr,
) {
    #[allow(clippy::unwrap_used)]
    contract_new(ergo_tree_ptr, contract_out).unwrap();
}

/// Create new contract that allow spending of the guarded box by a given recipient
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_contract_pay_to_address(
    address_ptr: ConstAddressPtr,
    contract_out: *mut ContractPtr,
) -> ErrorPtr {
    let res = contract_pay_to_address(address_ptr, contract_out);
    Error::c_api_from(res)
}

/// Compiles a contract from ErgoScript source code
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_contract_compile(
    source: *const c_char,
    contract_out: *mut ContractPtr,
) -> ErrorPtr {
    let source = CStr::from_ptr(source).to_string_lossy();
    let res = contract_compile(&source, contract_out);
    Error::c_api_from(res)
}

/// Get the ErgoTree of the contract
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_contract_ergo_tree(
    contract_ptr: ConstContractPtr,
    ergo_tree_out: *mut ErgoTreePtr,
) {
    #[allow(clippy::unwrap_used)]
    contract_ergo_tree(contract_ptr, ergo_tree_out).unwrap();
}

/// Drop `Contract`
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_contract_delete(ptr: ContractPtr) {
    delete_ptr(ptr)
}

make_ffi_eq!(Contract);