inkpad_sandbox/
contract.rs1use crate::Sandbox;
3use inkpad_std::Vec;
4use parity_scale_codec::{Decode, Encode};
5
6#[derive(Default, Clone, Decode, Encode)]
8pub struct GasMeter {
9 pub gas_limit: u64,
10 pub gas_left: u64,
11}
12
13impl GasMeter {
14 pub fn new(gas: u64) -> GasMeter {
15 GasMeter {
16 gas_limit: gas,
17 gas_left: gas,
18 }
19 }
20
21 pub fn gas_left_bytes(&self) -> Vec<u8> {
22 self.gas_left.encode()
23 }
24
25 pub fn with_nested<R, F>(&mut self, amount: u64, f: F) -> R
26 where
27 F: FnOnce(Option<&mut GasMeter>) -> R,
28 {
29 if self.gas_left < amount {
32 f(None)
33 } else {
34 self.gas_left -= amount;
35
36 let mut nested = GasMeter::new(amount);
37 let r = f(Some(&mut nested));
38
39 self.gas_left += nested.gas_left;
40 r
41 }
42 }
43}
44
45#[derive(Default, Clone, Encode, Decode, PartialEq, Eq)]
47pub struct RentParams {
48 total_balance: u64,
50 free_balance: u64,
52 subsistence_threshold: u64,
54 deposit_per_contract: u64,
56 deposit_per_storage_byte: u64,
58 deposit_per_storage_item: u64,
60 rent_allowance: u64,
62 rent_fraction: u16,
64 storage_size: u32,
66 code_size: u32,
68 code_refcount: u32,
70 _reserved: Option<()>,
72}
73
74impl Sandbox {
75 pub fn tombstone_deposit(&self) -> [u8; 32] {
76 [1; 32]
77 }
78
79 pub fn rent_allowance(&self) -> [u8; 32] {
80 self.ext.rent_allowance
81 }
82
83 pub fn set_rent_allowance(&mut self, rent_allowence: [u8; 32]) {
84 self.ext.rent_allowance = rent_allowence;
85 }
86
87 pub fn rent_params(&self) -> Vec<u8> {
88 self.ext.rent_params.encode()
89 }
90}