|
1 | 1 | package convex.actors;
|
2 | 2 |
|
3 |
| -import static convex.test.Assertions.assertCVMEquals; |
4 |
| -import static convex.test.Assertions.assertError; |
| 3 | +import static convex.test.Assertions.*; |
5 | 4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
6 | 5 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
7 | 6 | import static org.junit.jupiter.api.Assertions.assertNull;
|
|
10 | 9 |
|
11 | 10 | import org.junit.jupiter.api.Test;
|
12 | 11 |
|
| 12 | +import convex.core.ErrorCodes; |
13 | 13 | import convex.core.data.AVector;
|
14 | 14 | import convex.core.data.Address;
|
15 | 15 | import convex.core.data.prim.CVMDouble;
|
@@ -56,23 +56,69 @@ public class TorusTest extends ACVMTest {
|
56 | 56 | long STK=1000000;
|
57 | 57 | Context baseContext=context();
|
58 | 58 | baseContext=exec(baseContext,"(torus/add-liquidity USD "+STK+" "+STK+")");
|
| 59 | + long USDBAL = evalL(baseContext,"(fun/balance USD)"); |
| 60 | + assertEquals(USDBAL,SUPPLY-STK); |
| 61 | + |
59 | 62 | { // Buy 100 USD
|
60 | 63 | Context ctx=baseContext;
|
61 | 64 | ctx=exec(ctx,"(torus/buy-tokens USD 100)");
|
62 | 65 | assertEquals(101L,RT.ensureLong(ctx.getResult()).longValue()); ;; // price should be 101
|
63 |
| - assertEquals(1000000000-STK+100,evalL(ctx,"(fun/balance USD)")); // should have gained 100 USD |
| 66 | + assertEquals(USDBAL+100,evalL(ctx,"(fun/balance USD)")); // should have gained 100 USD |
64 | 67 | assertEquals(STK,evalL(ctx,"(fun/balance USDM)")); // market shares unchanged
|
65 | 68 | assertTrue(evalB(ctx,"(< 1.0 (torus/price USD))")); // price has increased
|
66 | 69 | }
|
67 | 70 |
|
| 71 | + { // Buy whole pool |
| 72 | + Context ctx=baseContext; |
| 73 | + ctx=step(ctx,"(torus/buy-tokens USD "+STK+")"); |
| 74 | + assertEquals(ErrorCodes.LIQUIDITY,ctx.getErrorCode()); |
| 75 | + } |
| 76 | + |
68 | 77 | { // Buy 0 USD
|
69 | 78 | Context ctx=baseContext;
|
70 | 79 | ctx=exec(ctx,"(torus/buy-tokens USD 0)");
|
71 | 80 | assertEquals(0L,RT.ensureLong(ctx.getResult()).longValue()); ;; // price should be 0
|
72 |
| - assertEquals(1000000000-STK,evalL(ctx,"(fun/balance USD)")); // should have gained 100 USD |
| 81 | + assertEquals(USDBAL,evalL(ctx,"(fun/balance USD)")); // no balance change |
| 82 | + assertEquals(STK,evalL(ctx,"(fun/balance USDM)")); // market shares unchanged |
| 83 | + assertTrue(evalB(ctx,"(= 1.0 (torus/price USD))")); // price should be unchanged |
| 84 | + } |
| 85 | + |
| 86 | + { // Buy -100 USD |
| 87 | + Context ctx=baseContext; |
| 88 | + ctx=step(ctx,"(torus/buy-tokens USD -100)"); |
| 89 | + assertArgumentError(ctx); |
| 90 | + } |
| 91 | + |
| 92 | + { // Sell 100 USD |
| 93 | + Context ctx=baseContext; |
| 94 | + ctx=exec(ctx,"(torus/sell-tokens USD 100)"); |
| 95 | + assertEquals(99L,RT.ensureLong(ctx.getResult()).longValue()); ;; // price should be 99 |
| 96 | + assertEquals(USDBAL-100,evalL(ctx,"(fun/balance USD)")); // should have gained 100 USD |
| 97 | + assertEquals(STK,evalL(ctx,"(fun/balance USDM)")); // market shares unchanged |
| 98 | + assertTrue(evalB(ctx,"(> 1.0 (torus/price USD))")); // price has decreased |
| 99 | + } |
| 100 | + |
| 101 | + { // Sell whole USD holding |
| 102 | + Context ctx=baseContext; |
| 103 | + ctx=step(ctx,"(torus/sell-tokens USD (fun/balance USD))"); |
| 104 | + assertEquals(0,evalL(ctx,"(fun/balance USD)")); // should have no USD left |
| 105 | + } |
| 106 | + |
| 107 | + { // Sell 0 USD |
| 108 | + Context ctx=baseContext; |
| 109 | + ctx=exec(ctx,"(torus/sell-tokens USD 0)"); |
| 110 | + assertEquals(0L,RT.ensureLong(ctx.getResult()).longValue()); ;; // price should be 0 |
| 111 | + assertEquals(USDBAL,evalL(ctx,"(fun/balance USD)")); // no balance change |
73 | 112 | assertEquals(STK,evalL(ctx,"(fun/balance USDM)")); // market shares unchanged
|
74 | 113 | assertTrue(evalB(ctx,"(= 1.0 (torus/price USD))")); // price should be unchanged
|
75 | 114 | }
|
| 115 | + |
| 116 | + { // Sell -100 USD |
| 117 | + Context ctx=baseContext; |
| 118 | + ctx=step(ctx,"(torus/sell-tokens USD -100)"); |
| 119 | + assertArgumentError(ctx); |
| 120 | + } |
| 121 | + |
76 | 122 | }
|
77 | 123 |
|
78 | 124 | @Test public void testMissingMarket() {
|
|
0 commit comments