@paraswap/dex-lib 3.5.6-maverick-v2-7 → 3.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/launch.json +53 -0
- package/.vscode/settings.json +2 -0
- package/.vscode/tasks.json +15 -0
- package/build/abi/uniswap-v3/AlienBaseV3Router.abi.json +52 -0
- package/build/abi/wombat/pool.json +1289 -0
- package/build/constants.d.ts +2 -0
- package/build/constants.js +3 -1
- package/build/constants.js.map +1 -1
- package/build/dex/aave-v1/aave-v1.d.ts +2 -1
- package/build/dex/aave-v1/aave-v1.js +23 -0
- package/build/dex/aave-v1/aave-v1.js.map +1 -1
- package/build/dex/aave-v2/tokens-avalanche.json +44 -0
- package/build/dex/aave-v2/tokens-mainnet.json +188 -0
- package/build/dex/aave-v2/tokens-polygon.json +44 -0
- package/build/dex/compound.js +1 -1
- package/build/dex/compound.js.map +1 -1
- package/build/dex/index.js +0 -2
- package/build/dex/index.js.map +1 -1
- package/build/dex/maker-psm/scripts/gem.abi.json +136 -0
- package/build/dex/maker-psm/scripts/validate-state.d.ts +0 -0
- package/build/dex/maker-psm/scripts/validate-state.js +185 -0
- package/build/dex/maker-psm/scripts/validate-state.js.map +1 -0
- package/build/dex/maker-psm/scripts/vat.abi.json +243 -0
- package/build/dex/maverick-v2/config.d.ts +2 -1
- package/build/dex/maverick-v2/config.js +6 -2
- package/build/dex/maverick-v2/config.js.map +1 -1
- package/build/dex/maverick-v2/maverick-v2.d.ts +3 -2
- package/build/dex/maverick-v2/maverick-v2.js +17 -6
- package/build/dex/maverick-v2/maverick-v2.js.map +1 -1
- package/build/dex/platypus/platypus.d.ts +2 -1
- package/build/dex/platypus/platypus.js +45 -0
- package/build/dex/platypus/platypus.js.map +1 -1
- package/build/dex/solidly-v3/scripts/check-event-pool-event.js +53 -0
- package/build/dex/solidly-v3/scripts/check-event-pool-event.js.map +1 -0
- package/build/dex/stable-pool.js +1 -8
- package/build/dex/stable-pool.js.map +1 -1
- package/build/dex/swaap-v1/swaap-v1.d.ts +2 -1
- package/build/dex/swaap-v1/swaap-v1.js +14 -0
- package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
- package/build/dex/uniswap-v2/uniswap-v2.d.ts +2 -2
- package/build/dex/uniswap-v2/uniswap-v2.js.map +1 -1
- package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.d.ts +17 -0
- package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js +64 -0
- package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js.map +1 -0
- package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.d.ts +17 -0
- package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js +79 -0
- package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js.map +1 -0
- package/build/dex/uniswap-v3/uniswap-v3.d.ts +2 -2
- package/build/dex/uniswap-v3/uniswap-v3.js.map +1 -1
- package/build/dex/zerox/index.d.ts +1 -1
- package/build/dex/zerox/index.js.map +1 -1
- package/build/executor/Executor03BytecodeBuilder.js +1 -12
- package/build/executor/Executor03BytecodeBuilder.js.map +1 -1
- package/build/generic-swap-transaction-builder.d.ts +5 -3
- package/build/generic-swap-transaction-builder.js +22 -7
- package/build/generic-swap-transaction-builder.js.map +1 -1
- package/package.json +2 -2
- package/src/constants.ts +2 -0
- package/src/dex/index.ts +0 -2
- package/src/dex/maker-psm/scripts/gem.abi.json +136 -0
- package/src/dex/maker-psm/scripts/validate-state.ts +221 -0
- package/src/dex/maker-psm/scripts/vat.abi.json +243 -0
- package/src/dex/uniswap-v2/uniswap-v2.ts +2 -2
- package/src/dex/uniswap-v3/uniswap-v3.ts +2 -2
- package/src/dex/zerox/index.ts +1 -1
- package/src/executor/Executor03BytecodeBuilder.ts +1 -15
- package/src/generic-swap-transaction-builder.ts +40 -0
- package/tests/constants-e2e.ts +2 -2
- package/build/dex/etherfi/etherfi.d.ts +0 -26
- package/build/dex/etherfi/etherfi.js +0 -96
- package/build/dex/etherfi/etherfi.js.map +0 -1
- package/build/dex/sdai/config.d.ts +0 -11
- package/build/dex/sdai/config.js +0 -21
- package/build/dex/sdai/config.js.map +0 -1
- package/build/dex/sdai/constants.d.ts +0 -2
- package/build/dex/sdai/constants.js +0 -6
- package/build/dex/sdai/constants.js.map +0 -1
- package/build/dex/sdai/scripts/validate-state.js +0 -102
- package/build/dex/sdai/scripts/validate-state.js.map +0 -1
- package/build/dex/sdai/sdai-pool.d.ts +0 -16
- package/build/dex/sdai/sdai-pool.js +0 -85
- package/build/dex/sdai/sdai-pool.js.map +0 -1
- package/build/dex/sdai/sdai.d.ts +0 -51
- package/build/dex/sdai/sdai.js +0 -172
- package/build/dex/sdai/sdai.js.map +0 -1
- package/build/dex/sdai/types.d.ts +0 -21
- package/build/dex/sdai/types.js +0 -11
- package/build/dex/sdai/types.js.map +0 -1
- package/build/dex/sdai/utils.d.ts +0 -4
- package/build/dex/sdai/utils.js +0 -39
- package/build/dex/sdai/utils.js.map +0 -1
- package/src/abi/maverick-v2/MaverickV2Pool.json +0 -1234
- package/src/abi/maverick-v2/MaverickV2PoolLens.json +0 -1232
- package/src/abi/maverick-v2/MaverickV2Quoter.json +0 -278
- package/src/dex/maverick-v2/config.ts +0 -17
- package/src/dex/maverick-v2/maverick-math/maverick-basic-math.ts +0 -152
- package/src/dex/maverick-v2/maverick-math/maverick-bin-math.ts +0 -288
- package/src/dex/maverick-v2/maverick-math/maverick-delta-math.ts +0 -32
- package/src/dex/maverick-v2/maverick-math/maverick-math.ts +0 -30
- package/src/dex/maverick-v2/maverick-math/maverick-pool-lib.ts +0 -42
- package/src/dex/maverick-v2/maverick-math/maverick-pool-math.ts +0 -709
- package/src/dex/maverick-v2/maverick-math/maverick-swap-math.ts +0 -199
- package/src/dex/maverick-v2/maverick-math/maverick-tick-math.ts +0 -171
- package/src/dex/maverick-v2/maverick-math/maverick-twa-math.ts +0 -54
- package/src/dex/maverick-v2/maverick-v2-e2e.test.ts +0 -176
- package/src/dex/maverick-v2/maverick-v2-events.test.ts +0 -112
- package/src/dex/maverick-v2/maverick-v2-integration.test.ts +0 -269
- package/src/dex/maverick-v2/maverick-v2-pool.ts +0 -276
- package/src/dex/maverick-v2/maverick-v2.ts +0 -424
- package/src/dex/maverick-v2/types.ts +0 -152
- /package/build/dex/{sdai/scripts/validate-state.d.ts → solidly-v3/scripts/check-event-pool-event.d.ts} +0 -0
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
|
-
import dotenv from 'dotenv';
|
|
3
|
-
dotenv.config();
|
|
4
|
-
|
|
5
|
-
import { Interface, Result } from '@ethersproject/abi';
|
|
6
|
-
import { DummyDexHelper } from '../../dex-helper/index';
|
|
7
|
-
import { Network, SwapSide } from '../../constants';
|
|
8
|
-
import { BI_POWS } from '../../bigint-constants';
|
|
9
|
-
import { MaverickV2 } from './maverick-v2';
|
|
10
|
-
import {
|
|
11
|
-
checkPoolPrices,
|
|
12
|
-
checkPoolsLiquidity,
|
|
13
|
-
checkConstantPoolPrices,
|
|
14
|
-
} from '../../../tests/utils';
|
|
15
|
-
import { Tokens } from '../../../tests/constants-e2e';
|
|
16
|
-
import { MaverickV2Data } from './types';
|
|
17
|
-
import { ExchangePrices, PoolPrices } from '../../types';
|
|
18
|
-
import MaverickV2QuoterABI from '../../abi/maverick-v2/MaverickV2Quoter.json';
|
|
19
|
-
import { MaverickV2Config } from './config';
|
|
20
|
-
import { AbiItem } from 'web3-utils';
|
|
21
|
-
|
|
22
|
-
/*
|
|
23
|
-
README
|
|
24
|
-
======
|
|
25
|
-
|
|
26
|
-
This test script adds tests for MaverickV2 general integration
|
|
27
|
-
with the DEX interface. The test cases below are example tests.
|
|
28
|
-
It is recommended to add tests which cover MaverickV2 specific
|
|
29
|
-
logic.
|
|
30
|
-
|
|
31
|
-
You can run this individual test script by running:
|
|
32
|
-
`npx jest src/dex/<dex-name>/<dex-name>-integration.test.ts`
|
|
33
|
-
|
|
34
|
-
(This comment should be removed from the final implementation)
|
|
35
|
-
*/
|
|
36
|
-
|
|
37
|
-
const network = Network.BASE;
|
|
38
|
-
const dexKey = 'MaverickV2';
|
|
39
|
-
const dexHelper = new DummyDexHelper(network);
|
|
40
|
-
|
|
41
|
-
function calculateSwap(
|
|
42
|
-
network: Network,
|
|
43
|
-
pool: PoolPrices<MaverickV2Data>,
|
|
44
|
-
blockNumber: number,
|
|
45
|
-
poolAddress: string,
|
|
46
|
-
srcToken: string,
|
|
47
|
-
side: SwapSide,
|
|
48
|
-
amount: bigint,
|
|
49
|
-
) {
|
|
50
|
-
return new Promise(async (resolve, reject) => {
|
|
51
|
-
let quoterContract = new dexHelper.web3Provider.eth.Contract(
|
|
52
|
-
MaverickV2QuoterABI as AbiItem[],
|
|
53
|
-
MaverickV2Config[dexKey][network].quoterAddress,
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
await quoterContract.methods
|
|
57
|
-
.calculateSwap(
|
|
58
|
-
poolAddress,
|
|
59
|
-
amount,
|
|
60
|
-
pool.data.tokenA.toLowerCase() == srcToken.toLowerCase(),
|
|
61
|
-
side == SwapSide.BUY,
|
|
62
|
-
pool.data.tokenA.toLowerCase() == srcToken.toLowerCase()
|
|
63
|
-
? 1000000n
|
|
64
|
-
: -1000000n,
|
|
65
|
-
)
|
|
66
|
-
.call({}, blockNumber, (err: any, result: any) => {
|
|
67
|
-
resolve(result);
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async function checkOnChainPricing(
|
|
73
|
-
maverickV2: MaverickV2,
|
|
74
|
-
funcName: string,
|
|
75
|
-
blockNumber: number,
|
|
76
|
-
pools: ExchangePrices<MaverickV2Data>,
|
|
77
|
-
amounts: bigint[],
|
|
78
|
-
side: SwapSide,
|
|
79
|
-
srcToken: string,
|
|
80
|
-
) {
|
|
81
|
-
await Promise.all(
|
|
82
|
-
pools.map(async pool => {
|
|
83
|
-
const poolAddress = pool.data.pool;
|
|
84
|
-
|
|
85
|
-
const results = await Promise.all(
|
|
86
|
-
amounts.slice(1).map(async amount => {
|
|
87
|
-
return calculateSwap(
|
|
88
|
-
network,
|
|
89
|
-
pool,
|
|
90
|
-
blockNumber,
|
|
91
|
-
poolAddress,
|
|
92
|
-
srcToken,
|
|
93
|
-
side,
|
|
94
|
-
amount,
|
|
95
|
-
);
|
|
96
|
-
}),
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
const expectedPrices = [0n].concat(
|
|
100
|
-
results.map((result: any) =>
|
|
101
|
-
BigInt(side == SwapSide.SELL ? result.amountOut : result.amountIn),
|
|
102
|
-
),
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
expect(pool.prices).toEqual(expectedPrices);
|
|
106
|
-
}),
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
async function testPricingOnNetwork(
|
|
111
|
-
maverickV2: MaverickV2,
|
|
112
|
-
network: Network,
|
|
113
|
-
dexKey: string,
|
|
114
|
-
blockNumber: number,
|
|
115
|
-
srcTokenSymbol: string,
|
|
116
|
-
destTokenSymbol: string,
|
|
117
|
-
side: SwapSide,
|
|
118
|
-
amounts: bigint[],
|
|
119
|
-
funcNameToCheck: string,
|
|
120
|
-
) {
|
|
121
|
-
const networkTokens = Tokens[network];
|
|
122
|
-
const pools = await maverickV2.getPoolIdentifiers(
|
|
123
|
-
networkTokens[srcTokenSymbol],
|
|
124
|
-
networkTokens[destTokenSymbol],
|
|
125
|
-
side,
|
|
126
|
-
blockNumber,
|
|
127
|
-
);
|
|
128
|
-
console.log(
|
|
129
|
-
`${srcTokenSymbol} <> ${destTokenSymbol} Pool Identifiers: `,
|
|
130
|
-
pools,
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
expect(pools.length).toBeGreaterThan(0);
|
|
134
|
-
|
|
135
|
-
const poolPrices = await maverickV2.getPricesVolume(
|
|
136
|
-
networkTokens[srcTokenSymbol],
|
|
137
|
-
networkTokens[destTokenSymbol],
|
|
138
|
-
amounts,
|
|
139
|
-
side,
|
|
140
|
-
blockNumber,
|
|
141
|
-
pools,
|
|
142
|
-
);
|
|
143
|
-
console.log(
|
|
144
|
-
`${srcTokenSymbol} <> ${destTokenSymbol} Pool Prices: `,
|
|
145
|
-
poolPrices,
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
expect(poolPrices).not.toBeNull();
|
|
149
|
-
if (maverickV2.hasConstantPriceLargeAmounts) {
|
|
150
|
-
checkConstantPoolPrices(poolPrices!, amounts, dexKey);
|
|
151
|
-
} else {
|
|
152
|
-
checkPoolPrices(poolPrices!, amounts, side, dexKey);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// Check if onchain pricing equals to calculated ones
|
|
156
|
-
await checkOnChainPricing(
|
|
157
|
-
maverickV2,
|
|
158
|
-
funcNameToCheck,
|
|
159
|
-
blockNumber,
|
|
160
|
-
poolPrices!,
|
|
161
|
-
amounts,
|
|
162
|
-
side,
|
|
163
|
-
Tokens[network]['DAI'].address,
|
|
164
|
-
);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
describe('MaverickV2', function () {
|
|
168
|
-
const dexKey = 'MaverickV2';
|
|
169
|
-
let blockNumber: number;
|
|
170
|
-
let maverickV2: MaverickV2;
|
|
171
|
-
|
|
172
|
-
describe('Mainnet', () => {
|
|
173
|
-
const network = Network.BASE;
|
|
174
|
-
const dexHelper = new DummyDexHelper(network);
|
|
175
|
-
|
|
176
|
-
const tokens = Tokens[network];
|
|
177
|
-
|
|
178
|
-
// TODO: Put here token Symbol to check against
|
|
179
|
-
// Don't forget to update relevant tokens in constant-e2e.ts
|
|
180
|
-
const srcTokenSymbol = 'DAI';
|
|
181
|
-
const destTokenSymbol = 'USDC';
|
|
182
|
-
|
|
183
|
-
const amountsForSell = [
|
|
184
|
-
0n,
|
|
185
|
-
1n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
186
|
-
2n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
187
|
-
3n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
188
|
-
4n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
189
|
-
5n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
190
|
-
6n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
191
|
-
7n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
192
|
-
8n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
193
|
-
9n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
194
|
-
10n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
195
|
-
];
|
|
196
|
-
|
|
197
|
-
const amountsForBuy = [
|
|
198
|
-
0n,
|
|
199
|
-
1n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
200
|
-
2n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
201
|
-
3n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
202
|
-
4n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
203
|
-
5n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
204
|
-
6n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
205
|
-
7n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
206
|
-
8n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
207
|
-
9n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
208
|
-
10n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
209
|
-
];
|
|
210
|
-
|
|
211
|
-
beforeAll(async () => {
|
|
212
|
-
blockNumber = await dexHelper.web3Provider.eth.getBlockNumber();
|
|
213
|
-
maverickV2 = new MaverickV2(network, dexKey, dexHelper);
|
|
214
|
-
if (maverickV2.initializePricing) {
|
|
215
|
-
await maverickV2.initializePricing(blockNumber);
|
|
216
|
-
}
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
it('getPoolIdentifiers and getPricesVolume SELL', async function () {
|
|
220
|
-
await testPricingOnNetwork(
|
|
221
|
-
maverickV2,
|
|
222
|
-
network,
|
|
223
|
-
dexKey,
|
|
224
|
-
blockNumber,
|
|
225
|
-
srcTokenSymbol,
|
|
226
|
-
destTokenSymbol,
|
|
227
|
-
SwapSide.SELL,
|
|
228
|
-
amountsForSell,
|
|
229
|
-
'calculatePrice', // TODO: Put here proper function name to check pricing
|
|
230
|
-
);
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
it('getPoolIdentifiers and getPricesVolume BUY', async function () {
|
|
234
|
-
await testPricingOnNetwork(
|
|
235
|
-
maverickV2,
|
|
236
|
-
network,
|
|
237
|
-
dexKey,
|
|
238
|
-
blockNumber,
|
|
239
|
-
srcTokenSymbol,
|
|
240
|
-
destTokenSymbol,
|
|
241
|
-
SwapSide.BUY,
|
|
242
|
-
amountsForBuy,
|
|
243
|
-
'calculatePrice', // TODO: Put here proper function name to check pricing
|
|
244
|
-
);
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
it('getTopPoolsForToken', async function () {
|
|
248
|
-
// We have to check without calling initializePricing, because
|
|
249
|
-
// pool-tracker is not calling that function
|
|
250
|
-
const newMaverickV2 = new MaverickV2(network, dexKey, dexHelper);
|
|
251
|
-
if (newMaverickV2.updatePoolState) {
|
|
252
|
-
await newMaverickV2.updatePoolState();
|
|
253
|
-
}
|
|
254
|
-
const poolLiquidity = await newMaverickV2.getTopPoolsForToken(
|
|
255
|
-
tokens[srcTokenSymbol].address,
|
|
256
|
-
10,
|
|
257
|
-
);
|
|
258
|
-
console.log(`${srcTokenSymbol} Top Pools:`, poolLiquidity);
|
|
259
|
-
|
|
260
|
-
if (!newMaverickV2.hasConstantPriceLargeAmounts) {
|
|
261
|
-
checkPoolsLiquidity(
|
|
262
|
-
poolLiquidity,
|
|
263
|
-
tokens[srcTokenSymbol].address,
|
|
264
|
-
dexKey,
|
|
265
|
-
);
|
|
266
|
-
}
|
|
267
|
-
});
|
|
268
|
-
});
|
|
269
|
-
});
|
|
@@ -1,276 +0,0 @@
|
|
|
1
|
-
import { Interface } from '@ethersproject/abi';
|
|
2
|
-
import { DeepReadonly } from 'ts-essentials';
|
|
3
|
-
import { Address, BlockHeader, Log, Logger, Token } from '../../types';
|
|
4
|
-
import { catchParseLogError } from '../../utils';
|
|
5
|
-
import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
|
|
6
|
-
import { IDexHelper } from '../../dex-helper/idex-helper';
|
|
7
|
-
import { PoolState } from './types';
|
|
8
|
-
import MaverickV2PoolABI from '../../abi/maverick-v2/MaverickV2Pool.json';
|
|
9
|
-
import MaverickV2PoolLensABI from '../../abi/maverick-v2/MaverickV2PoolLens.json';
|
|
10
|
-
import { AbiItem } from 'web3-utils';
|
|
11
|
-
import { Contract } from 'web3-eth-contract';
|
|
12
|
-
import { MaverickPoolMath } from './maverick-math/maverick-pool-math';
|
|
13
|
-
import _ from 'lodash';
|
|
14
|
-
``;
|
|
15
|
-
export class MaverickV2EventPool extends StatefulEventSubscriber<PoolState> {
|
|
16
|
-
handlers: {
|
|
17
|
-
[event: string]: (
|
|
18
|
-
event: any,
|
|
19
|
-
state: DeepReadonly<PoolState>,
|
|
20
|
-
log: Readonly<Log>,
|
|
21
|
-
blockHeader: BlockHeader,
|
|
22
|
-
) => DeepReadonly<PoolState> | null;
|
|
23
|
-
} = {};
|
|
24
|
-
|
|
25
|
-
poolContract: Contract;
|
|
26
|
-
poolLensContract: Contract;
|
|
27
|
-
|
|
28
|
-
logDecoder: (log: Log) => any;
|
|
29
|
-
|
|
30
|
-
addressesSubscribed: string[];
|
|
31
|
-
|
|
32
|
-
poolMath: MaverickPoolMath;
|
|
33
|
-
|
|
34
|
-
constructor(
|
|
35
|
-
readonly parentName: string,
|
|
36
|
-
protected network: number,
|
|
37
|
-
protected dexHelper: IDexHelper,
|
|
38
|
-
logger: Logger,
|
|
39
|
-
public tokenA: Token,
|
|
40
|
-
public tokenB: Token,
|
|
41
|
-
public feeAIn: number,
|
|
42
|
-
public feeBIn: number,
|
|
43
|
-
public tickSpacing: number,
|
|
44
|
-
public protocolFeeRatio: number,
|
|
45
|
-
public lookback: number,
|
|
46
|
-
public activeTick: number,
|
|
47
|
-
public address: Address,
|
|
48
|
-
public poolLensAddress: Address,
|
|
49
|
-
protected maverickV2Iface = new Interface(MaverickV2PoolABI), // TODO: add any additional params required for event subscriber
|
|
50
|
-
) {
|
|
51
|
-
const name = `${parentName}-${tokenA.symbol}-${tokenB.symbol}-${feeAIn}-${feeBIn}-${tickSpacing}-${protocolFeeRatio}-${lookback}-${activeTick}`;
|
|
52
|
-
super(parentName, name, dexHelper, logger);
|
|
53
|
-
|
|
54
|
-
// TODO: make logDecoder decode logs that
|
|
55
|
-
this.logDecoder = (log: Log) => this.maverickV2Iface.parseLog(log);
|
|
56
|
-
this.addressesSubscribed = [address];
|
|
57
|
-
|
|
58
|
-
// Add handlers
|
|
59
|
-
this.handlers['PoolAddLiquidity'] = this.handleAddLiquidityEvent.bind(this);
|
|
60
|
-
this.handlers['PoolRemoveLiquidity'] =
|
|
61
|
-
this.handleRemoveLiquidityEvent.bind(this);
|
|
62
|
-
this.handlers['PoolSwap'] = this.handleSwapEvent.bind(this);
|
|
63
|
-
|
|
64
|
-
this.poolMath = new MaverickPoolMath(
|
|
65
|
-
BigInt(feeAIn),
|
|
66
|
-
BigInt(feeBIn),
|
|
67
|
-
BigInt(lookback),
|
|
68
|
-
BigInt(tickSpacing),
|
|
69
|
-
BigInt(protocolFeeRatio),
|
|
70
|
-
BigInt(activeTick),
|
|
71
|
-
BigInt(tokenA.decimals),
|
|
72
|
-
BigInt(tokenB.decimals),
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
this.poolContract = new this.dexHelper.web3Provider.eth.Contract(
|
|
76
|
-
MaverickV2PoolABI as AbiItem[],
|
|
77
|
-
this.address,
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
this.poolLensContract = new this.dexHelper.web3Provider.eth.Contract(
|
|
81
|
-
MaverickV2PoolLensABI as AbiItem[],
|
|
82
|
-
this.poolLensAddress,
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* The function is called every time any of the subscribed
|
|
88
|
-
* addresses release log. The function accepts the current
|
|
89
|
-
* state, updates the state according to the log, and returns
|
|
90
|
-
* the updated state.
|
|
91
|
-
* @param state - Current state of event subscriber
|
|
92
|
-
* @param log - Log released by one of the subscribed addresses
|
|
93
|
-
* @returns Updates state of the event subscriber after the log
|
|
94
|
-
*/
|
|
95
|
-
protected processLog(
|
|
96
|
-
state: DeepReadonly<PoolState>,
|
|
97
|
-
log: Readonly<Log>,
|
|
98
|
-
blockHeader: Readonly<BlockHeader>,
|
|
99
|
-
): DeepReadonly<PoolState> | null {
|
|
100
|
-
try {
|
|
101
|
-
const event = this.logDecoder(log);
|
|
102
|
-
if (event.name in this.handlers) {
|
|
103
|
-
return this.handlers[event.name](event, state, log, blockHeader);
|
|
104
|
-
}
|
|
105
|
-
} catch (e) {
|
|
106
|
-
catchParseLogError(e, this.logger);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return null;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* The function generates state using on-chain calls. This
|
|
114
|
-
* function is called to regenerate state if the event based
|
|
115
|
-
* system fails to fetch events and the local state is no
|
|
116
|
-
* more correct.
|
|
117
|
-
* @param blockNumber - Blocknumber for which the state should
|
|
118
|
-
* should be generated
|
|
119
|
-
* @returns state of the event subscriber at blocknumber
|
|
120
|
-
*/
|
|
121
|
-
|
|
122
|
-
async generateState(blockNumber: number): Promise<DeepReadonly<PoolState>> {
|
|
123
|
-
try {
|
|
124
|
-
const poolContractState = await this.poolContract.methods
|
|
125
|
-
.getState()
|
|
126
|
-
.call({}, blockNumber);
|
|
127
|
-
|
|
128
|
-
const poolState: PoolState = {
|
|
129
|
-
activeTick: BigInt(poolContractState.activeTick),
|
|
130
|
-
binCounter: BigInt(poolContractState.binCounter),
|
|
131
|
-
reserveA: BigInt(poolContractState.reserveA),
|
|
132
|
-
reserveB: BigInt(poolContractState.reserveB),
|
|
133
|
-
lastTwaD8: BigInt(poolContractState.lastTwaD8),
|
|
134
|
-
lastLogPriceD8: BigInt(poolContractState.lastLogPriceD8),
|
|
135
|
-
lastTimestamp: BigInt(poolContractState.lastTimestamp),
|
|
136
|
-
bins: {},
|
|
137
|
-
ticks: {},
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
for (let i = 0n; i < poolState.binCounter / 5000n + 1n; i++) {
|
|
141
|
-
const poolLensState = await this.poolLensContract.methods[
|
|
142
|
-
'getFullPoolState'
|
|
143
|
-
](this.address, i * 5000n, (i + 1n) * 5000n).call({}, blockNumber);
|
|
144
|
-
|
|
145
|
-
poolLensState.binStateMapping.forEach((bin: any, i: number) => {
|
|
146
|
-
if (i == 0) return;
|
|
147
|
-
const tick = poolLensState.tickStateMapping[i];
|
|
148
|
-
poolState.bins[i.toString()] = {
|
|
149
|
-
mergeBinBalance: BigInt(bin.mergeBinBalance),
|
|
150
|
-
mergeId: BigInt(bin.mergeId),
|
|
151
|
-
totalSupply: BigInt(bin.totalSupply),
|
|
152
|
-
kind: BigInt(bin.kind),
|
|
153
|
-
tick: BigInt(bin.tick),
|
|
154
|
-
tickBalance: BigInt(bin.tickBalance),
|
|
155
|
-
};
|
|
156
|
-
poolState.ticks[bin.tick.toString()] = {
|
|
157
|
-
reserveA: BigInt(tick.reserveA),
|
|
158
|
-
reserveB: BigInt(tick.reserveB),
|
|
159
|
-
totalSupply: BigInt(tick.totalSupply),
|
|
160
|
-
binIdsByTick: {},
|
|
161
|
-
};
|
|
162
|
-
tick.binIdsByTick.forEach((id: any, i: number) => {
|
|
163
|
-
if (id != 0n) {
|
|
164
|
-
poolState.ticks[bin.tick.toString()].binIdsByTick[i.toString()] =
|
|
165
|
-
BigInt(id);
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
return poolState;
|
|
171
|
-
} catch {
|
|
172
|
-
return {
|
|
173
|
-
activeTick: BigInt(0),
|
|
174
|
-
binCounter: BigInt(0),
|
|
175
|
-
reserveA: BigInt(0),
|
|
176
|
-
reserveB: BigInt(0),
|
|
177
|
-
lastTwaD8: BigInt(0),
|
|
178
|
-
lastLogPriceD8: BigInt(0),
|
|
179
|
-
lastTimestamp: BigInt(0),
|
|
180
|
-
bins: {},
|
|
181
|
-
ticks: {},
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
handleRemoveLiquidityEvent(
|
|
187
|
-
event: any,
|
|
188
|
-
state: PoolState,
|
|
189
|
-
log: Log,
|
|
190
|
-
blockHeader: BlockHeader,
|
|
191
|
-
) {
|
|
192
|
-
const blockTimestamp = BigInt(blockHeader.timestamp);
|
|
193
|
-
this.poolMath.removeLiquidity(state, blockTimestamp, {
|
|
194
|
-
binIds: event.args.params.binIds.map((id: any) => BigInt(id)),
|
|
195
|
-
amounts: event.args.params.amounts.map((amount: any) => BigInt(amount)),
|
|
196
|
-
});
|
|
197
|
-
return state;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
handleAddLiquidityEvent(
|
|
201
|
-
event: any,
|
|
202
|
-
state: PoolState,
|
|
203
|
-
log: Log,
|
|
204
|
-
blockHeader: BlockHeader,
|
|
205
|
-
) {
|
|
206
|
-
const blockTimestamp = BigInt(blockHeader.timestamp);
|
|
207
|
-
this.poolMath.addLiquidity(state, blockTimestamp, {
|
|
208
|
-
ticks: event.args.params.ticks.map((amount: any) => BigInt(amount)),
|
|
209
|
-
amounts: event.args.params.amounts.map((amount: any) => BigInt(amount)),
|
|
210
|
-
kind: BigInt(event.args.params.kind),
|
|
211
|
-
});
|
|
212
|
-
return state;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
handleSwapEvent(
|
|
216
|
-
event: any,
|
|
217
|
-
state: PoolState,
|
|
218
|
-
log: Log,
|
|
219
|
-
blockHeader: BlockHeader,
|
|
220
|
-
) {
|
|
221
|
-
const blockTimestamp = BigInt(blockHeader.timestamp);
|
|
222
|
-
this.poolMath.swap(
|
|
223
|
-
state,
|
|
224
|
-
blockTimestamp,
|
|
225
|
-
BigInt(event.args.params.amount),
|
|
226
|
-
event.args.params.tokenAIn,
|
|
227
|
-
event.args.params.exactOutput,
|
|
228
|
-
event.args.params.tickLimit,
|
|
229
|
-
);
|
|
230
|
-
return state;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
swap(
|
|
234
|
-
amount: bigint,
|
|
235
|
-
from: Token,
|
|
236
|
-
to: Token,
|
|
237
|
-
side: boolean,
|
|
238
|
-
): [bigint, number] {
|
|
239
|
-
try {
|
|
240
|
-
const tempState = _.cloneDeep(this.state!);
|
|
241
|
-
|
|
242
|
-
const preActiveTick = tempState.activeTick;
|
|
243
|
-
|
|
244
|
-
const output = this.poolMath.estimateSwap(
|
|
245
|
-
tempState,
|
|
246
|
-
amount,
|
|
247
|
-
from.address.toLowerCase() == this.tokenA.address.toLowerCase(),
|
|
248
|
-
side,
|
|
249
|
-
from.address.toLowerCase() == this.tokenA.address.toLowerCase()
|
|
250
|
-
? tempState.activeTick + 20n
|
|
251
|
-
: tempState.activeTick - 20n,
|
|
252
|
-
);
|
|
253
|
-
|
|
254
|
-
if (output[0] == 0n && output[1] == 0n) {
|
|
255
|
-
this.logger.trace(
|
|
256
|
-
`Reached max swap iteration calculation for address=${this.address} amount=${amount}, from=${from.address}, to=${to.address}, side=${side}`,
|
|
257
|
-
);
|
|
258
|
-
return [0n, 0];
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
const postActiveTick = tempState.activeTick;
|
|
262
|
-
const tickDiff = Math.abs(Number(postActiveTick) - Number(preActiveTick));
|
|
263
|
-
|
|
264
|
-
return [
|
|
265
|
-
side ? output[0] : output[1],
|
|
266
|
-
// Tick calculation must be started from 1 to account at least one tick
|
|
267
|
-
tickDiff + 1,
|
|
268
|
-
];
|
|
269
|
-
} catch (e) {
|
|
270
|
-
this.logger.debug(
|
|
271
|
-
`Failed to calculate swap for address=${this.address} amount=${amount}, from=${from.address}, to=${to.address}, side=${side} math: ${e}`,
|
|
272
|
-
);
|
|
273
|
-
return [0n, 0];
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
}
|