@paraswap/dex-lib 3.9.6 → 3.9.7-fluid-dex.1
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/.idea/aws.xml +17 -0
- package/.idea/codeStyles/Project.xml +19 -0
- package/.idea/codeStyles/codeStyleConfig.xml +5 -0
- package/.idea/misc.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/paraswap-dex-lib.iml +9 -0
- package/.idea/prettier.xml +7 -0
- package/.idea/vcs.xml +6 -0
- package/build/abi/fluid-dex/dexFactory.abi.json +506 -0
- package/build/abi/fluid-dex/fluid-dex.abi.json +922 -0
- package/build/abi/fluid-dex/liquidityUserModule.abi.json +145 -0
- package/build/abi/fluid-dex/resolver.abi.json +999 -0
- package/build/abi/integral/factory.json +333 -0
- package/build/abi/integral/oracle.json +501 -0
- package/build/abi/{inception/inception-ineth-pool.json → integral/pool.json} +522 -543
- package/build/abi/{wombat/pool.json → integral/relayer.json} +720 -473
- package/build/dex/aave-v1/aave-v1.d.ts +1 -2
- package/build/dex/aave-v1/aave-v1.js +0 -23
- package/build/dex/aave-v1/aave-v1.js.map +1 -1
- package/build/dex/fluid-dex/config.d.ts +4 -0
- package/build/dex/fluid-dex/config.js +21 -0
- package/build/dex/fluid-dex/config.js.map +1 -0
- package/build/dex/fluid-dex/fluid-dex-generate-pool.d.ts +49 -0
- package/build/dex/fluid-dex/fluid-dex-generate-pool.js +104 -0
- package/build/dex/fluid-dex/fluid-dex-generate-pool.js.map +1 -0
- package/build/dex/fluid-dex/fluid-dex-pool.d.ts +47 -0
- package/build/dex/fluid-dex/fluid-dex-pool.js +119 -0
- package/build/dex/fluid-dex/fluid-dex-pool.js.map +1 -0
- package/build/dex/fluid-dex/fluid-dex.d.ts +128 -0
- package/build/dex/fluid-dex/fluid-dex.js +597 -0
- package/build/dex/fluid-dex/fluid-dex.js.map +1 -0
- package/build/dex/fluid-dex/types.d.ts +53 -0
- package/build/dex/{inception → fluid-dex}/types.js.map +1 -1
- package/build/dex/index.js +2 -0
- package/build/dex/index.js.map +1 -1
- package/build/dex/integral/config.d.ts +4 -0
- package/build/dex/integral/config.js +20 -0
- package/build/dex/integral/config.js.map +1 -0
- package/build/dex/integral/context.d.ts +40 -0
- package/build/dex/integral/context.js +158 -0
- package/build/dex/integral/context.js.map +1 -0
- package/build/dex/integral/integral-factory.d.ts +24 -0
- package/build/dex/integral/integral-factory.js +95 -0
- package/build/dex/integral/integral-factory.js.map +1 -0
- package/build/dex/integral/integral-pool.d.ts +50 -0
- package/build/dex/integral/integral-pool.js +149 -0
- package/build/dex/integral/integral-pool.js.map +1 -0
- package/build/dex/integral/integral-pricing.d.ts +18 -0
- package/build/dex/integral/integral-pricing.js +114 -0
- package/build/dex/integral/integral-pricing.js.map +1 -0
- package/build/dex/integral/integral-relayer.d.ts +42 -0
- package/build/dex/integral/integral-relayer.js +192 -0
- package/build/dex/integral/integral-relayer.js.map +1 -0
- package/build/dex/integral/integral-token.d.ts +27 -0
- package/build/dex/integral/integral-token.js +59 -0
- package/build/dex/integral/integral-token.js.map +1 -0
- package/build/dex/integral/integral.d.ts +41 -0
- package/build/dex/integral/integral.js +376 -0
- package/build/dex/integral/integral.js.map +1 -0
- package/build/dex/integral/types.d.ts +85 -0
- package/build/dex/integral/types.js +9 -0
- package/build/dex/integral/types.js.map +1 -0
- package/build/dex/integral/utils-e2e.d.ts +9 -0
- package/build/dex/integral/utils-e2e.js +131 -0
- package/build/dex/integral/utils-e2e.js.map +1 -0
- package/build/dex/integral/utils.d.ts +17 -0
- package/build/dex/integral/utils.js +94 -0
- package/build/dex/integral/utils.js.map +1 -0
- package/build/dex/jarvis.d.ts +1 -2
- package/build/dex/jarvis.js +0 -58
- package/build/dex/jarvis.js.map +1 -1
- package/build/dex/platypus/platypus.d.ts +1 -2
- package/build/dex/platypus/platypus.js +0 -45
- package/build/dex/platypus/platypus.js.map +1 -1
- package/build/dex/stable-pool.js +8 -1
- package/build/dex/stable-pool.js.map +1 -1
- package/build/dex/swaap-v1/swaap-v1.d.ts +1 -2
- package/build/dex/swaap-v1/swaap-v1.js +0 -14
- package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
- package/build/dex/trader-joe-v2.1/optimizer.d.ts +2 -0
- package/build/dex/trader-joe-v2.1/optimizer.js +47 -0
- package/build/dex/trader-joe-v2.1/optimizer.js.map +1 -0
- package/build/dex/zerox/index.d.ts +26 -4
- package/build/dex/zerox/index.js +42 -50
- package/build/dex/zerox/index.js.map +1 -1
- package/build/dex/zerox/types.d.ts +0 -29
- package/build/dex/zerox/types.js +1 -8
- package/build/dex/zerox/types.js.map +1 -1
- package/package.json +1 -1
- package/src/abi/fluid-dex/dexFactory.abi.json +506 -0
- package/src/abi/fluid-dex/fluid-dex.abi.json +922 -0
- package/src/abi/fluid-dex/liquidityUserModule.abi.json +145 -0
- package/src/abi/fluid-dex/resolver.abi.json +999 -0
- package/src/dex/fluid-dex/config.ts +21 -0
- package/src/dex/fluid-dex/fluid-dex-e2e.test.ts +138 -0
- package/src/dex/fluid-dex/fluid-dex-events.test.ts +294 -0
- package/src/dex/fluid-dex/fluid-dex-generate-pool.ts +143 -0
- package/src/dex/fluid-dex/fluid-dex-integration.test.ts +225 -0
- package/src/dex/fluid-dex/fluid-dex-pool.ts +178 -0
- package/src/dex/fluid-dex/fluid-dex.ts +878 -0
- package/src/dex/fluid-dex/types.ts +63 -0
- package/src/dex/index.ts +2 -0
- package/tests/constants-e2e.d.ts +22 -0
- package/tests/constants-e2e.js +1942 -0
- package/tests/constants-e2e.js.map +1 -0
- package/tests/constants-e2e.ts +2 -2
- package/tests/smart-tokens.d.ts +36 -0
- package/tests/smart-tokens.js +64 -0
- package/tests/smart-tokens.js.map +1 -0
- package/.vscode/launch.json +0 -53
- package/.vscode/settings.json +0 -2
- package/.vscode/tasks.json +0 -15
- package/build/abi/inception/inception-ratio-feed.json +0 -329
- package/build/abi/inception/inception-vault.json +0 -991
- package/build/abi/uniswap-v3/AlienBaseV3Router.abi.json +0 -52
- package/build/dex/aave-v2/tokens-avalanche.json +0 -44
- package/build/dex/aave-v2/tokens-mainnet.json +0 -188
- package/build/dex/aave-v2/tokens-polygon.json +0 -44
- package/build/dex/inception/config.d.ts +0 -4
- package/build/dex/inception/config.js +0 -109
- package/build/dex/inception/config.js.map +0 -1
- package/build/dex/inception/inception-event-pool.d.ts +0 -16
- package/build/dex/inception/inception-event-pool.js +0 -49
- package/build/dex/inception/inception-event-pool.js.map +0 -1
- package/build/dex/inception/inception.d.ts +0 -43
- package/build/dex/inception/inception.js +0 -180
- package/build/dex/inception/inception.js.map +0 -1
- package/build/dex/inception/tokens.d.ts +0 -9
- package/build/dex/inception/tokens.js +0 -28
- package/build/dex/inception/tokens.js.map +0 -1
- package/build/dex/inception/types.d.ts +0 -22
- package/build/dex/inception/utils.d.ts +0 -7
- package/build/dex/inception/utils.js +0 -58
- package/build/dex/inception/utils.js.map +0 -1
- package/build/dex/maker-psm/scripts/gem.abi.json +0 -136
- package/build/dex/maker-psm/scripts/validate-state.d.ts +0 -0
- package/build/dex/maker-psm/scripts/validate-state.js +0 -185
- package/build/dex/maker-psm/scripts/validate-state.js.map +0 -1
- package/build/dex/maker-psm/scripts/vat.abi.json +0 -243
- package/build/dex/solidly-v3/scripts/check-event-pool-event.d.ts +0 -1
- package/build/dex/solidly-v3/scripts/check-event-pool-event.js +0 -53
- package/build/dex/solidly-v3/scripts/check-event-pool-event.js.map +0 -1
- package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.d.ts +0 -17
- package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js +0 -64
- package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js.map +0 -1
- package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.d.ts +0 -17
- package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js +0 -79
- package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js.map +0 -1
- package/build/dex/uniswap-v3/uniswap-v3-pricing.d.ts +0 -4
- package/build/dex/uniswap-v3/uniswap-v3-pricing.js +0 -43
- package/build/dex/uniswap-v3/uniswap-v3-pricing.js.map +0 -1
- package/build/dex/uniswap-v3/uniswap-v3-worker.d.ts +0 -1
- package/build/dex/uniswap-v3/uniswap-v3-worker.js +0 -10
- package/build/dex/uniswap-v3/uniswap-v3-worker.js.map +0 -1
- /package/build/dex/{inception → fluid-dex}/types.js +0 -0
|
@@ -0,0 +1,225 @@
|
|
|
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 { FluidDex } from './fluid-dex';
|
|
10
|
+
import {
|
|
11
|
+
checkPoolPrices,
|
|
12
|
+
checkPoolsLiquidity,
|
|
13
|
+
checkConstantPoolPrices,
|
|
14
|
+
} from '../../../tests/utils';
|
|
15
|
+
import { Tokens } from '../../../tests/constants-e2e';
|
|
16
|
+
import ResolverABI from '../../abi/fluid-dex/resolver.abi.json';
|
|
17
|
+
|
|
18
|
+
/*
|
|
19
|
+
README
|
|
20
|
+
======
|
|
21
|
+
|
|
22
|
+
This test script adds tests for FluidDex general integration
|
|
23
|
+
with the DEX interface. The test cases below are example tests.
|
|
24
|
+
It is recommended to add tests which cover FluidDex specific
|
|
25
|
+
logic.
|
|
26
|
+
|
|
27
|
+
You can run this individual test script by running:
|
|
28
|
+
`npx jest src/dex/<dex-name>/<dex-name>-integration.test.ts`
|
|
29
|
+
|
|
30
|
+
(This comment should be removed from the final implementation)
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
function getReaderCalldata(
|
|
34
|
+
exchangeAddress: string,
|
|
35
|
+
readerIface: Interface,
|
|
36
|
+
amounts: bigint[],
|
|
37
|
+
funcName: string,
|
|
38
|
+
) {
|
|
39
|
+
return amounts.map(amount => ({
|
|
40
|
+
target: exchangeAddress,
|
|
41
|
+
callData: readerIface.encodeFunctionData(funcName, [
|
|
42
|
+
'0x0b1a513ee24972daef112bc777a5610d4325c9e7',
|
|
43
|
+
funcName == 'estimateSwapIn' ? true : false,
|
|
44
|
+
amount,
|
|
45
|
+
funcName == 'estimateSwapIn' ? 0 : 2n * amount,
|
|
46
|
+
]),
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function decodeReaderResult(
|
|
51
|
+
results: Result,
|
|
52
|
+
readerIface: Interface,
|
|
53
|
+
funcName: string,
|
|
54
|
+
) {
|
|
55
|
+
return results.map(result => {
|
|
56
|
+
return BigInt(result);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function checkOnChainPricing(
|
|
61
|
+
fluidDex: FluidDex,
|
|
62
|
+
funcName: string,
|
|
63
|
+
blockNumber: number,
|
|
64
|
+
prices: bigint[],
|
|
65
|
+
amounts: bigint[],
|
|
66
|
+
) {
|
|
67
|
+
const resolverAddress = '0xE8a07a32489BD9d5a00f01A55749Cf5cB854Fd13';
|
|
68
|
+
|
|
69
|
+
const readerIface = new Interface(ResolverABI);
|
|
70
|
+
|
|
71
|
+
const readerCallData = getReaderCalldata(
|
|
72
|
+
resolverAddress,
|
|
73
|
+
readerIface,
|
|
74
|
+
amounts.slice(1),
|
|
75
|
+
funcName,
|
|
76
|
+
);
|
|
77
|
+
const readerResult = (
|
|
78
|
+
await fluidDex.dexHelper.multiContract.methods
|
|
79
|
+
.aggregate(readerCallData)
|
|
80
|
+
.call({}, blockNumber)
|
|
81
|
+
).returnData;
|
|
82
|
+
|
|
83
|
+
const expectedPrices = [0n].concat(
|
|
84
|
+
decodeReaderResult(readerResult, readerIface, funcName),
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function testPricingOnNetwork(
|
|
89
|
+
fluidDex: FluidDex,
|
|
90
|
+
network: Network,
|
|
91
|
+
dexKey: string,
|
|
92
|
+
blockNumber: number,
|
|
93
|
+
srcTokenSymbol: string,
|
|
94
|
+
destTokenSymbol: string,
|
|
95
|
+
side: SwapSide,
|
|
96
|
+
amounts: bigint[],
|
|
97
|
+
funcNameToCheck: string,
|
|
98
|
+
) {
|
|
99
|
+
const networkTokens = Tokens[network];
|
|
100
|
+
|
|
101
|
+
const pools = await fluidDex.getPoolIdentifiers(
|
|
102
|
+
networkTokens[srcTokenSymbol],
|
|
103
|
+
networkTokens[destTokenSymbol],
|
|
104
|
+
side,
|
|
105
|
+
blockNumber,
|
|
106
|
+
);
|
|
107
|
+
console.log(
|
|
108
|
+
`${srcTokenSymbol} <> ${destTokenSymbol} Pool Identifiers: `,
|
|
109
|
+
pools,
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
expect(pools.length).toBeGreaterThan(0);
|
|
113
|
+
|
|
114
|
+
const poolPrices = await fluidDex.getPricesVolume(
|
|
115
|
+
networkTokens[srcTokenSymbol],
|
|
116
|
+
networkTokens[destTokenSymbol],
|
|
117
|
+
amounts,
|
|
118
|
+
side,
|
|
119
|
+
blockNumber,
|
|
120
|
+
pools,
|
|
121
|
+
);
|
|
122
|
+
console.log(
|
|
123
|
+
`${srcTokenSymbol} <> ${destTokenSymbol} Pool Prices: `,
|
|
124
|
+
poolPrices,
|
|
125
|
+
);
|
|
126
|
+
console.log(
|
|
127
|
+
'logged params : ',
|
|
128
|
+
networkTokens[srcTokenSymbol],
|
|
129
|
+
networkTokens[destTokenSymbol],
|
|
130
|
+
amounts,
|
|
131
|
+
side,
|
|
132
|
+
blockNumber,
|
|
133
|
+
pools,
|
|
134
|
+
);
|
|
135
|
+
expect(poolPrices).not.toBeNull();
|
|
136
|
+
if (fluidDex.hasConstantPriceLargeAmounts) {
|
|
137
|
+
checkConstantPoolPrices(poolPrices!, amounts, dexKey);
|
|
138
|
+
} else {
|
|
139
|
+
checkPoolPrices(poolPrices!, amounts, side, dexKey);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Check if onchain pricing equals to calculated ones
|
|
143
|
+
await checkOnChainPricing(
|
|
144
|
+
fluidDex,
|
|
145
|
+
funcNameToCheck,
|
|
146
|
+
blockNumber,
|
|
147
|
+
poolPrices![0].prices,
|
|
148
|
+
amounts,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
describe('FluidDex', function () {
|
|
153
|
+
const dexKey = 'FluidDex';
|
|
154
|
+
let blockNumber: number;
|
|
155
|
+
let fluidDex: FluidDex;
|
|
156
|
+
|
|
157
|
+
describe('Mainnet', () => {
|
|
158
|
+
const network = Network.MAINNET;
|
|
159
|
+
const dexHelper = new DummyDexHelper(network);
|
|
160
|
+
|
|
161
|
+
const tokens = Tokens[network];
|
|
162
|
+
|
|
163
|
+
// Don't forget to update relevant tokens in constant-e2e.ts
|
|
164
|
+
const srcTokenSymbol = 'wstETH';
|
|
165
|
+
const destTokenSymbol = 'ETH';
|
|
166
|
+
|
|
167
|
+
const amountsForSell = [
|
|
168
|
+
0n,
|
|
169
|
+
1n * BI_POWS[tokens[srcTokenSymbol].decimals - 10],
|
|
170
|
+
2n * BI_POWS[tokens[srcTokenSymbol].decimals - 10],
|
|
171
|
+
3n * BI_POWS[tokens[srcTokenSymbol].decimals - 10],
|
|
172
|
+
4n * BI_POWS[tokens[srcTokenSymbol].decimals - 10],
|
|
173
|
+
5n * BI_POWS[tokens[srcTokenSymbol].decimals - 10],
|
|
174
|
+
6n * BI_POWS[tokens[srcTokenSymbol].decimals - 10],
|
|
175
|
+
7n * BI_POWS[tokens[srcTokenSymbol].decimals - 10],
|
|
176
|
+
8n * BI_POWS[tokens[srcTokenSymbol].decimals - 10],
|
|
177
|
+
9n * BI_POWS[tokens[srcTokenSymbol].decimals - 10],
|
|
178
|
+
10n * BI_POWS[tokens[srcTokenSymbol].decimals - 10],
|
|
179
|
+
];
|
|
180
|
+
|
|
181
|
+
beforeAll(async () => {
|
|
182
|
+
blockNumber = await dexHelper.provider.getBlockNumber();
|
|
183
|
+
fluidDex = new FluidDex(network, dexKey, dexHelper);
|
|
184
|
+
if (fluidDex.initializePricing) {
|
|
185
|
+
await fluidDex.initializePricing(blockNumber);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('getPoolIdentifiers and getPricesVolume SELL', async function () {
|
|
190
|
+
await testPricingOnNetwork(
|
|
191
|
+
fluidDex,
|
|
192
|
+
network,
|
|
193
|
+
dexKey,
|
|
194
|
+
blockNumber,
|
|
195
|
+
srcTokenSymbol,
|
|
196
|
+
destTokenSymbol,
|
|
197
|
+
SwapSide.SELL,
|
|
198
|
+
amountsForSell,
|
|
199
|
+
'estimateSwapIn',
|
|
200
|
+
);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('getTopPoolsForToken', async function () {
|
|
204
|
+
// We have to check without calling initializePricing, because
|
|
205
|
+
// pool-tracker is not calling that function
|
|
206
|
+
const newFluidDex = new FluidDex(network, dexKey, dexHelper);
|
|
207
|
+
await newFluidDex.initializePricing(blockNumber);
|
|
208
|
+
if (newFluidDex.updatePoolState) {
|
|
209
|
+
await newFluidDex.updatePoolState();
|
|
210
|
+
}
|
|
211
|
+
const poolLiquidity = await newFluidDex.getTopPoolsForToken(
|
|
212
|
+
tokens[srcTokenSymbol].address,
|
|
213
|
+
1,
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
if (!newFluidDex.hasConstantPriceLargeAmounts) {
|
|
217
|
+
checkPoolsLiquidity(
|
|
218
|
+
poolLiquidity,
|
|
219
|
+
Tokens[network][srcTokenSymbol].address,
|
|
220
|
+
dexKey,
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
});
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { Interface } from '@ethersproject/abi';
|
|
2
|
+
import { DeepReadonly } from 'ts-essentials';
|
|
3
|
+
import { Log, Logger } from '../../types';
|
|
4
|
+
import { catchParseLogError } from '../../utils';
|
|
5
|
+
import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
|
|
6
|
+
import { IDexHelper } from '../../dex-helper/idex-helper';
|
|
7
|
+
import ResolverABI from '../../abi/fluid-dex/resolver.abi.json';
|
|
8
|
+
import LiquidityABI from '../../abi/fluid-dex/liquidityUserModule.abi.json';
|
|
9
|
+
import {
|
|
10
|
+
CommonAddresses,
|
|
11
|
+
FluidDexPoolState,
|
|
12
|
+
CollateralReserves,
|
|
13
|
+
DebtReserves,
|
|
14
|
+
} from './types';
|
|
15
|
+
import { Address } from '../../types';
|
|
16
|
+
import { Contract } from 'ethers';
|
|
17
|
+
|
|
18
|
+
export class FluidDexEventPool extends StatefulEventSubscriber<FluidDexPoolState> {
|
|
19
|
+
handlers: {
|
|
20
|
+
[event: string]: (
|
|
21
|
+
event: any,
|
|
22
|
+
state: DeepReadonly<FluidDexPoolState>,
|
|
23
|
+
log: Readonly<Log>,
|
|
24
|
+
) => Promise<DeepReadonly<FluidDexPoolState> | null>;
|
|
25
|
+
} = {};
|
|
26
|
+
|
|
27
|
+
logDecoder: (log: Log) => any;
|
|
28
|
+
|
|
29
|
+
addressesSubscribed: Address[];
|
|
30
|
+
protected liquidityIface = new Interface(LiquidityABI);
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
readonly parentName: string,
|
|
34
|
+
readonly pool: Address,
|
|
35
|
+
readonly commonAddresses: CommonAddresses,
|
|
36
|
+
protected network: number,
|
|
37
|
+
readonly dexHelper: IDexHelper,
|
|
38
|
+
logger: Logger,
|
|
39
|
+
) {
|
|
40
|
+
super(parentName, 'FluidDex_' + pool, dexHelper, logger);
|
|
41
|
+
|
|
42
|
+
this.logDecoder = (log: Log) => this.liquidityIface.parseLog(log);
|
|
43
|
+
this.addressesSubscribed = [commonAddresses.liquidityProxy];
|
|
44
|
+
|
|
45
|
+
// Add handlers
|
|
46
|
+
this.handlers['LogOperate'] = this.handleOperate.bind(this);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Handle a trade rate change on the pool.
|
|
51
|
+
*/
|
|
52
|
+
async handleOperate(
|
|
53
|
+
event: any,
|
|
54
|
+
state: DeepReadonly<FluidDexPoolState>,
|
|
55
|
+
log: Readonly<Log>,
|
|
56
|
+
): Promise<DeepReadonly<FluidDexPoolState> | null> {
|
|
57
|
+
if (!(event.args.user in [this.pool])) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
const resolverContract = new Contract(
|
|
61
|
+
this.commonAddresses.resolver,
|
|
62
|
+
ResolverABI,
|
|
63
|
+
this.dexHelper.provider,
|
|
64
|
+
);
|
|
65
|
+
const rawResult = await resolverContract.callStatic.getPoolReservesAdjusted(
|
|
66
|
+
this.pool,
|
|
67
|
+
{
|
|
68
|
+
blockTag: this.dexHelper.provider,
|
|
69
|
+
},
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const generatedState = this.convertToFluidDexPoolState(rawResult);
|
|
73
|
+
|
|
74
|
+
this.setState(
|
|
75
|
+
generatedState,
|
|
76
|
+
await this.dexHelper.provider.getBlockNumber(),
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
return generatedState;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The function is called every time any of the subscribed
|
|
84
|
+
* addresses release log. The function accepts the current
|
|
85
|
+
* state, updates the state according to the log, and returns
|
|
86
|
+
* the updated state.
|
|
87
|
+
* @param state - Current state of event subscriber
|
|
88
|
+
* @param log - Log released by one of the subscribed addresses
|
|
89
|
+
* @returns Updates state of the event subscriber after the log
|
|
90
|
+
*/
|
|
91
|
+
async processLog(
|
|
92
|
+
state: DeepReadonly<FluidDexPoolState>,
|
|
93
|
+
log: Readonly<Log>,
|
|
94
|
+
): Promise<DeepReadonly<FluidDexPoolState> | null> {
|
|
95
|
+
try {
|
|
96
|
+
const event = this.logDecoder(log);
|
|
97
|
+
if (event.name in this.handlers) {
|
|
98
|
+
return await this.handlers[event.name](event, state, log);
|
|
99
|
+
}
|
|
100
|
+
} catch (e) {
|
|
101
|
+
catchParseLogError(e, this.logger);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async getStateOrGenerate(
|
|
108
|
+
blockNumber: number,
|
|
109
|
+
readonly: boolean = false,
|
|
110
|
+
): Promise<FluidDexPoolState> {
|
|
111
|
+
let state = this.getState(blockNumber);
|
|
112
|
+
if (!state) {
|
|
113
|
+
state = await this.generateState(blockNumber);
|
|
114
|
+
if (!readonly) this.setState(state, blockNumber);
|
|
115
|
+
}
|
|
116
|
+
return state;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The function generates state using on-chain calls. This
|
|
121
|
+
* function is called to regenerate state if the event based
|
|
122
|
+
* system fails to fetch events and the local state is no
|
|
123
|
+
* more correct.
|
|
124
|
+
* @param blockNumber - Blocknumber for which the state should
|
|
125
|
+
* should be generated
|
|
126
|
+
* @returns state of the event subscriber at blocknumber
|
|
127
|
+
*/
|
|
128
|
+
async generateState(
|
|
129
|
+
blockNumber: number,
|
|
130
|
+
): Promise<DeepReadonly<FluidDexPoolState>> {
|
|
131
|
+
const resolverContract = new Contract(
|
|
132
|
+
this.commonAddresses.resolver,
|
|
133
|
+
ResolverABI,
|
|
134
|
+
this.dexHelper.provider,
|
|
135
|
+
);
|
|
136
|
+
const rawResult = await resolverContract.callStatic.getPoolReservesAdjusted(
|
|
137
|
+
this.pool,
|
|
138
|
+
{
|
|
139
|
+
blockTag: blockNumber,
|
|
140
|
+
},
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const convertedResult = this.convertToFluidDexPoolState(rawResult);
|
|
144
|
+
|
|
145
|
+
return convertedResult;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private convertToFluidDexPoolState(input: any[]): FluidDexPoolState {
|
|
149
|
+
// Ignore the first three addresses
|
|
150
|
+
const [, , , feeHex, collateralReservesHex, debtReservesHex] = input;
|
|
151
|
+
// Convert fee from hex to number
|
|
152
|
+
const fee = Number(feeHex.toString());
|
|
153
|
+
|
|
154
|
+
// Convert collateral reserves
|
|
155
|
+
const collateralReserves: CollateralReserves = {
|
|
156
|
+
token0RealReserves: BigInt(collateralReservesHex[0].toString()),
|
|
157
|
+
token1RealReserves: BigInt(collateralReservesHex[1].toString()),
|
|
158
|
+
token0ImaginaryReserves: BigInt(collateralReservesHex[2].toString()),
|
|
159
|
+
token1ImaginaryReserves: BigInt(collateralReservesHex[3].toString()),
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
// Convert debt reserves
|
|
163
|
+
const debtReserves: DebtReserves = {
|
|
164
|
+
token0Debt: BigInt(debtReservesHex[0].toString()),
|
|
165
|
+
token1Debt: BigInt(debtReservesHex[1].toString()),
|
|
166
|
+
token0RealReserves: BigInt(debtReservesHex[2].toString()),
|
|
167
|
+
token1RealReserves: BigInt(debtReservesHex[3].toString()),
|
|
168
|
+
token0ImaginaryReserves: BigInt(debtReservesHex[4].toString()),
|
|
169
|
+
token1ImaginaryReserves: BigInt(debtReservesHex[5].toString()),
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
collateralReserves,
|
|
174
|
+
debtReserves,
|
|
175
|
+
fee,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
}
|