-
Notifications
You must be signed in to change notification settings - Fork 7
/
ft.rs
202 lines (177 loc) · 5.89 KB
/
ft.rs
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
use loam_sdk::{
soroban_sdk::{self, contracttype, env, Address, Lazy, Map, String},
IntoKey,
};
use loam_subcontract_core::Core;
use loam_subcontract_ft::{IsFungible, IsInitable, IsSep41};
use crate::Contract;
#[contracttype]
pub struct Txn(Address, Address);
#[contracttype]
pub struct Allowance {
amount: i128,
live_until_ledger: u32,
}
#[contracttype]
#[derive(IntoKey)]
pub struct MyFungibleToken {
balances: Map<Address, i128>,
allowances: Map<Txn, Allowance>,
authorized: Map<Address, bool>,
admin: Address,
name: String,
symbol: String,
decimals: u32,
}
impl MyFungibleToken {
#[must_use]
pub fn new(admin: Address, name: String, symbol: String, decimals: u32) -> Self {
MyFungibleToken {
balances: Map::new(env()),
allowances: Map::new(env()),
authorized: Map::new(env()),
admin,
name,
symbol,
decimals,
}
}
}
impl Default for MyFungibleToken {
fn default() -> Self {
Self::new(
env().current_contract_address(),
String::from_str(env(), ""),
String::from_str(env(), ""),
0,
)
}
}
impl IsInitable for MyFungibleToken {
fn ft_init(&mut self, admin: Address, name: String, symbol: String, decimals: u32) {
Contract::admin_get().unwrap().require_auth();
MyFungibleToken::set_lazy(MyFungibleToken::new(admin, name, symbol, decimals));
}
}
impl IsSep41 for MyFungibleToken {
fn allowance(&self, from: Address, spender: Address) -> i128 {
let allowance = self.allowances.get(Txn(from, spender));
match allowance {
Some(a) => {
if env().ledger().sequence() <= a.live_until_ledger {
a.amount
} else {
0
}
}
None => 0,
}
}
fn approve(&mut self, from: Address, spender: Address, amount: i128, live_until_ledger: u32) {
from.require_auth();
let current_ledger = env().ledger().sequence();
assert!(
!(live_until_ledger < current_ledger && amount != 0),
"live_until_ledger must be greater than or equal to the current ledger number"
);
self.allowances.set(
Txn(from, spender),
Allowance {
amount,
live_until_ledger,
},
);
}
fn balance(&self, id: Address) -> i128 {
self.balances.get(id).unwrap_or_default()
}
fn transfer(&mut self, from: Address, to: Address, amount: i128) {
from.require_auth();
let from_balance = self.balance(from.clone()) - amount;
let to_balance = self.balance(to.clone()) + amount;
self.balances.set(from, from_balance);
self.balances.set(to, to_balance);
}
fn transfer_from(&mut self, spender: Address, from: Address, to: Address, amount: i128) {
spender.require_auth();
let allowance = self.allowance(from.clone(), spender.clone());
if allowance >= amount {
self.transfer(from.clone(), to, amount);
self.decrease_allowance(from, spender, amount);
}
}
fn burn(&mut self, from: Address, amount: i128) {
from.require_auth();
let balance = self.balance(from.clone()) - amount;
self.balances.set(from, balance);
}
fn burn_from(&mut self, spender: Address, from: Address, amount: i128) {
spender.require_auth();
let allowance = self.allowance(from.clone(), spender.clone());
if allowance >= amount {
self.burn(from.clone(), amount);
self.decrease_allowance(from, spender, amount);
}
}
fn decimals(&self) -> u32 {
self.decimals
}
fn name(&self) -> String {
self.name.clone()
}
fn symbol(&self) -> String {
self.symbol.clone()
}
}
impl IsFungible for MyFungibleToken {
fn increase_allowance(&mut self, from: Address, spender: Address, amount: i128) {
from.require_auth();
let current_allowance = self.allowance(from.clone(), spender.clone());
let new_amount = current_allowance + amount;
let current_ledger = env().ledger().sequence();
self.allowances.set(
Txn(from, spender),
Allowance {
amount: new_amount,
live_until_ledger: current_ledger + 1000, // Example: set to expire after 1000 ledgers
},
);
}
fn decrease_allowance(&mut self, from: Address, spender: Address, amount: i128) {
from.require_auth();
let current_allowance = self.allowance(from.clone(), spender.clone());
let new_amount = current_allowance.checked_sub(amount).unwrap_or(0);
let current_ledger = env().ledger().sequence();
self.allowances.set(
Txn(from, spender),
Allowance {
amount: new_amount,
live_until_ledger: current_ledger + 1000, // Example: set to expire after 1000 ledgers
},
);
}
fn spendable_balance(&self, id: Address) -> i128 {
self.balance(id)
}
fn authorized(&self, id: Address) -> bool {
self.authorized.get(id).unwrap_or_default()
}
fn set_authorized(&mut self, id: Address, authorize: bool) {
self.admin.require_auth();
self.authorized.set(id, authorize);
}
fn mint(&mut self, to: Address, amount: i128) {
self.admin.require_auth();
let balance = self.balance(to.clone()) + amount;
self.balances.set(to, balance);
}
fn clawback(&mut self, from: Address, amount: i128) {
self.admin.require_auth();
let balance = self.balance(from.clone()) - amount;
self.balances.set(from, balance);
}
fn set_admin(&mut self, new_admin: Address) {
self.admin.require_auth();
self.admin = new_admin;
}
}