@paraswap/dex-lib 3.4.2-balancer-v2-single-swap.0 → 3.4.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/README.md +377 -377
- package/build/config.js +9 -5
- package/build/config.js.map +1 -1
- package/build/dex/algebra/algebra.js +25 -25
- package/build/dex/balancer-v1/balancer-v1.js +34 -34
- package/build/dex/balancer-v2/balancer-v2.d.ts +1 -1
- package/build/dex/balancer-v2/balancer-v2.js +65 -78
- package/build/dex/balancer-v2/balancer-v2.js.map +1 -1
- package/build/dex/camelot/camelot.js +27 -27
- package/build/dex/kyberdmm/kyberdmm.js +27 -27
- package/build/dex/pancakeswap-v3/pancakeswap-v3.js +25 -25
- package/build/dex/solidly/solidly.js +27 -27
- package/build/dex/solidly-v3/solidly-v3.js +25 -25
- package/build/dex/swaap-v2/config.js +15 -0
- package/build/dex/swaap-v2/config.js.map +1 -1
- package/build/dex/uniswap-v2/uniswap-v2.js +26 -26
- package/build/dex/uniswap-v3/uniswap-v3.js +25 -25
- package/funding.json +5 -5
- package/package.json +1 -1
- package/src/config.ts +500 -496
- package/src/constants.ts +67 -67
- package/src/dex/aave-v2/aave-v2.ts +409 -409
- package/src/dex/aave-v3/aave-v3.ts +320 -320
- package/src/dex/algebra/algebra.ts +1180 -1180
- package/src/dex/angle-staked-stable/angle-staked-stable.ts +347 -347
- package/src/dex/augustus-rfq.ts +180 -180
- package/src/dex/bProtocol/bProtocol.ts +108 -108
- package/src/dex/balancer-v1/balancer-v1.ts +572 -572
- package/src/dex/balancer-v2/balancer-v2-e2e.test.ts +1699 -1699
- package/src/dex/balancer-v2/balancer-v2.ts +1669 -1701
- package/src/dex/bancor/bancor.ts +128 -128
- package/src/dex/camelot/camelot.ts +873 -873
- package/src/dex/compound/compound.ts +107 -107
- package/src/dex/curve-v1/config.ts +1179 -1179
- package/src/dex/curve-v1/curve-v1-e2e.test.ts +507 -507
- package/src/dex/curve-v1/curve-v1.ts +1315 -1315
- package/src/dex/curve-v1-factory/config.ts +1018 -1018
- package/src/dex/curve-v1-factory/curve-v1-factory.ts +1288 -1288
- package/src/dex/curve-v2/curve-v2.ts +440 -440
- package/src/dex/dexalot/dexalot.ts +1100 -1100
- package/src/dex/dodo-v1/dodo-v1.ts +132 -132
- package/src/dex/dodo-v2.ts +266 -266
- package/src/dex/etherfi.ts +239 -239
- package/src/dex/generic-rfq/generic-rfq.ts +431 -431
- package/src/dex/gmx/gmx.ts +321 -321
- package/src/dex/hashflow/hashflow.ts +1135 -1135
- package/src/dex/idex.ts +254 -254
- package/src/dex/index.ts +357 -357
- package/src/dex/jarvis-v6/jarvis-v6.ts +509 -509
- package/src/dex/jarvis.ts +262 -262
- package/src/dex/kyberdmm/kyberdmm.ts +649 -649
- package/src/dex/lido/lido.ts +110 -110
- package/src/dex/maker-psm/maker-psm.ts +592 -592
- package/src/dex/maverick-v1/maverick-v1.ts +537 -537
- package/src/dex/nerve/config.ts +635 -635
- package/src/dex/nerve/nerve-e2e.test.ts +731 -731
- package/src/dex/nerve/nerve-events.test.ts +911 -911
- package/src/dex/nerve/nerve.ts +467 -467
- package/src/dex/onebit/onebit.ts +113 -113
- package/src/dex/pancakeswap-v3/pancakeswap-v3.ts +1150 -1150
- package/src/dex/paraswap-limit-orders/config.ts +60 -60
- package/src/dex/paraswap-limit-orders/paraswap-limit-orders-e2e.test.ts +137 -137
- package/src/dex/paraswap-limit-orders/paraswap-limit-orders-integration.test.ts +328 -328
- package/src/dex/paraswap-limit-orders/paraswap-limit-orders.ts +631 -631
- package/src/dex/polygon-migrator/polygon-migrator.ts +194 -194
- package/src/dex/quick-perps/quick-perps.ts +337 -337
- package/src/dex/quickswap/config.ts +16 -16
- package/src/dex/quickswap/quickswap-e2e.test.ts +384 -384
- package/src/dex/smoothy/smoothy.ts +110 -110
- package/src/dex/solidly/config.ts +225 -225
- package/src/dex/solidly/solidly-e2e.test.ts +1421 -1421
- package/src/dex/solidly/solidly-integration.test.ts +1753 -1753
- package/src/dex/solidly/solidly.ts +687 -687
- package/src/dex/solidly-v3/solidly-v3.ts +968 -968
- package/src/dex/spark/spark.ts +284 -284
- package/src/dex/stable-pool/stable-pool.ts +129 -129
- package/src/dex/swaap-v2/config.ts +15 -0
- package/src/dex/swaap-v2/swaap-v2-e2e.test.ts +233 -17
- package/src/dex/swaap-v2/swaap-v2-integration.test.ts +1 -1
- package/src/dex/swaap-v2/swaap-v2.ts +1056 -1056
- package/src/dex/swell/swell.ts +312 -312
- package/src/dex/synthetix/synthetix.ts +420 -420
- package/src/dex/trader-joe-v2.1/trader-joe-v2.1.ts +180 -180
- package/src/dex/uniswap-v2/config.ts +637 -637
- package/src/dex/uniswap-v2/constants.ts +58 -58
- package/src/dex/uniswap-v2/uniswap-v2-e2e-avalanche.test.ts +1257 -1257
- package/src/dex/uniswap-v2/uniswap-v2-e2e-base.test.ts +42 -42
- package/src/dex/uniswap-v2/uniswap-v2-e2e-bsc.test.ts +1295 -1295
- package/src/dex/uniswap-v2/uniswap-v2-e2e-mainnet.test.ts +1834 -1834
- package/src/dex/uniswap-v2/uniswap-v2-e2e-polygon.test.ts +871 -871
- package/src/dex/uniswap-v2/uniswap-v2.ts +1117 -1117
- package/src/dex/uniswap-v3/uniswap-v3.ts +1501 -1501
- package/src/dex/uniswap-v3.ts +242 -242
- package/src/dex/weth/config.ts +73 -73
- package/src/dex/weth/weth.ts +230 -230
- package/src/dex/wombat/wombat.ts +467 -467
- package/src/dex/woo-fi-v2/woo-fi-v2.ts +529 -529
- package/src/dex/wsteth/wsteth.ts +304 -304
- package/src/dex/zerox/index.ts +328 -328
- package/src/executor/Executor01BytecodeBuilder.ts +474 -474
- package/src/executor/Executor02BytecodeBuilder.ts +1392 -1392
- package/src/executor/ExecutorBytecodeBuilder.ts +390 -390
- package/src/executor/ExecutorDetector.ts +131 -131
- package/src/executor/constants.ts +66 -66
- package/src/executor/executor01-bytecode-builder-e2e.test.ts +1478 -1478
- package/src/executor/utils.ts +62 -62
- package/src/router/simpleswap.ts +366 -366
- package/src/types.ts +342 -342
- package/tests/constants-e2e.ts +1632 -1622
- package/tests/generate-new-limit-order.ts +107 -107
|
@@ -1,529 +1,529 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Token,
|
|
3
|
-
Address,
|
|
4
|
-
ExchangePrices,
|
|
5
|
-
PoolPrices,
|
|
6
|
-
AdapterExchangeParam,
|
|
7
|
-
SimpleExchangeParam,
|
|
8
|
-
PoolLiquidity,
|
|
9
|
-
Logger,
|
|
10
|
-
NumberAsString,
|
|
11
|
-
DexExchangeParam,
|
|
12
|
-
} from '../../types';
|
|
13
|
-
import { SwapSide, Network, NULL_ADDRESS } from '../../constants';
|
|
14
|
-
import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
|
|
15
|
-
import { getBigIntPow, getDexKeysWithNetwork } from '../../utils';
|
|
16
|
-
import { IDex } from '../../dex/idex';
|
|
17
|
-
import { addressArrayDecode, uint8ToNumber } from '../../lib/decoders';
|
|
18
|
-
import { IDexHelper } from '../../dex-helper/idex-helper';
|
|
19
|
-
import { WooFiV2Data, DexParams } from './types';
|
|
20
|
-
import { SimpleExchange } from '../simple-exchange';
|
|
21
|
-
import { WooFiV2Config, Adapters } from './config';
|
|
22
|
-
import { WooFiV2PollingPool } from './woo-fi-v2-pool';
|
|
23
|
-
import { WooFiV2Math } from './woo-fi-v2-math';
|
|
24
|
-
import { MIN_CONVERSION_RATE, WOO_FI_V2_GAS_COST } from './constants';
|
|
25
|
-
import { assert } from 'ts-essentials';
|
|
26
|
-
import { ethers } from 'ethers';
|
|
27
|
-
import { addressDecode } from '../../lib/decoders';
|
|
28
|
-
import { ifaces } from './utils';
|
|
29
|
-
import { StatePollingManager } from '../../lib/stateful-rpc-poller/state-polling-manager';
|
|
30
|
-
import { solidityPack } from 'ethers/lib/utils';
|
|
31
|
-
import { extractReturnAmountPosition } from '../../executor/utils';
|
|
32
|
-
|
|
33
|
-
export class WooFiV2 extends SimpleExchange implements IDex<WooFiV2Data> {
|
|
34
|
-
readonly math: WooFiV2Math;
|
|
35
|
-
|
|
36
|
-
protected pollingManager: StatePollingManager;
|
|
37
|
-
|
|
38
|
-
protected pollingPool?: WooFiV2PollingPool;
|
|
39
|
-
|
|
40
|
-
readonly hasConstantPriceLargeAmounts = false;
|
|
41
|
-
readonly needWrapNative = true;
|
|
42
|
-
|
|
43
|
-
readonly isFeeOnTransferSupported = false;
|
|
44
|
-
|
|
45
|
-
readonly isStatePollingDex = true;
|
|
46
|
-
|
|
47
|
-
readonly quoteTokenAddress: Address;
|
|
48
|
-
|
|
49
|
-
protected _baseTokens?: Token[];
|
|
50
|
-
|
|
51
|
-
// Since exchange anyways takes fee, we provide our fee wallet
|
|
52
|
-
// to take part of that fee and move to DAO
|
|
53
|
-
protected rebateTo?: Address;
|
|
54
|
-
|
|
55
|
-
tokenByAddress?: Record<Address, Token>;
|
|
56
|
-
|
|
57
|
-
vaultUSDBalance: number = 0;
|
|
58
|
-
|
|
59
|
-
public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
|
|
60
|
-
getDexKeysWithNetwork(WooFiV2Config);
|
|
61
|
-
|
|
62
|
-
logger: Logger;
|
|
63
|
-
|
|
64
|
-
constructor(
|
|
65
|
-
protected network: Network,
|
|
66
|
-
dexKey: string,
|
|
67
|
-
readonly dexHelper: IDexHelper,
|
|
68
|
-
protected adapters = Adapters[network] || {},
|
|
69
|
-
readonly config = WooFiV2Config[dexKey][network],
|
|
70
|
-
) {
|
|
71
|
-
super(dexHelper, dexKey);
|
|
72
|
-
const loggerName = `${dexKey}-${network}`;
|
|
73
|
-
this.logger = dexHelper.getLogger(loggerName);
|
|
74
|
-
|
|
75
|
-
// Normalise once all config addresses and use across all scenarios
|
|
76
|
-
this.config = this._toLowerForAllConfigAddresses();
|
|
77
|
-
|
|
78
|
-
this.quoteTokenAddress = this.config.quoteToken.address;
|
|
79
|
-
|
|
80
|
-
// Do not do it singleton, because different networks will have different
|
|
81
|
-
// states at the same time
|
|
82
|
-
this.math = new WooFiV2Math(
|
|
83
|
-
dexHelper.getLogger(`${loggerName}_math`),
|
|
84
|
-
this.quoteTokenAddress,
|
|
85
|
-
);
|
|
86
|
-
this.pollingManager = StatePollingManager.getInstance(dexHelper);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
private _toLowerForAllConfigAddresses() {
|
|
90
|
-
// If new config property will be added, the TS will throw compile error
|
|
91
|
-
const newConfig: DexParams = {
|
|
92
|
-
wooPPV2Address: this.config.wooPPV2Address.toLowerCase(),
|
|
93
|
-
wooOracleV2Address: this.config.wooOracleV2Address.toLowerCase(),
|
|
94
|
-
integrationHelperAddress:
|
|
95
|
-
this.config.integrationHelperAddress.toLowerCase(),
|
|
96
|
-
quoteToken: {
|
|
97
|
-
...this.config.quoteToken,
|
|
98
|
-
address: this.config.quoteToken.address.toLowerCase(),
|
|
99
|
-
},
|
|
100
|
-
};
|
|
101
|
-
return newConfig;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
get baseTokens(): Token[] {
|
|
105
|
-
if (!this._baseTokens) {
|
|
106
|
-
throw new Error(
|
|
107
|
-
`Base tokens are not set for ${this.dexKey}-${this.network}. Not properly initialized`,
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
return this._baseTokens;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
async initializePricing(blockNumber: number) {
|
|
114
|
-
await this.initializeTokensAndAddresses();
|
|
115
|
-
this._initializePollingPool();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
getAdapters(side: SwapSide): { name: string; index: number }[] | null {
|
|
119
|
-
return this.adapters[side] ? this.adapters[side] : null;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async getPoolIdentifiers(
|
|
123
|
-
srcToken: Token,
|
|
124
|
-
destToken: Token,
|
|
125
|
-
side: SwapSide,
|
|
126
|
-
blockNumber: number,
|
|
127
|
-
): Promise<string[]> {
|
|
128
|
-
if (side === SwapSide.BUY) return [];
|
|
129
|
-
|
|
130
|
-
if (this.tokenByAddress === undefined) {
|
|
131
|
-
this.logger.error(`tokenByAddress is undefined in getPoolIdentifiers`);
|
|
132
|
-
return [];
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const _srcToken = this.dexHelper.config.wrapETH(srcToken);
|
|
136
|
-
const _destToken = this.dexHelper.config.wrapETH(destToken);
|
|
137
|
-
|
|
138
|
-
const _srcAddress = _srcToken.address.toLowerCase();
|
|
139
|
-
const _destAddress = _destToken.address.toLowerCase();
|
|
140
|
-
|
|
141
|
-
if (_srcAddress === _destAddress) return [];
|
|
142
|
-
|
|
143
|
-
if (
|
|
144
|
-
!this.tokenByAddress[_srcAddress] ||
|
|
145
|
-
!this.tokenByAddress[_destAddress]
|
|
146
|
-
) {
|
|
147
|
-
return [];
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return [this.getIdentifier()];
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
getIdentifier() {
|
|
154
|
-
return `${this.dexKey}_wooppv2`;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
async getPricesVolume(
|
|
158
|
-
srcToken: Token,
|
|
159
|
-
destToken: Token,
|
|
160
|
-
amounts: bigint[],
|
|
161
|
-
side: SwapSide,
|
|
162
|
-
blockNumber: number,
|
|
163
|
-
limitPools?: string[],
|
|
164
|
-
): Promise<null | ExchangePrices<WooFiV2Data>> {
|
|
165
|
-
if (side === SwapSide.BUY) return null;
|
|
166
|
-
|
|
167
|
-
try {
|
|
168
|
-
if (this.tokenByAddress === undefined) {
|
|
169
|
-
this.logger.error(`tokenByAddress is undefined in getPricesVolume`);
|
|
170
|
-
return null;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const _srcToken = this.dexHelper.config.wrapETH(srcToken);
|
|
174
|
-
const _destToken = this.dexHelper.config.wrapETH(destToken);
|
|
175
|
-
|
|
176
|
-
const _srcAddress = _srcToken.address.toLowerCase();
|
|
177
|
-
const _destAddress = _destToken.address.toLowerCase();
|
|
178
|
-
|
|
179
|
-
if (_srcAddress === _destAddress) return null;
|
|
180
|
-
|
|
181
|
-
if (
|
|
182
|
-
!this.tokenByAddress[_srcAddress] ||
|
|
183
|
-
!this.tokenByAddress[_destAddress]
|
|
184
|
-
)
|
|
185
|
-
return null;
|
|
186
|
-
|
|
187
|
-
const expectedIdentifier = this.getIdentifier();
|
|
188
|
-
|
|
189
|
-
if (
|
|
190
|
-
limitPools !== undefined &&
|
|
191
|
-
!limitPools.some(p => p === expectedIdentifier)
|
|
192
|
-
)
|
|
193
|
-
return null;
|
|
194
|
-
|
|
195
|
-
const unitVolume = getBigIntPow(_srcToken.decimals);
|
|
196
|
-
|
|
197
|
-
const _amounts = [unitVolume, ...amounts.slice(1)];
|
|
198
|
-
|
|
199
|
-
assert(
|
|
200
|
-
this.pollingPool,
|
|
201
|
-
'pollingPool is not initialized in getPricesVolume',
|
|
202
|
-
);
|
|
203
|
-
|
|
204
|
-
const state = await this.pollingPool.getState(blockNumber);
|
|
205
|
-
|
|
206
|
-
if (state === null) {
|
|
207
|
-
this.logger.error(`State is null in getPricesVolume`);
|
|
208
|
-
return null;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
if (state.value.isPaused) {
|
|
212
|
-
this.logger.warn(
|
|
213
|
-
`${this.dexKey} is paused on ${this.network} in getPricesVolume`,
|
|
214
|
-
);
|
|
215
|
-
return null;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
this.math.state = state.value;
|
|
219
|
-
|
|
220
|
-
const _prices = this.math.query(_srcAddress, _destAddress, _amounts);
|
|
221
|
-
|
|
222
|
-
if (!_prices) return null;
|
|
223
|
-
|
|
224
|
-
const unit = _prices[0];
|
|
225
|
-
|
|
226
|
-
let gasCost = WOO_FI_V2_GAS_COST.baseToBase;
|
|
227
|
-
if (_srcAddress === this.config.quoteToken.address) {
|
|
228
|
-
gasCost = WOO_FI_V2_GAS_COST.sellQuote;
|
|
229
|
-
} else if (_destAddress === this.config.quoteToken.address) {
|
|
230
|
-
gasCost = WOO_FI_V2_GAS_COST.sellBase;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
return [
|
|
234
|
-
{
|
|
235
|
-
unit,
|
|
236
|
-
prices: [0n, ..._prices.slice(1)],
|
|
237
|
-
data: {},
|
|
238
|
-
poolIdentifier: expectedIdentifier,
|
|
239
|
-
exchange: this.dexKey,
|
|
240
|
-
gasCost,
|
|
241
|
-
poolAddresses: [this.config.wooPPV2Address],
|
|
242
|
-
},
|
|
243
|
-
];
|
|
244
|
-
} catch (e) {
|
|
245
|
-
this.logger.error(
|
|
246
|
-
`Error_getPricesVolume ${srcToken.symbol || srcToken.address}, ${
|
|
247
|
-
destToken.symbol || destToken.address
|
|
248
|
-
}, ${side}:`,
|
|
249
|
-
e,
|
|
250
|
-
);
|
|
251
|
-
return null;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
getCalldataGasCost(poolPrices: PoolPrices<WooFiV2Data>): number | number[] {
|
|
256
|
-
return CALLDATA_GAS_COST.DEX_OVERHEAD + CALLDATA_GAS_COST.ADDRESS;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
getAdapterParam(
|
|
260
|
-
srcToken: string,
|
|
261
|
-
destToken: string,
|
|
262
|
-
srcAmount: string,
|
|
263
|
-
destAmount: string,
|
|
264
|
-
data: WooFiV2Data,
|
|
265
|
-
side: SwapSide,
|
|
266
|
-
): AdapterExchangeParam {
|
|
267
|
-
if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
|
|
268
|
-
|
|
269
|
-
let rebateTo = this.rebateTo;
|
|
270
|
-
if (rebateTo === undefined) {
|
|
271
|
-
this.logger.error(`rebateTo is undefined in getAdapterParam`);
|
|
272
|
-
rebateTo = NULL_ADDRESS;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
const payload = this.abiCoder.encodeParameter('address', rebateTo);
|
|
276
|
-
|
|
277
|
-
return {
|
|
278
|
-
targetExchange: this.config.wooPPV2Address,
|
|
279
|
-
payload,
|
|
280
|
-
networkFee: '0',
|
|
281
|
-
};
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
async getSimpleParam(
|
|
285
|
-
srcToken: string,
|
|
286
|
-
destToken: string,
|
|
287
|
-
srcAmount: string,
|
|
288
|
-
destAmount: string,
|
|
289
|
-
data: WooFiV2Data,
|
|
290
|
-
side: SwapSide,
|
|
291
|
-
): Promise<SimpleExchangeParam> {
|
|
292
|
-
if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
|
|
293
|
-
|
|
294
|
-
const _srcToken = srcToken.toLowerCase();
|
|
295
|
-
const _destToken = destToken.toLowerCase();
|
|
296
|
-
|
|
297
|
-
let rebateTo = this.rebateTo;
|
|
298
|
-
if (rebateTo === undefined) {
|
|
299
|
-
this.logger.error(`rebateTo is undefined in getAdapterParam`);
|
|
300
|
-
rebateTo = NULL_ADDRESS;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
const swapData = ifaces.PPV2.encodeFunctionData('swap', [
|
|
304
|
-
_srcToken,
|
|
305
|
-
_destToken,
|
|
306
|
-
srcAmount,
|
|
307
|
-
MIN_CONVERSION_RATE,
|
|
308
|
-
this.augustusAddress,
|
|
309
|
-
rebateTo,
|
|
310
|
-
]);
|
|
311
|
-
|
|
312
|
-
const transferParams = this.erc20Interface.encodeFunctionData('transfer', [
|
|
313
|
-
this.config.wooPPV2Address,
|
|
314
|
-
srcAmount,
|
|
315
|
-
]);
|
|
316
|
-
|
|
317
|
-
return {
|
|
318
|
-
callees: [srcToken, this.config.wooPPV2Address],
|
|
319
|
-
calldata: [transferParams, swapData],
|
|
320
|
-
values: ['0', '0'],
|
|
321
|
-
networkFee: '0',
|
|
322
|
-
};
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
getDexParam(
|
|
326
|
-
srcToken: Address,
|
|
327
|
-
destToken: Address,
|
|
328
|
-
srcAmount: NumberAsString,
|
|
329
|
-
destAmount: NumberAsString,
|
|
330
|
-
recipient: Address,
|
|
331
|
-
data: WooFiV2Data,
|
|
332
|
-
side: SwapSide,
|
|
333
|
-
): DexExchangeParam {
|
|
334
|
-
if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
|
|
335
|
-
|
|
336
|
-
const _srcToken = srcToken.toLowerCase();
|
|
337
|
-
const _destToken = destToken.toLowerCase();
|
|
338
|
-
|
|
339
|
-
let rebateTo = this.rebateTo;
|
|
340
|
-
if (rebateTo === undefined) {
|
|
341
|
-
this.logger.error(`rebateTo is undefined in getDexParam`);
|
|
342
|
-
rebateTo = NULL_ADDRESS;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
const swapData = ifaces.PPV2.encodeFunctionData('swap', [
|
|
346
|
-
_srcToken,
|
|
347
|
-
_destToken,
|
|
348
|
-
srcAmount,
|
|
349
|
-
MIN_CONVERSION_RATE,
|
|
350
|
-
recipient,
|
|
351
|
-
// this.augustusAddress,
|
|
352
|
-
rebateTo,
|
|
353
|
-
]);
|
|
354
|
-
|
|
355
|
-
return {
|
|
356
|
-
needWrapNative: this.needWrapNative,
|
|
357
|
-
dexFuncHasRecipient: true,
|
|
358
|
-
exchangeData: swapData,
|
|
359
|
-
targetExchange: this.config.wooPPV2Address,
|
|
360
|
-
transferSrcTokenBeforeSwap: this.config.wooPPV2Address,
|
|
361
|
-
returnAmountPos:
|
|
362
|
-
side === SwapSide.SELL
|
|
363
|
-
? extractReturnAmountPosition(ifaces.PPV2, 'swap', 'realToAmount')
|
|
364
|
-
: undefined,
|
|
365
|
-
};
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
async updatePoolState(): Promise<void> {
|
|
369
|
-
if (this._baseTokens === undefined || this.tokenByAddress === undefined) {
|
|
370
|
-
await this.initializeTokensAndAddresses();
|
|
371
|
-
}
|
|
372
|
-
if (this.pollingPool === undefined) {
|
|
373
|
-
this._initializePollingPool();
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
if (this.tokenByAddress === undefined) {
|
|
377
|
-
this.logger.error(`tokenByAddress is undefined in updatePoolState`);
|
|
378
|
-
return;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
assert(
|
|
382
|
-
this.pollingPool,
|
|
383
|
-
'pollingPool is not initialized in updatePoolState',
|
|
384
|
-
);
|
|
385
|
-
const state = await this.pollingPool.getState();
|
|
386
|
-
|
|
387
|
-
if (!state) {
|
|
388
|
-
this.logger.error(
|
|
389
|
-
`Failed to updatePoolState. State is null. VaultUSDBalance: ${this.vaultUSDBalance} was not updated`,
|
|
390
|
-
);
|
|
391
|
-
return;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
const tokenBalancesUSD = await Promise.all(
|
|
395
|
-
Object.values(this.tokenByAddress).map(t =>
|
|
396
|
-
this.dexHelper.getTokenUSDPrice(
|
|
397
|
-
t,
|
|
398
|
-
state.value.tokenInfos[t.address].reserve,
|
|
399
|
-
),
|
|
400
|
-
),
|
|
401
|
-
);
|
|
402
|
-
this.vaultUSDBalance = tokenBalancesUSD.reduce(
|
|
403
|
-
(sum: number, curr: number) => sum + curr,
|
|
404
|
-
);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
async getTopPoolsForToken(
|
|
408
|
-
tokenAddress: Address,
|
|
409
|
-
limit: number,
|
|
410
|
-
): Promise<PoolLiquidity[]> {
|
|
411
|
-
const wrappedTokenAddress = this.dexHelper.config
|
|
412
|
-
.wrapETH({ address: tokenAddress, decimals: 0 })
|
|
413
|
-
.address.toLowerCase();
|
|
414
|
-
|
|
415
|
-
if (this.tokenByAddress === undefined) {
|
|
416
|
-
this.logger.error(`tokenByAddress is undefined in getTopPoolsForToken`);
|
|
417
|
-
return [];
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
if (!this.tokenByAddress[wrappedTokenAddress]) return [];
|
|
421
|
-
|
|
422
|
-
assert(
|
|
423
|
-
this.pollingPool,
|
|
424
|
-
'pollingPool is not initialized in getTopPoolsForToken',
|
|
425
|
-
);
|
|
426
|
-
const latestState = await this.pollingPool.getState();
|
|
427
|
-
if (!latestState) return [];
|
|
428
|
-
|
|
429
|
-
if (latestState.value.isPaused) {
|
|
430
|
-
this.logger.warn(`Paused on ${this.network} in getTopPoolsForToken`);
|
|
431
|
-
return [];
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
const connectorTokens = Object.values(this.tokenByAddress).filter(
|
|
435
|
-
t => t.address !== wrappedTokenAddress,
|
|
436
|
-
);
|
|
437
|
-
|
|
438
|
-
return [
|
|
439
|
-
{
|
|
440
|
-
exchange: this.dexKey,
|
|
441
|
-
address: this.config.wooPPV2Address,
|
|
442
|
-
connectorTokens,
|
|
443
|
-
liquidityUSD: this.vaultUSDBalance,
|
|
444
|
-
},
|
|
445
|
-
];
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
private _initializeTokenByAddress(baseTokens: Token[]) {
|
|
449
|
-
this.tokenByAddress = baseTokens.reduce(
|
|
450
|
-
(acc, cur) => {
|
|
451
|
-
acc[cur.address] = cur;
|
|
452
|
-
return acc;
|
|
453
|
-
},
|
|
454
|
-
{ [this.quoteTokenAddress]: this.config.quoteToken },
|
|
455
|
-
);
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
async _fetchBaseTokensAndFeeWallet(): Promise<Address[]> {
|
|
459
|
-
return (
|
|
460
|
-
await this.dexHelper.multiWrapper.aggregate([
|
|
461
|
-
{
|
|
462
|
-
target: this.augustusAddress,
|
|
463
|
-
callData: this.augustusInterface.encodeFunctionData(
|
|
464
|
-
'getFeeWallet',
|
|
465
|
-
[],
|
|
466
|
-
),
|
|
467
|
-
decodeFunction: addressDecode,
|
|
468
|
-
},
|
|
469
|
-
{
|
|
470
|
-
target: this.config.integrationHelperAddress,
|
|
471
|
-
callData:
|
|
472
|
-
ifaces.integrationHelper.encodeFunctionData('allBaseTokens'),
|
|
473
|
-
decodeFunction: addressArrayDecode,
|
|
474
|
-
},
|
|
475
|
-
])
|
|
476
|
-
).flat();
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
async _fetchDecimals(baseTokenAddresses: Address[]): Promise<number[]> {
|
|
480
|
-
return this.dexHelper.multiWrapper.aggregate(
|
|
481
|
-
baseTokenAddresses.map(address => {
|
|
482
|
-
return {
|
|
483
|
-
target: address,
|
|
484
|
-
callData: this.erc20Interface.encodeFunctionData('decimals'),
|
|
485
|
-
decodeFunction: uint8ToNumber,
|
|
486
|
-
};
|
|
487
|
-
}),
|
|
488
|
-
);
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
async initializeTokensAndAddresses() {
|
|
492
|
-
const [rebateTo, ...baseTokenAddresses] =
|
|
493
|
-
await this._fetchBaseTokensAndFeeWallet();
|
|
494
|
-
|
|
495
|
-
assert(ethers.utils.isAddress(rebateTo), 'Invalid rebateTo address');
|
|
496
|
-
|
|
497
|
-
this.rebateTo = rebateTo;
|
|
498
|
-
const decimals = await this._fetchDecimals(baseTokenAddresses);
|
|
499
|
-
|
|
500
|
-
assert(
|
|
501
|
-
decimals.length === baseTokenAddresses.length,
|
|
502
|
-
'Decimals length mismatch',
|
|
503
|
-
);
|
|
504
|
-
|
|
505
|
-
this._baseTokens = baseTokenAddresses.reduce<Token[]>((acc, curr, i) => {
|
|
506
|
-
acc.push({
|
|
507
|
-
address: curr,
|
|
508
|
-
decimals: decimals[i],
|
|
509
|
-
});
|
|
510
|
-
return acc;
|
|
511
|
-
}, []);
|
|
512
|
-
|
|
513
|
-
this._initializeTokenByAddress(this.baseTokens);
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
private _initializePollingPool() {
|
|
517
|
-
assert(this.tokenByAddress, 'tokenByAddress is not properly');
|
|
518
|
-
|
|
519
|
-
this.pollingPool = new WooFiV2PollingPool(
|
|
520
|
-
this.dexKey,
|
|
521
|
-
this.getIdentifier(),
|
|
522
|
-
this.dexHelper,
|
|
523
|
-
this.config,
|
|
524
|
-
Object.values(this.tokenByAddress),
|
|
525
|
-
);
|
|
526
|
-
|
|
527
|
-
this.pollingManager.initializeAllPendingPools();
|
|
528
|
-
}
|
|
529
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
Token,
|
|
3
|
+
Address,
|
|
4
|
+
ExchangePrices,
|
|
5
|
+
PoolPrices,
|
|
6
|
+
AdapterExchangeParam,
|
|
7
|
+
SimpleExchangeParam,
|
|
8
|
+
PoolLiquidity,
|
|
9
|
+
Logger,
|
|
10
|
+
NumberAsString,
|
|
11
|
+
DexExchangeParam,
|
|
12
|
+
} from '../../types';
|
|
13
|
+
import { SwapSide, Network, NULL_ADDRESS } from '../../constants';
|
|
14
|
+
import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
|
|
15
|
+
import { getBigIntPow, getDexKeysWithNetwork } from '../../utils';
|
|
16
|
+
import { IDex } from '../../dex/idex';
|
|
17
|
+
import { addressArrayDecode, uint8ToNumber } from '../../lib/decoders';
|
|
18
|
+
import { IDexHelper } from '../../dex-helper/idex-helper';
|
|
19
|
+
import { WooFiV2Data, DexParams } from './types';
|
|
20
|
+
import { SimpleExchange } from '../simple-exchange';
|
|
21
|
+
import { WooFiV2Config, Adapters } from './config';
|
|
22
|
+
import { WooFiV2PollingPool } from './woo-fi-v2-pool';
|
|
23
|
+
import { WooFiV2Math } from './woo-fi-v2-math';
|
|
24
|
+
import { MIN_CONVERSION_RATE, WOO_FI_V2_GAS_COST } from './constants';
|
|
25
|
+
import { assert } from 'ts-essentials';
|
|
26
|
+
import { ethers } from 'ethers';
|
|
27
|
+
import { addressDecode } from '../../lib/decoders';
|
|
28
|
+
import { ifaces } from './utils';
|
|
29
|
+
import { StatePollingManager } from '../../lib/stateful-rpc-poller/state-polling-manager';
|
|
30
|
+
import { solidityPack } from 'ethers/lib/utils';
|
|
31
|
+
import { extractReturnAmountPosition } from '../../executor/utils';
|
|
32
|
+
|
|
33
|
+
export class WooFiV2 extends SimpleExchange implements IDex<WooFiV2Data> {
|
|
34
|
+
readonly math: WooFiV2Math;
|
|
35
|
+
|
|
36
|
+
protected pollingManager: StatePollingManager;
|
|
37
|
+
|
|
38
|
+
protected pollingPool?: WooFiV2PollingPool;
|
|
39
|
+
|
|
40
|
+
readonly hasConstantPriceLargeAmounts = false;
|
|
41
|
+
readonly needWrapNative = true;
|
|
42
|
+
|
|
43
|
+
readonly isFeeOnTransferSupported = false;
|
|
44
|
+
|
|
45
|
+
readonly isStatePollingDex = true;
|
|
46
|
+
|
|
47
|
+
readonly quoteTokenAddress: Address;
|
|
48
|
+
|
|
49
|
+
protected _baseTokens?: Token[];
|
|
50
|
+
|
|
51
|
+
// Since exchange anyways takes fee, we provide our fee wallet
|
|
52
|
+
// to take part of that fee and move to DAO
|
|
53
|
+
protected rebateTo?: Address;
|
|
54
|
+
|
|
55
|
+
tokenByAddress?: Record<Address, Token>;
|
|
56
|
+
|
|
57
|
+
vaultUSDBalance: number = 0;
|
|
58
|
+
|
|
59
|
+
public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
|
|
60
|
+
getDexKeysWithNetwork(WooFiV2Config);
|
|
61
|
+
|
|
62
|
+
logger: Logger;
|
|
63
|
+
|
|
64
|
+
constructor(
|
|
65
|
+
protected network: Network,
|
|
66
|
+
dexKey: string,
|
|
67
|
+
readonly dexHelper: IDexHelper,
|
|
68
|
+
protected adapters = Adapters[network] || {},
|
|
69
|
+
readonly config = WooFiV2Config[dexKey][network],
|
|
70
|
+
) {
|
|
71
|
+
super(dexHelper, dexKey);
|
|
72
|
+
const loggerName = `${dexKey}-${network}`;
|
|
73
|
+
this.logger = dexHelper.getLogger(loggerName);
|
|
74
|
+
|
|
75
|
+
// Normalise once all config addresses and use across all scenarios
|
|
76
|
+
this.config = this._toLowerForAllConfigAddresses();
|
|
77
|
+
|
|
78
|
+
this.quoteTokenAddress = this.config.quoteToken.address;
|
|
79
|
+
|
|
80
|
+
// Do not do it singleton, because different networks will have different
|
|
81
|
+
// states at the same time
|
|
82
|
+
this.math = new WooFiV2Math(
|
|
83
|
+
dexHelper.getLogger(`${loggerName}_math`),
|
|
84
|
+
this.quoteTokenAddress,
|
|
85
|
+
);
|
|
86
|
+
this.pollingManager = StatePollingManager.getInstance(dexHelper);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private _toLowerForAllConfigAddresses() {
|
|
90
|
+
// If new config property will be added, the TS will throw compile error
|
|
91
|
+
const newConfig: DexParams = {
|
|
92
|
+
wooPPV2Address: this.config.wooPPV2Address.toLowerCase(),
|
|
93
|
+
wooOracleV2Address: this.config.wooOracleV2Address.toLowerCase(),
|
|
94
|
+
integrationHelperAddress:
|
|
95
|
+
this.config.integrationHelperAddress.toLowerCase(),
|
|
96
|
+
quoteToken: {
|
|
97
|
+
...this.config.quoteToken,
|
|
98
|
+
address: this.config.quoteToken.address.toLowerCase(),
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
return newConfig;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
get baseTokens(): Token[] {
|
|
105
|
+
if (!this._baseTokens) {
|
|
106
|
+
throw new Error(
|
|
107
|
+
`Base tokens are not set for ${this.dexKey}-${this.network}. Not properly initialized`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
return this._baseTokens;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async initializePricing(blockNumber: number) {
|
|
114
|
+
await this.initializeTokensAndAddresses();
|
|
115
|
+
this._initializePollingPool();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
getAdapters(side: SwapSide): { name: string; index: number }[] | null {
|
|
119
|
+
return this.adapters[side] ? this.adapters[side] : null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async getPoolIdentifiers(
|
|
123
|
+
srcToken: Token,
|
|
124
|
+
destToken: Token,
|
|
125
|
+
side: SwapSide,
|
|
126
|
+
blockNumber: number,
|
|
127
|
+
): Promise<string[]> {
|
|
128
|
+
if (side === SwapSide.BUY) return [];
|
|
129
|
+
|
|
130
|
+
if (this.tokenByAddress === undefined) {
|
|
131
|
+
this.logger.error(`tokenByAddress is undefined in getPoolIdentifiers`);
|
|
132
|
+
return [];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const _srcToken = this.dexHelper.config.wrapETH(srcToken);
|
|
136
|
+
const _destToken = this.dexHelper.config.wrapETH(destToken);
|
|
137
|
+
|
|
138
|
+
const _srcAddress = _srcToken.address.toLowerCase();
|
|
139
|
+
const _destAddress = _destToken.address.toLowerCase();
|
|
140
|
+
|
|
141
|
+
if (_srcAddress === _destAddress) return [];
|
|
142
|
+
|
|
143
|
+
if (
|
|
144
|
+
!this.tokenByAddress[_srcAddress] ||
|
|
145
|
+
!this.tokenByAddress[_destAddress]
|
|
146
|
+
) {
|
|
147
|
+
return [];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return [this.getIdentifier()];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
getIdentifier() {
|
|
154
|
+
return `${this.dexKey}_wooppv2`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async getPricesVolume(
|
|
158
|
+
srcToken: Token,
|
|
159
|
+
destToken: Token,
|
|
160
|
+
amounts: bigint[],
|
|
161
|
+
side: SwapSide,
|
|
162
|
+
blockNumber: number,
|
|
163
|
+
limitPools?: string[],
|
|
164
|
+
): Promise<null | ExchangePrices<WooFiV2Data>> {
|
|
165
|
+
if (side === SwapSide.BUY) return null;
|
|
166
|
+
|
|
167
|
+
try {
|
|
168
|
+
if (this.tokenByAddress === undefined) {
|
|
169
|
+
this.logger.error(`tokenByAddress is undefined in getPricesVolume`);
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const _srcToken = this.dexHelper.config.wrapETH(srcToken);
|
|
174
|
+
const _destToken = this.dexHelper.config.wrapETH(destToken);
|
|
175
|
+
|
|
176
|
+
const _srcAddress = _srcToken.address.toLowerCase();
|
|
177
|
+
const _destAddress = _destToken.address.toLowerCase();
|
|
178
|
+
|
|
179
|
+
if (_srcAddress === _destAddress) return null;
|
|
180
|
+
|
|
181
|
+
if (
|
|
182
|
+
!this.tokenByAddress[_srcAddress] ||
|
|
183
|
+
!this.tokenByAddress[_destAddress]
|
|
184
|
+
)
|
|
185
|
+
return null;
|
|
186
|
+
|
|
187
|
+
const expectedIdentifier = this.getIdentifier();
|
|
188
|
+
|
|
189
|
+
if (
|
|
190
|
+
limitPools !== undefined &&
|
|
191
|
+
!limitPools.some(p => p === expectedIdentifier)
|
|
192
|
+
)
|
|
193
|
+
return null;
|
|
194
|
+
|
|
195
|
+
const unitVolume = getBigIntPow(_srcToken.decimals);
|
|
196
|
+
|
|
197
|
+
const _amounts = [unitVolume, ...amounts.slice(1)];
|
|
198
|
+
|
|
199
|
+
assert(
|
|
200
|
+
this.pollingPool,
|
|
201
|
+
'pollingPool is not initialized in getPricesVolume',
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
const state = await this.pollingPool.getState(blockNumber);
|
|
205
|
+
|
|
206
|
+
if (state === null) {
|
|
207
|
+
this.logger.error(`State is null in getPricesVolume`);
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (state.value.isPaused) {
|
|
212
|
+
this.logger.warn(
|
|
213
|
+
`${this.dexKey} is paused on ${this.network} in getPricesVolume`,
|
|
214
|
+
);
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
this.math.state = state.value;
|
|
219
|
+
|
|
220
|
+
const _prices = this.math.query(_srcAddress, _destAddress, _amounts);
|
|
221
|
+
|
|
222
|
+
if (!_prices) return null;
|
|
223
|
+
|
|
224
|
+
const unit = _prices[0];
|
|
225
|
+
|
|
226
|
+
let gasCost = WOO_FI_V2_GAS_COST.baseToBase;
|
|
227
|
+
if (_srcAddress === this.config.quoteToken.address) {
|
|
228
|
+
gasCost = WOO_FI_V2_GAS_COST.sellQuote;
|
|
229
|
+
} else if (_destAddress === this.config.quoteToken.address) {
|
|
230
|
+
gasCost = WOO_FI_V2_GAS_COST.sellBase;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return [
|
|
234
|
+
{
|
|
235
|
+
unit,
|
|
236
|
+
prices: [0n, ..._prices.slice(1)],
|
|
237
|
+
data: {},
|
|
238
|
+
poolIdentifier: expectedIdentifier,
|
|
239
|
+
exchange: this.dexKey,
|
|
240
|
+
gasCost,
|
|
241
|
+
poolAddresses: [this.config.wooPPV2Address],
|
|
242
|
+
},
|
|
243
|
+
];
|
|
244
|
+
} catch (e) {
|
|
245
|
+
this.logger.error(
|
|
246
|
+
`Error_getPricesVolume ${srcToken.symbol || srcToken.address}, ${
|
|
247
|
+
destToken.symbol || destToken.address
|
|
248
|
+
}, ${side}:`,
|
|
249
|
+
e,
|
|
250
|
+
);
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
getCalldataGasCost(poolPrices: PoolPrices<WooFiV2Data>): number | number[] {
|
|
256
|
+
return CALLDATA_GAS_COST.DEX_OVERHEAD + CALLDATA_GAS_COST.ADDRESS;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
getAdapterParam(
|
|
260
|
+
srcToken: string,
|
|
261
|
+
destToken: string,
|
|
262
|
+
srcAmount: string,
|
|
263
|
+
destAmount: string,
|
|
264
|
+
data: WooFiV2Data,
|
|
265
|
+
side: SwapSide,
|
|
266
|
+
): AdapterExchangeParam {
|
|
267
|
+
if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
|
|
268
|
+
|
|
269
|
+
let rebateTo = this.rebateTo;
|
|
270
|
+
if (rebateTo === undefined) {
|
|
271
|
+
this.logger.error(`rebateTo is undefined in getAdapterParam`);
|
|
272
|
+
rebateTo = NULL_ADDRESS;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const payload = this.abiCoder.encodeParameter('address', rebateTo);
|
|
276
|
+
|
|
277
|
+
return {
|
|
278
|
+
targetExchange: this.config.wooPPV2Address,
|
|
279
|
+
payload,
|
|
280
|
+
networkFee: '0',
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async getSimpleParam(
|
|
285
|
+
srcToken: string,
|
|
286
|
+
destToken: string,
|
|
287
|
+
srcAmount: string,
|
|
288
|
+
destAmount: string,
|
|
289
|
+
data: WooFiV2Data,
|
|
290
|
+
side: SwapSide,
|
|
291
|
+
): Promise<SimpleExchangeParam> {
|
|
292
|
+
if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
|
|
293
|
+
|
|
294
|
+
const _srcToken = srcToken.toLowerCase();
|
|
295
|
+
const _destToken = destToken.toLowerCase();
|
|
296
|
+
|
|
297
|
+
let rebateTo = this.rebateTo;
|
|
298
|
+
if (rebateTo === undefined) {
|
|
299
|
+
this.logger.error(`rebateTo is undefined in getAdapterParam`);
|
|
300
|
+
rebateTo = NULL_ADDRESS;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const swapData = ifaces.PPV2.encodeFunctionData('swap', [
|
|
304
|
+
_srcToken,
|
|
305
|
+
_destToken,
|
|
306
|
+
srcAmount,
|
|
307
|
+
MIN_CONVERSION_RATE,
|
|
308
|
+
this.augustusAddress,
|
|
309
|
+
rebateTo,
|
|
310
|
+
]);
|
|
311
|
+
|
|
312
|
+
const transferParams = this.erc20Interface.encodeFunctionData('transfer', [
|
|
313
|
+
this.config.wooPPV2Address,
|
|
314
|
+
srcAmount,
|
|
315
|
+
]);
|
|
316
|
+
|
|
317
|
+
return {
|
|
318
|
+
callees: [srcToken, this.config.wooPPV2Address],
|
|
319
|
+
calldata: [transferParams, swapData],
|
|
320
|
+
values: ['0', '0'],
|
|
321
|
+
networkFee: '0',
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
getDexParam(
|
|
326
|
+
srcToken: Address,
|
|
327
|
+
destToken: Address,
|
|
328
|
+
srcAmount: NumberAsString,
|
|
329
|
+
destAmount: NumberAsString,
|
|
330
|
+
recipient: Address,
|
|
331
|
+
data: WooFiV2Data,
|
|
332
|
+
side: SwapSide,
|
|
333
|
+
): DexExchangeParam {
|
|
334
|
+
if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
|
|
335
|
+
|
|
336
|
+
const _srcToken = srcToken.toLowerCase();
|
|
337
|
+
const _destToken = destToken.toLowerCase();
|
|
338
|
+
|
|
339
|
+
let rebateTo = this.rebateTo;
|
|
340
|
+
if (rebateTo === undefined) {
|
|
341
|
+
this.logger.error(`rebateTo is undefined in getDexParam`);
|
|
342
|
+
rebateTo = NULL_ADDRESS;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const swapData = ifaces.PPV2.encodeFunctionData('swap', [
|
|
346
|
+
_srcToken,
|
|
347
|
+
_destToken,
|
|
348
|
+
srcAmount,
|
|
349
|
+
MIN_CONVERSION_RATE,
|
|
350
|
+
recipient,
|
|
351
|
+
// this.augustusAddress,
|
|
352
|
+
rebateTo,
|
|
353
|
+
]);
|
|
354
|
+
|
|
355
|
+
return {
|
|
356
|
+
needWrapNative: this.needWrapNative,
|
|
357
|
+
dexFuncHasRecipient: true,
|
|
358
|
+
exchangeData: swapData,
|
|
359
|
+
targetExchange: this.config.wooPPV2Address,
|
|
360
|
+
transferSrcTokenBeforeSwap: this.config.wooPPV2Address,
|
|
361
|
+
returnAmountPos:
|
|
362
|
+
side === SwapSide.SELL
|
|
363
|
+
? extractReturnAmountPosition(ifaces.PPV2, 'swap', 'realToAmount')
|
|
364
|
+
: undefined,
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
async updatePoolState(): Promise<void> {
|
|
369
|
+
if (this._baseTokens === undefined || this.tokenByAddress === undefined) {
|
|
370
|
+
await this.initializeTokensAndAddresses();
|
|
371
|
+
}
|
|
372
|
+
if (this.pollingPool === undefined) {
|
|
373
|
+
this._initializePollingPool();
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (this.tokenByAddress === undefined) {
|
|
377
|
+
this.logger.error(`tokenByAddress is undefined in updatePoolState`);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
assert(
|
|
382
|
+
this.pollingPool,
|
|
383
|
+
'pollingPool is not initialized in updatePoolState',
|
|
384
|
+
);
|
|
385
|
+
const state = await this.pollingPool.getState();
|
|
386
|
+
|
|
387
|
+
if (!state) {
|
|
388
|
+
this.logger.error(
|
|
389
|
+
`Failed to updatePoolState. State is null. VaultUSDBalance: ${this.vaultUSDBalance} was not updated`,
|
|
390
|
+
);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const tokenBalancesUSD = await Promise.all(
|
|
395
|
+
Object.values(this.tokenByAddress).map(t =>
|
|
396
|
+
this.dexHelper.getTokenUSDPrice(
|
|
397
|
+
t,
|
|
398
|
+
state.value.tokenInfos[t.address].reserve,
|
|
399
|
+
),
|
|
400
|
+
),
|
|
401
|
+
);
|
|
402
|
+
this.vaultUSDBalance = tokenBalancesUSD.reduce(
|
|
403
|
+
(sum: number, curr: number) => sum + curr,
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
async getTopPoolsForToken(
|
|
408
|
+
tokenAddress: Address,
|
|
409
|
+
limit: number,
|
|
410
|
+
): Promise<PoolLiquidity[]> {
|
|
411
|
+
const wrappedTokenAddress = this.dexHelper.config
|
|
412
|
+
.wrapETH({ address: tokenAddress, decimals: 0 })
|
|
413
|
+
.address.toLowerCase();
|
|
414
|
+
|
|
415
|
+
if (this.tokenByAddress === undefined) {
|
|
416
|
+
this.logger.error(`tokenByAddress is undefined in getTopPoolsForToken`);
|
|
417
|
+
return [];
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (!this.tokenByAddress[wrappedTokenAddress]) return [];
|
|
421
|
+
|
|
422
|
+
assert(
|
|
423
|
+
this.pollingPool,
|
|
424
|
+
'pollingPool is not initialized in getTopPoolsForToken',
|
|
425
|
+
);
|
|
426
|
+
const latestState = await this.pollingPool.getState();
|
|
427
|
+
if (!latestState) return [];
|
|
428
|
+
|
|
429
|
+
if (latestState.value.isPaused) {
|
|
430
|
+
this.logger.warn(`Paused on ${this.network} in getTopPoolsForToken`);
|
|
431
|
+
return [];
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const connectorTokens = Object.values(this.tokenByAddress).filter(
|
|
435
|
+
t => t.address !== wrappedTokenAddress,
|
|
436
|
+
);
|
|
437
|
+
|
|
438
|
+
return [
|
|
439
|
+
{
|
|
440
|
+
exchange: this.dexKey,
|
|
441
|
+
address: this.config.wooPPV2Address,
|
|
442
|
+
connectorTokens,
|
|
443
|
+
liquidityUSD: this.vaultUSDBalance,
|
|
444
|
+
},
|
|
445
|
+
];
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
private _initializeTokenByAddress(baseTokens: Token[]) {
|
|
449
|
+
this.tokenByAddress = baseTokens.reduce(
|
|
450
|
+
(acc, cur) => {
|
|
451
|
+
acc[cur.address] = cur;
|
|
452
|
+
return acc;
|
|
453
|
+
},
|
|
454
|
+
{ [this.quoteTokenAddress]: this.config.quoteToken },
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
async _fetchBaseTokensAndFeeWallet(): Promise<Address[]> {
|
|
459
|
+
return (
|
|
460
|
+
await this.dexHelper.multiWrapper.aggregate([
|
|
461
|
+
{
|
|
462
|
+
target: this.augustusAddress,
|
|
463
|
+
callData: this.augustusInterface.encodeFunctionData(
|
|
464
|
+
'getFeeWallet',
|
|
465
|
+
[],
|
|
466
|
+
),
|
|
467
|
+
decodeFunction: addressDecode,
|
|
468
|
+
},
|
|
469
|
+
{
|
|
470
|
+
target: this.config.integrationHelperAddress,
|
|
471
|
+
callData:
|
|
472
|
+
ifaces.integrationHelper.encodeFunctionData('allBaseTokens'),
|
|
473
|
+
decodeFunction: addressArrayDecode,
|
|
474
|
+
},
|
|
475
|
+
])
|
|
476
|
+
).flat();
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
async _fetchDecimals(baseTokenAddresses: Address[]): Promise<number[]> {
|
|
480
|
+
return this.dexHelper.multiWrapper.aggregate(
|
|
481
|
+
baseTokenAddresses.map(address => {
|
|
482
|
+
return {
|
|
483
|
+
target: address,
|
|
484
|
+
callData: this.erc20Interface.encodeFunctionData('decimals'),
|
|
485
|
+
decodeFunction: uint8ToNumber,
|
|
486
|
+
};
|
|
487
|
+
}),
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
async initializeTokensAndAddresses() {
|
|
492
|
+
const [rebateTo, ...baseTokenAddresses] =
|
|
493
|
+
await this._fetchBaseTokensAndFeeWallet();
|
|
494
|
+
|
|
495
|
+
assert(ethers.utils.isAddress(rebateTo), 'Invalid rebateTo address');
|
|
496
|
+
|
|
497
|
+
this.rebateTo = rebateTo;
|
|
498
|
+
const decimals = await this._fetchDecimals(baseTokenAddresses);
|
|
499
|
+
|
|
500
|
+
assert(
|
|
501
|
+
decimals.length === baseTokenAddresses.length,
|
|
502
|
+
'Decimals length mismatch',
|
|
503
|
+
);
|
|
504
|
+
|
|
505
|
+
this._baseTokens = baseTokenAddresses.reduce<Token[]>((acc, curr, i) => {
|
|
506
|
+
acc.push({
|
|
507
|
+
address: curr,
|
|
508
|
+
decimals: decimals[i],
|
|
509
|
+
});
|
|
510
|
+
return acc;
|
|
511
|
+
}, []);
|
|
512
|
+
|
|
513
|
+
this._initializeTokenByAddress(this.baseTokens);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
private _initializePollingPool() {
|
|
517
|
+
assert(this.tokenByAddress, 'tokenByAddress is not properly');
|
|
518
|
+
|
|
519
|
+
this.pollingPool = new WooFiV2PollingPool(
|
|
520
|
+
this.dexKey,
|
|
521
|
+
this.getIdentifier(),
|
|
522
|
+
this.dexHelper,
|
|
523
|
+
this.config,
|
|
524
|
+
Object.values(this.tokenByAddress),
|
|
525
|
+
);
|
|
526
|
+
|
|
527
|
+
this.pollingManager.initializeAllPendingPools();
|
|
528
|
+
}
|
|
529
|
+
}
|