odra_modules/cep18/
storage.rs1#![allow(missing_docs)]
2
3use odra::casper_types::U256;
4use odra::named_keys::{
5 base64_encoded_key_value_storage, compound_key_value_storage, single_value_storage
6};
7use odra::prelude::*;
8
9const ALLOWANCES_KEY: &str = "allowances";
10const BALANCES_KEY: &str = "balances";
11const NAME_KEY: &str = "name";
12const DECIMALS_KEY: &str = "decimals";
13const SYMBOL_KEY: &str = "symbol";
14const TOTAL_SUPPLY_KEY: &str = "total_supply";
15
16single_value_storage!(
17 Cep18NameStorage,
18 String,
19 NAME_KEY,
20 ExecutionError::KeyNotFound
21);
22single_value_storage!(
23 Cep18DecimalsStorage,
24 u8,
25 DECIMALS_KEY,
26 ExecutionError::KeyNotFound
27);
28single_value_storage!(
29 Cep18SymbolStorage,
30 String,
31 SYMBOL_KEY,
32 ExecutionError::KeyNotFound
33);
34single_value_storage!(
35 Cep18TotalSupplyStorage,
36 U256,
37 TOTAL_SUPPLY_KEY,
38 ExecutionError::KeyNotFound
39);
40impl Cep18TotalSupplyStorage {
41 pub fn add(&self, amount: U256) {
43 let total_supply = self.get();
44 let new_total_supply = total_supply
45 .checked_add(amount)
46 .unwrap_or_revert_with(&self.env(), ExecutionError::AdditionOverflow);
47 self.set(new_total_supply);
48 }
49
50 pub fn subtract(&self, amount: U256) {
52 let total_supply = self.get();
53 let new_total_supply = total_supply
54 .checked_sub(amount)
55 .unwrap_or_revert_with(&self.env(), ExecutionError::SubtractionOverflow);
56 self.set(new_total_supply);
57 }
58}
59base64_encoded_key_value_storage!(Cep18BalancesStorage, BALANCES_KEY, Address, U256);
60impl Cep18BalancesStorage {
61 pub fn add(&self, account: &Address, amount: U256) {
63 let balance = self.get(account).unwrap_or_default();
64 let new_balance = balance.checked_add(amount).unwrap_or_revert(self);
65 self.set(account, new_balance);
66 }
67
68 pub fn subtract(&self, account: &Address, amount: U256) {
70 let balance = self.get(account).unwrap_or_default();
71 let new_balance = balance
72 .checked_sub(amount)
73 .unwrap_or_revert_with(self, ExecutionError::SubtractionOverflow);
74 self.set(account, new_balance);
75 }
76}
77compound_key_value_storage!(Cep18AllowancesStorage, ALLOWANCES_KEY, Address, U256);
78impl Cep18AllowancesStorage {
79 pub fn add(&self, owner: &Address, spender: &Address, amount: U256) {
81 let allowance = self.get_or_default(owner, spender);
82 let new_allowance = allowance
83 .checked_add(amount)
84 .unwrap_or_revert_with(&self.env(), ExecutionError::AdditionOverflow);
85 self.set(owner, spender, new_allowance);
86 }
87
88 pub fn subtract(&self, owner: &Address, spender: &Address, amount: U256) {
90 let allowance = self.get_or_default(owner, spender);
91 let new_allowance = allowance
92 .checked_sub(amount)
93 .unwrap_or_revert_with(&self.env(), ExecutionError::AdditionOverflow);
94 self.set(owner, spender, new_allowance);
95 }
96}