-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiquidityPoolTests.m
118 lines (95 loc) · 3.69 KB
/
LiquidityPoolTests.m
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
classdef LiquidityPoolTests < matlab.unittest.TestCase
methods (Test)
function testPoolInitialization(TestCase)
T_a = Token("TokenA");
T_b = Token("TokenB");
Q_a = 100000;
Q_b = 10000;
pool = LiquidityPool(T_a, T_b, Q_a, Q_b);
actValues = [pool.T_a, pool.T_b, pool.Q_a, pool.Q_b];
expValues = [T_a, T_b, Q_a, Q_b];
TestCase.verifyEqual(actValues, expValues);
end
function testPoolSingleTokenSwap(TestCase)
T_a = Token("TokenA");
T_b = Token("TokenB");
Q_a = 100000;
Q_b = 10000;
pool = LiquidityPool(T_a, T_b, Q_a, Q_b);
K = Q_a * Q_b;
expToken = T_b;
expQuantity = Q_b - (K / (Q_a+1));
[actToken, actQuantity] = pool.swap(T_a, 1);
TestCase.verifyEqual(actToken, expToken);
TestCase.verifyEqual(actQuantity, expQuantity);
end
function testPoolSwap(TestCase)
T_a = Token("TokenA");
T_b = Token("TokenB");
Q_a = 100000;
Q_b = 10000;
pool = LiquidityPool(T_a, T_b, Q_a, Q_b);
K = Q_a * Q_b;
swappedQuantity = 15;
expToken = T_b;
expQuantity = Q_b - (K / (Q_a+swappedQuantity));
[actToken, actQuantity] = pool.swap(T_a, swappedQuantity);
TestCase.verifyEqual(actToken, expToken);
TestCase.verifyEqual(actQuantity, expQuantity);
end
function testMultipleSwaps(TestCase)
T_a = Token("TokenA");
T_b = Token("TokenB");
Q_a = 100000;
Q_b = 10000;
pool = LiquidityPool(T_a, T_b, Q_a, Q_b);
K = Q_a * Q_b;
expToken = T_b;
expQuantity = (Q_b - (K / (Q_a+5))) - (Q_b - (K / (Q_a+3)));
pool.swap(T_a, 3);
[actToken, actQuantity] = pool.swap(T_a, 2);
TestCase.verifyEqual(actToken, expToken);
TestCase.verifyEqual(actQuantity, expQuantity);
end
function testPoolGetTokenPriceAfterSwap(TestCase)
T_a = Token("TokenA");
T_b = Token("TokenB");
Q_a = 100000;
Q_b = 10000;
P_b = 10;
K = Q_a * Q_b;
pool = LiquidityPool(T_a, T_b, Q_a, Q_b);
pool.swap(T_a, 10);
expValue = (K / ((Q_a+10)^2 + Q_a+10)) * P_b;
actValue = pool.getTokenPrice(T_a, P_b);
delta = expValue - actValue;
TestCase.verifyLessThan(delta, 1e-10);
end
function testPoolSwapWithFee(TestCase)
T_a = Token("TokenA");
T_b = Token("TokenB");
Q_a = 100000;
Q_b = 10000;
K = Q_a * Q_b;
f = 0.003;
pool = LiquidityPool(T_a, T_b, Q_a, Q_b, f);
fee = 10 * f;
expToken = T_b;
expQuantity = Q_b - (K / (Q_a+10 - fee));
[actToken, actQuantity] = pool.swap(T_a, 10);
TestCase.verifyEqual(actToken, expToken);
TestCase.verifyEqual(actQuantity, expQuantity);
end
function testComputeSwapValue(TestCase)
T_a = Token("TokenA");
T_b = Token("TokenB");
Q_a = 100000;
Q_b = 10000;
K = Q_a * Q_b;
pool = LiquidityPool(T_a, T_b, Q_a, Q_b);
actValue = pool.computeSwapValue(T_a, 500);
expValue = Q_b - (K / (Q_a + 500));
TestCase.verifyEqual(actValue, expValue);
end
end
end