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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#![warn(missing_docs)]
#![no_std]
extern crate alloc;
mod address;
mod allowances;
mod balances;
pub mod constants;
mod detail;
pub mod entry_points;
mod error;
mod total_supply;
use alloc::string::{String, ToString};
use once_cell::unsync::OnceCell;
use casper_contract::{
contract_api::{runtime, storage},
unwrap_or_revert::UnwrapOrRevert,
};
use casper_types::{contracts::NamedKeys, EntryPoints, Key, URef, U256};
pub use address::Address;
use constants::{
ALLOWANCES_KEY_NAME, BALANCES_KEY_NAME, DECIMALS_KEY_NAME, ERC20_TOKEN_CONTRACT_KEY_NAME,
NAME_KEY_NAME, SYMBOL_KEY_NAME, TOTAL_SUPPLY_KEY_NAME,
};
pub use error::Error;
#[derive(Default)]
pub struct ERC20 {
balances_uref: OnceCell<URef>,
allowances_uref: OnceCell<URef>,
total_supply_uref: OnceCell<URef>,
}
impl ERC20 {
fn new(balances_uref: URef, allowances_uref: URef, total_supply_uref: URef) -> Self {
Self {
balances_uref: balances_uref.into(),
allowances_uref: allowances_uref.into(),
total_supply_uref: total_supply_uref.into(),
}
}
fn total_supply_uref(&self) -> URef {
*self
.total_supply_uref
.get_or_init(total_supply::total_supply_uref)
}
fn read_total_supply(&self) -> U256 {
total_supply::read_total_supply_from(self.total_supply_uref())
}
fn write_total_supply(&self, total_supply: U256) {
total_supply::write_total_supply_to(self.total_supply_uref(), total_supply)
}
fn balances_uref(&self) -> URef {
*self.balances_uref.get_or_init(balances::get_balances_uref)
}
fn read_balance(&self, owner: Address) -> U256 {
balances::read_balance_from(self.balances_uref(), owner)
}
fn write_balance(&mut self, owner: Address, amount: U256) {
balances::write_balance_to(self.balances_uref(), owner, amount)
}
fn allowances_uref(&self) -> URef {
*self
.allowances_uref
.get_or_init(allowances::allowances_uref)
}
fn read_allowance(&self, owner: Address, spender: Address) -> U256 {
allowances::read_allowance_from(self.allowances_uref(), owner, spender)
}
fn write_allowance(&mut self, owner: Address, spender: Address, amount: U256) {
allowances::write_allowance_to(self.allowances_uref(), owner, spender, amount)
}
fn transfer_balance(
&mut self,
sender: Address,
recipient: Address,
amount: U256,
) -> Result<(), Error> {
balances::transfer_balance(self.balances_uref(), sender, recipient, amount)
}
pub fn install(
name: String,
symbol: String,
decimals: u8,
initial_supply: U256,
) -> Result<ERC20, Error> {
let default_entry_points = entry_points::default();
ERC20::install_custom(
name,
symbol,
decimals,
initial_supply,
ERC20_TOKEN_CONTRACT_KEY_NAME,
default_entry_points,
)
}
pub fn name(&self) -> String {
detail::read_from(NAME_KEY_NAME)
}
pub fn symbol(&self) -> String {
detail::read_from(SYMBOL_KEY_NAME)
}
pub fn decimals(&self) -> u8 {
detail::read_from(DECIMALS_KEY_NAME)
}
pub fn total_supply(&self) -> U256 {
self.read_total_supply()
}
pub fn balance_of(&self, owner: Address) -> U256 {
self.read_balance(owner)
}
pub fn transfer(&mut self, recipient: Address, amount: U256) -> Result<(), Error> {
let sender = detail::get_immediate_caller_address()?;
self.transfer_balance(sender, recipient, amount)
}
pub fn transfer_from(
&mut self,
owner: Address,
recipient: Address,
amount: U256,
) -> Result<(), Error> {
let spender = detail::get_immediate_caller_address()?;
if amount.is_zero() {
return Ok(());
}
let spender_allowance = self.read_allowance(owner, spender);
let new_spender_allowance = spender_allowance
.checked_sub(amount)
.ok_or(Error::InsufficientAllowance)?;
self.transfer_balance(owner, recipient, amount)?;
self.write_allowance(owner, spender, new_spender_allowance);
Ok(())
}
pub fn approve(&mut self, spender: Address, amount: U256) -> Result<(), Error> {
let owner = detail::get_immediate_caller_address()?;
self.write_allowance(owner, spender, amount);
Ok(())
}
pub fn allowance(&self, owner: Address, spender: Address) -> U256 {
self.read_allowance(owner, spender)
}
pub fn mint(&mut self, owner: Address, amount: U256) -> Result<(), Error> {
let new_balance = {
let balance = self.read_balance(owner);
balance.checked_add(amount).ok_or(Error::Overflow)?
};
let new_total_supply = {
let total_supply: U256 = self.read_total_supply();
total_supply.checked_add(amount).ok_or(Error::Overflow)?
};
self.write_balance(owner, new_balance);
self.write_total_supply(new_total_supply);
Ok(())
}
pub fn burn(&mut self, owner: Address, amount: U256) -> Result<(), Error> {
let new_balance = {
let balance = self.read_balance(owner);
balance
.checked_sub(amount)
.ok_or(Error::InsufficientBalance)?
};
let new_total_supply = {
let total_supply = self.read_total_supply();
total_supply.checked_sub(amount).ok_or(Error::Overflow)?
};
self.write_balance(owner, new_balance);
self.write_total_supply(new_total_supply);
Ok(())
}
#[doc(hidden)]
pub fn install_custom(
name: String,
symbol: String,
decimals: u8,
initial_supply: U256,
contract_key_name: &str,
entry_points: EntryPoints,
) -> Result<ERC20, Error> {
let balances_uref = storage::new_dictionary(BALANCES_KEY_NAME).unwrap_or_revert();
let allowances_uref = storage::new_dictionary(ALLOWANCES_KEY_NAME).unwrap_or_revert();
let total_supply_uref = storage::new_uref(initial_supply).into_read_write();
let mut named_keys = NamedKeys::new();
let name_key = {
let name_uref = storage::new_uref(name).into_read();
Key::from(name_uref)
};
let symbol_key = {
let symbol_uref = storage::new_uref(symbol).into_read();
Key::from(symbol_uref)
};
let decimals_key = {
let decimals_uref = storage::new_uref(decimals).into_read();
Key::from(decimals_uref)
};
let total_supply_key = Key::from(total_supply_uref);
let balances_dictionary_key = {
let caller = detail::get_caller_address()?;
balances::write_balance_to(balances_uref, caller, initial_supply);
runtime::remove_key(BALANCES_KEY_NAME);
Key::from(balances_uref)
};
let allowances_dictionary_key = {
runtime::remove_key(ALLOWANCES_KEY_NAME);
Key::from(allowances_uref)
};
named_keys.insert(NAME_KEY_NAME.to_string(), name_key);
named_keys.insert(SYMBOL_KEY_NAME.to_string(), symbol_key);
named_keys.insert(DECIMALS_KEY_NAME.to_string(), decimals_key);
named_keys.insert(BALANCES_KEY_NAME.to_string(), balances_dictionary_key);
named_keys.insert(ALLOWANCES_KEY_NAME.to_string(), allowances_dictionary_key);
named_keys.insert(TOTAL_SUPPLY_KEY_NAME.to_string(), total_supply_key);
let (contract_hash, _version) =
storage::new_locked_contract(entry_points, Some(named_keys), None, None);
runtime::put_key(contract_key_name, Key::from(contract_hash));
Ok(ERC20::new(
balances_uref,
allowances_uref,
total_supply_uref,
))
}
}