@paraswap/dex-lib 2.13.1 → 2.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/config.js +5 -23
- package/build/config.js.map +1 -1
- package/build/dex/curve-v1/config.js +0 -2
- package/build/dex/curve-v1/config.js.map +1 -1
- package/build/dex/curve-v1/curve-v1.js +0 -2
- package/build/dex/curve-v1/curve-v1.js.map +1 -1
- package/build/dex/curve-v1/types.d.ts +0 -1
- package/build/dex/curve-v1/types.js.map +1 -1
- package/build/dex/generic-rfq/rate-fetcher.js +0 -2
- 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 +26 -36
- 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 +0 -1
- package/build/dex/simple-exchange.js +0 -2
- package/build/dex/simple-exchange.js.map +1 -1
- package/build/lib/decoders.d.ts +1 -2
- package/build/lib/decoders.js +1 -8
- package/build/lib/decoders.js.map +1 -1
- package/build/lib/fetcher/fetcher.js +0 -3
- package/build/lib/fetcher/fetcher.js.map +1 -1
- package/build/lib/multi-wrapper.d.ts +1 -2
- package/build/lib/multi-wrapper.js.map +1 -1
- package/build/lib/tokens/balancer-fetcher.js +5 -4
- 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 +0 -3
- package/dex-template/__DexName__-e2e.test.ts.template +0 -1
- package/dex-template/__DexName__-events.test.ts.template +0 -1
- package/dex-template/__DexName__-integration.test.ts.template +0 -1
- package/dex-template/__DexName__-pool.ts.template +5 -8
- package/package.json +1 -1
- package/scripts/generate-bytecode.ts +0 -2
- package/src/abi/woo-fi/WooFeeManager.abi.json +318 -0
- package/src/abi/{woo-fi-v2/WooPPV2.abi.json → woo-fi/WooPP.abi.json} +169 -201
- package/src/abi/{woo-fi-v2/WooOracleV2.abi.json → woo-fi/Wooracle.abi.json} +50 -148
- package/src/config.ts +5 -27
- package/src/dex/balancer-v2/balancer-v2-e2e.test.ts +1 -0
- package/src/dex/curve-v1/config.ts +0 -2
- package/src/dex/curve-v1/curve-v1-e2e.test.ts +37 -18
- package/src/dex/curve-v1/curve-v1.ts +0 -2
- package/src/dex/curve-v1/types.ts +0 -1
- package/src/dex/generic-rfq/generic-rfq-e2e.test.ts +258 -63
- package/src/dex/generic-rfq/rate-fetcher.ts +0 -2
- package/src/dex/generic-rfq/validators.test.ts +32 -6
- package/src/dex/generic-rfq/validators.ts +32 -57
- package/src/dex/index.ts +2 -2
- package/src/dex/simple-exchange.ts +0 -3
- package/src/dex/woo-fi/config.ts +141 -0
- package/src/dex/woo-fi/constants.ts +21 -0
- package/src/dex/woo-fi/types.ts +55 -0
- package/src/dex/woo-fi/woo-fi-decimal-math.ts +45 -0
- package/src/dex/woo-fi/woo-fi-e2e.test.ts +183 -0
- package/src/dex/woo-fi/woo-fi-integration.test.ts +416 -0
- package/src/dex/woo-fi/woo-fi-math.ts +527 -0
- package/src/dex/woo-fi/woo-fi.ts +693 -0
- package/src/lib/decoders.ts +1 -12
- package/src/lib/fetcher/fetcher.ts +0 -6
- package/src/lib/multi-wrapper.ts +1 -2
- package/src/lib/tokens/balancer-fetcher.ts +6 -5
- package/src/lib/tokens/utils.ts +3 -5
- package/src/types.ts +0 -4
- package/tests/constants-e2e.ts +9 -11
- package/tests/tenderly-simulation.ts +1 -1
- package/tests/utils-e2e.ts +48 -61
- package/tests/utils.ts +0 -2
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/.vscode/launch.json +0 -21
- package/build/dex/aave-v3/tokens/arbitrum.json +0 -50
- package/build/dex/aave-v3/tokens/avalanche.json +0 -50
- package/build/dex/aave-v3/tokens/fantom.json +0 -62
- package/build/dex/aave-v3/tokens/mainnet.json +0 -44
- package/build/dex/aave-v3/tokens/optimism.json +0 -50
- package/build/dex/aave-v3/tokens/polygon.json +0 -98
- package/build/dex/balancer-v2/VirtualBoostedPool.d.ts +0 -79
- package/build/dex/balancer-v2/VirtualBoostedPool.js +0 -382
- package/build/dex/balancer-v2/VirtualBoostedPool.js.map +0 -1
- package/build/dex/generic-rfq/e2e-test-config.d.ts +0 -9
- package/build/dex/generic-rfq/e2e-test-config.js +0 -33
- package/build/dex/generic-rfq/e2e-test-config.js.map +0 -1
- package/build/dex/test-aave-v3/config.d.ts +0 -4
- package/build/dex/test-aave-v3/config.js +0 -15
- package/build/dex/test-aave-v3/config.js.map +0 -1
- package/build/dex/test-aave-v3/test-aave-v3-pool.d.ts +0 -39
- package/build/dex/test-aave-v3/test-aave-v3-pool.js +0 -63
- package/build/dex/test-aave-v3/test-aave-v3-pool.js.map +0 -1
- package/build/dex/test-aave-v3/test-aave-v3.d.ts +0 -37
- package/build/dex/test-aave-v3/test-aave-v3.js +0 -126
- package/build/dex/test-aave-v3/test-aave-v3.js.map +0 -1
- package/build/dex/test-aave-v3/types.d.ts +0 -6
- package/build/dex/test-aave-v3/types.js +0 -3
- package/build/dex/test-aave-v3/types.js.map +0 -1
- package/src/abi/woo-fi-v2/IntegrationHelper.abi.json +0 -116
- package/src/dex/generic-rfq/e2e-test-config.ts +0 -37
- package/src/dex/generic-rfq/generic-rfq-local-e2e.test.ts +0 -121
- package/src/dex/woo-fi-v2/config.ts +0 -87
- package/src/dex/woo-fi-v2/constants.ts +0 -22
- package/src/dex/woo-fi-v2/decoders.ts +0 -45
- package/src/dex/woo-fi-v2/types.ts +0 -63
- package/src/dex/woo-fi-v2/utils.ts +0 -19
- package/src/dex/woo-fi-v2/woo-fi-v2-e2e.test.ts +0 -216
- package/src/dex/woo-fi-v2/woo-fi-v2-integration.test.ts +0 -495
- package/src/dex/woo-fi-v2/woo-fi-v2-math.ts +0 -195
- package/src/dex/woo-fi-v2/woo-fi-v2-pool.ts +0 -112
- package/src/dex/woo-fi-v2/woo-fi-v2.ts +0 -487
- package/src/lib/log-messages-suppressor.ts +0 -173
- package/src/lib/stateful-rpc-poller/state-polling-manager.ts +0 -320
- package/src/lib/stateful-rpc-poller/stateful-rpc-poller.ts +0 -604
- package/src/lib/stateful-rpc-poller/types.ts +0 -66
- package/src/lib/stateful-rpc-poller/utils.ts +0 -23
|
@@ -0,0 +1,693 @@
|
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import { Interface, Result } from '@ethersproject/abi';
|
|
3
|
+
import {
|
|
4
|
+
Token,
|
|
5
|
+
Address,
|
|
6
|
+
ExchangePrices,
|
|
7
|
+
PoolPrices,
|
|
8
|
+
AdapterExchangeParam,
|
|
9
|
+
SimpleExchangeParam,
|
|
10
|
+
PoolLiquidity,
|
|
11
|
+
Logger,
|
|
12
|
+
} from '../../types';
|
|
13
|
+
import { SwapSide, Network } 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 { IDexHelper } from '../../dex-helper/idex-helper';
|
|
18
|
+
import {
|
|
19
|
+
DexParams,
|
|
20
|
+
LatestRoundData,
|
|
21
|
+
PoolState,
|
|
22
|
+
RefInfo,
|
|
23
|
+
WooFiData,
|
|
24
|
+
} from './types';
|
|
25
|
+
import { SimpleExchange } from '../simple-exchange';
|
|
26
|
+
import { WooFiConfig, Adapters } from './config';
|
|
27
|
+
import { WooFiMath } from './woo-fi-math';
|
|
28
|
+
import { MIN_CONVERSION_RATE, WOO_FI_GAS_COST } from './constants';
|
|
29
|
+
import wooPPABI from '../../abi/woo-fi/WooPP.abi.json';
|
|
30
|
+
import wooFeeManagerABI from '../../abi/woo-fi/WooFeeManager.abi.json';
|
|
31
|
+
import woOracleABI from '../../abi/woo-fi/Wooracle.abi.json';
|
|
32
|
+
|
|
33
|
+
export class WooFi extends SimpleExchange implements IDex<WooFiData> {
|
|
34
|
+
readonly math: WooFiMath;
|
|
35
|
+
|
|
36
|
+
latestState: PoolState | null = null;
|
|
37
|
+
|
|
38
|
+
latestBlockNumber: number = 0;
|
|
39
|
+
|
|
40
|
+
vaultUSDBalance: number = 0;
|
|
41
|
+
|
|
42
|
+
tokenByAddress: Record<Address, Token>;
|
|
43
|
+
|
|
44
|
+
private _refInfos: Record<Address, RefInfo> = {};
|
|
45
|
+
|
|
46
|
+
private _encodedStateRequestCalldata?: {
|
|
47
|
+
target: Address;
|
|
48
|
+
callData: string;
|
|
49
|
+
}[];
|
|
50
|
+
|
|
51
|
+
static readonly ifaces = {
|
|
52
|
+
PP: new Interface(wooPPABI),
|
|
53
|
+
fee: new Interface(wooFeeManagerABI),
|
|
54
|
+
oracle: new Interface(woOracleABI),
|
|
55
|
+
guardian: new Interface([
|
|
56
|
+
'function globalBound() view returns (uint64)',
|
|
57
|
+
'function refInfo(address) view ' +
|
|
58
|
+
'returns(tuple(address chainlinkRefOracle, uint96 refPriceFixCoeff, ' +
|
|
59
|
+
'uint96 minInputAmount, uint96 maxInputAmount, uint64 bound))',
|
|
60
|
+
]),
|
|
61
|
+
chainlink: new Interface([
|
|
62
|
+
'function latestRoundData() view returns (tuple(uint80 roundId, ' +
|
|
63
|
+
'int256 answer, uint256 startedAt, uint256 updatedAt, ' +
|
|
64
|
+
'uint80 answeredInRound))',
|
|
65
|
+
]),
|
|
66
|
+
erc20BalanceOf: new Interface([
|
|
67
|
+
'function balanceOf(address) view returns (uint256)',
|
|
68
|
+
]),
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
readonly hasConstantPriceLargeAmounts = false;
|
|
72
|
+
readonly needWrapNative = true;
|
|
73
|
+
readonly isFeeOnTransferSupported = false;
|
|
74
|
+
|
|
75
|
+
readonly quoteTokenAddress: Address;
|
|
76
|
+
|
|
77
|
+
public static dexKeysWithNetwork: {
|
|
78
|
+
key: string;
|
|
79
|
+
networks: Network[];
|
|
80
|
+
}[] = getDexKeysWithNetwork(WooFiConfig);
|
|
81
|
+
|
|
82
|
+
logger: Logger;
|
|
83
|
+
|
|
84
|
+
constructor(
|
|
85
|
+
protected network: Network,
|
|
86
|
+
dexKey: string,
|
|
87
|
+
protected dexHelper: IDexHelper,
|
|
88
|
+
protected adapters = Adapters[network] || {},
|
|
89
|
+
readonly config = WooFiConfig[dexKey][network],
|
|
90
|
+
) {
|
|
91
|
+
super(dexHelper, dexKey);
|
|
92
|
+
this.logger = dexHelper.getLogger(dexKey);
|
|
93
|
+
|
|
94
|
+
// Normalise once all config addresses and use across all scenarios
|
|
95
|
+
this.config = this._toLowerForAllConfigAddresses();
|
|
96
|
+
|
|
97
|
+
this.quoteTokenAddress = this.config.quoteToken.address;
|
|
98
|
+
|
|
99
|
+
// Do not do it singleton, because different networks will have different
|
|
100
|
+
// states at the same time
|
|
101
|
+
this.math = new WooFiMath(
|
|
102
|
+
dexHelper.getLogger(`${dexKey}_math`),
|
|
103
|
+
this.quoteTokenAddress,
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
this.tokenByAddress = Object.values(this.baseTokens).reduce(
|
|
107
|
+
(acc, cur) => {
|
|
108
|
+
acc[cur.address] = cur;
|
|
109
|
+
return acc;
|
|
110
|
+
},
|
|
111
|
+
{ [this.quoteTokenAddress]: this.config.quoteToken },
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private _toLowerForAllConfigAddresses() {
|
|
116
|
+
// If new config property will be added, the TS will throw compile error
|
|
117
|
+
const newConfig: DexParams = {
|
|
118
|
+
wooPPAddress: this.config.wooPPAddress.toLowerCase(),
|
|
119
|
+
wooOracleAddress: this.config.wooOracleAddress.toLowerCase(),
|
|
120
|
+
wooFeeManagerAddress: this.config.wooFeeManagerAddress.toLowerCase(),
|
|
121
|
+
wooGuardianAddress: this.config.wooGuardianAddress.toLowerCase(),
|
|
122
|
+
rebateTo: this.config.rebateTo.toLowerCase(),
|
|
123
|
+
quoteToken: {
|
|
124
|
+
...this.config.quoteToken,
|
|
125
|
+
address: this.config.quoteToken.address.toLowerCase(),
|
|
126
|
+
},
|
|
127
|
+
baseTokens: Object.keys(this.config.baseTokens).reduce<
|
|
128
|
+
Record<string, Token>
|
|
129
|
+
>((acc, cur) => {
|
|
130
|
+
const token = this.config.baseTokens[cur];
|
|
131
|
+
token.address = token.address.toLowerCase();
|
|
132
|
+
acc[cur] = token;
|
|
133
|
+
return acc;
|
|
134
|
+
}, {}),
|
|
135
|
+
};
|
|
136
|
+
return newConfig;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private async _getRefInfos() {
|
|
140
|
+
const calldata = this.baseTokens
|
|
141
|
+
.map(t => [
|
|
142
|
+
{
|
|
143
|
+
target: this.config.wooGuardianAddress,
|
|
144
|
+
callData: WooFi.ifaces.guardian.encodeFunctionData('refInfo', [
|
|
145
|
+
t.address,
|
|
146
|
+
]),
|
|
147
|
+
},
|
|
148
|
+
])
|
|
149
|
+
.flat();
|
|
150
|
+
|
|
151
|
+
calldata.push({
|
|
152
|
+
target: this.config.wooGuardianAddress,
|
|
153
|
+
callData: WooFi.ifaces.guardian.encodeFunctionData('refInfo', [
|
|
154
|
+
this.quoteTokenAddress,
|
|
155
|
+
]),
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const data = await this.dexHelper.multiContract.methods
|
|
159
|
+
.tryAggregate(false, calldata)
|
|
160
|
+
.call({}, 'latest');
|
|
161
|
+
|
|
162
|
+
const parsed = data.map(([success, _data]: [boolean, string]) => {
|
|
163
|
+
if (!success)
|
|
164
|
+
throw new Error(
|
|
165
|
+
`Unexpected multicall error in ${this.dexKey} _getRefInfos`,
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const decoded = WooFi.ifaces.guardian.decodeFunctionResult(
|
|
169
|
+
'refInfo',
|
|
170
|
+
_data,
|
|
171
|
+
)[0];
|
|
172
|
+
return {
|
|
173
|
+
chainlinkRefOracle: decoded.chainlinkRefOracle.toLowerCase(),
|
|
174
|
+
refPriceFixCoeff: decoded.refPriceFixCoeff.toBigInt(),
|
|
175
|
+
minInputAmount: decoded.minInputAmount.toBigInt(),
|
|
176
|
+
maxInputAmount: decoded.maxInputAmount.toBigInt(),
|
|
177
|
+
bound: decoded.bound.toBigInt(),
|
|
178
|
+
};
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
return this.baseTokens.reduce<Record<string, RefInfo>>(
|
|
182
|
+
(acc, curr, ind) => {
|
|
183
|
+
acc[curr.address] = parsed[ind];
|
|
184
|
+
return acc;
|
|
185
|
+
},
|
|
186
|
+
{ [this.quoteTokenAddress]: parsed.slice(-1)[0] },
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
get baseTokens(): Token[] {
|
|
191
|
+
return Object.values(this.config.baseTokens);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
get isRefInfosEmpty(): boolean {
|
|
195
|
+
return Object.keys(this._refInfos).length === 0;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
protected _fillTokenInfoState(
|
|
199
|
+
state: PoolState,
|
|
200
|
+
address: string,
|
|
201
|
+
values: Result,
|
|
202
|
+
) {
|
|
203
|
+
state.tokenInfos[address] = {
|
|
204
|
+
reserve: BigInt(values.reserve._hex),
|
|
205
|
+
R: BigInt(values.R._hex),
|
|
206
|
+
threshold: BigInt(values.threshold._hex),
|
|
207
|
+
target: BigInt(values.target._hex),
|
|
208
|
+
lastResetTimestamp: BigInt(values.lastResetTimestamp),
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private _readChanLinkResponse(data: [boolean, string]): LatestRoundData {
|
|
213
|
+
const [success, returnData] = data;
|
|
214
|
+
const answer =
|
|
215
|
+
success === false || returnData === '0x'
|
|
216
|
+
? -1n
|
|
217
|
+
: WooFi.ifaces.chainlink
|
|
218
|
+
.decodeFunctionResult('latestRoundData', returnData)[0]
|
|
219
|
+
.answer.toBigInt();
|
|
220
|
+
return { answer };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
protected _getStateRequestCallData() {
|
|
224
|
+
if (!this._encodedStateRequestCalldata) {
|
|
225
|
+
const calldata = this.baseTokens
|
|
226
|
+
.map(t => [
|
|
227
|
+
{
|
|
228
|
+
target: this.config.wooFeeManagerAddress,
|
|
229
|
+
callData: WooFi.ifaces.fee.encodeFunctionData('feeRate', [
|
|
230
|
+
t.address,
|
|
231
|
+
]),
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
target: this.config.wooOracleAddress,
|
|
235
|
+
callData: WooFi.ifaces.oracle.encodeFunctionData('state', [
|
|
236
|
+
t.address,
|
|
237
|
+
]),
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
target: this.config.wooPPAddress,
|
|
241
|
+
callData: WooFi.ifaces.PP.encodeFunctionData('tokenInfo', [
|
|
242
|
+
t.address,
|
|
243
|
+
]),
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
target: this._refInfos[t.address].chainlinkRefOracle,
|
|
247
|
+
callData: WooFi.ifaces.chainlink.encodeFunctionData(
|
|
248
|
+
'latestRoundData',
|
|
249
|
+
[],
|
|
250
|
+
),
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
target: t.address,
|
|
254
|
+
callData: WooFi.ifaces.erc20BalanceOf.encodeFunctionData(
|
|
255
|
+
'balanceOf',
|
|
256
|
+
[this.config.wooPPAddress],
|
|
257
|
+
),
|
|
258
|
+
},
|
|
259
|
+
])
|
|
260
|
+
.flat();
|
|
261
|
+
|
|
262
|
+
calldata.push({
|
|
263
|
+
target: this.config.wooPPAddress,
|
|
264
|
+
callData: WooFi.ifaces.PP.encodeFunctionData('tokenInfo', [
|
|
265
|
+
this.quoteTokenAddress,
|
|
266
|
+
]),
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
calldata.push({
|
|
270
|
+
target: this._refInfos[this.quoteTokenAddress].chainlinkRefOracle,
|
|
271
|
+
callData: WooFi.ifaces.chainlink.encodeFunctionData(
|
|
272
|
+
'latestRoundData',
|
|
273
|
+
[],
|
|
274
|
+
),
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
calldata.push({
|
|
278
|
+
target: this.config.wooOracleAddress,
|
|
279
|
+
callData: WooFi.ifaces.oracle.encodeFunctionData('timestamp', []),
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
calldata.push({
|
|
283
|
+
target: this.config.wooPPAddress,
|
|
284
|
+
callData: WooFi.ifaces.PP.encodeFunctionData('paused', []),
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
calldata.push({
|
|
288
|
+
target: this.config.wooGuardianAddress,
|
|
289
|
+
callData: WooFi.ifaces.guardian.encodeFunctionData('globalBound', []),
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
calldata.push({
|
|
293
|
+
target: this.quoteTokenAddress,
|
|
294
|
+
callData: WooFi.ifaces.erc20BalanceOf.encodeFunctionData('balanceOf', [
|
|
295
|
+
this.config.wooPPAddress,
|
|
296
|
+
]),
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
this._encodedStateRequestCalldata = calldata;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return this._encodedStateRequestCalldata;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private async _fetchStateForBlockNumber(
|
|
306
|
+
blockNumber?: number,
|
|
307
|
+
): Promise<PoolState> {
|
|
308
|
+
const calldata = this._getStateRequestCallData();
|
|
309
|
+
|
|
310
|
+
const data = await this.dexHelper.multiContract.methods
|
|
311
|
+
.tryAggregate(false, calldata)
|
|
312
|
+
.call({}, blockNumber || 'latest');
|
|
313
|
+
|
|
314
|
+
// Last requests are standalone
|
|
315
|
+
const maxNumber = calldata.length - 6;
|
|
316
|
+
|
|
317
|
+
const [
|
|
318
|
+
baseFeeRates,
|
|
319
|
+
baseInfos,
|
|
320
|
+
baseTokenInfos,
|
|
321
|
+
chainlinkLatestRoundDatas,
|
|
322
|
+
baseTokenBalances,
|
|
323
|
+
quoteTokenInfo,
|
|
324
|
+
quoteChainlinkAnswer,
|
|
325
|
+
oracleTimestamp,
|
|
326
|
+
isPaused,
|
|
327
|
+
globalBound,
|
|
328
|
+
quoteTokenBalance,
|
|
329
|
+
] = [
|
|
330
|
+
// Skip n steps as they are infos, tokenInfo and latestRoundData etc.
|
|
331
|
+
_.range(0, maxNumber, 5).map(index =>
|
|
332
|
+
WooFi.ifaces.fee.decodeFunctionResult('feeRate', data[index][1]),
|
|
333
|
+
),
|
|
334
|
+
_.range(1, maxNumber, 5).map(index =>
|
|
335
|
+
WooFi.ifaces.oracle.decodeFunctionResult('state', data[index][1]),
|
|
336
|
+
),
|
|
337
|
+
_.range(2, maxNumber, 5).map(index =>
|
|
338
|
+
WooFi.ifaces.PP.decodeFunctionResult('tokenInfo', data[index][1]),
|
|
339
|
+
),
|
|
340
|
+
_.range(3, maxNumber, 5).map(index =>
|
|
341
|
+
this._readChanLinkResponse(data[index]),
|
|
342
|
+
),
|
|
343
|
+
_.range(4, maxNumber, 5).map(index =>
|
|
344
|
+
WooFi.ifaces.erc20BalanceOf.decodeFunctionResult(
|
|
345
|
+
'balanceOf',
|
|
346
|
+
data[index][1],
|
|
347
|
+
),
|
|
348
|
+
),
|
|
349
|
+
WooFi.ifaces.PP.decodeFunctionResult('tokenInfo', data[maxNumber][1]),
|
|
350
|
+
this._readChanLinkResponse(data[maxNumber + 1]),
|
|
351
|
+
WooFi.ifaces.oracle
|
|
352
|
+
.decodeFunctionResult('timestamp', data[maxNumber + 2][1])[0]
|
|
353
|
+
.toBigInt(),
|
|
354
|
+
WooFi.ifaces.PP.decodeFunctionResult('paused', data[maxNumber + 3][1])[0],
|
|
355
|
+
WooFi.ifaces.guardian
|
|
356
|
+
.decodeFunctionResult('globalBound', data[maxNumber + 4][1])[0]
|
|
357
|
+
.toBigInt(),
|
|
358
|
+
WooFi.ifaces.erc20BalanceOf
|
|
359
|
+
.decodeFunctionResult('balanceOf', data[maxNumber + 5][1])[0]
|
|
360
|
+
.toBigInt(),
|
|
361
|
+
];
|
|
362
|
+
|
|
363
|
+
const state: PoolState = {
|
|
364
|
+
feeRates: {},
|
|
365
|
+
tokenInfos: {},
|
|
366
|
+
tokenStates: {},
|
|
367
|
+
oracleTimestamp,
|
|
368
|
+
isPaused,
|
|
369
|
+
guardian: {
|
|
370
|
+
globalBound,
|
|
371
|
+
refInfos: this._refInfos,
|
|
372
|
+
},
|
|
373
|
+
chainlink: {
|
|
374
|
+
latestRoundDatas: {},
|
|
375
|
+
},
|
|
376
|
+
wooPPBalances: {},
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
this._fillTokenInfoState(state, this.quoteTokenAddress, quoteTokenInfo);
|
|
380
|
+
state.chainlink.latestRoundDatas[
|
|
381
|
+
this._refInfos[this.quoteTokenAddress].chainlinkRefOracle
|
|
382
|
+
] = quoteChainlinkAnswer;
|
|
383
|
+
|
|
384
|
+
baseFeeRates.map((value, index) => {
|
|
385
|
+
state.feeRates[this.baseTokens[index].address] = BigInt(value[0]._hex);
|
|
386
|
+
});
|
|
387
|
+
baseInfos.map((value, index) => {
|
|
388
|
+
state.tokenStates[this.baseTokens[index].address] = {
|
|
389
|
+
priceNow: BigInt(value.priceNow._hex),
|
|
390
|
+
coeffNow: BigInt(value.coeffNow._hex),
|
|
391
|
+
spreadNow: BigInt(value.spreadNow._hex),
|
|
392
|
+
};
|
|
393
|
+
});
|
|
394
|
+
baseTokenInfos.map((values, index) => {
|
|
395
|
+
this._fillTokenInfoState(state, this.baseTokens[index].address, values);
|
|
396
|
+
});
|
|
397
|
+
chainlinkLatestRoundDatas.map((value, index) => {
|
|
398
|
+
const refInfo = this._refInfos[this.baseTokens[index].address];
|
|
399
|
+
state.chainlink.latestRoundDatas[refInfo.chainlinkRefOracle] = value;
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
baseTokenBalances.map((value, index) => {
|
|
403
|
+
state.wooPPBalances[this.baseTokens[index].address] = BigInt(
|
|
404
|
+
value[0]._hex,
|
|
405
|
+
);
|
|
406
|
+
});
|
|
407
|
+
state.wooPPBalances[this.config.quoteToken.address] = quoteTokenBalance;
|
|
408
|
+
|
|
409
|
+
return state;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
async initializePricing(blockNumber: number) {
|
|
413
|
+
this._refInfos = await this._getRefInfos();
|
|
414
|
+
await this.getState(blockNumber);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
getAdapters(side: SwapSide): { name: string; index: number }[] | null {
|
|
418
|
+
return this.adapters[side] ? this.adapters[side] : null;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
getIdentifier(isSrcQuote: boolean) {
|
|
422
|
+
// Expected lower cased addresses
|
|
423
|
+
// And checks if one of the tokens is quote already done outside
|
|
424
|
+
return isSrcQuote ? `${this.dexKey}_qb` : `${this.dexKey}_bq`;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
async getPoolIdentifiers(
|
|
428
|
+
srcToken: Token,
|
|
429
|
+
destToken: Token,
|
|
430
|
+
side: SwapSide,
|
|
431
|
+
blockNumber: number,
|
|
432
|
+
): Promise<string[]> {
|
|
433
|
+
if (side === SwapSide.BUY) return [];
|
|
434
|
+
|
|
435
|
+
const _srcToken = this.dexHelper.config.wrapETH(srcToken);
|
|
436
|
+
const _destToken = this.dexHelper.config.wrapETH(destToken);
|
|
437
|
+
|
|
438
|
+
const _srcAddress = _srcToken.address.toLowerCase();
|
|
439
|
+
const _destAddress = _destToken.address.toLowerCase();
|
|
440
|
+
|
|
441
|
+
if (_srcAddress === _destAddress) return [];
|
|
442
|
+
|
|
443
|
+
if (
|
|
444
|
+
!this.tokenByAddress[_srcAddress] ||
|
|
445
|
+
!this.tokenByAddress[_destAddress]
|
|
446
|
+
) {
|
|
447
|
+
return [];
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
const { isSrcQuote, isDestQuote } = this._identifyQuote(
|
|
451
|
+
_srcAddress,
|
|
452
|
+
_destAddress,
|
|
453
|
+
);
|
|
454
|
+
|
|
455
|
+
if (!isSrcQuote && !isDestQuote) return [];
|
|
456
|
+
|
|
457
|
+
return [this.getIdentifier(isSrcQuote)];
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
async getPricesVolume(
|
|
461
|
+
srcToken: Token,
|
|
462
|
+
destToken: Token,
|
|
463
|
+
amounts: bigint[],
|
|
464
|
+
side: SwapSide,
|
|
465
|
+
blockNumber: number,
|
|
466
|
+
limitPools?: string[],
|
|
467
|
+
): Promise<null | ExchangePrices<WooFiData>> {
|
|
468
|
+
if (side === SwapSide.BUY) return null;
|
|
469
|
+
|
|
470
|
+
try {
|
|
471
|
+
const _srcToken = this.dexHelper.config.wrapETH(srcToken);
|
|
472
|
+
const _destToken = this.dexHelper.config.wrapETH(destToken);
|
|
473
|
+
|
|
474
|
+
const _srcAddress = _srcToken.address.toLowerCase();
|
|
475
|
+
const _destAddress = _destToken.address.toLowerCase();
|
|
476
|
+
|
|
477
|
+
if (_srcAddress === _destAddress) return null;
|
|
478
|
+
|
|
479
|
+
if (
|
|
480
|
+
!this.tokenByAddress[_srcAddress] ||
|
|
481
|
+
!this.tokenByAddress[_destAddress]
|
|
482
|
+
)
|
|
483
|
+
return null;
|
|
484
|
+
|
|
485
|
+
const { isSrcQuote, isDestQuote } = this._identifyQuote(
|
|
486
|
+
_srcAddress,
|
|
487
|
+
_destAddress,
|
|
488
|
+
);
|
|
489
|
+
|
|
490
|
+
if (!isSrcQuote && !isDestQuote) return null;
|
|
491
|
+
|
|
492
|
+
const expectedIdentifier = this.getIdentifier(isSrcQuote);
|
|
493
|
+
|
|
494
|
+
if (
|
|
495
|
+
limitPools !== undefined &&
|
|
496
|
+
!limitPools.some(p => p === expectedIdentifier)
|
|
497
|
+
)
|
|
498
|
+
return null;
|
|
499
|
+
|
|
500
|
+
const unitVolume = getBigIntPow(_srcToken.decimals);
|
|
501
|
+
|
|
502
|
+
const _amounts = [unitVolume, ...amounts.slice(1)];
|
|
503
|
+
|
|
504
|
+
const state = await this.getState(blockNumber);
|
|
505
|
+
|
|
506
|
+
if (state.isPaused) {
|
|
507
|
+
this.logger.warn(
|
|
508
|
+
`${this.dexKey} is paused on ${this.network} in getPricesVolume`,
|
|
509
|
+
);
|
|
510
|
+
return null;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
let _prices: bigint[] | null;
|
|
514
|
+
if (isSrcQuote) {
|
|
515
|
+
_prices = this.math.querySellQuote(_destAddress, _amounts);
|
|
516
|
+
} else {
|
|
517
|
+
_prices = this.math.querySellBase(_srcAddress, _amounts);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if (!_prices) return null;
|
|
521
|
+
|
|
522
|
+
const unit = _prices[0];
|
|
523
|
+
|
|
524
|
+
return [
|
|
525
|
+
{
|
|
526
|
+
unit,
|
|
527
|
+
prices: [0n, ..._prices.slice(1)],
|
|
528
|
+
data: {},
|
|
529
|
+
poolIdentifier: expectedIdentifier,
|
|
530
|
+
exchange: this.dexKey,
|
|
531
|
+
gasCost: WOO_FI_GAS_COST,
|
|
532
|
+
poolAddresses: [this.config.wooPPAddress],
|
|
533
|
+
},
|
|
534
|
+
];
|
|
535
|
+
} catch (e) {
|
|
536
|
+
this.logger.error(
|
|
537
|
+
`Error_getPricesVolume ${srcToken.symbol || srcToken.address}, ${
|
|
538
|
+
destToken.symbol || destToken.address
|
|
539
|
+
}, ${side}:`,
|
|
540
|
+
e,
|
|
541
|
+
);
|
|
542
|
+
return null;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
// Returns estimated gas cost of calldata for this DEX in multiSwap
|
|
547
|
+
getCalldataGasCost(poolPrices: PoolPrices<WooFiData>): number | number[] {
|
|
548
|
+
return CALLDATA_GAS_COST.DEX_NO_PAYLOAD;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
getAdapterParam(
|
|
552
|
+
srcToken: string,
|
|
553
|
+
destToken: string,
|
|
554
|
+
srcAmount: string,
|
|
555
|
+
destAmount: string,
|
|
556
|
+
data: WooFiData,
|
|
557
|
+
side: SwapSide,
|
|
558
|
+
): AdapterExchangeParam {
|
|
559
|
+
if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
|
|
560
|
+
|
|
561
|
+
const payload = this.abiCoder.encodeParameter(
|
|
562
|
+
'address',
|
|
563
|
+
this.config.rebateTo,
|
|
564
|
+
);
|
|
565
|
+
|
|
566
|
+
return {
|
|
567
|
+
targetExchange: this.config.wooPPAddress,
|
|
568
|
+
payload,
|
|
569
|
+
networkFee: '0',
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
async getSimpleParam(
|
|
574
|
+
srcToken: string,
|
|
575
|
+
destToken: string,
|
|
576
|
+
srcAmount: string,
|
|
577
|
+
destAmount: string,
|
|
578
|
+
data: WooFiData,
|
|
579
|
+
side: SwapSide,
|
|
580
|
+
): Promise<SimpleExchangeParam> {
|
|
581
|
+
if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
|
|
582
|
+
|
|
583
|
+
const _srcToken = srcToken.toLowerCase();
|
|
584
|
+
const _destToken = destToken.toLowerCase();
|
|
585
|
+
|
|
586
|
+
let funcName: string;
|
|
587
|
+
let _amount: string;
|
|
588
|
+
let baseToken: string;
|
|
589
|
+
if (_srcToken === this.quoteTokenAddress) {
|
|
590
|
+
baseToken = _destToken;
|
|
591
|
+
funcName = 'sellQuote';
|
|
592
|
+
_amount = srcAmount;
|
|
593
|
+
} else if (_destToken === this.quoteTokenAddress) {
|
|
594
|
+
baseToken = _srcToken;
|
|
595
|
+
funcName = 'sellBase';
|
|
596
|
+
_amount = srcAmount;
|
|
597
|
+
} else {
|
|
598
|
+
throw new Error(
|
|
599
|
+
`srcToken ${srcToken} or destToken ${destToken} must be quoteToken`,
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
const swapData = WooFi.ifaces.PP.encodeFunctionData(funcName, [
|
|
604
|
+
baseToken, // baseToken
|
|
605
|
+
_amount, // amount
|
|
606
|
+
MIN_CONVERSION_RATE, // minAmount
|
|
607
|
+
this.augustusAddress, // to
|
|
608
|
+
this.config.rebateTo, // rebateTo
|
|
609
|
+
]);
|
|
610
|
+
|
|
611
|
+
return this.buildSimpleParamWithoutWETHConversion(
|
|
612
|
+
srcToken,
|
|
613
|
+
srcAmount,
|
|
614
|
+
destToken,
|
|
615
|
+
destAmount,
|
|
616
|
+
swapData,
|
|
617
|
+
this.config.wooPPAddress,
|
|
618
|
+
);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
async updatePoolState(): Promise<void> {
|
|
622
|
+
if (this.isRefInfosEmpty) {
|
|
623
|
+
this._refInfos = await this._getRefInfos();
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
const state = await this.getState();
|
|
627
|
+
|
|
628
|
+
const tokenBalancesUSD = await Promise.all(
|
|
629
|
+
Object.values(this.tokenByAddress).map(t =>
|
|
630
|
+
this.dexHelper.getTokenUSDPrice(t, state.tokenInfos[t.address].reserve),
|
|
631
|
+
),
|
|
632
|
+
);
|
|
633
|
+
this.vaultUSDBalance = tokenBalancesUSD.reduce(
|
|
634
|
+
(sum: number, curr: number) => sum + curr,
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
async getTopPoolsForToken(
|
|
639
|
+
tokenAddress: Address,
|
|
640
|
+
limit: number,
|
|
641
|
+
): Promise<PoolLiquidity[]> {
|
|
642
|
+
const wrappedTokenAddress = this.dexHelper.config
|
|
643
|
+
.wrapETH({ address: tokenAddress, decimals: 0 })
|
|
644
|
+
.address.toLowerCase();
|
|
645
|
+
|
|
646
|
+
if (!this.tokenByAddress[wrappedTokenAddress]) return [];
|
|
647
|
+
if (!this.latestState) return [];
|
|
648
|
+
|
|
649
|
+
if (this.latestState.isPaused) {
|
|
650
|
+
this.logger.warn(
|
|
651
|
+
`${this.dexKey} is paused on ${this.network} in getTopPoolsForToken`,
|
|
652
|
+
);
|
|
653
|
+
return [];
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
const connectorTokens =
|
|
657
|
+
wrappedTokenAddress === this.quoteTokenAddress
|
|
658
|
+
? this.baseTokens
|
|
659
|
+
: [this.tokenByAddress[this.quoteTokenAddress]];
|
|
660
|
+
|
|
661
|
+
return [
|
|
662
|
+
{
|
|
663
|
+
exchange: this.dexKey,
|
|
664
|
+
address: this.config.wooPPAddress,
|
|
665
|
+
connectorTokens,
|
|
666
|
+
liquidityUSD: this.vaultUSDBalance,
|
|
667
|
+
},
|
|
668
|
+
];
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
async getState(blockNumber?: number): Promise<PoolState> {
|
|
672
|
+
if (
|
|
673
|
+
blockNumber !== undefined &&
|
|
674
|
+
this.latestBlockNumber === blockNumber &&
|
|
675
|
+
this.latestState !== null
|
|
676
|
+
) {
|
|
677
|
+
return this.latestState;
|
|
678
|
+
}
|
|
679
|
+
this.latestState = await this._fetchStateForBlockNumber(blockNumber);
|
|
680
|
+
this.latestBlockNumber = blockNumber ? blockNumber : 0;
|
|
681
|
+
|
|
682
|
+
this.math.state = this.latestState;
|
|
683
|
+
|
|
684
|
+
return this.latestState;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
private _identifyQuote(srcAddress: Address, destAddress: Address) {
|
|
688
|
+
return {
|
|
689
|
+
isSrcQuote: srcAddress === this.quoteTokenAddress,
|
|
690
|
+
isDestQuote: destAddress === this.quoteTokenAddress,
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
}
|
package/src/lib/decoders.ts
CHANGED
|
@@ -28,15 +28,12 @@ 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,
|
|
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
|
-
}
|
|
40
37
|
return defaultValue;
|
|
41
38
|
}
|
|
42
39
|
|
|
@@ -139,14 +136,6 @@ export const addressDecode = (
|
|
|
139
136
|
);
|
|
140
137
|
};
|
|
141
138
|
|
|
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
|
-
|
|
150
139
|
export const stringDecode = (
|
|
151
140
|
result: MultiResult<BytesLike> | BytesLike,
|
|
152
141
|
): string => {
|