@paraswap/dex-lib 2.14.0 → 2.14.2
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/codeStyles/codeStyleConfig.xml +5 -0
- package/.idea/misc.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/vcs.xml +6 -0
- package/.vscode/launch.json +21 -0
- package/build/config.js +23 -5
- package/build/config.js.map +1 -1
- package/build/dex/aave-v3/tokens/arbitrum.json +50 -0
- package/build/dex/aave-v3/tokens/avalanche.json +50 -0
- package/build/dex/aave-v3/tokens/fantom.json +62 -0
- package/build/dex/aave-v3/tokens/mainnet.json +44 -0
- package/build/dex/aave-v3/tokens/optimism.json +50 -0
- package/build/dex/aave-v3/tokens/polygon.json +98 -0
- package/build/dex/balancer-v2/VirtualBoostedPool.d.ts +79 -0
- package/build/dex/balancer-v2/VirtualBoostedPool.js +382 -0
- package/build/dex/balancer-v2/VirtualBoostedPool.js.map +1 -0
- package/build/dex/generic-rfq/e2e-test-config.d.ts +9 -0
- package/build/dex/generic-rfq/e2e-test-config.js +33 -0
- package/build/dex/generic-rfq/e2e-test-config.js.map +1 -0
- package/build/dex/generic-rfq/rate-fetcher.js +2 -0
- package/build/dex/generic-rfq/rate-fetcher.js.map +1 -1
- package/build/dex/generic-rfq/validators.d.ts +1 -1
- package/build/dex/generic-rfq/validators.js +36 -26
- package/build/dex/generic-rfq/validators.js.map +1 -1
- package/build/dex/index.js +2 -2
- package/build/dex/index.js.map +1 -1
- package/build/dex/simple-exchange.d.ts +1 -0
- package/build/dex/simple-exchange.js +2 -0
- package/build/dex/simple-exchange.js.map +1 -1
- package/build/dex/test-aave-v3/config.d.ts +4 -0
- package/build/dex/test-aave-v3/config.js +15 -0
- package/build/dex/test-aave-v3/config.js.map +1 -0
- package/build/dex/test-aave-v3/test-aave-v3-pool.d.ts +39 -0
- package/build/dex/test-aave-v3/test-aave-v3-pool.js +63 -0
- package/build/dex/test-aave-v3/test-aave-v3-pool.js.map +1 -0
- package/build/dex/test-aave-v3/test-aave-v3.d.ts +37 -0
- package/build/dex/test-aave-v3/test-aave-v3.js +126 -0
- package/build/dex/test-aave-v3/test-aave-v3.js.map +1 -0
- package/build/dex/test-aave-v3/types.d.ts +6 -0
- package/build/dex/test-aave-v3/types.js +3 -0
- package/build/dex/test-aave-v3/types.js.map +1 -0
- package/build/lib/decoders.d.ts +2 -1
- package/build/lib/decoders.js +8 -1
- package/build/lib/decoders.js.map +1 -1
- package/build/lib/fetcher/fetcher.js +3 -0
- package/build/lib/fetcher/fetcher.js.map +1 -1
- package/build/lib/multi-wrapper.d.ts +2 -1
- package/build/lib/multi-wrapper.js.map +1 -1
- package/build/lib/tokens/balancer-fetcher.js +4 -5
- package/build/lib/tokens/balancer-fetcher.js.map +1 -1
- package/build/lib/tokens/utils.d.ts +2 -2
- package/build/lib/tokens/utils.js +1 -1
- package/build/lib/tokens/utils.js.map +1 -1
- package/build/types.d.ts +3 -0
- package/dex-template/__DexName__-e2e.test.ts.template +1 -0
- package/dex-template/__DexName__-events.test.ts.template +1 -0
- package/dex-template/__DexName__-integration.test.ts.template +1 -0
- package/dex-template/__DexName__-pool.ts.template +8 -5
- package/package.json +1 -1
- package/scripts/generate-bytecode.ts +2 -0
- package/src/abi/woo-fi-v2/IntegrationHelper.abi.json +116 -0
- package/src/abi/{woo-fi/Wooracle.abi.json → woo-fi-v2/WooOracleV2.abi.json} +148 -50
- package/src/abi/{woo-fi/WooPP.abi.json → woo-fi-v2/WooPPV2.abi.json} +201 -169
- package/src/config.ts +27 -5
- package/src/dex/balancer-v2/balancer-v2-e2e.test.ts +0 -1
- package/src/dex/generic-rfq/e2e-test-config.ts +37 -0
- package/src/dex/generic-rfq/generic-rfq-e2e.test.ts +63 -258
- package/src/dex/generic-rfq/generic-rfq-local-e2e.test.ts +121 -0
- package/src/dex/generic-rfq/rate-fetcher.ts +2 -0
- package/src/dex/generic-rfq/validators.test.ts +6 -32
- package/src/dex/generic-rfq/validators.ts +57 -32
- package/src/dex/index.ts +2 -2
- package/src/dex/simple-exchange.ts +3 -0
- package/src/dex/woo-fi-v2/config.ts +87 -0
- package/src/dex/woo-fi-v2/constants.ts +22 -0
- package/src/dex/woo-fi-v2/decoders.ts +45 -0
- package/src/dex/woo-fi-v2/types.ts +63 -0
- package/src/dex/woo-fi-v2/utils.ts +19 -0
- package/src/dex/woo-fi-v2/woo-fi-v2-e2e.test.ts +216 -0
- package/src/dex/woo-fi-v2/woo-fi-v2-integration.test.ts +495 -0
- package/src/dex/woo-fi-v2/woo-fi-v2-math.ts +195 -0
- package/src/dex/woo-fi-v2/woo-fi-v2-pool.ts +112 -0
- package/src/dex/woo-fi-v2/woo-fi-v2.ts +487 -0
- package/src/lib/decoders.ts +12 -1
- package/src/lib/fetcher/fetcher.ts +6 -0
- package/src/lib/log-messages-suppressor.ts +173 -0
- package/src/lib/multi-wrapper.ts +2 -1
- package/src/lib/stateful-rpc-poller/state-polling-manager.ts +320 -0
- package/src/lib/stateful-rpc-poller/stateful-rpc-poller.ts +604 -0
- package/src/lib/stateful-rpc-poller/types.ts +66 -0
- package/src/lib/stateful-rpc-poller/utils.ts +23 -0
- package/src/lib/tokens/balancer-fetcher.ts +5 -6
- package/src/lib/tokens/utils.ts +5 -3
- package/src/types.ts +4 -0
- package/tests/constants-e2e.ts +10 -3
- package/tests/tenderly-simulation.ts +1 -1
- package/tests/utils-e2e.ts +61 -48
- package/tests/utils.ts +2 -0
- package/src/abi/woo-fi/WooFeeManager.abi.json +0 -318
- package/src/dex/woo-fi/config.ts +0 -141
- package/src/dex/woo-fi/constants.ts +0 -21
- package/src/dex/woo-fi/types.ts +0 -55
- package/src/dex/woo-fi/woo-fi-decimal-math.ts +0 -45
- package/src/dex/woo-fi/woo-fi-e2e.test.ts +0 -183
- package/src/dex/woo-fi/woo-fi-integration.test.ts +0 -416
- package/src/dex/woo-fi/woo-fi-math.ts +0 -527
- package/src/dex/woo-fi/woo-fi.ts +0 -693
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Token,
|
|
3
|
+
Address,
|
|
4
|
+
ExchangePrices,
|
|
5
|
+
PoolPrices,
|
|
6
|
+
AdapterExchangeParam,
|
|
7
|
+
SimpleExchangeParam,
|
|
8
|
+
PoolLiquidity,
|
|
9
|
+
Logger,
|
|
10
|
+
} from '../../types';
|
|
11
|
+
import { SwapSide, Network, NULL_ADDRESS } from '../../constants';
|
|
12
|
+
import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
|
|
13
|
+
import { getBigIntPow, getDexKeysWithNetwork } from '../../utils';
|
|
14
|
+
import { IDex } from '../../dex/idex';
|
|
15
|
+
import { addressArrayDecode, uint8ToNumber } from '../../lib/decoders';
|
|
16
|
+
import { IDexHelper } from '../../dex-helper/idex-helper';
|
|
17
|
+
import { WooFiV2Data, DexParams } from './types';
|
|
18
|
+
import { SimpleExchange } from '../simple-exchange';
|
|
19
|
+
import { WooFiV2Config, Adapters } from './config';
|
|
20
|
+
import { WooFiV2PollingPool } from './woo-fi-v2-pool';
|
|
21
|
+
import { WooFiV2Math } from './woo-fi-v2-math';
|
|
22
|
+
import { MIN_CONVERSION_RATE, WOO_FI_V2_GAS_COST } from './constants';
|
|
23
|
+
import { assert } from 'ts-essentials';
|
|
24
|
+
import { ethers } from 'ethers';
|
|
25
|
+
import { addressDecode } from '../../lib/decoders';
|
|
26
|
+
import { ifaces } from './utils';
|
|
27
|
+
import { StatePollingManager } from '../../lib/stateful-rpc-poller/state-polling-manager';
|
|
28
|
+
|
|
29
|
+
export class WooFiV2 extends SimpleExchange implements IDex<WooFiV2Data> {
|
|
30
|
+
readonly math: WooFiV2Math;
|
|
31
|
+
|
|
32
|
+
protected pollingManager: StatePollingManager;
|
|
33
|
+
|
|
34
|
+
protected pollingPool?: WooFiV2PollingPool;
|
|
35
|
+
|
|
36
|
+
readonly hasConstantPriceLargeAmounts = false;
|
|
37
|
+
readonly needWrapNative = true;
|
|
38
|
+
|
|
39
|
+
readonly isFeeOnTransferSupported = false;
|
|
40
|
+
|
|
41
|
+
readonly isStatePollingDex = true;
|
|
42
|
+
|
|
43
|
+
readonly quoteTokenAddress: Address;
|
|
44
|
+
|
|
45
|
+
protected _baseTokens?: Token[];
|
|
46
|
+
|
|
47
|
+
// Since exchange anyways takes fee, we provide our fee wallet
|
|
48
|
+
// to take part of that fee and move to DAO
|
|
49
|
+
protected rebateTo?: Address;
|
|
50
|
+
|
|
51
|
+
tokenByAddress?: Record<Address, Token>;
|
|
52
|
+
|
|
53
|
+
vaultUSDBalance: number = 0;
|
|
54
|
+
|
|
55
|
+
public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
|
|
56
|
+
getDexKeysWithNetwork(WooFiV2Config);
|
|
57
|
+
|
|
58
|
+
logger: Logger;
|
|
59
|
+
|
|
60
|
+
constructor(
|
|
61
|
+
protected network: Network,
|
|
62
|
+
dexKey: string,
|
|
63
|
+
readonly dexHelper: IDexHelper,
|
|
64
|
+
protected adapters = Adapters[network] || {},
|
|
65
|
+
readonly config = WooFiV2Config[dexKey][network],
|
|
66
|
+
) {
|
|
67
|
+
super(dexHelper, dexKey);
|
|
68
|
+
const loggerName = `${dexKey}-${network}`;
|
|
69
|
+
this.logger = dexHelper.getLogger(loggerName);
|
|
70
|
+
|
|
71
|
+
// Normalise once all config addresses and use across all scenarios
|
|
72
|
+
this.config = this._toLowerForAllConfigAddresses();
|
|
73
|
+
|
|
74
|
+
this.quoteTokenAddress = this.config.quoteToken.address;
|
|
75
|
+
|
|
76
|
+
// Do not do it singleton, because different networks will have different
|
|
77
|
+
// states at the same time
|
|
78
|
+
this.math = new WooFiV2Math(
|
|
79
|
+
dexHelper.getLogger(`${loggerName}_math`),
|
|
80
|
+
this.quoteTokenAddress,
|
|
81
|
+
);
|
|
82
|
+
this.pollingManager = StatePollingManager.getInstance(dexHelper);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private _toLowerForAllConfigAddresses() {
|
|
86
|
+
// If new config property will be added, the TS will throw compile error
|
|
87
|
+
const newConfig: DexParams = {
|
|
88
|
+
wooPPV2Address: this.config.wooPPV2Address.toLowerCase(),
|
|
89
|
+
wooOracleV2Address: this.config.wooOracleV2Address.toLowerCase(),
|
|
90
|
+
integrationHelperAddress:
|
|
91
|
+
this.config.integrationHelperAddress.toLowerCase(),
|
|
92
|
+
quoteToken: {
|
|
93
|
+
...this.config.quoteToken,
|
|
94
|
+
address: this.config.quoteToken.address.toLowerCase(),
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
return newConfig;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
get baseTokens(): Token[] {
|
|
101
|
+
if (!this._baseTokens) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
`Base tokens are not set for ${this.dexKey}-${this.network}. Not properly initialized`,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
return this._baseTokens;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async initializePricing(blockNumber: number) {
|
|
110
|
+
await this.initializeTokensAndAddresses();
|
|
111
|
+
this._initializePollingPool();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
getAdapters(side: SwapSide): { name: string; index: number }[] | null {
|
|
115
|
+
return this.adapters[side] ? this.adapters[side] : null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async getPoolIdentifiers(
|
|
119
|
+
srcToken: Token,
|
|
120
|
+
destToken: Token,
|
|
121
|
+
side: SwapSide,
|
|
122
|
+
blockNumber: number,
|
|
123
|
+
): Promise<string[]> {
|
|
124
|
+
if (side === SwapSide.BUY) return [];
|
|
125
|
+
|
|
126
|
+
if (this.tokenByAddress === undefined) {
|
|
127
|
+
this.logger.error(`tokenByAddress is undefined in getPoolIdentifiers`);
|
|
128
|
+
return [];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const _srcToken = this.dexHelper.config.wrapETH(srcToken);
|
|
132
|
+
const _destToken = this.dexHelper.config.wrapETH(destToken);
|
|
133
|
+
|
|
134
|
+
const _srcAddress = _srcToken.address.toLowerCase();
|
|
135
|
+
const _destAddress = _destToken.address.toLowerCase();
|
|
136
|
+
|
|
137
|
+
if (_srcAddress === _destAddress) return [];
|
|
138
|
+
|
|
139
|
+
if (
|
|
140
|
+
!this.tokenByAddress[_srcAddress] ||
|
|
141
|
+
!this.tokenByAddress[_destAddress]
|
|
142
|
+
) {
|
|
143
|
+
return [];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return [this.getIdentifier()];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
getIdentifier() {
|
|
150
|
+
return `${this.dexKey}_wooppv2`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async getPricesVolume(
|
|
154
|
+
srcToken: Token,
|
|
155
|
+
destToken: Token,
|
|
156
|
+
amounts: bigint[],
|
|
157
|
+
side: SwapSide,
|
|
158
|
+
blockNumber: number,
|
|
159
|
+
limitPools?: string[],
|
|
160
|
+
): Promise<null | ExchangePrices<WooFiV2Data>> {
|
|
161
|
+
if (side === SwapSide.BUY) return null;
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
if (this.tokenByAddress === undefined) {
|
|
165
|
+
this.logger.error(`tokenByAddress is undefined in getPricesVolume`);
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const _srcToken = this.dexHelper.config.wrapETH(srcToken);
|
|
170
|
+
const _destToken = this.dexHelper.config.wrapETH(destToken);
|
|
171
|
+
|
|
172
|
+
const _srcAddress = _srcToken.address.toLowerCase();
|
|
173
|
+
const _destAddress = _destToken.address.toLowerCase();
|
|
174
|
+
|
|
175
|
+
if (_srcAddress === _destAddress) return null;
|
|
176
|
+
|
|
177
|
+
if (
|
|
178
|
+
!this.tokenByAddress[_srcAddress] ||
|
|
179
|
+
!this.tokenByAddress[_destAddress]
|
|
180
|
+
)
|
|
181
|
+
return null;
|
|
182
|
+
|
|
183
|
+
const expectedIdentifier = this.getIdentifier();
|
|
184
|
+
|
|
185
|
+
if (
|
|
186
|
+
limitPools !== undefined &&
|
|
187
|
+
!limitPools.some(p => p === expectedIdentifier)
|
|
188
|
+
)
|
|
189
|
+
return null;
|
|
190
|
+
|
|
191
|
+
const unitVolume = getBigIntPow(_srcToken.decimals);
|
|
192
|
+
|
|
193
|
+
const _amounts = [unitVolume, ...amounts.slice(1)];
|
|
194
|
+
|
|
195
|
+
assert(
|
|
196
|
+
this.pollingPool,
|
|
197
|
+
'pollingPool is not initialized in getPricesVolume',
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
const state = await this.pollingPool.getState(blockNumber);
|
|
201
|
+
|
|
202
|
+
if (state === null) {
|
|
203
|
+
this.logger.error(`State is null in getPricesVolume`);
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (state.value.isPaused) {
|
|
208
|
+
this.logger.warn(
|
|
209
|
+
`${this.dexKey} is paused on ${this.network} in getPricesVolume`,
|
|
210
|
+
);
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
this.math.state = state.value;
|
|
215
|
+
|
|
216
|
+
const _prices = this.math.query(_srcAddress, _destAddress, _amounts);
|
|
217
|
+
|
|
218
|
+
if (!_prices) return null;
|
|
219
|
+
|
|
220
|
+
const unit = _prices[0];
|
|
221
|
+
|
|
222
|
+
let gasCost = WOO_FI_V2_GAS_COST.baseToBase;
|
|
223
|
+
if (_srcAddress === this.config.quoteToken.address) {
|
|
224
|
+
gasCost = WOO_FI_V2_GAS_COST.sellQuote;
|
|
225
|
+
} else if (_destAddress === this.config.quoteToken.address) {
|
|
226
|
+
gasCost = WOO_FI_V2_GAS_COST.sellBase;
|
|
227
|
+
} else {
|
|
228
|
+
this.logger.error(
|
|
229
|
+
`Can not estimate gas cost in getPricesVolume. Check the logic: ` +
|
|
230
|
+
`${_srcAddress}, ${_destAddress}, ${this.config.quoteToken.address}`,
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return [
|
|
235
|
+
{
|
|
236
|
+
unit,
|
|
237
|
+
prices: [0n, ..._prices.slice(1)],
|
|
238
|
+
data: {},
|
|
239
|
+
poolIdentifier: expectedIdentifier,
|
|
240
|
+
exchange: this.dexKey,
|
|
241
|
+
gasCost,
|
|
242
|
+
poolAddresses: [this.config.wooPPV2Address],
|
|
243
|
+
},
|
|
244
|
+
];
|
|
245
|
+
} catch (e) {
|
|
246
|
+
this.logger.error(
|
|
247
|
+
`Error_getPricesVolume ${srcToken.symbol || srcToken.address}, ${
|
|
248
|
+
destToken.symbol || destToken.address
|
|
249
|
+
}, ${side}:`,
|
|
250
|
+
e,
|
|
251
|
+
);
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
getCalldataGasCost(poolPrices: PoolPrices<WooFiV2Data>): number | number[] {
|
|
257
|
+
return CALLDATA_GAS_COST.DEX_OVERHEAD + CALLDATA_GAS_COST.ADDRESS;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
getAdapterParam(
|
|
261
|
+
srcToken: string,
|
|
262
|
+
destToken: string,
|
|
263
|
+
srcAmount: string,
|
|
264
|
+
destAmount: string,
|
|
265
|
+
data: WooFiV2Data,
|
|
266
|
+
side: SwapSide,
|
|
267
|
+
): AdapterExchangeParam {
|
|
268
|
+
if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
|
|
269
|
+
|
|
270
|
+
let rebateTo = this.rebateTo;
|
|
271
|
+
if (rebateTo === undefined) {
|
|
272
|
+
this.logger.error(`rebateTo is undefined in getAdapterParam`);
|
|
273
|
+
rebateTo = NULL_ADDRESS;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const payload = this.abiCoder.encodeParameter('address', rebateTo);
|
|
277
|
+
|
|
278
|
+
return {
|
|
279
|
+
targetExchange: this.config.wooPPV2Address,
|
|
280
|
+
payload,
|
|
281
|
+
networkFee: '0',
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async getSimpleParam(
|
|
286
|
+
srcToken: string,
|
|
287
|
+
destToken: string,
|
|
288
|
+
srcAmount: string,
|
|
289
|
+
destAmount: string,
|
|
290
|
+
data: WooFiV2Data,
|
|
291
|
+
side: SwapSide,
|
|
292
|
+
): Promise<SimpleExchangeParam> {
|
|
293
|
+
if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
|
|
294
|
+
|
|
295
|
+
const _srcToken = srcToken.toLowerCase();
|
|
296
|
+
const _destToken = destToken.toLowerCase();
|
|
297
|
+
|
|
298
|
+
let rebateTo = this.rebateTo;
|
|
299
|
+
if (rebateTo === undefined) {
|
|
300
|
+
this.logger.error(`rebateTo is undefined in getAdapterParam`);
|
|
301
|
+
rebateTo = NULL_ADDRESS;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const swapData = ifaces.PPV2.encodeFunctionData('swap', [
|
|
305
|
+
_srcToken,
|
|
306
|
+
_destToken,
|
|
307
|
+
srcAmount,
|
|
308
|
+
MIN_CONVERSION_RATE,
|
|
309
|
+
this.augustusAddress,
|
|
310
|
+
rebateTo,
|
|
311
|
+
]);
|
|
312
|
+
|
|
313
|
+
const transferParams = this.erc20Interface.encodeFunctionData('transfer', [
|
|
314
|
+
this.config.wooPPV2Address,
|
|
315
|
+
srcAmount,
|
|
316
|
+
]);
|
|
317
|
+
|
|
318
|
+
return {
|
|
319
|
+
callees: [srcToken, this.config.wooPPV2Address],
|
|
320
|
+
calldata: [transferParams, swapData],
|
|
321
|
+
values: ['0', '0'],
|
|
322
|
+
networkFee: '0',
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
async updatePoolState(): Promise<void> {
|
|
327
|
+
if (this._baseTokens === undefined || this.tokenByAddress === undefined) {
|
|
328
|
+
await this.initializeTokensAndAddresses();
|
|
329
|
+
}
|
|
330
|
+
if (this.pollingPool === undefined) {
|
|
331
|
+
this._initializePollingPool();
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (this.tokenByAddress === undefined) {
|
|
335
|
+
this.logger.error(`tokenByAddress is undefined in updatePoolState`);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
assert(
|
|
340
|
+
this.pollingPool,
|
|
341
|
+
'pollingPool is not initialized in updatePoolState',
|
|
342
|
+
);
|
|
343
|
+
const state = await this.pollingPool.getState();
|
|
344
|
+
|
|
345
|
+
if (!state) {
|
|
346
|
+
this.logger.error(
|
|
347
|
+
`Failed to updatePoolState. State is null. VaultUSDBalance: ${this.vaultUSDBalance} was not updated`,
|
|
348
|
+
);
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const tokenBalancesUSD = await Promise.all(
|
|
353
|
+
Object.values(this.tokenByAddress).map(t =>
|
|
354
|
+
this.dexHelper.getTokenUSDPrice(
|
|
355
|
+
t,
|
|
356
|
+
state.value.tokenInfos[t.address].reserve,
|
|
357
|
+
),
|
|
358
|
+
),
|
|
359
|
+
);
|
|
360
|
+
this.vaultUSDBalance = tokenBalancesUSD.reduce(
|
|
361
|
+
(sum: number, curr: number) => sum + curr,
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
async getTopPoolsForToken(
|
|
366
|
+
tokenAddress: Address,
|
|
367
|
+
limit: number,
|
|
368
|
+
): Promise<PoolLiquidity[]> {
|
|
369
|
+
const wrappedTokenAddress = this.dexHelper.config
|
|
370
|
+
.wrapETH({ address: tokenAddress, decimals: 0 })
|
|
371
|
+
.address.toLowerCase();
|
|
372
|
+
|
|
373
|
+
if (this.tokenByAddress === undefined) {
|
|
374
|
+
this.logger.error(`tokenByAddress is undefined in getTopPoolsForToken`);
|
|
375
|
+
return [];
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (!this.tokenByAddress[wrappedTokenAddress]) return [];
|
|
379
|
+
|
|
380
|
+
assert(
|
|
381
|
+
this.pollingPool,
|
|
382
|
+
'pollingPool is not initialized in getTopPoolsForToken',
|
|
383
|
+
);
|
|
384
|
+
const latestState = await this.pollingPool.getState();
|
|
385
|
+
if (!latestState) return [];
|
|
386
|
+
|
|
387
|
+
if (latestState.value.isPaused) {
|
|
388
|
+
this.logger.warn(`Paused on ${this.network} in getTopPoolsForToken`);
|
|
389
|
+
return [];
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const connectorTokens = Object.values(this.tokenByAddress).filter(
|
|
393
|
+
t => t.address !== wrappedTokenAddress,
|
|
394
|
+
);
|
|
395
|
+
|
|
396
|
+
return [
|
|
397
|
+
{
|
|
398
|
+
exchange: this.dexKey,
|
|
399
|
+
address: this.config.wooPPV2Address,
|
|
400
|
+
connectorTokens,
|
|
401
|
+
liquidityUSD: this.vaultUSDBalance,
|
|
402
|
+
},
|
|
403
|
+
];
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
private _initializeTokenByAddress(baseTokens: Token[]) {
|
|
407
|
+
this.tokenByAddress = baseTokens.reduce(
|
|
408
|
+
(acc, cur) => {
|
|
409
|
+
acc[cur.address] = cur;
|
|
410
|
+
return acc;
|
|
411
|
+
},
|
|
412
|
+
{ [this.quoteTokenAddress]: this.config.quoteToken },
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
async _fetchBaseTokensAndFeeWallet(): Promise<Address[]> {
|
|
417
|
+
return (
|
|
418
|
+
await this.dexHelper.multiWrapper.aggregate([
|
|
419
|
+
{
|
|
420
|
+
target: this.augustusAddress,
|
|
421
|
+
callData: this.augustusInterface.encodeFunctionData(
|
|
422
|
+
'getFeeWallet',
|
|
423
|
+
[],
|
|
424
|
+
),
|
|
425
|
+
decodeFunction: addressDecode,
|
|
426
|
+
},
|
|
427
|
+
{
|
|
428
|
+
target: this.config.integrationHelperAddress,
|
|
429
|
+
callData:
|
|
430
|
+
ifaces.integrationHelper.encodeFunctionData('allBaseTokens'),
|
|
431
|
+
decodeFunction: addressArrayDecode,
|
|
432
|
+
},
|
|
433
|
+
])
|
|
434
|
+
).flat();
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
async _fetchDecimals(baseTokenAddresses: Address[]): Promise<number[]> {
|
|
438
|
+
return this.dexHelper.multiWrapper.aggregate(
|
|
439
|
+
baseTokenAddresses.map(address => {
|
|
440
|
+
return {
|
|
441
|
+
target: address,
|
|
442
|
+
callData: this.erc20Interface.encodeFunctionData('decimals'),
|
|
443
|
+
decodeFunction: uint8ToNumber,
|
|
444
|
+
};
|
|
445
|
+
}),
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
async initializeTokensAndAddresses() {
|
|
450
|
+
const [rebateTo, ...baseTokenAddresses] =
|
|
451
|
+
await this._fetchBaseTokensAndFeeWallet();
|
|
452
|
+
|
|
453
|
+
assert(ethers.utils.isAddress(rebateTo), 'Invalid rebateTo address');
|
|
454
|
+
|
|
455
|
+
this.rebateTo = rebateTo;
|
|
456
|
+
const decimals = await this._fetchDecimals(baseTokenAddresses);
|
|
457
|
+
|
|
458
|
+
assert(
|
|
459
|
+
decimals.length === baseTokenAddresses.length,
|
|
460
|
+
'Decimals length mismatch',
|
|
461
|
+
);
|
|
462
|
+
|
|
463
|
+
this._baseTokens = baseTokenAddresses.reduce<Token[]>((acc, curr, i) => {
|
|
464
|
+
acc.push({
|
|
465
|
+
address: curr,
|
|
466
|
+
decimals: decimals[i],
|
|
467
|
+
});
|
|
468
|
+
return acc;
|
|
469
|
+
}, []);
|
|
470
|
+
|
|
471
|
+
this._initializeTokenByAddress(this.baseTokens);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
private _initializePollingPool() {
|
|
475
|
+
assert(this.tokenByAddress, 'tokenByAddress is not properly');
|
|
476
|
+
|
|
477
|
+
this.pollingPool = new WooFiV2PollingPool(
|
|
478
|
+
this.dexKey,
|
|
479
|
+
this.getIdentifier(),
|
|
480
|
+
this.dexHelper,
|
|
481
|
+
this.config,
|
|
482
|
+
Object.values(this.tokenByAddress),
|
|
483
|
+
);
|
|
484
|
+
|
|
485
|
+
this.pollingManager.initializeAllPendingPools();
|
|
486
|
+
}
|
|
487
|
+
}
|
package/src/lib/decoders.ts
CHANGED
|
@@ -28,12 +28,15 @@ export const extractSuccessAndValue = (
|
|
|
28
28
|
export function generalDecoder<T>(
|
|
29
29
|
result: MultiResult<BytesLike> | BytesLike,
|
|
30
30
|
types: string[],
|
|
31
|
-
defaultValue: T,
|
|
31
|
+
defaultValue: T | undefined,
|
|
32
32
|
parser?: (v: Result) => T,
|
|
33
33
|
): T {
|
|
34
34
|
const [isSuccess, toDecode] = extractSuccessAndValue(result);
|
|
35
35
|
|
|
36
36
|
if (!isSuccess || toDecode === '0x') {
|
|
37
|
+
if (defaultValue === undefined) {
|
|
38
|
+
throw new Error(`Failed to decode result: ${result}`);
|
|
39
|
+
}
|
|
37
40
|
return defaultValue;
|
|
38
41
|
}
|
|
39
42
|
|
|
@@ -136,6 +139,14 @@ export const addressDecode = (
|
|
|
136
139
|
);
|
|
137
140
|
};
|
|
138
141
|
|
|
142
|
+
export const addressArrayDecode = (
|
|
143
|
+
result: MultiResult<BytesLike> | BytesLike,
|
|
144
|
+
): string => {
|
|
145
|
+
return generalDecoder(result, ['address[]'], [], v =>
|
|
146
|
+
v[0].map((a: string) => a.toLowerCase()),
|
|
147
|
+
);
|
|
148
|
+
};
|
|
149
|
+
|
|
139
150
|
export const stringDecode = (
|
|
140
151
|
result: MultiResult<BytesLike> | BytesLike,
|
|
141
152
|
): string => {
|
|
@@ -76,6 +76,12 @@ export default class Fetcher<T> {
|
|
|
76
76
|
.map((_, i) => i)
|
|
77
77
|
.filter(i => results[i] instanceof Error);
|
|
78
78
|
|
|
79
|
+
results.map(result => {
|
|
80
|
+
this.logger.info(
|
|
81
|
+
'Results Data:',
|
|
82
|
+
JSON.stringify((result as any).data, null, 4).substring(0, 500),
|
|
83
|
+
);
|
|
84
|
+
});
|
|
79
85
|
failures.forEach(i => {
|
|
80
86
|
this.logger.warn(
|
|
81
87
|
`failed polling ${this.requests[i].info.requestOptions.url} ${results[i]}`,
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { assert } from 'ts-essentials';
|
|
2
|
+
import _ from 'lodash';
|
|
3
|
+
import { Logger, LogLevels } from '../types';
|
|
4
|
+
import { Utils } from '../utils';
|
|
5
|
+
|
|
6
|
+
export const DEFAULT_LOG_PUBLISH_PERIOD_MS = 60_000;
|
|
7
|
+
|
|
8
|
+
// After this period previous logs, even if unpublished, would be discarded
|
|
9
|
+
// Because most likely they are outdated
|
|
10
|
+
export const DEFAULT_LOG_DISCARD_PERIOD_MS = 15 * 60 * 1000;
|
|
11
|
+
|
|
12
|
+
// We may have hundreds of pools failing at the same time,
|
|
13
|
+
// and we don't want to show them all
|
|
14
|
+
export const DEFAULT_LOG_MAX_IDENTITIES_TO_SHOW = 10;
|
|
15
|
+
|
|
16
|
+
export type StandardStringEnum = Readonly<Record<string, string>>;
|
|
17
|
+
|
|
18
|
+
export type MessageInfo<T extends StandardStringEnum> = {
|
|
19
|
+
key: keyof T;
|
|
20
|
+
message: T[keyof T];
|
|
21
|
+
logLevel: LogLevels;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type MessageData = {
|
|
25
|
+
counter: number;
|
|
26
|
+
lastLoggedAtMs: number;
|
|
27
|
+
identificationInfos: string[];
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type AggregatedLogForEntity<T extends StandardStringEnum> = {
|
|
31
|
+
[key in keyof T]?: MessageData;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/*
|
|
35
|
+
* This is helper module to reduce frequency of logging of spamming messages
|
|
36
|
+
*
|
|
37
|
+
* 1. You can only have predefined set of log messages to log
|
|
38
|
+
* First time log message is received, it is published with all arguments.
|
|
39
|
+
* Limitation - message can not include variables, but you can pass them as args
|
|
40
|
+
* If within cooldown period received another message of the same category, it
|
|
41
|
+
* is not published, but counter is increased with addition of some entity identifier
|
|
42
|
+
*
|
|
43
|
+
* 2. There is no background task running. In order to keep relevance of logs,
|
|
44
|
+
* if after certain amount of time we didn't receive logs, then we discarded accumulated ones
|
|
45
|
+
* on the next received ones and only publish relevant ones
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
export class LogMessagesSuppressor<T extends StandardStringEnum> {
|
|
49
|
+
// I don't really like this any, but couldn't come up with better solution
|
|
50
|
+
// in reasonable time. So, in order to unblock myself, do this
|
|
51
|
+
static instances: Record<string, LogMessagesSuppressor<any>> = {};
|
|
52
|
+
|
|
53
|
+
private _aggregatedLogs: AggregatedLogForEntity<T> = {};
|
|
54
|
+
|
|
55
|
+
constructor(
|
|
56
|
+
readonly entityName: string,
|
|
57
|
+
private _allMessages: Record<keyof T, MessageInfo<T>>,
|
|
58
|
+
protected logger: Logger,
|
|
59
|
+
) {}
|
|
60
|
+
|
|
61
|
+
static getLogSuppressorInstance<T extends StandardStringEnum>(
|
|
62
|
+
entityName: string,
|
|
63
|
+
allMessages: Record<keyof T, MessageInfo<T>>,
|
|
64
|
+
logger: Logger,
|
|
65
|
+
) {
|
|
66
|
+
const instance = LogMessagesSuppressor.instances[entityName];
|
|
67
|
+
if (instance === undefined) {
|
|
68
|
+
LogMessagesSuppressor.instances[entityName] =
|
|
69
|
+
new LogMessagesSuppressor<T>(entityName, allMessages, logger);
|
|
70
|
+
} else {
|
|
71
|
+
assert(
|
|
72
|
+
_.isEqual(allMessages, instance._allMessages),
|
|
73
|
+
'getLogSuppressorInstance: try to get existing instance with different allMessagesInfo',
|
|
74
|
+
);
|
|
75
|
+
assert(
|
|
76
|
+
_.isEqual(logger, instance.logger),
|
|
77
|
+
'getLogSuppressorInstance: try to get existing instance with different logger',
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
return LogMessagesSuppressor.instances[entityName];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
logMessage(
|
|
84
|
+
msgKey: keyof T,
|
|
85
|
+
identificationInfo: string = '',
|
|
86
|
+
...args: unknown[]
|
|
87
|
+
) {
|
|
88
|
+
const msgInfo = this._allMessages[msgKey];
|
|
89
|
+
assert(
|
|
90
|
+
msgInfo !== undefined,
|
|
91
|
+
`Used unrecognized msgKey=${String(msgKey)} to get msgInfo`,
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
if (this._aggregatedLogs[msgKey] === undefined) {
|
|
95
|
+
this._aggregatedLogs[msgKey] = {
|
|
96
|
+
counter: 0,
|
|
97
|
+
identificationInfos:
|
|
98
|
+
identificationInfo === '' ? [] : [identificationInfo],
|
|
99
|
+
lastLoggedAtMs: 0,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const msgData = this._aggregatedLogs[msgKey];
|
|
103
|
+
assert(
|
|
104
|
+
msgData !== undefined,
|
|
105
|
+
`msgData is undefined for msgKey=${String(msgKey)}`,
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const nowMs = Date.now();
|
|
109
|
+
|
|
110
|
+
const elapsedSinceLastPublish = nowMs - msgData.lastLoggedAtMs;
|
|
111
|
+
|
|
112
|
+
if (elapsedSinceLastPublish > DEFAULT_LOG_PUBLISH_PERIOD_MS) {
|
|
113
|
+
// Accumulated logs are too old to have any relevance, so better to discard them
|
|
114
|
+
if (elapsedSinceLastPublish > DEFAULT_LOG_DISCARD_PERIOD_MS) {
|
|
115
|
+
this._clearMessageData(msgData);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Make actual logging
|
|
119
|
+
// Logging is done using original logger of the entity to keep module info
|
|
120
|
+
// and not override it
|
|
121
|
+
const formattedMessage = this._formatLogMessage(
|
|
122
|
+
msgData,
|
|
123
|
+
msgInfo.message,
|
|
124
|
+
args,
|
|
125
|
+
);
|
|
126
|
+
this.logger[msgInfo.logLevel](formattedMessage);
|
|
127
|
+
this._clearMessageData(msgData);
|
|
128
|
+
} else {
|
|
129
|
+
msgData.counter++;
|
|
130
|
+
if (identificationInfo !== '') {
|
|
131
|
+
msgData.identificationInfos.push(identificationInfo);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private _formatLogMessage(
|
|
137
|
+
msgData: MessageData,
|
|
138
|
+
message: string,
|
|
139
|
+
args: unknown[],
|
|
140
|
+
) {
|
|
141
|
+
const { counter, identificationInfos } = msgData;
|
|
142
|
+
let identities =
|
|
143
|
+
identificationInfos && identificationInfos.length === 0
|
|
144
|
+
? ''
|
|
145
|
+
: ` for entities: ${
|
|
146
|
+
identificationInfos.length > DEFAULT_LOG_MAX_IDENTITIES_TO_SHOW
|
|
147
|
+
? identificationInfos
|
|
148
|
+
.slice(DEFAULT_LOG_MAX_IDENTITIES_TO_SHOW)
|
|
149
|
+
.join(',')
|
|
150
|
+
: identificationInfos.join(',')
|
|
151
|
+
}`;
|
|
152
|
+
|
|
153
|
+
if (
|
|
154
|
+
identificationInfos &&
|
|
155
|
+
identificationInfos.length > DEFAULT_LOG_MAX_IDENTITIES_TO_SHOW
|
|
156
|
+
) {
|
|
157
|
+
identities = `${identities}... ${identificationInfos.length} total`;
|
|
158
|
+
}
|
|
159
|
+
const argsFormatted =
|
|
160
|
+
args.length > 0
|
|
161
|
+
? ` with args: ${args.map(a => Utils.Serialize(a)).join('.')}`
|
|
162
|
+
: '';
|
|
163
|
+
return `Logged ${counter} occurrences of: "${message}"${identities}${argsFormatted}`;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private _clearMessageData(messageData: MessageData) {
|
|
167
|
+
messageData.counter = 0;
|
|
168
|
+
if (messageData.identificationInfos) {
|
|
169
|
+
messageData.identificationInfos.length = 0;
|
|
170
|
+
}
|
|
171
|
+
messageData.lastLoggedAtMs = Date.now();
|
|
172
|
+
}
|
|
173
|
+
}
|