ab_contract_example/
lib.rs

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
#![no_std]

use ab_contracts_common::env::{Env, MethodContext};
use ab_contracts_common::{Address, Balance, ContractError};
use ab_contracts_io_type::maybe_data::MaybeData;
use ab_contracts_io_type::trivial_type::TrivialType;
use ab_contracts_io_type::variable_bytes::VariableBytes;
use ab_contracts_macros::contract;
use ab_contracts_standards::Fungible;
use core::cmp::Ordering;

#[derive(Debug, Default, Copy, Clone, TrivialType)]
#[repr(u8)]
pub enum LastAction {
    #[default]
    None,
    Mint,
    Transfer,
}

#[derive(Debug, Default, Copy, Clone, TrivialType)]
#[repr(C)]
pub struct Slot {
    pub balance: Balance,
}

#[derive(Copy, Clone, TrivialType)]
#[repr(C)]
pub struct Example {
    pub total_supply: Balance,
    pub owner: Address,
    pub padding: [u8; 8],
}

#[contract]
impl Fungible for Example {
    #[update]
    fn transfer(
        #[env] env: &mut Env,
        #[input] from: &Address,
        #[input] to: &Address,
        #[input] amount: &Balance,
    ) -> Result<(), ContractError> {
        if !(env.context() == from || env.caller() == from || env.caller() == env.own_address()) {
            return Err(ContractError::AccessDenied);
        }

        env.example_transfer(&MethodContext::Replace, env.own_address(), from, to, amount)
    }

    #[view]
    fn balance(#[env] env: &Env, #[input] address: &Address) -> Result<Balance, ContractError> {
        env.example_balance(env.own_address(), address)
    }
}

#[contract]
impl Example {
    #[init]
    pub fn new(
        #[slot] (owner_addr, owner): (&Address, &mut MaybeData<Slot>),
        #[input] total_supply: &Balance,
    ) -> Self {
        owner.replace(Slot {
            balance: *total_supply,
        });
        Self {
            total_supply: *total_supply,
            owner: *owner_addr,
            padding: [0; 8],
        }
    }

    #[init]
    pub fn new_result(#[env] env: &mut Env, #[result] result: &mut MaybeData<Self>) {
        result.replace(Self {
            total_supply: Balance::MIN,
            owner: *env.context(),
            padding: [0; 8],
        });
    }

    #[update]
    pub fn mint(
        &mut self,
        #[env] env: &mut Env,
        #[tmp] last_action: &mut MaybeData<LastAction>,
        #[slot] to: &mut MaybeData<Slot>,
        #[input] &value: &Balance,
    ) -> Result<(), ContractError> {
        if env.context() != self.owner && env.caller() != self.owner {
            return Err(ContractError::AccessDenied);
        }

        if Balance::MAX - value > self.total_supply {
            return Err(ContractError::InvalidInput);
        }

        self.total_supply += value;

        match to.get_mut() {
            Some(contents) => {
                contents.balance += value;
            }
            None => {
                to.replace(Slot { balance: value });
            }
        }

        last_action.replace(LastAction::Mint);

        Ok(())
    }

    #[view]
    pub fn balance(#[slot] target: &MaybeData<Slot>) -> Balance {
        target
            .get()
            .map_or_else(Balance::default, |slot| slot.balance)
    }

    #[view]
    pub fn balance2(#[slot] target: &MaybeData<Slot>, #[output] balance: &mut MaybeData<Balance>) {
        balance.replace(
            target
                .get()
                .map_or_else(Balance::default, |slot| slot.balance),
        );
    }

    #[view]
    pub fn balance3(#[slot] target: &MaybeData<Slot>, #[result] result: &mut MaybeData<Balance>) {
        result.replace(
            target
                .get()
                .map_or_else(Balance::default, |slot| slot.balance),
        );
    }

    #[view]
    pub fn var_bytes(#[output] _out: &mut VariableBytes<1024>) {
        // TODO
    }

    #[update]
    pub fn transfer(
        #[env] env: &mut Env,
        #[tmp] last_action: &mut MaybeData<LastAction>,
        #[slot] (from_address, from): (&Address, &mut MaybeData<Slot>),
        #[slot] to: &mut MaybeData<Slot>,
        #[input] &amount: &Balance,
    ) -> Result<(), ContractError> {
        if !(env.context() == from_address
            || env.caller() == from_address
            || env.caller() == env.own_address())
        {
            return Err(ContractError::AccessDenied);
        }

        {
            let Some(contents) = from.get_mut() else {
                return Err(ContractError::InvalidState);
            };

            match contents.balance.cmp(&amount) {
                Ordering::Less => {
                    return Err(ContractError::InvalidInput);
                }
                Ordering::Equal => {
                    // All balance is transferred out, remove slot contents
                    from.remove();
                }
                Ordering::Greater => {
                    contents.balance -= amount;
                }
            }
        }

        match to.get_mut() {
            Some(contents) => {
                contents.balance += amount;
            }
            None => {
                to.replace(Slot { balance: amount });
            }
        }

        last_action.replace(LastAction::Transfer);

        Ok(())
    }

    #[update]
    pub fn last_action(#[tmp] maybe_last_action: &MaybeData<LastAction>) -> LastAction {
        maybe_last_action.get().copied().unwrap_or_default()
    }
}