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    /// Set name of the token.
226    pub fn set_name(&mut self, name: String) {
227        self.name.set(name);
228    }
229
230    /// Set symbol of the token.
231    pub fn set_symbol(&mut self, symbol: String) {
232        self.symbol.set(symbol);
233    }
234
235    /// Set decimals of the token.
236    pub fn set_decimals(&mut self, decimals: u8) {
237        self.decimals.set(decimals);
238    }
239}
240
241pub(crate) mod utils {
242    #![allow(missing_docs)]
243    #![allow(dead_code)]
244
245    use crate::access::Ownable;
246
247    use super::*;
248
249    #[odra::odra_error]
250    pub enum Error {
251        CantMint = 99,
252        CantBurn = 100
253    }
254
255    #[odra::module]
256    pub struct Cep18Example {
257        token: SubModule<Cep18>,
258        ownable: SubModule<Ownable>
259    }
260
261    #[odra::module]
262    impl Cep18Example {
263        delegate! {
264            to self.token {
265                fn name(&self) -> String;
266                fn symbol(&self) -> String;
267                fn decimals(&self) -> u8;
268                fn total_supply(&self) -> U256;
269                fn balance_of(&self, address: &Address) -> U256;
270                fn allowance(&self, owner: &Address, spender: &Address) -> U256;
271                fn approve(&mut self, spender: &Address, amount: &U256);
272                fn decrease_allowance(&mut self, spender: &Address, decr_by: &U256);
273                fn increase_allowance(&mut self, spender: &Address, inc_by: &U256);
274                fn transfer(&mut self, recipient: &Address, amount: &U256);
275                fn transfer_from(&mut self, owner: &Address, recipient: &Address, amount: &U256);
276            }
277        }
278
279        pub fn init(&mut self, symbol: String, name: String, decimals: u8, initial_supply: U256) {
280            let caller = self.env().caller();
281            self.ownable.init(caller);
282            self.token.init(symbol, name, decimals, initial_supply);
283        }
284
285        pub fn mint(&mut self, owner: &Address, amount: &U256) {
286            if self.env().caller() != self.ownable.get_owner() {
287                self.env().revert(Error::CantMint);
288            }
289            self.token.raw_mint(owner, amount);
290        }
291
292        pub fn burn(&mut self, owner: &Address, amount: &U256) {
293            if self.env().caller() != *owner {
294                self.env().revert(Error::CantBurn);
295            }
296            self.token.raw_burn(owner, amount);
297        }
298    }
299}
300
301#[cfg(test)]
302pub(crate) mod tests {
303    use alloc::string::ToString;
304
305    use odra::casper_types::account::AccountHash;
306    use odra::casper_types::contracts::ContractPackageHash;
307    use odra::host::{Deployer, HostEnv, HostRef};
308    use odra::prelude::*;
309
310    use super::utils::{Cep18Example, Cep18ExampleHostRef, Cep18ExampleInitArgs};
311
312    pub const TOKEN_NAME: &str = "Plascoin";
313    pub const TOKEN_SYMBOL: &str = "PLS";
314    pub const TOKEN_DECIMALS: u8 = 100;
315    pub const TOKEN_TOTAL_SUPPLY: u64 = 1_000_000_000;
316    pub const TOKEN_OWNER_AMOUNT_1: u64 = 1_000_000;
317    pub const TOKEN_OWNER_AMOUNT_2: u64 = 2_000_000;
318    pub const TRANSFER_AMOUNT_1: u64 = 200_001;
319    pub const ALLOWANCE_AMOUNT_1: u64 = 456_789;
320    pub const ALLOWANCE_AMOUNT_2: u64 = 87_654;
321
322    pub fn setup() -> Cep18ExampleHostRef {
323        let env = odra_test::env();
324        let init_args = Cep18ExampleInitArgs {
325            symbol: TOKEN_SYMBOL.to_string(),
326            name: TOKEN_NAME.to_string(),
327            decimals: TOKEN_DECIMALS,
328            initial_supply: TOKEN_TOTAL_SUPPLY.into()
329        };
330        setup_with_args(&env, init_args)
331    }
332
333    pub fn setup_with_args(env: &HostEnv, args: Cep18ExampleInitArgs) -> Cep18ExampleHostRef {
334        Cep18Example::deploy(env, args)
335    }
336
337    pub fn invert_address(address: Address) -> Address {
338        match address {
339            Address::Account(hash) => Address::Contract(ContractPackageHash::new(hash.value())),
340            Address::Contract(hash) => Address::Account(AccountHash(hash.value()))
341        }
342    }
343
344    #[test]
345    fn should_have_queryable_properties() {
346        let cep18_token = setup();
347
348        assert_eq!(cep18_token.name(), TOKEN_NAME);
349        assert_eq!(cep18_token.symbol(), TOKEN_SYMBOL);
350        assert_eq!(cep18_token.decimals(), TOKEN_DECIMALS);
351        assert_eq!(cep18_token.total_supply(), TOKEN_TOTAL_SUPPLY.into());
352
353        let owner_key = cep18_token.env().caller();
354        let owner_balance = cep18_token.balance_of(&owner_key);
355        assert_eq!(owner_balance, TOKEN_TOTAL_SUPPLY.into());
356
357        let contract_balance = cep18_token.balance_of(&cep18_token.address());
358        assert_eq!(contract_balance, 0.into());
359
360        // Ensures that Account and Contract ownership is respected, and we're not keying ownership under
361        // the raw bytes regardless of variant.
362        let inverted_owner_key = invert_address(owner_key);
363        let inverted_owner_balance = cep18_token.balance_of(&inverted_owner_key);
364        assert_eq!(inverted_owner_balance, 0.into());
365    }
366}