-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
executable file
·260 lines (213 loc) · 8.08 KB
/
lib.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
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
#![cfg_attr(not(feature = "std"), no_std, no_main)]
#[ink::contract]
mod erc20 {
use ink::storage::Mapping;
#[ink(storage)]
#[derive(Default)]
pub struct Erc20 {
total_supply: Balance,
balances: Mapping<AccountId, Balance>,
allowance: Mapping<(AccountId, AccountId), Balance>,
}
#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum Error {
BalanceTooLow,
AllowanceTooLow,
}
#[ink(event)]
pub struct Transfer {
#[ink(topic)]
from: Option<AccountId>,
#[ink(topic)]
to: AccountId,
value: Balance,
}
#[ink(event)]
pub struct Approval {
#[ink(topic)]
from: AccountId,
#[ink(topic)]
to: AccountId,
value: Balance,
}
type Result<T> = core::result::Result<T, Error>;
impl Erc20 {
#[ink(constructor)]
pub fn new(total_supply: Balance) -> Self {
let mut balances = Mapping::new();
balances.insert(Self::env().caller(), &total_supply);
Self::env().emit_event(Transfer {
from: None,
to: Self::env().caller(),
value: total_supply,
});
Self {
total_supply,
balances,
..Default::default()
}
}
#[ink(message)]
pub fn total_supply(&self) -> Balance {
self.total_supply
}
#[ink(message)]
pub fn balance_of(&self, who: AccountId) -> Balance {
self.balances.get(&who).unwrap_or_default()
}
#[ink(message)]
pub fn transfer(&mut self, to: AccountId, value: Balance) -> Result<()> {
let sender = self.env().caller();
self.do_transfer(&sender, &to, value)
}
#[ink(message)]
pub fn transfer_from(
&mut self,
from: AccountId,
to: AccountId,
value: Balance,
) -> Result<()> {
let sender = self.env().caller();
let allowance = self.allowance.get(&(from, sender)).unwrap_or_default();
if allowance < value {
return Err(Error::AllowanceTooLow);
}
self.allowance.insert(&(from, sender), &(allowance - value));
self.do_transfer(&from, &to, value)
}
#[ink(message)]
pub fn approve(&mut self, to: AccountId, value: Balance) -> Result<()> {
let sender = self.env().caller();
self.allowance.insert(&(sender, to), &value);
self.env().emit_event(Approval {
from: sender,
to,
value,
});
Ok(())
}
pub fn do_transfer(
&mut self,
from: &AccountId,
to: &AccountId,
value: Balance,
) -> Result<()> {
let balance_sender = self.balance_of(*from);
let balance_to = self.balance_of(*to);
if value > balance_sender {
return Err(Error::BalanceTooLow);
}
self.balances.insert(from, &(balance_sender - value));
self.balances.insert(to, &(balance_to + value));
self.env().emit_event(Transfer {
from: Some(*from),
to: *to,
value,
});
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
type Event = <Erc20 as ink::reflect::ContractEventBase>::Type;
#[ink::test]
fn constructor_works() {
let erc20 = Erc20::new(10000);
let accounts = ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
assert_eq!(erc20.total_supply(), 10000);
assert_eq!(erc20.balance_of(accounts.alice), 10000);
let emitted_events = ink::env::test::recorded_events().collect::<Vec<_>>();
let event = &emitted_events[0];
let decoded =
<Event as scale::Decode>::decode(&mut &event.data[..]).expect("decoded error");
match decoded {
Event::Transfer(Transfer { from, to, value }) => {
assert!(from.is_none(), "mint from error");
assert_eq!(to, accounts.alice, "mint from error");
assert_eq!(value, 10000, "mint from error");
}
_ => panic!("match error"),
}
}
#[ink::test]
fn balance_of_works() {
let erc20 = Erc20::new(10000);
let accounts = ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
assert_eq!(erc20.balance_of(accounts.alice), 10000);
}
#[ink::test]
fn transfer_works() {
let mut erc20 = Erc20::new(10000);
let accounts = ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
assert_eq!(erc20.transfer(accounts.bob, 500), Ok(()));
assert_eq!(erc20.balance_of(accounts.alice), 9500);
assert_eq!(erc20.balance_of(accounts.bob), 500);
}
#[ink::test]
fn transfer_fail() {
let mut erc20 = Erc20::new(10000);
let accounts = ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
ink::env::test::set_caller::<ink::env::DefaultEnvironment>(accounts.bob);
assert_eq!(
erc20.transfer(accounts.charlie, 500),
Err(Error::BalanceTooLow)
);
}
#[ink::test]
fn transfer_from_works() {
let mut erc20 = Erc20::new(10000);
let accounts = ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
assert_eq!(erc20.approve(accounts.bob, 500), Ok(()));
// Switch to Bob's account
ink::env::test::set_caller::<ink::env::DefaultEnvironment>(accounts.bob);
assert_eq!(
erc20.transfer_from(accounts.alice, accounts.charlie, 500),
Ok(())
);
assert_eq!(erc20.balance_of(accounts.alice), 9500);
assert_eq!(erc20.balance_of(accounts.charlie), 500);
}
#[ink::test]
fn approve_works() {
let mut erc20 = Erc20::new(10000);
let accounts = ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
assert_eq!(erc20.approve(accounts.bob, 500), Ok(()));
let allowance = erc20
.allowance
.get(&(accounts.alice, accounts.bob))
.unwrap_or_default();
assert_eq!(allowance, 500);
}
}
#[cfg(all(test, feature = "e2e-tests"))]
mod e2e_tests {
use super::*;
use ink_e2e::build_message;
type E2EResults<T> = std::result::Result<T, Box<dyn std::error::Error>>;
#[ink_e2e::test]
async fn transfer_works(mut client: ink_e2e::Client<C, E>) -> E2EResults<()> {
let total_apply = 123;
let constructor = Erc20Ref::new(total_apply);
let contract_acc_id = client
.instantiate("erc20", &ink_e2e::alice(), constructor, 0, None)
.await
.expect("instantiate failed")
.account_id;
let alice_acc = ink_e2e::account_id(ink_e2e::AccountKeyring::Alice);
let bob_acc = ink_e2e::account_id(ink_e2e::AccountKeyring::Bob);
let transfer_msg = build_message::<Erc20Ref>(contract_acc_id.clone())
.call(|erc20| erc20.transfer(bob_acc, 2));
let res = client.call(&ink_e2e::alice(), transfer_msg, 0, None).await;
assert!(res.is_ok());
let balance_of_msg = build_message::<Erc20Ref>(contract_acc_id.clone())
.call(|erc20| erc20.balance_of(alice_acc));
let balance_of_alice = client
.call_dry_run(&ink_e2e::alice(), &balance_of_msg, 0, None)
.await;
assert_eq!(balance_of_alice.return_value(), 121);
Ok(())
}
}
}