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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
use std::collections::{HashSet, HashMap};
use std::hash::Hash;
use crate::fungible_token::events::{FtMint, FtBurn, FtDeposit};
use crate::fungible_token::receiver::ext_ft_receiver;
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::collections::{LookupMap, UnorderedMap, UnorderedSet, Vector};
use near_sdk::json_types::U128;
use near_sdk::serde::{Serialize, Deserialize};
use near_sdk::serde_json::json;
use near_sdk::{
assert_one_yocto, env, log, require, AccountId, Balance, Gas, IntoStorageKey, PromiseOrValue,
PromiseResult, StorageUsage, ext_contract, FunctionError,
};
use crate::fungible_token::core::FungibleTokenCore;
use crate::fungible_token::resolver::{FungibleTokenResolver, ext_ft_resolver};
use crate::fungible_token::account::FungibleTokenAccount;
use crate::fungible_token::sender::FungibleTokenSender;
const GAS_FOR_RESOLVE_BURN: Gas = Gas(5_000_000_000_000);
const GAS_FOR_FT_BURN_CALL: Gas = Gas(25_000_000_000_000 + GAS_FOR_RESOLVE_BURN.0);
const GAS_FOR_RESOLVE_DEPOSIT: Gas = Gas(5_000_000_000_000);
const GAS_FOR_FT_DEPOSIT_CALL: Gas = Gas(25_000_000_000_000 + GAS_FOR_RESOLVE_DEPOSIT.0);
const GAS_FOR_RESOLVE_WITHDRAW: Gas = Gas(5_000_000_000_000);
const GAS_FOR_FT_WITHDRAW_CALL: Gas = Gas(25_000_000_000_000 + GAS_FOR_RESOLVE_WITHDRAW.0);
#[derive(BorshDeserialize, BorshSerialize)]
pub struct Account {
pub contract_ids: UnorderedMap<Option<AccountId>, Balance>,
pub deposit_map: UnorderedMap<AccountId, HashMap<Option<AccountId>, Balance>> }
impl Account {
pub fn new(prefix: String) -> Self {
let mut this = Self {
contract_ids: UnorderedMap::new(prefix.as_bytes()),
deposit_map: UnorderedMap::new((prefix + "deposit").as_bytes())
};
this.contract_ids.insert(&(None as Option<AccountId>), &0);
this
}
}
impl FungibleTokenAccount for Account {
fn deposit(&mut self, contract_id: &AccountId, amount: Balance) {
for contract_id in [Some(contract_id.clone()), None] {
let balance = self.contract_ids.get(&contract_id).unwrap_or(0);
if let Some(new_balance) = balance.checked_add(amount) {
self.contract_ids.insert(&contract_id, &new_balance);
} else {
env::panic_str("Balance overflow");
}
}
}
fn withdraw(&mut self, contract_id: &AccountId, amount: Balance) -> u128 {
for contract_id in [Some(contract_id.clone()), None] {
let balance = self.contract_ids.get(&contract_id).expect("not enough balance");
if let Some(new_balance) = balance.checked_sub(amount) {
self.contract_ids.insert(&contract_id, &new_balance);
} else {
env::panic_str("Not enough balance");
}
}
amount
}
fn contract_deposit(&mut self, contract_id: &AccountId, deposit_contract_id: &AccountId, amount: Balance) {
let mut contract = self.deposit_map.get(contract_id).unwrap_or(HashMap::new());
self.withdraw(contract_id, amount);
for contract_id in [Some(deposit_contract_id.clone()), None] {
let balance = contract.get(&contract_id).unwrap_or(&0).clone();
if let Some(new_balance) = balance.checked_add(amount) {
contract.insert(contract_id, new_balance);
} else {
env::panic_str("Balance overflow");
}
}
self.deposit_map.insert(contract_id, &contract);
}
fn contract_withdraw(&mut self, contract_id: &AccountId, deposit_contract_id: &AccountId, amount: Balance) {
let mut contract = self.deposit_map.get(contract_id).unwrap_or(HashMap::new());
for contract_id in [Some(contract_id.clone()), None] {
let balance = contract.get(&Some(deposit_contract_id.clone())).expect("not enough balance").clone();
if let Some(new_balance) = balance.checked_add(amount) {
contract.insert(contract_id, new_balance);
} else {
env::panic_str("Not enough balance");
}
}
self.deposit_map.insert(contract_id, &contract);
self.deposit(contract_id, amount);
}
fn get_available_balance(&self, contract_id: &Option<AccountId>) -> u128 {
match self.contract_ids.get(contract_id) {
Some(balance) => balance,
None => 0
}
}
fn get_deposit_balance(&self, contract_id: &Option<AccountId>, deposit_contract_id: &Option<AccountId>) -> u128 {
match contract_id {
Some(contract_id) => {
let contract = match self.deposit_map.get(contract_id) {
Some(contract) => contract,
None => return 0
};
match contract.get(deposit_contract_id) {
Some(balance) => balance.clone(),
None => 0
}
},
None => {
let mut total = 0;
for (_, deposit) in self.deposit_map.iter() {
match deposit.get(&None) {
Some(balance) => {
total += balance
},
None => {}
};
}
total
},
}
}
fn get_total_balance(&self, contract_id: &Option<AccountId>) -> u128 {
self.get_available_balance(contract_id) + self.get_deposit_balance(contract_id, &None)
}
fn is_registered(&self, contract_id: &AccountId) -> bool {
self.contract_ids.get(&Some(contract_id.clone())).is_some()
}
fn is_deposit_exist(&self, cotnract_id: &AccountId, deposit_contract_id: &AccountId) -> bool {
match self.deposit_map.get(cotnract_id) {
Some(contract) => {
contract.get(&Some(deposit_contract_id.clone())).is_some()
},
None => false
}
}
}
pub type TotalSupply = Account;
#[derive(BorshDeserialize, BorshSerialize)]
pub struct FungibleToken {
pub accounts: LookupMap<AccountId, Account>,
pub total_supply: TotalSupply,
pub account_storage_usage: StorageUsage,
}
impl FungibleToken {
pub fn new<S>(prefix: S) -> Self
where
S: IntoStorageKey,
{
let mut this =
Self { accounts: LookupMap::new(prefix), total_supply: TotalSupply::new("total_supply".to_string()), account_storage_usage: 0 };
this.measure_account_storage_usage();
this
}
fn measure_account_storage_usage(&mut self) {
let initial_storage_usage = env::storage_usage();
let tmp_account_id = AccountId::new_unchecked("a".repeat(64));
self.accounts.insert(&tmp_account_id, &Account::new(tmp_account_id.to_string()));
self.account_storage_usage = env::storage_usage() - initial_storage_usage;
env::storage_remove("contracts".as_bytes());
self.accounts.remove(&tmp_account_id);
}
pub fn internal_deposit(&mut self, account_id: &AccountId, amount: Balance, contract_id: &AccountId) {
let mut account = self.accounts.get(&account_id).expect(format!("The account {} is not registered", &account_id.to_string()).as_str());
account.deposit(contract_id, amount);
self.accounts.insert(account_id, &account);
self.total_supply.deposit(contract_id, amount);
FtMint {
owner_id: account_id,
amount: &amount.into(),
memo: Some(&json!({
"contract_id": contract_id
}).to_string()),
}
.emit();
}
pub fn internal_withdraw(&mut self, account_id: &AccountId, amount: Balance, contract_id: &AccountId) {
let mut account = self.accounts.get(&account_id).expect(format!("The account {} is not registered", &account_id.to_string()).as_str());
let balance = account.get_available_balance(&Some(contract_id.clone()));
assert!(balance >= amount, "not enough balance");
account.withdraw(contract_id, amount);
self.total_supply.withdraw(contract_id, amount);
self.accounts.insert(account_id, &account);
FtBurn {
owner_id: account_id,
amount: &amount.into(),
memo: Some(&json!({
"contract_id": contract_id
}).to_string()),
}
.emit();
}
pub fn internal_contract_deposit(&mut self, account_id: &AccountId, amount: Balance, contract_id: &AccountId, deposit_contract_id: &AccountId) {
let mut account = self.accounts.get(&account_id).expect(format!("The account {} is not registered", &account_id.to_string()).as_str());
let balance = account.get_available_balance(&Some(contract_id.clone()));
assert!(balance >= amount, "not enough balance");
account.contract_deposit(contract_id, deposit_contract_id, amount)
}
pub fn internal_contract_withdraw(&mut self, account_id: &AccountId, amount: Balance, contract_id: &AccountId, deposit_contract_id: &AccountId) {
let mut account = self.accounts.get(&account_id).expect(format!("The account {} is not registered", &account_id.to_string()).as_str());
let deposit_balance = account.get_deposit_balance(&Some(contract_id.clone()), &Some(deposit_contract_id.clone()));
assert!(deposit_balance >= amount, "not enough balance");
account.contract_withdraw(contract_id, deposit_contract_id, amount);
self.accounts.insert(account_id, &account);
}
pub fn internal_register_account(&mut self, account_id: &AccountId) {
if self.accounts.insert(account_id, &Account::new(account_id.to_string())).is_some() {
env::panic_str("The account is already registered");
}
}
}
impl FungibleTokenSender for FungibleToken {
fn ft_deposit_call(
&mut self,
receiver_id: AccountId,
contract_id: AccountId,
amount: U128,
msg: String,
) -> PromiseOrValue<U128> {
assert!(env::attached_deposit() >= 1, "not enough deposit");
require!(env::prepaid_gas() > GAS_FOR_FT_DEPOSIT_CALL, "More gas is required");
let sender_id = env::predecessor_account_id();
let account = self.accounts.get(&sender_id).expect(format!("The account {} is not registered", &sender_id.to_string()).as_str());
assert!(account.get_available_balance(&Some(contract_id.clone())) >= amount.0, "not enough balance");
self.internal_contract_deposit(&sender_id, amount.0, &contract_id, &receiver_id);
ext_ft_receiver::ext(receiver_id.clone())
.with_static_gas(env::prepaid_gas() - GAS_FOR_FT_DEPOSIT_CALL)
.with_attached_deposit(env::attached_deposit())
.ft_on_deposit(sender_id.clone(), contract_id.clone(), amount.into(), msg)
.into()
}
fn ft_withdraw_call(
&mut self,
receiver_id: AccountId,
contract_id: AccountId,
amount: U128,
) -> PromiseOrValue<U128> {
assert_one_yocto();
require!(env::prepaid_gas() > GAS_FOR_FT_WITHDRAW_CALL, "More gas is required");
let sender_id = env::predecessor_account_id();
let account = self.accounts.get(&sender_id).expect(format!("The account {} is not registered", &sender_id.to_string()).as_str());
assert!(account.get_deposit_balance(&Some(contract_id.clone()), &Some(receiver_id.clone())) >= amount.0, "not enough balance");
self.internal_contract_withdraw(&sender_id, amount.0, &contract_id, &receiver_id);
ext_ft_receiver::ext(receiver_id.clone())
.with_static_gas(env::prepaid_gas() - GAS_FOR_FT_WITHDRAW_CALL)
.with_attached_deposit(env::attached_deposit())
.ft_on_withdraw(sender_id.clone(), contract_id.clone(), amount.into())
.into()
}
fn ft_burn_call(
&mut self,
receiver_id: AccountId,
contract_id: AccountId,
amount: U128,
msg: String,
) -> PromiseOrValue<U128> {
assert!(env::attached_deposit() >= 1, "not enough deposit");
require!(env::prepaid_gas() > GAS_FOR_FT_BURN_CALL, "More gas is required");
let sender_id = env::predecessor_account_id();
let account = self.accounts.get(&sender_id).expect(format!("The account {} is not registered", &sender_id.to_string()).as_str());
assert!(account.get_total_balance(&Some(contract_id.clone())) >= amount.0, "not enough balance");
ext_ft_receiver::ext(receiver_id.clone())
.with_static_gas(env::prepaid_gas() - GAS_FOR_FT_BURN_CALL)
.with_attached_deposit(env::attached_deposit())
.ft_on_burn(sender_id.clone(), contract_id.clone(), amount.into(), msg)
.then(
ext_ft_resolver::ext(env::current_account_id())
.with_static_gas(GAS_FOR_RESOLVE_BURN)
.ft_resolve_burn(sender_id, contract_id, amount),
)
.into()
}
}
impl FungibleTokenCore for FungibleToken {
fn ft_total_supply(&self, contract_id: Option<AccountId>) -> U128 {
self.total_supply.get_total_balance(&contract_id).into()
}
fn ft_balance_of(&self, account_id: AccountId, contract_id: Option<AccountId>) -> U128 {
match self.accounts.get(&account_id) {
Some(account) => account.get_total_balance(&contract_id).into(),
None => 0.into()
}
}
}
impl FungibleToken {
pub fn internal_ft_resolve_burn(
&mut self,
owner_id: &AccountId,
contract_id: &AccountId,
amount: u128,
) -> (u128, u128) {
let used_amount: Balance = match env::promise_result(0) {
PromiseResult::NotReady => env::abort(),
PromiseResult::Successful(value) => {
if let Ok(unused_amount) = near_sdk::serde_json::from_slice::<U128>(&value) {
if let Some(used_amount) = amount.checked_sub(unused_amount.0) {
used_amount
} else {
amount
}
} else {
0
}
}
PromiseResult::Failed => 0,
};
if used_amount > 0 {
self.internal_withdraw(owner_id, used_amount, &contract_id);
return (amount, used_amount)
}
(amount, 0)
}
}
impl FungibleTokenResolver for FungibleToken {
fn ft_resolve_burn(
&mut self,
owner_id: AccountId,
contract_id: AccountId,
amount: U128,
) -> U128 {
self.internal_ft_resolve_burn(&owner_id, &contract_id, amount.0).0.into()
}
}