-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
216 lines (188 loc) · 6.68 KB
/
Copy pathlib.rs
File metadata and controls
216 lines (188 loc) · 6.68 KB
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
//! SECURE: Canonical Secure Token
//!
//! Reference implementation combining all token security patterns:
//! - Admin-gated mint/burn with `admin.require_auth()`
//! - `from.require_auth()` on transfer
//! - Checked arithmetic on every balance mutation
//! - Self-transfer guard
//! - Events emitted on every state change
//! - Total supply tracked on mint/burn
#![no_std]
use soroban_sdk::{contract, contractimpl, contracttype, symbol_short, Address, Env};
#[contracttype]
pub enum DataKey {
Admin,
Balance(Address),
TotalSupply,
}
#[contract]
pub struct SecureToken;
#[contractimpl]
impl SecureToken {
/// One-time initialisation — sets the admin.
pub fn initialize(env: Env, admin: Address) {
if env.storage().persistent().has(&DataKey::Admin) {
panic!("already initialized");
}
env.storage().persistent().set(&DataKey::Admin, &admin);
}
/// Mint `amount` tokens to `to`. Requires admin auth.
pub fn mint(env: Env, to: Address, amount: i128) {
assert!(amount > 0, "amount must be positive");
let admin: Address = env
.storage()
.persistent()
.get(&DataKey::Admin)
.expect("not initialized");
admin.require_auth();
let bal: i128 = env
.storage()
.persistent()
.get(&DataKey::Balance(to.clone()))
.unwrap_or(0);
env.storage().persistent().set(
&DataKey::Balance(to.clone()),
&bal.checked_add(amount).expect("overflow"),
);
let supply: i128 = env
.storage()
.persistent()
.get(&DataKey::TotalSupply)
.unwrap_or(0);
env.storage().persistent().set(
&DataKey::TotalSupply,
&supply.checked_add(amount).expect("overflow"),
);
env.events().publish((symbol_short!("mint"),), (to, amount));
}
/// Burn `amount` tokens from `from`. Requires `from` auth.
pub fn burn(env: Env, from: Address, amount: i128) {
assert!(amount > 0, "amount must be positive");
from.require_auth();
let bal: i128 = env
.storage()
.persistent()
.get(&DataKey::Balance(from.clone()))
.unwrap_or(0);
let new_bal = bal.checked_sub(amount).expect("insufficient balance");
assert!(new_bal >= 0, "insufficient balance");
env.storage()
.persistent()
.set(&DataKey::Balance(from.clone()), &new_bal);
let supply: i128 = env
.storage()
.persistent()
.get(&DataKey::TotalSupply)
.unwrap_or(0);
env.storage().persistent().set(
&DataKey::TotalSupply,
&supply.checked_sub(amount).expect("supply underflow"),
);
env.events()
.publish((symbol_short!("burn"),), (from, amount));
}
/// Transfer `amount` from `from` to `to`. Requires `from` auth.
pub fn transfer(env: Env, from: Address, to: Address, amount: i128) {
assert!(amount > 0, "amount must be positive");
assert!(from != to, "self-transfer not allowed");
from.require_auth();
let from_bal: i128 = env
.storage()
.persistent()
.get(&DataKey::Balance(from.clone()))
.unwrap_or(0);
let to_bal: i128 = env
.storage()
.persistent()
.get(&DataKey::Balance(to.clone()))
.unwrap_or(0);
let new_from = from_bal.checked_sub(amount).expect("insufficient balance");
assert!(new_from >= 0, "insufficient balance");
let new_to = to_bal.checked_add(amount).expect("overflow");
env.storage()
.persistent()
.set(&DataKey::Balance(from.clone()), &new_from);
env.storage()
.persistent()
.set(&DataKey::Balance(to.clone()), &new_to);
env.events()
.publish((symbol_short!("transfer"),), (from, to, amount));
}
pub fn balance(env: Env, account: Address) -> i128 {
env.storage()
.persistent()
.get(&DataKey::Balance(account))
.unwrap_or(0)
}
pub fn total_supply(env: Env) -> i128 {
env.storage()
.persistent()
.get(&DataKey::TotalSupply)
.unwrap_or(0)
}
}
#[cfg(test)]
mod tests {
use super::*;
use soroban_sdk::{testutils::Address as _, Address, Env};
fn setup() -> (Env, Address, SecureTokenClient<'static>) {
let env = Env::default();
let id = env.register_contract(None, SecureToken);
let client = SecureTokenClient::new(&env, &id);
let admin = Address::generate(&env);
env.mock_all_auths();
client.initialize(&admin);
(env, admin, client)
}
#[test]
fn test_admin_mint_user_transfer_user_burn() {
let (env, _admin, client) = setup();
let alice = Address::generate(&env);
let bob = Address::generate(&env);
env.mock_all_auths();
client.mint(&alice, &1000);
assert_eq!(client.balance(&alice), 1000);
assert_eq!(client.total_supply(), 1000);
client.transfer(&alice, &bob, &400);
assert_eq!(client.balance(&alice), 600);
assert_eq!(client.balance(&bob), 400);
client.burn(&bob, &100);
assert_eq!(client.balance(&bob), 300);
assert_eq!(client.total_supply(), 900);
}
#[test]
#[should_panic]
fn test_non_admin_cannot_mint() {
let env = Env::default();
let id = env.register_contract(None, SecureToken);
let client = SecureTokenClient::new(&env, &id);
let admin = Address::generate(&env);
env.mock_all_auths();
client.initialize(&admin);
// Drop mock_all_auths scope — new env without mocked auth.
let env2 = Env::default();
let client2 = SecureTokenClient::new(&env2, &id);
let attacker = Address::generate(&env2);
// No mock_all_auths — require_auth on admin should fail.
client2.mint(&attacker, &1000);
}
#[test]
#[should_panic(expected = "self-transfer not allowed")]
fn test_self_transfer_rejected() {
let (env, _admin, client) = setup();
let alice = Address::generate(&env);
env.mock_all_auths();
client.mint(&alice, &500);
client.transfer(&alice, &alice, &100);
}
#[test]
#[should_panic(expected = "overflow")]
fn test_mint_overflow_panics() {
let (env, _admin, client) = setup();
let alice = Address::generate(&env);
env.mock_all_auths();
client.mint(&alice, &i128::MAX);
// Second mint pushes balance past i128::MAX
client.mint(&alice, &1);
}
}