-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
executable file
·392 lines (331 loc) · 14.7 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
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
// Sources
// https://paritytech.github.io/ink-docs/datastructures/mapping
//
// Run tests: cargo +nightly test -- --nocapture
//
// Deploying
// cargo +nightly contract build --release
// TODO
// - fee earnings are not handled here ... do it via eg. earnings: Balances
//#![allow(non_snake_case)]
#![cfg_attr(not(feature = "std"), no_std)]
use ink_lang as ink;
const PRECISION: u128 = 1_000_000; // Precision of 6 digits
#[ink::contract]
mod amm {
#[cfg(not(feature = "ink-as-dependency"))]
//use ink_storage::collections::HashMap;
//use std::collections::HashMap;
use ink_storage::traits::SpreadAllocate;
type Balances = ink_storage::Mapping<AccountId, Balance>;
/// Defines the storage of your contract.
/// Add new fields to the below struct in order
/// to add new static storage fields to your contract.
#[derive(Default)]
#[ink(storage)]
#[derive(SpreadAllocate)]
pub struct Amm {
total_shares: Balance, // Stores the total amount of share issued for the pool
token1_total: Balance, // Stores the amount of Token1 locked in the pool
token2_total: Balance, // Stores the amount of Token2 locked in the pool
shares: Balances, // Stores the share holding of each provider
token1_balances: Balances, // Stores the token1 balance of each user
token2_balances: Balances, // Stores the token2 balance of each user
fees: Balance,
}
#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum Error {
/// Zero Liquidity
ZeroLiquidity,
/// Amount cannot be zero!
ZeroAmount,
/// Insufficient amount
InsufficientAmount,
/// Equivalent value of tokens not provided
NonEquivalentValue,
/// Asset value less than threshold for contribution!
ThresholdNotReached,
/// Share should be less than totalShare
InvalidShare,
/// Insufficient pool balance
InsufficientLiquidity,
/// Slippage tolerance exceeded
SlippageExceeded,
}
impl Amm {
fn valid_amount_check(&self, balances: &Balances, qty: Balance) -> Result<(), Error> {
let caller = self.env().caller();
let my_balance = balances.get(&caller).unwrap_or(0);
match qty {
0 => Err(Error::ZeroAmount),
_ if qty > my_balance => Err(Error::InsufficientAmount),
_ => Ok(()),
}
}
fn get_k(&self) -> Balance {
self.token1_total * self.token2_total
}
fn active_pool(&self) -> Result<(), Error> {
match self.get_k() {
0 => Err(Error::ZeroLiquidity),
_ => Ok(()),
}
}
/// Constructs a new AMM instance
/// @param _fees: valid interval -> [0,1000]
#[ink(constructor)]
pub fn new(fees: Balance) -> Self {
//Self {
//fees: if fees > 1000 { 0 } else { fees },
//..Default::default()
//}
ink_lang::utils::initialize_contract(|contract: &mut Self| {
let caller = Self::env().caller();
contract.shares.insert(&caller, &0);
contract.token1_balances.insert(&caller, &0);
contract.token2_balances.insert(&caller, &0);
contract.fees = fees;
})
}
#[ink(constructor)]
pub fn default() -> Self {
// Even though we're not explicitly initializing the `Mapping`,
// we still need to call this
ink_lang::utils::initialize_contract(|_| {})
}
#[ink(message)]
pub fn faucet(&mut self, token1_amount: Balance, token2_amount: Balance) {
let caller = self.env().caller();
let token1_balance = self.token1_balances.get(&caller).unwrap_or(0);
let token2_balance = self.token2_balances.get(&caller).unwrap_or(0);
self.token1_balances
.insert(caller, &(token1_balance + token1_amount));
self.token2_balances
.insert(caller, &(token2_balance + token2_amount));
}
#[ink(message)]
pub fn get_my_holdings(&self) -> (Balance, Balance, Balance) {
let caller = self.env().caller();
let token1_balance = self.token1_balances.get(&caller).unwrap_or(0);
let token2_balance = self.token2_balances.get(&caller).unwrap_or(0);
let shares = self.shares.get(&caller).unwrap_or(0);
(token1_balance, token2_balance, shares)
}
#[ink(message)]
pub fn get_pool_details(&self) -> (Balance, Balance, Balance, Balance) {
(
self.token1_total,
self.token2_total,
self.total_shares,
self.fees,
)
}
#[ink(message)]
pub fn provide(
&mut self,
token1_amount: Balance,
token2_amount: Balance,
) -> Result<Balance, Error> {
self.valid_amount_check(&self.token1_balances, token1_amount)?;
self.valid_amount_check(&self.token2_balances, token2_amount)?;
let share = if self.total_shares == 0 {
100 * super::PRECISION
} else {
let share1 = self.total_shares * token1_amount / self.token1_total;
let share2 = self.total_shares * token2_amount / self.token2_total;
if share1 != share2 {
return Err(Error::NonEquivalentValue);
}
share1
};
if share == 0 {
return Err(Error::ThresholdNotReached);
}
let caller = self.env().caller();
let token1_balance = self.token1_balances.get(&caller).unwrap();
let token2_balance = self.token2_balances.get(&caller).unwrap();
self.token1_balances
.insert(caller, &(token1_balance - token1_amount));
self.token2_balances
.insert(caller, &(token2_balance - token2_amount));
self.token1_total += token1_amount;
self.token2_total += token2_amount;
self.total_shares += share;
let caller_share = self.shares.get(&caller).unwrap_or(0);
self.shares.insert(caller, &(caller_share + share));
Ok(share)
}
#[ink(message)]
pub fn get_withdraw_estimate(&self, share: Balance) -> Result<(Balance, Balance), Error> {
self.active_pool()?;
if share > self.total_shares {
return Err(Error::InvalidShare);
}
let token1_amount = share * self.token1_total / self.total_shares;
let token2_amount = share * self.token2_total / self.total_shares;
Ok((token1_amount, token2_amount))
}
#[ink(message)]
pub fn withdraw(&mut self, share: Balance) -> Result<(Balance, Balance), Error> {
let caller = self.env().caller();
self.valid_amount_check(&self.shares, share)?;
let caller_share = self.shares.get(&caller).unwrap();
let caller_token1_balance = self.token1_balances.get(&caller).unwrap();
let caller_token2_balance = self.token2_balances.get(&caller).unwrap();
let (token1_amount, token2_amount) = self.get_withdraw_estimate(share)?;
self.shares.insert(caller, &(caller_share - share));
self.total_shares -= share;
self.token1_total -= token1_amount;
self.token2_total -= token2_amount;
self.token1_balances
.insert(caller, &(caller_token1_balance + token1_amount));
self.token2_balances
.insert(caller, &(caller_token2_balance + token2_amount));
Ok((token1_amount, token2_amount))
}
#[ink(message)]
pub fn swap_token1_to_token2(
&mut self,
token1_amount: Balance,
token2_min: Balance,
) -> Result<Balance, Error> {
self.active_pool()?;
self.valid_amount_check(&self.token1_balances, token1_amount)?;
if token1_amount >= self.token1_total {
return Err(Error::InsufficientLiquidity);
}
let caller = self.env().caller();
let fee = self.fees * token1_amount / 1000;
let token1_w_fee = token1_amount - fee;
let total_token1_after = self.token1_total + token1_w_fee;
let total_token2_after = self.get_k() / total_token1_after;
// current total - calculated total after swap by K formula (x * y = K) ^^^
// it means we won't get token2 amount related to rate BEFORE exchange
// but related to rate AFTER exchange .... SLIPPAGE
let token2_withdraw = self.token2_total - total_token2_after;
// check slippage
if token2_withdraw < token2_min {
return Err(Error::SlippageExceeded);
}
self.token1_total = total_token1_after;
self.token2_total = total_token2_after;
let caller_token2_balance = self.token2_balances.get(caller).unwrap_or(0);
self.token2_balances
.insert(caller, &(caller_token2_balance + token2_withdraw));
let caller_token1_balance = self.token1_balances.get(caller).unwrap();
self.token1_balances
.insert(caller, &(caller_token1_balance - token1_amount));
Ok(token2_withdraw)
}
#[ink(message)]
pub fn swap_token2_to_token1(
&mut self,
token2_amount: Balance,
token1_min: Balance,
) -> Result<Balance, Error> {
self.active_pool()?;
self.valid_amount_check(&self.token2_balances, token2_amount)?;
if token2_amount >= self.token2_total {
return Err(Error::InsufficientLiquidity);
}
let caller = self.env().caller();
let fee = self.fees * token2_amount / 1000;
let token2_w_fee = token2_amount - fee;
let total_token2_after = self.token2_total + token2_w_fee;
let total_token1_after = self.get_k() / total_token2_after;
let token1_withdraw = self.token1_total - total_token1_after;
// check slippage
if token1_withdraw < token1_min {
return Err(Error::SlippageExceeded);
}
self.token1_total = total_token1_after;
self.token2_total = total_token2_after;
let caller_token1_balance = self.token1_balances.get(caller).unwrap_or(0);
self.token1_balances
.insert(caller, &(caller_token1_balance + token1_withdraw));
let caller_token2_balance = self.token2_balances.get(caller).unwrap();
self.token2_balances
.insert(caller, &(caller_token2_balance - token2_amount));
Ok(token1_withdraw)
}
}
/// Unit tests in Rust are normally defined within such a `#[cfg(test)]`
/// module and test functions are marked with a `#[test]` attribute.
/// The below code is technically just normal Rust code.
#[cfg(test)]
mod tests {
/// Imports all the definitions from the outer scope so we can use them here.
use super::*;
/// Imports `ink_lang` so we can use `#[ink::test]`.
use ink_lang as ink;
#[ink::test]
fn new_works() {
let contract = Amm::new(0);
assert_eq!(contract.get_my_holdings(), (0, 0, 0));
assert_eq!(contract.get_pool_details(), (0, 0, 0, 0));
}
#[ink::test]
fn faucet_works() {
let mut contract = Amm::new(0);
contract.faucet(10, 20);
assert_eq!(contract.get_my_holdings(), (10, 20, 0));
assert_eq!(contract.get_pool_details(), (0, 0, 0, 0));
}
#[ink::test]
fn active_pool_test() {
let contract = Amm::new(0);
let res = contract.active_pool();
assert_eq!(res, Err(Error::ZeroLiquidity));
}
#[ink::test]
fn provide_test() {
let mut contract = Amm::new(0);
contract.faucet(100, 200);
let share = contract.provide(10, 20).unwrap();
assert_eq!(share, 100_000_000);
assert_eq!(contract.get_my_holdings(), (90, 180, share));
assert_eq!(contract.get_pool_details(), (10, 20, share, 0));
}
#[ink::test]
fn withdraw_test() {
let mut contract = Amm::new(0);
contract.faucet(100, 200);
let share = contract.provide(10, 20).unwrap();
assert_eq!(contract.withdraw(share / 2).unwrap(), (5, 10));
assert_eq!(contract.get_my_holdings(), (95, 190, share / 2));
assert_eq!(contract.get_pool_details(), (5, 10, share / 2, 0));
}
#[ink::test]
fn swap_token1_to_token2_test() {
let mut contract = Amm::new(500); // 50%
contract.faucet(200, 200);
let share = contract.provide(100, 200).unwrap();
assert_eq!(contract.get_my_holdings(), (100, 0, share));
assert_eq!(contract.get_pool_details(), (100, 200, share, 500));
let _res = contract.swap_token1_to_token2(50, 10);
// 50 token1 provided ... w/ fee (50%) it is 25
// rate in pool is 1 token1 / 2 token2 ... so 25 token1 * 2 -> 50 token2
// with slippage it will be 40 token2 (pool state/rate 125 / 160)
assert_eq!(contract.get_my_holdings(), (50, 40, share));
// token1 100 + 25 (given amount w/o fee), token2 200 - withdrawed 40
assert_eq!(contract.get_pool_details(), (125, 160, share, 500));
}
#[ink::test]
fn swap_token2_to_token1_test() {
let mut contract = Amm::new(0);
contract.faucet(200, 200);
let share = contract.provide(100, 100).unwrap();
assert_eq!(contract.get_my_holdings(), (100, 100, share));
assert_eq!(contract.get_pool_details(), (100, 100, share, 0));
let _res = contract.swap_token2_to_token1(50, 20);
println!("res: {:?}", _res);
assert_eq!(contract.get_my_holdings(), (134, 50, share));
// token2 100 + 50 (given amount), token2 66 - withdrawed 34
// before K: 100 * 100 = 10_000
// after K: 66 * 150 = 9_900 .... Balance is u128 so it doesn't have decimal
// precission. Correctly it should be 66.66666 * 150 = 10_000
assert_eq!(contract.get_pool_details(), (66, 150, share, 0));
}
}
}