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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//! Functions for interacting with the system contracts.

use alloc::vec::Vec;
use core::mem::MaybeUninit;

use casper_types::{
    account::AccountHash,
    api_error, bytesrepr,
    system::{
        auction::{self, EraInfo},
        SystemContractType,
    },
    ApiError, ContractHash, EraId, HashAddr, PublicKey, TransferResult, TransferredTo, URef, U512,
    UREF_SERIALIZED_LENGTH,
};

use crate::{
    contract_api::{self, account, runtime},
    ext_ffi,
    unwrap_or_revert::UnwrapOrRevert,
};

fn get_system_contract(system_contract: SystemContractType) -> ContractHash {
    let system_contract_index = system_contract.into();
    let contract_hash: ContractHash = {
        let result = {
            let mut hash_data_raw: HashAddr = ContractHash::default().value();
            let value = unsafe {
                ext_ffi::casper_get_system_contract(
                    system_contract_index,
                    hash_data_raw.as_mut_ptr(),
                    hash_data_raw.len(),
                )
            };
            api_error::result_from(value).map(|_| hash_data_raw)
        };
        // Revert for any possible error that happened on host side
        #[allow(clippy::redundant_closure)] // false positive
        let contract_hash_bytes = result.unwrap_or_else(|e| runtime::revert(e));
        // Deserializes a valid URef passed from the host side
        bytesrepr::deserialize(contract_hash_bytes.to_vec()).unwrap_or_revert()
    };
    contract_hash
}

/// Returns a read-only pointer to the Mint contract.
///
/// Any failure will trigger [`revert`](runtime::revert) with an appropriate [`ApiError`].
pub fn get_mint() -> ContractHash {
    get_system_contract(SystemContractType::Mint)
}

/// Returns a read-only pointer to the Handle Payment contract.
///
/// Any failure will trigger [`revert`](runtime::revert) with an appropriate [`ApiError`].
pub fn get_handle_payment() -> ContractHash {
    get_system_contract(SystemContractType::HandlePayment)
}

/// Returns a read-only pointer to the Standard Payment contract.
///
/// Any failure will trigger [`revert`](runtime::revert) with an appropriate [`ApiError`].
pub fn get_standard_payment() -> ContractHash {
    get_system_contract(SystemContractType::StandardPayment)
}

/// Returns a read-only pointer to the Auction contract.
///
/// Any failure will trigger [`revert`](runtime::revert) with appropriate [`ApiError`].
pub fn get_auction() -> ContractHash {
    get_system_contract(SystemContractType::Auction)
}

/// Creates a new empty purse and returns its [`URef`].
pub fn create_purse() -> URef {
    let purse_non_null_ptr = contract_api::alloc_bytes(UREF_SERIALIZED_LENGTH);
    let ret = unsafe {
        ext_ffi::casper_create_purse(purse_non_null_ptr.as_ptr(), UREF_SERIALIZED_LENGTH)
    };
    api_error::result_from(ret).unwrap_or_revert();
    let bytes = unsafe {
        Vec::from_raw_parts(
            purse_non_null_ptr.as_ptr(),
            UREF_SERIALIZED_LENGTH,
            UREF_SERIALIZED_LENGTH,
        )
    };
    bytesrepr::deserialize(bytes).unwrap_or_revert()
}

/// Returns the balance in motes of the given purse.
pub fn get_purse_balance(purse: URef) -> Option<U512> {
    let (purse_ptr, purse_size, _bytes) = contract_api::to_ptr(purse);

    let value_size = {
        let mut output_size = MaybeUninit::uninit();
        let ret =
            unsafe { ext_ffi::casper_get_balance(purse_ptr, purse_size, output_size.as_mut_ptr()) };
        match api_error::result_from(ret) {
            Ok(_) => unsafe { output_size.assume_init() },
            Err(ApiError::InvalidPurse) => return None,
            Err(error) => runtime::revert(error),
        }
    };
    let value_bytes = runtime::read_host_buffer(value_size).unwrap_or_revert();
    let value: U512 = bytesrepr::deserialize(value_bytes).unwrap_or_revert();
    Some(value)
}

/// Returns the balance in motes of a purse.
pub fn get_balance() -> Option<U512> {
    get_purse_balance(account::get_main_purse())
}

/// Transfers `amount` of motes from the default purse of the account to `target`
/// account.  If `target` does not exist it will be created.
pub fn transfer_to_account(target: AccountHash, amount: U512, id: Option<u64>) -> TransferResult {
    let (target_ptr, target_size, _bytes1) = contract_api::to_ptr(target);
    let (amount_ptr, amount_size, _bytes2) = contract_api::to_ptr(amount);
    let (id_ptr, id_size, _bytes3) = contract_api::to_ptr(id);
    let mut maybe_result_value = MaybeUninit::uninit();

    let return_code = unsafe {
        ext_ffi::casper_transfer_to_account(
            target_ptr,
            target_size,
            amount_ptr,
            amount_size,
            id_ptr,
            id_size,
            maybe_result_value.as_mut_ptr(),
        )
    };

    // Propagate error (if any)
    api_error::result_from(return_code)?;

    // Return appropriate result if transfer was successful
    let transferred_to_value = unsafe { maybe_result_value.assume_init() };
    TransferredTo::result_from(transferred_to_value)
}

/// Transfers `amount` of motes from the main purse of the caller's account to the main purse of
/// `target`.  If the account referenced by `target` does not exist, it will be created.
pub fn transfer_to_public_key(target: PublicKey, amount: U512, id: Option<u64>) -> TransferResult {
    let target = AccountHash::from(&target);
    transfer_to_account(target, amount, id)
}

/// Transfers `amount` of motes from `source` purse to `target` account.  If `target` does not exist
/// it will be created.
pub fn transfer_from_purse_to_account(
    source: URef,
    target: AccountHash,
    amount: U512,
    id: Option<u64>,
) -> TransferResult {
    let (source_ptr, source_size, _bytes1) = contract_api::to_ptr(source);
    let (target_ptr, target_size, _bytes2) = contract_api::to_ptr(target);
    let (amount_ptr, amount_size, _bytes3) = contract_api::to_ptr(amount);
    let (id_ptr, id_size, _bytes4) = contract_api::to_ptr(id);

    let mut maybe_result_value = MaybeUninit::uninit();
    let return_code = unsafe {
        ext_ffi::casper_transfer_from_purse_to_account(
            source_ptr,
            source_size,
            target_ptr,
            target_size,
            amount_ptr,
            amount_size,
            id_ptr,
            id_size,
            maybe_result_value.as_mut_ptr(),
        )
    };

    // Propagate error (if any)
    api_error::result_from(return_code)?;

    // Return appropriate result if transfer was successful
    let transferred_to_value = unsafe { maybe_result_value.assume_init() };
    TransferredTo::result_from(transferred_to_value)
}

/// Transfers `amount` of motes from `source` to the main purse of `target`.  If the account
/// referenced by `target` does not exist, it will be created.
pub fn transfer_from_purse_to_public_key(
    source: URef,
    target: PublicKey,
    amount: U512,
    id: Option<u64>,
) -> TransferResult {
    let target = AccountHash::from(&target);
    transfer_from_purse_to_account(source, target, amount, id)
}

/// Transfers `amount` of motes from `source` purse to `target` purse.  If `target` does not exist
/// the transfer fails.
pub fn transfer_from_purse_to_purse(
    source: URef,
    target: URef,
    amount: U512,
    id: Option<u64>,
) -> Result<(), ApiError> {
    let (source_ptr, source_size, _bytes1) = contract_api::to_ptr(source);
    let (target_ptr, target_size, _bytes2) = contract_api::to_ptr(target);
    let (amount_ptr, amount_size, _bytes3) = contract_api::to_ptr(amount);
    let (id_ptr, id_size, _bytes4) = contract_api::to_ptr(id);
    let result = unsafe {
        ext_ffi::casper_transfer_from_purse_to_purse(
            source_ptr,
            source_size,
            target_ptr,
            target_size,
            amount_ptr,
            amount_size,
            id_ptr,
            id_size,
        )
    };
    api_error::result_from(result)
}

/// Records a transfer.  Can only be called from within the mint contract.
/// Needed to support system contract-based execution.
#[doc(hidden)]
pub fn record_transfer(
    maybe_to: Option<AccountHash>,
    source: URef,
    target: URef,
    amount: U512,
    id: Option<u64>,
) -> Result<(), ApiError> {
    let (maybe_to_ptr, maybe_to_size, _bytes1) = contract_api::to_ptr(maybe_to);
    let (source_ptr, source_size, _bytes2) = contract_api::to_ptr(source);
    let (target_ptr, target_size, _bytes3) = contract_api::to_ptr(target);
    let (amount_ptr, amount_size, _bytes4) = contract_api::to_ptr(amount);
    let (id_ptr, id_size, _bytes5) = contract_api::to_ptr(id);
    let result = unsafe {
        ext_ffi::casper_record_transfer(
            maybe_to_ptr,
            maybe_to_size,
            source_ptr,
            source_size,
            target_ptr,
            target_size,
            amount_ptr,
            amount_size,
            id_ptr,
            id_size,
        )
    };
    if result == 0 {
        Ok(())
    } else {
        Err(ApiError::Transfer)
    }
}

/// Records era info.  Can only be called from within the auction contract.
/// Needed to support system contract-based execution.
#[doc(hidden)]
pub fn record_era_info(era_id: EraId, era_info: EraInfo) -> Result<(), ApiError> {
    let (era_id_ptr, era_id_size, _bytes1) = contract_api::to_ptr(era_id);
    let (era_info_ptr, era_info_size, _bytes2) = contract_api::to_ptr(era_info);
    let result = unsafe {
        ext_ffi::casper_record_era_info(era_id_ptr, era_id_size, era_info_ptr, era_info_size)
    };
    if result == 0 {
        Ok(())
    } else {
        Err(auction::Error::RecordEraInfo.into())
    }
}