odra_modules/
cep18_token.rs

1//! CEP-18 Casper Fungible Token standard implementation.
2use odra::casper_types::U256;
3use odra::prelude::*;
4
5use crate::cep18::errors::Error;
6
7use crate::cep18::events::{
8    Burn, DecreaseAllowance, IncreaseAllowance, Mint, SetAllowance, Transfer, TransferFrom
9};
10use crate::cep18::storage::{
11    Cep18AllowancesStorage, Cep18BalancesStorage, Cep18DecimalsStorage, Cep18NameStorage,
12    Cep18SymbolStorage, Cep18TotalSupplyStorage
13};
14
15/// CEP-18 token module
16#[odra::module(
17    events = [
18        Mint, Burn, SetAllowance, IncreaseAllowance, DecreaseAllowance, Transfer, TransferFrom
19    ],
20    errors = Error
21)]
22pub struct Cep18 {
23    decimals: SubModule<Cep18DecimalsStorage>,
24    symbol: SubModule<Cep18SymbolStorage>,
25    name: SubModule<Cep18NameStorage>,
26    total_supply: SubModule<Cep18TotalSupplyStorage>,
27    balances: SubModule<Cep18BalancesStorage>,
28    allowances: SubModule<Cep18AllowancesStorage>
29}
30
31#[odra::module]
32impl Cep18 {
33    /// Initializes the contract with the given metadata, initial supply.
34    pub fn init(&mut self, symbol: String, name: String, decimals: u8, initial_supply: U256) {
35        let caller = self.env().caller();
36
37        // Set the metadata
38        self.symbol.set(symbol);
39        self.name.set(name);
40        self.decimals.set(decimals);
41        self.total_supply.set(initial_supply);
42
43        if !initial_supply.is_zero() {
44            // If the initial supply is not zero:
45            // - mint the initial supply to the caller,
46            // - emit the `Mint` event.
47            self.balances.set(&caller, initial_supply);
48            self.env().emit_event(Mint {
49                recipient: caller,
50                amount: initial_supply
51            });
52        } else {
53            // If the initial supply is zero, initialize `balances`.
54            self.balances.init();
55        }
56
57        // Initialize allowances.
58        self.allowances.init();
59    }
60
61    /// Returns the name of the token.
62    pub fn name(&self) -> String {
63        self.name.get()
64    }
65
66    /// Returns the symbol of the token.
67    pub fn symbol(&self) -> String {
68        self.symbol.get()
69    }
70
71    /// Returns the number of decimals the token uses.
72    pub fn decimals(&self) -> u8 {
73        self.decimals.get()
74    }
75
76    /// Returns the total supply of the token.
77    pub fn total_supply(&self) -> U256 {
78        self.total_supply.get()
79    }
80
81    /// Returns the balance of the given address.
82    pub fn balance_of(&self, address: &Address) -> U256 {
83        self.balances.get(address).unwrap_or_default()
84    }
85
86    /// Returns the amount of tokens the owner has allowed the spender to spend.
87    pub fn allowance(&self, owner: &Address, spender: &Address) -> U256 {
88        self.allowances.get_or_default(owner, spender)
89    }
90
91    /// Approves the spender to spend the given amount of tokens on behalf of the caller.
92    pub fn approve(&mut self, spender: &Address, amount: &U256) {
93        let owner = self.env().caller();
94        if owner == *spender {
95            self.env().revert(Error::CannotTargetSelfUser);
96        }
97
98        self.allowances.set(&owner, spender, *amount);
99        self.env().emit_event(SetAllowance {
100            owner,
101            spender: *spender,
102            allowance: *amount
103        });
104    }
105
106    /// Decreases the allowance of the spender by the given amount.
107    pub fn decrease_allowance(&mut self, spender: &Address, decr_by: &U256) {
108        let owner = self.env().caller();
109        let allowance = self.allowance(&owner, spender);
110        self.allowances
111            .set(&owner, spender, allowance.saturating_sub(*decr_by));
112        self.env().emit_event(DecreaseAllowance {
113            owner,
114            spender: *spender,
115            allowance,
116            decr_by: *decr_by
117        });
118    }
119
120    /// Increases the allowance of the spender by the given amount.
121    pub fn increase_allowance(&mut self, spender: &Address, inc_by: &U256) {
122        let owner = self.env().caller();
123        if owner == *spender {
124            self.env().revert(Error::CannotTargetSelfUser);
125        }
126        let allowance = self.allowances.get_or_default(&owner, spender);
127
128        self.allowances
129            .set(&owner, spender, allowance.saturating_add(*inc_by));
130        self.env().emit_event(IncreaseAllowance {
131            owner,
132            spender: *spender,
133            allowance,
134            inc_by: *inc_by
135        });
136    }
137
138    /// Transfers tokens from the caller to the recipient.
139    pub fn transfer(&mut self, recipient: &Address, amount: &U256) {
140        let caller = self.env().caller();
141        if caller == *recipient {
142            self.env().revert(Error::CannotTargetSelfUser);
143        }
144
145        self.raw_transfer(&caller, recipient, amount);
146
147        self.env().emit_event(Transfer {
148            sender: caller,
149            recipient: *recipient,
150            amount: *amount
151        });
152    }
153
154    /// Transfers tokens from the owner to the recipient using the spender's allowance.
155    pub fn transfer_from(&mut self, owner: &Address, recipient: &Address, amount: &U256) {
156        let spender = self.env().caller();
157
158        if owner == recipient {
159            self.env().revert(Error::CannotTargetSelfUser);
160        }
161
162        if amount.is_zero() {
163            return;
164        }
165
166        let allowance = self.allowance(owner, &spender);
167
168        self.allowances.set(
169            owner,
170            recipient,
171            allowance
172                .checked_sub(*amount)
173                .unwrap_or_revert_with(self, Error::InsufficientAllowance)
174        );
175        self.raw_transfer(owner, recipient, amount);
176
177        self.env().emit_event(TransferFrom {
178            spender,
179            owner: *owner,
180            recipient: *recipient,
181            amount: *amount
182        });
183    }
184}
185
186impl Cep18 {
187    /// Transfers tokens from the sender to the recipient without checking the permissions.
188    pub fn raw_transfer(&mut self, sender: &Address, recipient: &Address, amount: &U256) {
189        if amount > &self.balance_of(sender) {
190            self.env().revert(Error::InsufficientBalance)
191        }
192
193        if amount > &U256::zero() {
194            self.balances.subtract(sender, *amount);
195            self.balances.add(recipient, *amount);
196        }
197    }
198
199    /// Mints new tokens and assigns them to the given address without checking the permissions.
200    pub fn raw_mint(&mut self, owner: &Address, amount: &U256) {
201        self.total_supply.add(*amount);
202        self.balances.add(owner, *amount);
203
204        self.env().emit_event(Mint {
205            recipient: *owner,
206            amount: *amount
207        });
208    }
209
210    /// Burns the given amount of tokens from the given address without checking the permissions.
211    pub fn raw_burn(&mut self, owner: &Address, amount: &U256) {
212        if &self.balance_of(owner) < amount {
213            self.env().revert(Error::InsufficientBalance);
214        }
215
216        self.total_supply.subtract(*amount);
217        self.balances.subtract(owner, *amount);
218
219        self.env().emit_event(Burn {
220            owner: *owner,
221            amount: *amount
222        });
223    }
224}
225
226pub(crate) mod utils {
227    #![allow(missing_docs)]
228    #![allow(dead_code)]
229
230    use crate::access::Ownable;
231
232    use super::*;
233
234    #[odra::odra_error]
235    pub enum Error {
236        CantMint = 99,
237        CantBurn = 100
238    }
239
240    #[odra::module]
241    pub struct Cep18Example {
242        token: SubModule<Cep18>,
243        ownable: SubModule<Ownable>
244    }
245
246    #[odra::module]
247    impl Cep18Example {
248        delegate! {
249            to self.token {
250                fn name(&self) -> String;
251                fn symbol(&self) -> String;
252                fn decimals(&self) -> u8;
253                fn total_supply(&self) -> U256;
254                fn balance_of(&self, address: &Address) -> U256;
255                fn allowance(&self, owner: &Address, spender: &Address) -> U256;
256                fn approve(&mut self, spender: &Address, amount: &U256);
257                fn decrease_allowance(&mut self, spender: &Address, decr_by: &U256);
258                fn increase_allowance(&mut self, spender: &Address, inc_by: &U256);
259                fn transfer(&mut self, recipient: &Address, amount: &U256);
260                fn transfer_from(&mut self, owner: &Address, recipient: &Address, amount: &U256);
261            }
262        }
263
264        pub fn init(&mut self, symbol: String, name: String, decimals: u8, initial_supply: U256) {
265            let caller = self.env().caller();
266            self.ownable.init(caller);
267            self.token.init(symbol, name, decimals, initial_supply);
268        }
269
270        pub fn mint(&mut self, owner: &Address, amount: &U256) {
271            if self.env().caller() != self.ownable.get_owner() {
272                self.env().revert(Error::CantMint);
273            }
274            self.token.raw_mint(owner, amount);
275        }
276
277        pub fn burn(&mut self, owner: &Address, amount: &U256) {
278            if self.env().caller() != *owner {
279                self.env().revert(Error::CantBurn);
280            }
281            self.token.raw_burn(owner, amount);
282        }
283    }
284}
285
286#[cfg(test)]
287pub(crate) mod tests {
288    use alloc::string::ToString;
289
290    use odra::casper_types::account::AccountHash;
291    use odra::casper_types::contracts::ContractPackageHash;
292    use odra::host::{Deployer, HostEnv, HostRef};
293    use odra::prelude::*;
294
295    use super::utils::{Cep18Example, Cep18ExampleHostRef, Cep18ExampleInitArgs};
296
297    pub const TOKEN_NAME: &str = "Plascoin";
298    pub const TOKEN_SYMBOL: &str = "PLS";
299    pub const TOKEN_DECIMALS: u8 = 100;
300    pub const TOKEN_TOTAL_SUPPLY: u64 = 1_000_000_000;
301    pub const TOKEN_OWNER_AMOUNT_1: u64 = 1_000_000;
302    pub const TOKEN_OWNER_AMOUNT_2: u64 = 2_000_000;
303    pub const TRANSFER_AMOUNT_1: u64 = 200_001;
304    pub const ALLOWANCE_AMOUNT_1: u64 = 456_789;
305    pub const ALLOWANCE_AMOUNT_2: u64 = 87_654;
306
307    pub fn setup() -> Cep18ExampleHostRef {
308        let env = odra_test::env();
309        let init_args = Cep18ExampleInitArgs {
310            symbol: TOKEN_SYMBOL.to_string(),
311            name: TOKEN_NAME.to_string(),
312            decimals: TOKEN_DECIMALS,
313            initial_supply: TOKEN_TOTAL_SUPPLY.into()
314        };
315        setup_with_args(&env, init_args)
316    }
317
318    pub fn setup_with_args(env: &HostEnv, args: Cep18ExampleInitArgs) -> Cep18ExampleHostRef {
319        Cep18Example::deploy(env, args)
320    }
321
322    pub fn invert_address(address: Address) -> Address {
323        match address {
324            Address::Account(hash) => Address::Contract(ContractPackageHash::new(hash.value())),
325            Address::Contract(hash) => Address::Account(AccountHash(hash.value()))
326        }
327    }
328
329    #[test]
330    fn should_have_queryable_properties() {
331        let cep18_token = setup();
332
333        assert_eq!(cep18_token.name(), TOKEN_NAME);
334        assert_eq!(cep18_token.symbol(), TOKEN_SYMBOL);
335        assert_eq!(cep18_token.decimals(), TOKEN_DECIMALS);
336        assert_eq!(cep18_token.total_supply(), TOKEN_TOTAL_SUPPLY.into());
337
338        let owner_key = cep18_token.env().caller();
339        let owner_balance = cep18_token.balance_of(&owner_key);
340        assert_eq!(owner_balance, TOKEN_TOTAL_SUPPLY.into());
341
342        let contract_balance = cep18_token.balance_of(&cep18_token.address());
343        assert_eq!(contract_balance, 0.into());
344
345        // Ensures that Account and Contract ownership is respected, and we're not keying ownership under
346        // the raw bytes regardless of variant.
347        let inverted_owner_key = invert_address(owner_key);
348        let inverted_owner_balance = cep18_token.balance_of(&inverted_owner_key);
349        assert_eq!(inverted_owner_balance, 0.into());
350    }
351}