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
123
124
125
126
127
128
129
use crate::{num_bigint, tx_mock::TxPanic, DebugApi};
use dharitri_wasm::{
    api::{CallValueApi, CallValueApiImpl, Handle},
    err_msg,
    types::DctTokenType,
};
use num_traits::Zero;

impl DebugApi {
    fn fail_if_more_than_one_dct_transfer(&self) {
        if self.dct_num_transfers() > 1 {
            std::panic::panic_any(TxPanic {
                status: 10,
                message: err_msg::TOO_MANY_DCT_TRANSFERS.to_string(),
            });
        }
    }
}

impl CallValueApi for DebugApi {
    type CallValueApiImpl = DebugApi;

    fn call_value_api_impl() -> Self::CallValueApiImpl {
        DebugApi::new_from_static()
    }
}

impl CallValueApiImpl for DebugApi {
    fn check_not_payable(&self) {
        if self.input_ref().moax_value > num_bigint::BigUint::zero() {
            std::panic::panic_any(TxPanic {
                status: 10,
                message: err_msg::NON_PAYABLE_FUNC_MOAX.to_string(),
            });
        }
        if self.dct_num_transfers() > 0 {
            std::panic::panic_any(TxPanic {
                status: 10,
                message: err_msg::NON_PAYABLE_FUNC_DCT.to_string(),
            });
        }
    }

    #[inline]
    fn load_moax_value(&self, dest: Handle) {
        self.set_big_uint(dest, self.input_ref().moax_value.clone())
    }

    #[inline]
    fn load_single_dct_value(&self, dest: Handle) {
        self.fail_if_more_than_one_dct_transfer();
        if let Some(dct_value) = self.input_ref().dct_values.get(0) {
            self.set_big_uint(dest, dct_value.value.clone());
        } else {
            std::panic::panic_any(TxPanic {
                status: 10,
                message: err_msg::DCT_INVALID_TOKEN_INDEX.to_string(),
            });
        }
    }

    #[inline]
    fn token(&self) -> Handle {
        self.fail_if_more_than_one_dct_transfer();
        self.token_by_index(0)
    }

    #[inline]
    fn dct_token_nonce(&self) -> u64 {
        self.fail_if_more_than_one_dct_transfer();
        self.dct_token_nonce_by_index(0)
    }

    #[inline]
    fn dct_token_type(&self) -> DctTokenType {
        self.fail_if_more_than_one_dct_transfer();
        self.dct_token_type_by_index(0)
    }

    #[inline]
    fn dct_num_transfers(&self) -> usize {
        self.input_ref().dct_values.len()
    }

    #[inline]
    fn dct_value_by_index(&self, index: usize) -> Handle {
        if let Some(dct_value) = self.input_ref().dct_values.get(index) {
            self.insert_new_big_uint(dct_value.value.clone())
        } else {
            std::panic::panic_any(TxPanic {
                status: 10,
                message: err_msg::DCT_INVALID_TOKEN_INDEX.to_string(),
            });
        }
    }

    #[inline]
    fn token_by_index(&self, index: usize) -> Handle {
        if let Some(dct_value) = self.input_ref().dct_values.get(index) {
            self.insert_new_managed_buffer(dct_value.token_identifier.clone())
        } else {
            std::panic::panic_any(TxPanic {
                status: 10,
                message: err_msg::DCT_INVALID_TOKEN_INDEX.to_string(),
            });
        }
    }

    #[inline]
    fn dct_token_nonce_by_index(&self, index: usize) -> u64 {
        if let Some(dct_value) = self.input_ref().dct_values.get(index) {
            dct_value.nonce
        } else {
            std::panic::panic_any(TxPanic {
                status: 10,
                message: err_msg::DCT_INVALID_TOKEN_INDEX.to_string(),
            });
        }
    }

    #[inline]
    fn dct_token_type_by_index(&self, index: usize) -> DctTokenType {
        if self.dct_token_nonce_by_index(index) == 0 {
            DctTokenType::Fungible
        } else {
            DctTokenType::NonFungible
        }
    }
}