@paraswap/dex-lib 4.0.4 → 4.0.5-fluid-dex-pool-discovery-issue.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/abi/fluid-dex/fluid-dex.abi.json +579 -25
- package/build/dex/fluid-dex/constants.d.ts +1 -0
- package/build/dex/fluid-dex/constants.js +5 -0
- package/build/dex/fluid-dex/constants.js.map +1 -0
- package/build/dex/fluid-dex/fluid-dex-liquidity-proxy.d.ts +2 -1
- package/build/dex/fluid-dex/fluid-dex-liquidity-proxy.js +2 -0
- package/build/dex/fluid-dex/fluid-dex-liquidity-proxy.js.map +1 -1
- package/build/dex/fluid-dex/fluid-dex-pool.d.ts +19 -21
- package/build/dex/fluid-dex/fluid-dex-pool.js +48 -62
- package/build/dex/fluid-dex/fluid-dex-pool.js.map +1 -1
- package/build/dex/fluid-dex/fluid-dex.d.ts +21 -2
- package/build/dex/fluid-dex/fluid-dex.js +108 -35
- package/build/dex/fluid-dex/fluid-dex.js.map +1 -1
- package/build/dex/fluid-dex/types.d.ts +3 -1
- package/package.json +1 -1
- package/src/abi/fluid-dex/fluid-dex.abi.json +581 -25
- package/src/dex/fluid-dex/constants.ts +1 -0
- package/src/dex/fluid-dex/fluid-dex-e2e.test.ts +17 -0
- package/src/dex/fluid-dex/fluid-dex-events.test.ts +55 -5
- package/src/dex/fluid-dex/fluid-dex-liquidity-proxy.ts +5 -3
- package/src/dex/fluid-dex/fluid-dex-pool.ts +139 -0
- package/src/dex/fluid-dex/fluid-dex.ts +226 -68
- package/src/dex/fluid-dex/types.ts +3 -1
- package/tests/constants-e2e.ts +5 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const MIN_SWAP_LIQUIDITY: bigint;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/dex/fluid-dex/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,kBAAkB,GAAG,GAAG,IAAI,EAAE,CAAC"}
|
|
@@ -16,7 +16,8 @@ export declare class FluidDexLiquidityProxy extends StatefulEventSubscriber<Flui
|
|
|
16
16
|
};
|
|
17
17
|
logDecoder: (log: Log) => any;
|
|
18
18
|
addressesSubscribed: Address[];
|
|
19
|
-
|
|
19
|
+
readonly liquidityIface: Interface;
|
|
20
|
+
readonly poolIface: Interface;
|
|
20
21
|
readonly resolverIface: Interface;
|
|
21
22
|
resolverContract: Contract;
|
|
22
23
|
constructor(parentName: string, commonAddresses: CommonAddresses, network: number, dexHelper: IDexHelper, logger: Logger);
|
|
@@ -8,6 +8,7 @@ const abi_1 = require("@ethersproject/abi");
|
|
|
8
8
|
const utils_1 = require("../../utils");
|
|
9
9
|
const stateful_event_subscriber_1 = require("../../stateful-event-subscriber");
|
|
10
10
|
const resolver_abi_json_1 = __importDefault(require("../../abi/fluid-dex/resolver.abi.json"));
|
|
11
|
+
const fluid_dex_abi_json_1 = __importDefault(require("../../abi/fluid-dex/fluid-dex.abi.json"));
|
|
11
12
|
const liquidityUserModule_abi_json_1 = __importDefault(require("../../abi/fluid-dex/liquidityUserModule.abi.json"));
|
|
12
13
|
const ethers_1 = require("ethers");
|
|
13
14
|
class FluidDexLiquidityProxy extends stateful_event_subscriber_1.StatefulEventSubscriber {
|
|
@@ -19,6 +20,7 @@ class FluidDexLiquidityProxy extends stateful_event_subscriber_1.StatefulEventSu
|
|
|
19
20
|
logDecoder;
|
|
20
21
|
addressesSubscribed;
|
|
21
22
|
liquidityIface = new abi_1.Interface(liquidityUserModule_abi_json_1.default);
|
|
23
|
+
poolIface = new abi_1.Interface(fluid_dex_abi_json_1.default);
|
|
22
24
|
resolverIface = new abi_1.Interface(resolver_abi_json_1.default);
|
|
23
25
|
resolverContract;
|
|
24
26
|
constructor(parentName, commonAddresses, network, dexHelper, logger) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fluid-dex-liquidity-proxy.js","sourceRoot":"","sources":["../../../src/dex/fluid-dex/fluid-dex-liquidity-proxy.ts"],"names":[],"mappings":";;;;;;AAAA,4CAA+C;AAG/C,uCAA4D;AAC5D,+EAA0E;AAE1E,8FAAgE;AAChE,oHAA4E;AAQ5E,
|
|
1
|
+
{"version":3,"file":"fluid-dex-liquidity-proxy.js","sourceRoot":"","sources":["../../../src/dex/fluid-dex/fluid-dex-liquidity-proxy.ts"],"names":[],"mappings":";;;;;;AAAA,4CAA+C;AAG/C,uCAA4D;AAC5D,+EAA0E;AAE1E,8FAAgE;AAChE,gGAAqE;AACrE,oHAA4E;AAQ5E,mCAA0C;AAE1C,MAAa,sBAAuB,SAAQ,mDAAoD;IAsBnF;IACA;IACC;IACD;IAxBX,QAAQ,GAMJ,EAAE,CAAC;IAEP,UAAU,CAAoB;IAE9B,mBAAmB,CAAY;IAEtB,cAAc,GAAG,IAAI,eAAS,CAAC,sCAAY,CAAC,CAAC;IAE7C,SAAS,GAAG,IAAI,eAAS,CAAC,4BAAe,CAAC,CAAC;IAE3C,aAAa,GAAG,IAAI,eAAS,CAAC,2BAAW,CAAC,CAAC;IAEpD,gBAAgB,CAAW;IAE3B,YACW,UAAkB,EAClB,eAAgC,EAC/B,OAAe,EAChB,SAAqB,EAC9B,MAAc;QAEd,KAAK,CAAC,UAAU,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;QAN/C,eAAU,GAAV,UAAU,CAAQ;QAClB,oBAAe,GAAf,eAAe,CAAiB;QAC/B,YAAO,GAAP,OAAO,CAAQ;QAChB,cAAS,GAAT,SAAS,CAAY;QAK9B,IAAI,CAAC,UAAU,GAAG,CAAC,GAAQ,EAAE,EAAE,CAAC,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QAElE,IAAI,CAAC,gBAAgB,GAAG,IAAI,iBAAQ,CAClC,IAAI,CAAC,eAAe,CAAC,QAAQ,EAC7B,2BAAW,EACX,IAAI,CAAC,SAAS,CAAC,QAAQ,CACxB,CAAC;Q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,CAAC;gBACxC,SAAS,EAAE,IAAA,iBAAS,EAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;gBACxC,eAAe,EAAE,IAAA,iBAAS,EAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;aAC/C,CAAC;YAEF,MAAM,SAAS,GAAG;gBAChB,kBAAkB;gBAClB,kBAAkB;gBAClB,gBAAgB;gBAChB,gBAAgB;aACjB,CAAC;YAEF,OAAO;gBACL,IAAI;gBACJ,MAAM;gBACN,MAAM;gBACN,GAAG;gBACH,kBAAkB;gBAClB,YAAY;gBACZ,SAAS;aACV,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,CAAC;IACnC,CAAC;CACF;AA/LD,wDA+LC"}
|
|
@@ -1,27 +1,23 @@
|
|
|
1
|
-
import { Interface } from '@ethersproject/abi';
|
|
2
|
-
import { DeepReadonly } from 'ts-essentials';
|
|
3
|
-
import { Log, Logger } from '../../types';
|
|
4
1
|
import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
2
|
+
import { DeepReadonly } from 'ts-essentials';
|
|
3
|
+
import { Address, Log, Logger } from '../../types';
|
|
4
|
+
import { IDexHelper } from '../../dex-helper';
|
|
5
|
+
import { Interface } from '@ethersproject/abi';
|
|
6
|
+
type PoolState = {
|
|
7
|
+
isSwapAndArbitragePaused: boolean;
|
|
8
|
+
};
|
|
9
|
+
export declare class FluidDexEventPool extends StatefulEventSubscriber<PoolState> {
|
|
9
10
|
readonly parentName: string;
|
|
10
|
-
readonly
|
|
11
|
-
readonly commonAddresses: CommonAddresses;
|
|
11
|
+
readonly poolAddress: string;
|
|
12
12
|
protected network: number;
|
|
13
|
-
|
|
13
|
+
protected dexHelper: IDexHelper;
|
|
14
14
|
handlers: {
|
|
15
|
-
[event: string]: (event: any, state: DeepReadonly<
|
|
15
|
+
[event: string]: (event: any, state: DeepReadonly<PoolState>, log: Readonly<Log>) => DeepReadonly<PoolState> | null;
|
|
16
16
|
};
|
|
17
17
|
logDecoder: (log: Log) => any;
|
|
18
18
|
addressesSubscribed: Address[];
|
|
19
|
-
protected
|
|
20
|
-
constructor(parentName: string,
|
|
21
|
-
/**
|
|
22
|
-
* Handle a trade rate change on the pool.
|
|
23
|
-
*/
|
|
24
|
-
handleOperate(event: any, state: DeepReadonly<FluidDexPoolState>, log: Readonly<Log>): Promise<DeepReadonly<FluidDexPoolState> | null>;
|
|
19
|
+
protected poolIface: Interface;
|
|
20
|
+
constructor(parentName: string, poolAddress: string, network: number, dexHelper: IDexHelper, logger: Logger);
|
|
25
21
|
/**
|
|
26
22
|
* The function is called every time any of the subscribed
|
|
27
23
|
* addresses release log. The function accepts the current
|
|
@@ -31,8 +27,10 @@ export declare class FluidDexEventPool extends StatefulEventSubscriber<FluidDexP
|
|
|
31
27
|
* @param log - Log released by one of the subscribed addresses
|
|
32
28
|
* @returns Updates state of the event subscriber after the log
|
|
33
29
|
*/
|
|
34
|
-
processLog(state: DeepReadonly<
|
|
35
|
-
|
|
30
|
+
processLog(state: DeepReadonly<PoolState>, log: Readonly<Log>): Promise<DeepReadonly<PoolState> | null>;
|
|
31
|
+
handleLogPauseSwapAndArbitrage(): PoolState;
|
|
32
|
+
handleLogUnpauseSwapAndArbitrage(): PoolState;
|
|
33
|
+
getStateOrGenerate(blockNumber: number, readonly?: boolean): Promise<DeepReadonly<PoolState>>;
|
|
36
34
|
/**
|
|
37
35
|
* The function generates state using on-chain calls. This
|
|
38
36
|
* function is called to regenerate state if the event based
|
|
@@ -42,6 +40,6 @@ export declare class FluidDexEventPool extends StatefulEventSubscriber<FluidDexP
|
|
|
42
40
|
* should be generated
|
|
43
41
|
* @returns state of the event subscriber at blocknumber
|
|
44
42
|
*/
|
|
45
|
-
generateState(blockNumber: number): Promise<DeepReadonly<
|
|
46
|
-
private convertToFluidDexPoolState;
|
|
43
|
+
generateState(blockNumber: number): Promise<DeepReadonly<PoolState>>;
|
|
47
44
|
}
|
|
45
|
+
export {};
|
|
@@ -4,41 +4,35 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.FluidDexEventPool = void 0;
|
|
7
|
+
const stateful_event_subscriber_1 = require("../../stateful-event-subscriber");
|
|
7
8
|
const abi_1 = require("@ethersproject/abi");
|
|
9
|
+
const fluid_dex_abi_json_1 = __importDefault(require("../../abi/fluid-dex/fluid-dex.abi.json"));
|
|
8
10
|
const utils_1 = require("../../utils");
|
|
9
|
-
const stateful_event_subscriber_1 = require("../../stateful-event-subscriber");
|
|
10
|
-
const resolver_abi_json_1 = __importDefault(require("../../abi/fluid-dex/resolver.abi.json"));
|
|
11
|
-
const liquidityUserModule_abi_json_1 = __importDefault(require("../../abi/fluid-dex/liquidityUserModule.abi.json"));
|
|
12
11
|
const ethers_1 = require("ethers");
|
|
12
|
+
const decoders_1 = require("../../lib/decoders");
|
|
13
|
+
const { utils: { hexlify, hexZeroPad }, } = ethers_1.ethers;
|
|
13
14
|
class FluidDexEventPool extends stateful_event_subscriber_1.StatefulEventSubscriber {
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
parentName;
|
|
16
|
+
poolAddress;
|
|
17
|
+
network;
|
|
18
|
+
dexHelper;
|
|
19
|
+
handlers = {};
|
|
20
|
+
logDecoder;
|
|
21
|
+
addressesSubscribed;
|
|
22
|
+
poolIface = new abi_1.Interface(fluid_dex_abi_json_1.default);
|
|
23
|
+
constructor(parentName, poolAddress, network, dexHelper, logger) {
|
|
24
|
+
super(parentName, 'pool', dexHelper, logger);
|
|
16
25
|
this.parentName = parentName;
|
|
17
|
-
this.
|
|
18
|
-
this.commonAddresses = commonAddresses;
|
|
26
|
+
this.poolAddress = poolAddress;
|
|
19
27
|
this.network = network;
|
|
20
28
|
this.dexHelper = dexHelper;
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
23
|
-
this.logDecoder = (log) => this.liquidityIface.parseLog(log);
|
|
24
|
-
this.addressesSubscribed = [commonAddresses.liquidityProxy];
|
|
29
|
+
this.logDecoder = (log) => this.poolIface.parseLog(log);
|
|
30
|
+
this.addressesSubscribed = [poolAddress];
|
|
25
31
|
// Add handlers
|
|
26
|
-
this.handlers['
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
*/
|
|
31
|
-
async handleOperate(event, state, log) {
|
|
32
|
-
if (!(event.args.user in [this.pool])) {
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
const resolverContract = new ethers_1.Contract(this.commonAddresses.resolver, resolver_abi_json_1.default, this.dexHelper.provider);
|
|
36
|
-
const rawResult = await resolverContract.callStatic.getPoolReservesAdjusted(this.pool, {
|
|
37
|
-
blockTag: this.dexHelper.provider,
|
|
38
|
-
});
|
|
39
|
-
const generatedState = this.convertToFluidDexPoolState(rawResult);
|
|
40
|
-
this.setState(generatedState, await this.dexHelper.provider.getBlockNumber());
|
|
41
|
-
return generatedState;
|
|
32
|
+
this.handlers['LogPauseSwapAndArbitrage'] =
|
|
33
|
+
this.handleLogPauseSwapAndArbitrage.bind(this);
|
|
34
|
+
this.handlers['LogUnpauseSwapAndArbitrage'] =
|
|
35
|
+
this.handleLogUnpauseSwapAndArbitrage.bind(this);
|
|
42
36
|
}
|
|
43
37
|
/**
|
|
44
38
|
* The function is called every time any of the subscribed
|
|
@@ -51,9 +45,15 @@ class FluidDexEventPool extends stateful_event_subscriber_1.StatefulEventSubscri
|
|
|
51
45
|
*/
|
|
52
46
|
async processLog(state, log) {
|
|
53
47
|
try {
|
|
54
|
-
|
|
48
|
+
let event;
|
|
49
|
+
try {
|
|
50
|
+
event = this.logDecoder(log);
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
55
|
if (event.name in this.handlers) {
|
|
56
|
-
return
|
|
56
|
+
return this.handlers[event.name](event, state, log);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
catch (e) {
|
|
@@ -61,6 +61,12 @@ class FluidDexEventPool extends stateful_event_subscriber_1.StatefulEventSubscri
|
|
|
61
61
|
}
|
|
62
62
|
return null;
|
|
63
63
|
}
|
|
64
|
+
handleLogPauseSwapAndArbitrage() {
|
|
65
|
+
return { isSwapAndArbitragePaused: true };
|
|
66
|
+
}
|
|
67
|
+
handleLogUnpauseSwapAndArbitrage() {
|
|
68
|
+
return { isSwapAndArbitragePaused: false };
|
|
69
|
+
}
|
|
64
70
|
async getStateOrGenerate(blockNumber, readonly = false) {
|
|
65
71
|
let state = this.getState(blockNumber);
|
|
66
72
|
if (!state) {
|
|
@@ -80,39 +86,19 @@ class FluidDexEventPool extends stateful_event_subscriber_1.StatefulEventSubscri
|
|
|
80
86
|
* @returns state of the event subscriber at blocknumber
|
|
81
87
|
*/
|
|
82
88
|
async generateState(blockNumber) {
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const collateralReserves = {
|
|
97
|
-
token0RealReserves: BigInt(collateralReservesHex[0].toString()),
|
|
98
|
-
token1RealReserves: BigInt(collateralReservesHex[1].toString()),
|
|
99
|
-
token0ImaginaryReserves: BigInt(collateralReservesHex[2].toString()),
|
|
100
|
-
token1ImaginaryReserves: BigInt(collateralReservesHex[3].toString()),
|
|
101
|
-
};
|
|
102
|
-
// Convert debt reserves
|
|
103
|
-
const debtReserves = {
|
|
104
|
-
token0Debt: BigInt(debtReservesHex[0].toString()),
|
|
105
|
-
token1Debt: BigInt(debtReservesHex[1].toString()),
|
|
106
|
-
token0RealReserves: BigInt(debtReservesHex[2].toString()),
|
|
107
|
-
token1RealReserves: BigInt(debtReservesHex[3].toString()),
|
|
108
|
-
token0ImaginaryReserves: BigInt(debtReservesHex[4].toString()),
|
|
109
|
-
token1ImaginaryReserves: BigInt(debtReservesHex[5].toString()),
|
|
110
|
-
};
|
|
111
|
-
return {
|
|
112
|
-
collateralReserves,
|
|
113
|
-
debtReserves,
|
|
114
|
-
fee,
|
|
115
|
-
};
|
|
89
|
+
const multicallData = [
|
|
90
|
+
{
|
|
91
|
+
target: this.addressesSubscribed[0],
|
|
92
|
+
callData: this.poolIface.encodeFunctionData('readFromStorage', [
|
|
93
|
+
hexZeroPad(hexlify(1), 32),
|
|
94
|
+
]),
|
|
95
|
+
decodeFunction: decoders_1.uint256ToBigInt,
|
|
96
|
+
},
|
|
97
|
+
];
|
|
98
|
+
const storageResults = await this.dexHelper.multiWrapper.tryAggregate(false, multicallData, blockNumber, this.dexHelper.multiWrapper.defaultBatchSize, false);
|
|
99
|
+
const isSwapAndArbitragePaused = BigInt(storageResults[0].returnData.toString()) >> BigInt(255) ===
|
|
100
|
+
BigInt(1);
|
|
101
|
+
return { isSwapAndArbitragePaused };
|
|
116
102
|
}
|
|
117
103
|
}
|
|
118
104
|
exports.FluidDexEventPool = FluidDexEventPool;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fluid-dex-pool.js","sourceRoot":"","sources":["../../../src/dex/fluid-dex/fluid-dex-pool.ts"],"names":[],"mappings":";;;;;;AAAA,4CAA+C;
|
|
1
|
+
{"version":3,"file":"fluid-dex-pool.js","sourceRoot":"","sources":["../../../src/dex/fluid-dex/fluid-dex-pool.ts"],"names":[],"mappings":";;;;;;AAAA,+EAA0E;AAI1E,4CAA+C;AAC/C,gGAAqE;AACrE,uCAAiD;AACjD,mCAAgC;AAChC,iDAAqD;AAGrD,MAAM,EACJ,KAAK,EAAE,EAAE,OAAO,EAAE,UAAU,EAAE,GAC/B,GAAG,eAAM,CAAC;AAMX,MAAa,iBAAkB,SAAQ,mDAAkC;IAc5D;IACA;IACC;IACA;IAhB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AwB,GAC5B,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC;YAC9D,MAAM,CAAC,CAAC,CAAC,CAAC;QAEZ,OAAO,EAAE,wBAAwB,EAAE,CAAC;IACtC,CAAC;CACF;AAvHD,8CAuHC"}
|
|
@@ -9,6 +9,7 @@ import { SimpleExchange } from '../simple-exchange';
|
|
|
9
9
|
import { FluidDexFactory } from './fluid-dex-factory';
|
|
10
10
|
import { MultiResult } from '../../lib/multi-wrapper';
|
|
11
11
|
import { FluidDexLiquidityProxy } from './fluid-dex-liquidity-proxy';
|
|
12
|
+
import { FluidDexEventPool } from './fluid-dex-pool';
|
|
12
13
|
export declare class FluidDex extends SimpleExchange implements IDex<FluidDexData> {
|
|
13
14
|
readonly network: Network;
|
|
14
15
|
readonly dexKey: string;
|
|
@@ -22,10 +23,10 @@ export declare class FluidDex extends SimpleExchange implements IDex<FluidDexDat
|
|
|
22
23
|
}[];
|
|
23
24
|
logger: Logger;
|
|
24
25
|
pools: FluidDexPool[];
|
|
26
|
+
eventPools: FluidDexEventPool[];
|
|
25
27
|
readonly factory: FluidDexFactory;
|
|
26
28
|
readonly liquidityProxy: FluidDexLiquidityProxy;
|
|
27
29
|
readonly fluidDexPoolIface: Interface;
|
|
28
|
-
FEE_100_PERCENT: bigint;
|
|
29
30
|
constructor(network: Network, dexKey: string, dexHelper: IDexHelper);
|
|
30
31
|
private fetchFluidDexPools;
|
|
31
32
|
private generateFluidDexPoolsFromPoolsFactory;
|
|
@@ -34,7 +35,7 @@ export declare class FluidDex extends SimpleExchange implements IDex<FluidDexDat
|
|
|
34
35
|
protected onPoolCreatedUpdatePools(poolsFromFactory: readonly Pool[]): void;
|
|
35
36
|
decodePools: (result: MultiResult<BytesLike> | BytesLike) => Pool[];
|
|
36
37
|
getPoolIdentifiers(srcToken: Token, destToken: Token, side: SwapSide, blockNumber: number): Promise<string[]>;
|
|
37
|
-
|
|
38
|
+
getPoolsByTokenPair(srcToken: Address, destToken: Address): FluidDexPool[];
|
|
38
39
|
getPricesVolume(srcToken: Token, destToken: Token, amounts: bigint[], side: SwapSide, blockNumber: number, limitPools?: string[]): Promise<null | ExchangePrices<FluidDexData>>;
|
|
39
40
|
getCalldataGasCost(poolPrices: PoolPrices<FluidDexData>): number | number[];
|
|
40
41
|
getAdapterParam(srcToken: string, destToken: string, srcAmount: string, destAmount: string, data: FluidDexData, side: SwapSide): AdapterExchangeParam;
|
|
@@ -60,6 +61,24 @@ export declare class FluidDex extends SimpleExchange implements IDex<FluidDexDat
|
|
|
60
61
|
* @returns The calculated output amount.
|
|
61
62
|
*/
|
|
62
63
|
swapInAdjusted(swap0To1: boolean, amountToSwap: bigint, colReserves: CollateralReserves, debtReserves: DebtReserves, fee: bigint, outDecimals: number, currentLimits: DexLimits, syncTime: number): bigint;
|
|
64
|
+
/**
|
|
65
|
+
* Checks if token0 reserves are sufficient compared to token1 reserves.
|
|
66
|
+
* This helps prevent edge cases and ensures high precision in calculations.
|
|
67
|
+
* @param {number} token0Reserves - The reserves of token0.
|
|
68
|
+
* @param {number} token1Reserves - The reserves of token1.
|
|
69
|
+
* @param {number} price - The current price used for calculation.
|
|
70
|
+
* @returns {boolean} - Returns false if token0 reserves are too low, true otherwise.
|
|
71
|
+
*/
|
|
72
|
+
protected verifyToken0Reserves(token0Reserves: bigint, token1Reserves: bigint, price: bigint): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Checks if token1 reserves are sufficient compared to token0 reserves.
|
|
75
|
+
* This helps prevent edge cases and ensures high precision in calculations.
|
|
76
|
+
* @param {number} token0Reserves - The reserves of token0.
|
|
77
|
+
* @param {number} token1Reserves - The reserves of token1.
|
|
78
|
+
* @param {number} price - The current price used for calculation.
|
|
79
|
+
* @returns {boolean} - Returns false if token1 reserves are too low, true otherwise.
|
|
80
|
+
*/
|
|
81
|
+
protected verifyToken1Reserves(token0Reserves: bigint, token1Reserves: bigint, price: bigint): boolean;
|
|
63
82
|
/**
|
|
64
83
|
* Calculates the currently available swappable amount for a token limit considering expansion since last syncTime.
|
|
65
84
|
* @param syncTime - timestamp in seconds when the limits were synced
|
|
@@ -50,6 +50,8 @@ const decoders_1 = require("../../lib/decoders");
|
|
|
50
50
|
const ethers_1 = require("ethers");
|
|
51
51
|
const utils_3 = require("./utils");
|
|
52
52
|
const fluid_dex_liquidity_proxy_1 = require("./fluid-dex-liquidity-proxy");
|
|
53
|
+
const fluid_dex_pool_1 = require("./fluid-dex-pool");
|
|
54
|
+
const constants_2 = require("./constants");
|
|
53
55
|
class FluidDex extends simple_exchange_1.SimpleExchange {
|
|
54
56
|
network;
|
|
55
57
|
dexKey;
|
|
@@ -60,10 +62,10 @@ class FluidDex extends simple_exchange_1.SimpleExchange {
|
|
|
60
62
|
static dexKeysWithNetwork = (0, utils_1.getDexKeysWithNetwork)(config_1.FluidDexConfig);
|
|
61
63
|
logger;
|
|
62
64
|
pools = [];
|
|
65
|
+
eventPools = [];
|
|
63
66
|
factory;
|
|
64
67
|
liquidityProxy;
|
|
65
68
|
fluidDexPoolIface;
|
|
66
|
-
FEE_100_PERCENT = BigInt(1000000);
|
|
67
69
|
constructor(network, dexKey, dexHelper) {
|
|
68
70
|
super(dexHelper, dexKey);
|
|
69
71
|
this.network = network;
|
|
@@ -93,6 +95,11 @@ class FluidDex extends simple_exchange_1.SimpleExchange {
|
|
|
93
95
|
async initializePricing(blockNumber) {
|
|
94
96
|
await this.factory.initialize(blockNumber);
|
|
95
97
|
this.pools = await this.fetchFluidDexPools(blockNumber);
|
|
98
|
+
this.eventPools = await Promise.all(this.pools.map(async (pool) => {
|
|
99
|
+
const eventPool = new fluid_dex_pool_1.FluidDexEventPool(this.dexKey, pool.address, this.network, this.dexHelper, this.logger);
|
|
100
|
+
await eventPool.initialize(blockNumber);
|
|
101
|
+
return eventPool;
|
|
102
|
+
}));
|
|
96
103
|
await this.liquidityProxy.initialize(blockNumber);
|
|
97
104
|
}
|
|
98
105
|
getAdapters(side) {
|
|
@@ -116,22 +123,18 @@ class FluidDex extends simple_exchange_1.SimpleExchange {
|
|
|
116
123
|
// across DEXes. It is recommended to use
|
|
117
124
|
// ${dexKey}_${poolAddress} as a poolIdentifier
|
|
118
125
|
async getPoolIdentifiers(srcToken, destToken, side, blockNumber) {
|
|
119
|
-
const
|
|
120
|
-
return pool
|
|
126
|
+
const pools = this.getPoolsByTokenPair(srcToken.address, destToken.address);
|
|
127
|
+
return pools.map(pool => pool.id);
|
|
121
128
|
}
|
|
122
|
-
|
|
129
|
+
getPoolsByTokenPair(srcToken, destToken) {
|
|
123
130
|
const srcAddress = srcToken.toLowerCase();
|
|
124
131
|
const destAddress = destToken.toLowerCase();
|
|
125
132
|
// A pair must have 2 different tokens.
|
|
126
133
|
if (srcAddress === destAddress)
|
|
127
|
-
return
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
return pool;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return null;
|
|
134
|
+
return [];
|
|
135
|
+
const pools = this.pools.filter(pool => (srcAddress === pool.token0 && destAddress === pool.token1) ||
|
|
136
|
+
(srcAddress === pool.token1 && destAddress === pool.token0));
|
|
137
|
+
return pools;
|
|
135
138
|
}
|
|
136
139
|
// Returns pool prices for amounts.
|
|
137
140
|
// If limitPools is defined only pools in limitPools
|
|
@@ -141,37 +144,50 @@ class FluidDex extends simple_exchange_1.SimpleExchange {
|
|
|
141
144
|
try {
|
|
142
145
|
if (srcToken.address.toLowerCase() === destToken.address.toLowerCase())
|
|
143
146
|
return null;
|
|
144
|
-
// Get the
|
|
145
|
-
const
|
|
146
|
-
if (!
|
|
147
|
+
// Get the pools to use.
|
|
148
|
+
const pools = this.getPoolsByTokenPair(srcToken.address, destToken.address);
|
|
149
|
+
if (!pools.length)
|
|
147
150
|
return null;
|
|
151
|
+
const poolIds = pools.map(pool => pool.id);
|
|
148
152
|
// Make sure the pool meets the optional limitPools filter.
|
|
149
|
-
if (limitPools &&
|
|
153
|
+
if (limitPools &&
|
|
154
|
+
!limitPools.every(limitPool => poolIds.includes(limitPool)))
|
|
150
155
|
return null;
|
|
151
156
|
const liquidityProxyState = await this.liquidityProxy.getStateOrGenerate(blockNumber);
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
return
|
|
157
|
+
const poolsPrices = await Promise.all(pools.map(async (pool) => {
|
|
158
|
+
const currentPoolReserves = liquidityProxyState.poolsReserves.find(poolReserve => poolReserve.pool.toLowerCase() === pool.address.toLowerCase());
|
|
159
|
+
const eventPool = this.eventPools.find(eventPool => eventPool.poolAddress.toLowerCase() ===
|
|
160
|
+
pool.address.toLowerCase());
|
|
161
|
+
if (!eventPool) {
|
|
162
|
+
this.logger.warn(`${this.dexKey}-${this.network}: Event pool ${pool.address} was not found...`);
|
|
163
|
+
return null;
|
|
159
164
|
}
|
|
160
|
-
|
|
161
|
-
|
|
165
|
+
const state = await eventPool.getStateOrGenerate(blockNumber);
|
|
166
|
+
if (!currentPoolReserves || state.isSwapAndArbitragePaused === true) {
|
|
167
|
+
return null;
|
|
162
168
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
169
|
+
const prices = amounts.map(amount => {
|
|
170
|
+
if (side === constants_1.SwapSide.SELL) {
|
|
171
|
+
return this.swapIn(srcToken.address.toLowerCase() === pool.token0.toLowerCase(), amount, currentPoolReserves.collateralReserves, currentPoolReserves.debtReserves, srcToken.decimals, destToken.decimals, BigInt(currentPoolReserves.fee), currentPoolReserves.dexLimits, Math.floor(Date.now() / 1000));
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
return this.swapOut(srcToken.address.toLowerCase() === pool.token0.toLowerCase(), amount, currentPoolReserves.collateralReserves, currentPoolReserves.debtReserves, srcToken.decimals, destToken.decimals, BigInt(currentPoolReserves.fee), currentPoolReserves.dexLimits, Math.floor(Date.now() / 1000));
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
return {
|
|
166
178
|
prices: prices,
|
|
167
179
|
unit: (0, utils_1.getBigIntPow)(destToken.decimals),
|
|
168
|
-
data: {
|
|
180
|
+
data: {
|
|
181
|
+
poolId: pool.id,
|
|
182
|
+
},
|
|
169
183
|
exchange: this.dexKey,
|
|
170
184
|
poolIdentifier: pool.id,
|
|
171
185
|
gasCost: config_1.FLUID_DEX_GAS_COST,
|
|
172
186
|
poolAddresses: [pool.address],
|
|
173
|
-
}
|
|
174
|
-
|
|
187
|
+
};
|
|
188
|
+
}));
|
|
189
|
+
const notNullResults = poolsPrices.filter(res => res !== null);
|
|
190
|
+
return notNullResults;
|
|
175
191
|
}
|
|
176
192
|
catch (e) {
|
|
177
193
|
this.logger.error(`Error_getPricesVolume ${srcToken.address || srcToken.symbol}, ${destToken.address || destToken.symbol}, ${side}:`, e);
|
|
@@ -189,9 +205,8 @@ class FluidDex extends simple_exchange_1.SimpleExchange {
|
|
|
189
205
|
getAdapterParam(srcToken, destToken, srcAmount, destAmount, data, side) {
|
|
190
206
|
// Encode here the payload for adapter
|
|
191
207
|
const payload = '';
|
|
192
|
-
const pool = this.getPoolByTokenPair(srcToken, destToken);
|
|
193
208
|
return {
|
|
194
|
-
targetExchange:
|
|
209
|
+
targetExchange: '0x',
|
|
195
210
|
payload,
|
|
196
211
|
networkFee: '0',
|
|
197
212
|
};
|
|
@@ -207,7 +222,9 @@ class FluidDex extends simple_exchange_1.SimpleExchange {
|
|
|
207
222
|
let returnAmountPos;
|
|
208
223
|
const method = side === constants_1.SwapSide.SELL ? 'swapIn' : 'swapOut';
|
|
209
224
|
returnAmountPos = (0, utils_2.extractReturnAmountPosition)(this.fluidDexPoolIface, method, side === constants_1.SwapSide.SELL ? 'amountOut_' : 'amountIn_');
|
|
210
|
-
const pool = this.
|
|
225
|
+
const pool = this.pools.find(pool => pool.id === data.poolId);
|
|
226
|
+
if (!pool)
|
|
227
|
+
throw new Error(`${this.dexKey}-${this.network}: Pool with id: ${data.poolId} was not found`);
|
|
211
228
|
if (side === constants_1.SwapSide.SELL) {
|
|
212
229
|
if (pool.token0.toLowerCase() !== srcToken.toLowerCase()) {
|
|
213
230
|
args = [false, BigInt(srcAmount), BigInt(destAmount), recipient];
|
|
@@ -387,9 +404,49 @@ class FluidDex extends simple_exchange_1.SimpleExchange {
|
|
|
387
404
|
if (priceDiff > maxAllowedDiff) {
|
|
388
405
|
return 0n;
|
|
389
406
|
}
|
|
407
|
+
if (amountInCollateral > 0) {
|
|
408
|
+
let reservesRatioValid = swap0To1
|
|
409
|
+
? this.verifyToken1Reserves(colReserveIn + amountInCollateral, colReserveOut - amountOutCollateral, oldPrice)
|
|
410
|
+
: this.verifyToken0Reserves(colReserveOut - amountOutCollateral, colReserveIn + amountInCollateral, oldPrice);
|
|
411
|
+
if (!reservesRatioValid) {
|
|
412
|
+
return 0n;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
if (amountInDebt > 0) {
|
|
416
|
+
let reservesRatioValid = swap0To1
|
|
417
|
+
? this.verifyToken1Reserves(debtReserveIn + amountInDebt, debtReserveOut - amountOutDebt, oldPrice)
|
|
418
|
+
: this.verifyToken0Reserves(debtReserveOut - amountOutDebt, debtReserveIn + amountInDebt, oldPrice);
|
|
419
|
+
if (!reservesRatioValid) {
|
|
420
|
+
return 0n;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
390
423
|
const totalAmountOut = amountOutCollateral + amountOutDebt;
|
|
391
424
|
return totalAmountOut;
|
|
392
425
|
}
|
|
426
|
+
/**
|
|
427
|
+
* Checks if token0 reserves are sufficient compared to token1 reserves.
|
|
428
|
+
* This helps prevent edge cases and ensures high precision in calculations.
|
|
429
|
+
* @param {number} token0Reserves - The reserves of token0.
|
|
430
|
+
* @param {number} token1Reserves - The reserves of token1.
|
|
431
|
+
* @param {number} price - The current price used for calculation.
|
|
432
|
+
* @returns {boolean} - Returns false if token0 reserves are too low, true otherwise.
|
|
433
|
+
*/
|
|
434
|
+
verifyToken0Reserves(token0Reserves, token1Reserves, price) {
|
|
435
|
+
return (token0Reserves >=
|
|
436
|
+
(token1Reserves * 10n ** 27n) / (price * constants_2.MIN_SWAP_LIQUIDITY));
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Checks if token1 reserves are sufficient compared to token0 reserves.
|
|
440
|
+
* This helps prevent edge cases and ensures high precision in calculations.
|
|
441
|
+
* @param {number} token0Reserves - The reserves of token0.
|
|
442
|
+
* @param {number} token1Reserves - The reserves of token1.
|
|
443
|
+
* @param {number} price - The current price used for calculation.
|
|
444
|
+
* @returns {boolean} - Returns false if token1 reserves are too low, true otherwise.
|
|
445
|
+
*/
|
|
446
|
+
verifyToken1Reserves(token0Reserves, token1Reserves, price) {
|
|
447
|
+
return (token1Reserves >=
|
|
448
|
+
(token0Reserves * price) / (10n ** 27n * constants_2.MIN_SWAP_LIQUIDITY));
|
|
449
|
+
}
|
|
393
450
|
/**
|
|
394
451
|
* Calculates the currently available swappable amount for a token limit considering expansion since last syncTime.
|
|
395
452
|
* @param syncTime - timestamp in seconds when the limits were synced
|
|
@@ -503,7 +560,7 @@ class FluidDex extends simple_exchange_1.SimpleExchange {
|
|
|
503
560
|
swapOut(swap0to1, amountOut, colReserves, debtReserves, inDecimals, outDecimals, fee, currentLimits, syncTime) {
|
|
504
561
|
const amountOutAdjusted = (amountOut * BigInt(10 ** 12)) / BigInt(10 ** outDecimals);
|
|
505
562
|
const amountIn = this.swapOutAdjusted(swap0to1, amountOutAdjusted, colReserves, debtReserves, fee, outDecimals, currentLimits, syncTime);
|
|
506
|
-
if (amountIn
|
|
563
|
+
if (amountIn === 2n ** 256n - 1n) {
|
|
507
564
|
return amountIn;
|
|
508
565
|
}
|
|
509
566
|
const ans = (amountIn * BigInt(10 ** inDecimals)) / BigInt(10 ** 12);
|
|
@@ -652,6 +709,22 @@ class FluidDex extends simple_exchange_1.SimpleExchange {
|
|
|
652
709
|
// if price diff is > 5% then swap would revert.
|
|
653
710
|
return 2n ** 256n - 1n;
|
|
654
711
|
}
|
|
712
|
+
if (amountInCollateral > 0) {
|
|
713
|
+
let reservesRatioValid = swap0to1
|
|
714
|
+
? this.verifyToken1Reserves(colReserveIn + amountInCollateral, colReserveOut - amountOutCollateral, oldPrice)
|
|
715
|
+
: this.verifyToken0Reserves(colReserveOut - amountOutCollateral, colReserveIn + amountInCollateral, oldPrice);
|
|
716
|
+
if (!reservesRatioValid) {
|
|
717
|
+
return 0n;
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
if (amountInDebt > 0) {
|
|
721
|
+
let reservesRatioValid = swap0to1
|
|
722
|
+
? this.verifyToken1Reserves(debtReserveIn + amountInDebt, debtReserveOut - amountOutDebt, oldPrice)
|
|
723
|
+
: this.verifyToken0Reserves(debtReserveOut - amountOutDebt, debtReserveIn + amountInDebt, oldPrice);
|
|
724
|
+
if (!reservesRatioValid) {
|
|
725
|
+
return 0n;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
655
728
|
const totalAmountIn = amountInCollateral + amountInDebt;
|
|
656
729
|
return totalAmountIn;
|
|
657
730
|
}
|