-
Notifications
You must be signed in to change notification settings - Fork 136
/
gyro2Pool.spec.ts
201 lines (176 loc) · 7.93 KB
/
gyro2Pool.spec.ts
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
import 'dotenv/config';
// TS_NODE_PROJECT='tsconfig.testing.json' npx mocha -r ts-node/register test/gyro2Pool.spec.ts
import { expect } from 'chai';
import cloneDeep from 'lodash.clonedeep';
import { formatFixed, parseFixed, BigNumber } from '@ethersproject/bignumber';
import { JsonRpcProvider } from '@ethersproject/providers';
import { bnum } from '../src/utils/bignumber';
import { USDC, DAI, sorConfigEth } from './lib/constants';
import { SwapTypes, SOR, SwapInfo } from '../src';
// Add new PoolType
import { Gyro2Pool } from '../src/pools/gyro2Pool/gyro2Pool';
// Add new pool test data in Subgraph Schema format
import testPools from './testData/gyro2Pools/gyro2TestPool.json';
import { MockPoolDataService } from './lib/mockPoolDataService';
import { mockTokenPriceService } from './lib/mockTokenPriceService';
describe('Gyro2Pool tests USDC > DAI', () => {
const testPool = cloneDeep(testPools).pools[0];
const pool = Gyro2Pool.fromPool(testPool);
const poolPairData = pool.parsePoolPairData(USDC.address, DAI.address);
const poolPairData2 = pool.parsePoolPairData(DAI.address, USDC.address);
context('parsePoolPairData', () => {
it(`should correctly parse USDC > DAI`, async () => {
// Tests that compare poolPairData to known results with correct number scaling, etc, i.e.:
expect(poolPairData.swapFee.toString()).to.eq(
parseFixed(testPool.swapFee, 18).toString()
);
expect(poolPairData.id).to.eq(testPool.id);
});
it(`should correctly calculate price bounds USDC > DAI`, async () => {
expect(
Number(formatFixed(poolPairData.sqrtAlpha, 18))
).to.be.approximately(0.9995003747, 0.00000001);
expect(
Number(formatFixed(poolPairData.sqrtBeta, 18))
).to.be.approximately(1.000500375, 0.00000001);
});
it(`should correctly calculate price bounds DAI > USDC`, async () => {
expect(
Number(formatFixed(poolPairData2.sqrtAlpha, 18))
).to.be.approximately(0.9994998749, 0.00000001);
expect(
Number(formatFixed(poolPairData2.sqrtBeta, 18))
).to.be.approximately(1.000499875, 0.00000001);
});
});
context('limit amounts', () => {
it(`should correctly calculate limit amounts, USDC > DAI`, async () => {
let amount = pool.getLimitAmountSwap(
poolPairData,
SwapTypes.SwapExactIn
);
expect(amount.toString()).to.eq('1243.74395782517101711');
amount = pool.getLimitAmountSwap(
poolPairData,
SwapTypes.SwapExactOut
);
expect(amount.toString()).to.eq('1231.998768');
});
});
context('normalized liquidity', () => {
it(`should correctly calculate normalized liquidity, USDC > DAI`, async () => {
const normalizedLiquidity =
pool.getNormalizedLiquidity(poolPairData);
expect(Number(normalizedLiquidity.toString())).to.be.approximately(
1116333.916257166990921337,
0.00001
);
});
it(`should correctly calculate normalized liquidity, DAI > USDC`, async () => {
const normalizedLiquidity =
pool.getNormalizedLiquidity(poolPairData2);
expect(Number(normalizedLiquidity.toString())).to.be.approximately(
1116217.358286598731855228,
0.00001
);
});
});
context('Test Swaps', () => {
context('SwapExactIn', () => {
const amountIn = bnum('13.5');
it('should correctly calculate amountOut given amountIn', async () => {
const amountOut = pool._exactTokenInForTokenOut(
poolPairData,
amountIn
);
expect(amountOut.toString()).to.eq('13.379816831223414577');
});
it('should correctly calculate newSpotPrice', async () => {
const newSpotPrice =
pool._spotPriceAfterSwapExactTokenInForTokenOut(
poolPairData,
amountIn
);
expect(newSpotPrice.toString()).to.eq('1.008988469190824523');
});
it('should correctly calculate derivative of spot price function at newSpotPrice', async () => {
const derivative =
pool._derivativeSpotPriceAfterSwapExactTokenInForTokenOut(
poolPairData,
amountIn
);
expect(derivative.toString()).to.eq('0.000000895794688507');
});
});
context('SwapExactOut', () => {
const amountOut = bnum('45.568');
it('should correctly calculate amountIn given amountOut', async () => {
const amountIn = pool._tokenInForExactTokenOut(
poolPairData,
amountOut
);
expect(amountIn.toString()).to.eq('45.977973896504501314');
});
it('should correctly calculate newSpotPrice', async () => {
const newSpotPrice =
pool._spotPriceAfterSwapTokenInForExactTokenOut(
poolPairData,
amountOut
);
expect(newSpotPrice.toString()).to.eq('1.00901756299705875');
});
it('should correctly calculate derivative of spot price function at newSpotPrice', async () => {
const derivative =
pool._derivativeSpotPriceAfterSwapTokenInForExactTokenOut(
poolPairData,
amountOut
);
expect(derivative.toString()).to.eq('0.000000903885604863');
});
});
context('FullSwap', () => {
it(`Full Swap - swapExactIn, Token>Token`, async () => {
const pools: any = cloneDeep(testPools.pools);
const tokenIn = USDC.address;
const tokenOut = DAI.address;
const swapType = SwapTypes.SwapExactIn;
const swapAmt = parseFixed('13.5', 6);
const gasPrice = parseFixed('30', 9);
const maxPools = 4;
const provider = new JsonRpcProvider(
`https://mainnet.infura.io/v3/${process.env.INFURA}`
);
const sor = new SOR(
provider,
sorConfigEth,
new MockPoolDataService(pools),
mockTokenPriceService
);
const fetchSuccess = await sor.fetchPools();
expect(fetchSuccess).to.be.true;
const swapInfo: SwapInfo = await sor.getSwaps(
tokenIn,
tokenOut,
swapType,
swapAmt,
{ gasPrice, maxPools }
);
console.log(`Return amt:`);
console.log(swapInfo.returnAmount.toString());
// This value is hard coded as sanity check if things unexpectedly change. Taken from V2 test run (with extra fee logic added).
// TO DO - expect(swapInfo.returnAmount.toString()).eq('999603');
expect(swapInfo.swaps.length).eq(1);
expect(swapInfo.swaps[0].amount.toString()).eq(
swapAmt.toString()
);
expect(swapInfo.swaps[0].poolId).eq(testPools.pools[0].id);
expect(
swapInfo.tokenAddresses[swapInfo.swaps[0].assetInIndex]
).eq(tokenIn);
expect(
swapInfo.tokenAddresses[swapInfo.swaps[0].assetOutIndex]
).eq(tokenOut);
});
});
});
});