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
use ergo_lib_c_core::{
    box_selector::*,
    collections::{CollectionPtr, ConstCollectionPtr},
    ergo_box::{ConstBoxValuePtr, ErgoBox, ErgoBoxAssetsData},
    token::Token,
    Error,
};

use paste::paste;

use crate::{delete_ptr, ErrorPtr};

// `BoxSelection` bindings -------------------------------------------------------------------------

/// Create a selection to easily inject custom selection algorithms
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_box_selection_new(
    ergo_boxes_ptr: ConstCollectionPtr<ErgoBox>,
    change_ergo_boxes_ptr: ConstCollectionPtr<ErgoBoxAssetsData>,
    box_selection_out: *mut BoxSelectionPtr,
) {
    #[allow(clippy::unwrap_used)]
    box_selection_new(ergo_boxes_ptr, change_ergo_boxes_ptr, box_selection_out).unwrap();
}

/// Selected boxes to spend as transaction inputs
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_box_selection_boxes(
    box_selection_ptr: ConstBoxSelectionPtr,
    ergo_boxes_out: *mut CollectionPtr<ErgoBox>,
) {
    #[allow(clippy::unwrap_used)]
    box_selection_boxes(box_selection_ptr, ergo_boxes_out).unwrap();
}

/// Selected boxes to use as change
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_box_selection_change(
    box_selection_ptr: ConstBoxSelectionPtr,
    change_ergo_boxes_out: *mut CollectionPtr<ErgoBoxAssetsData>,
) {
    #[allow(clippy::unwrap_used)]
    box_selection_change(box_selection_ptr, change_ergo_boxes_out).unwrap();
}

/// Drop `BoxSelection`
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_box_selection_delete(ptr: BoxSelectionPtr) {
    delete_ptr(ptr)
}

make_ffi_eq!(BoxSelection);

// `SimpleBoxSelector` bindings ---------------------------------------------------------------------

/// Naive box selector, collects inputs until target balance is reached
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_simple_box_selector_new(
    simple_box_selector_out: *mut SimpleBoxSelectorPtr,
) {
    #[allow(clippy::unwrap_used)]
    simple_box_selector_new(simple_box_selector_out).unwrap();
}

/// Selects inputs to satisfy target balance and tokens.
/// `inputs` - available inputs (returns an error, if empty),
/// `target_balance` - coins (in nanoERGs) needed,
/// `target_tokens` - amount of tokens needed.
/// Returns selected inputs and box assets(value+tokens) with change.
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_simple_box_selector_select(
    simple_box_selector_ptr: ConstSimpleBoxSelectorPtr,
    inputs_ptr: ConstCollectionPtr<ErgoBox>,
    target_balance_ptr: ConstBoxValuePtr,
    target_tokens_ptr: ConstCollectionPtr<Token>,
    box_selection_out: *mut BoxSelectionPtr,
) -> ErrorPtr {
    let res = simple_box_selector_select(
        simple_box_selector_ptr,
        inputs_ptr,
        target_balance_ptr,
        target_tokens_ptr,
        box_selection_out,
    );
    Error::c_api_from(res)
}

/// Drop `SimpleBoxSelector`
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_simple_box_selector_delete(ptr: SimpleBoxSelectorPtr) {
    delete_ptr(ptr)
}