@paraswap/dex-lib 4.2.31 → 4.2.33
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/dex/miro-migrator/constants.d.ts +1 -1
- package/build/dex/miro-migrator/constants.js +1 -1
- package/build/dex/miro-migrator/miro-migrator-pool.d.ts +5 -5
- package/build/dex/miro-migrator/miro-migrator-pool.js +22 -13
- package/build/dex/miro-migrator/miro-migrator-pool.js.map +1 -1
- package/build/dex/miro-migrator/miro-migrator.js +2 -4
- package/build/dex/miro-migrator/miro-migrator.js.map +1 -1
- package/build/dex/uniswap-v2/rpc-pool-tracker.d.ts +14 -9
- package/build/dex/uniswap-v2/rpc-pool-tracker.js +163 -51
- package/build/dex/uniswap-v2/rpc-pool-tracker.js.map +1 -1
- package/build/dex/uniswap-v4/encoder.d.ts +6 -5
- package/build/dex/uniswap-v4/encoder.js +121 -30
- package/build/dex/uniswap-v4/encoder.js.map +1 -1
- package/build/dex/uniswap-v4/uniswap-v4-pool-manager.js +31 -2
- package/build/dex/uniswap-v4/uniswap-v4-pool-manager.js.map +1 -1
- package/build/dex/uniswap-v4/uniswap-v4.js +25 -22
- package/build/dex/uniswap-v4/uniswap-v4.js.map +1 -1
- package/build/executor/Executor02BytecodeBuilder.d.ts +6 -0
- package/build/executor/Executor02BytecodeBuilder.js +104 -15
- package/build/executor/Executor02BytecodeBuilder.js.map +1 -1
- package/build/generic-swap-transaction-builder.d.ts +3 -2
- package/build/generic-swap-transaction-builder.js +27 -2
- package/build/generic-swap-transaction-builder.js.map +1 -1
- package/package.json +1 -1
- package/build/abi/algebra-integral/Quoter.abi.json +0 -231
- package/build/abi/algebra-integral/SwapRouter.abi.json +0 -810
- package/build/dex/algebra-integral/algebra-integral.d.ts +0 -43
- package/build/dex/algebra-integral/algebra-integral.js +0 -446
- package/build/dex/algebra-integral/algebra-integral.js.map +0 -1
- package/build/dex/algebra-integral/config.d.ts +0 -3
- package/build/dex/algebra-integral/config.js +0 -17
- package/build/dex/algebra-integral/config.js.map +0 -1
- package/build/dex/algebra-integral/types.d.ts +0 -33
- package/build/dex/algebra-integral/types.js +0 -10
- package/build/dex/algebra-integral/types.js.map +0 -1
- package/build/dex/miro-migrator/miro-migrator-state.d.ts +0 -27
- package/build/dex/miro-migrator/miro-migrator-state.js +0 -89
- package/build/dex/miro-migrator/miro-migrator-state.js.map +0 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const MIRO_MIGRATION_GAS_COST =
|
|
1
|
+
export declare const MIRO_MIGRATION_GAS_COST = 95000;
|
|
2
2
|
export declare const TRANSFER_TOPIC = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TRANSFER_TOPIC = exports.MIRO_MIGRATION_GAS_COST = void 0;
|
|
4
|
-
exports.MIRO_MIGRATION_GAS_COST =
|
|
4
|
+
exports.MIRO_MIGRATION_GAS_COST = 95_000;
|
|
5
5
|
exports.TRANSFER_TOPIC = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef';
|
|
6
6
|
//# sourceMappingURL=constants.js.map
|
|
@@ -4,7 +4,6 @@ import { Log, Logger } from '../../types';
|
|
|
4
4
|
import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
|
|
5
5
|
import { IDexHelper } from '../../dex-helper/idex-helper';
|
|
6
6
|
import { PoolState } from './types';
|
|
7
|
-
import { Contract } from 'ethers';
|
|
8
7
|
export declare class MiroMigratorEventPool extends StatefulEventSubscriber<PoolState> {
|
|
9
8
|
readonly parentName: string;
|
|
10
9
|
protected network: number;
|
|
@@ -12,16 +11,17 @@ export declare class MiroMigratorEventPool extends StatefulEventSubscriber<PoolS
|
|
|
12
11
|
protected migratorAddress: string;
|
|
13
12
|
protected xyzAddress: string;
|
|
14
13
|
protected transferTopic: string;
|
|
14
|
+
protected migratorInterface: Interface;
|
|
15
15
|
protected xyzInterface: Interface;
|
|
16
|
-
protected xyzContract: Contract;
|
|
17
16
|
handlers: {
|
|
18
17
|
[event: string]: (event: any, state: DeepReadonly<PoolState>, log: Readonly<Log>) => DeepReadonly<PoolState> | null;
|
|
19
18
|
};
|
|
20
19
|
logDecoder: (log: Log) => any;
|
|
21
|
-
|
|
20
|
+
addressesSubscribed: string[];
|
|
21
|
+
constructor(parentName: string, network: number, dexHelper: IDexHelper, logger: Logger, migratorAddress: string, xyzAddress: string, transferTopic: string, migratorInterface?: Interface, xyzInterface?: Interface);
|
|
22
22
|
protected processLog(state: DeepReadonly<PoolState>, log: Readonly<Log>): Promise<DeepReadonly<PoolState> | null>;
|
|
23
23
|
generateState(blockNumber?: number | 'latest'): Promise<DeepReadonly<PoolState>>;
|
|
24
24
|
getOrGenerateState(blockNumber: number): Promise<PoolState>;
|
|
25
|
-
handleTransferTo(event: any, state: DeepReadonly<PoolState>): Promise<DeepReadonly<PoolState>>;
|
|
26
|
-
handleTransferFrom(event: any, state: DeepReadonly<PoolState>): Promise<DeepReadonly<PoolState>>;
|
|
25
|
+
handleTransferTo(event: any, state: DeepReadonly<PoolState>, log: Readonly<Log>): Promise<DeepReadonly<PoolState>>;
|
|
26
|
+
handleTransferFrom(event: any, state: DeepReadonly<PoolState>, log: Readonly<Log>): Promise<DeepReadonly<PoolState>>;
|
|
27
27
|
}
|
|
@@ -7,8 +7,9 @@ exports.MiroMigratorEventPool = void 0;
|
|
|
7
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
|
+
const decoders_1 = require("../../lib/decoders");
|
|
11
|
+
const MiroMigrator_abi_json_1 = __importDefault(require("../../abi/miro-migrator/MiroMigrator.abi.json"));
|
|
10
12
|
const erc20_json_1 = __importDefault(require("../../abi/erc20.json"));
|
|
11
|
-
const ethers_1 = require("ethers");
|
|
12
13
|
class MiroMigratorEventPool extends stateful_event_subscriber_1.StatefulEventSubscriber {
|
|
13
14
|
parentName;
|
|
14
15
|
network;
|
|
@@ -16,20 +17,21 @@ class MiroMigratorEventPool extends stateful_event_subscriber_1.StatefulEventSub
|
|
|
16
17
|
migratorAddress;
|
|
17
18
|
xyzAddress;
|
|
18
19
|
transferTopic;
|
|
20
|
+
migratorInterface;
|
|
19
21
|
xyzInterface;
|
|
20
|
-
xyzContract;
|
|
21
22
|
handlers = {};
|
|
22
23
|
logDecoder;
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
addressesSubscribed;
|
|
25
|
+
constructor(parentName, network, dexHelper, logger, migratorAddress, xyzAddress, transferTopic, migratorInterface = new abi_1.Interface(MiroMigrator_abi_json_1.default), xyzInterface = new abi_1.Interface(erc20_json_1.default)) {
|
|
26
|
+
super(parentName, 'state', dexHelper, logger);
|
|
25
27
|
this.parentName = parentName;
|
|
26
28
|
this.network = network;
|
|
27
29
|
this.dexHelper = dexHelper;
|
|
28
30
|
this.migratorAddress = migratorAddress;
|
|
29
31
|
this.xyzAddress = xyzAddress;
|
|
30
32
|
this.transferTopic = transferTopic;
|
|
33
|
+
this.migratorInterface = migratorInterface;
|
|
31
34
|
this.xyzInterface = xyzInterface;
|
|
32
|
-
this.xyzContract = xyzContract;
|
|
33
35
|
this.logDecoder = (log) => this.xyzInterface.parseLog(log);
|
|
34
36
|
this.addressesSubscribed = [xyzAddress];
|
|
35
37
|
}
|
|
@@ -38,11 +40,11 @@ class MiroMigratorEventPool extends stateful_event_subscriber_1.StatefulEventSub
|
|
|
38
40
|
const event = this.logDecoder(log);
|
|
39
41
|
if (log.topics[0] === this.transferTopic &&
|
|
40
42
|
event.args.dst.toLowerCase() === this.migratorAddress.toLowerCase()) {
|
|
41
|
-
return this.handleTransferTo(event, state);
|
|
43
|
+
return this.handleTransferTo(event, state, log);
|
|
42
44
|
}
|
|
43
45
|
if (log.topics[0] === this.transferTopic &&
|
|
44
46
|
event.args.src.toLowerCase() === this.migratorAddress.toLowerCase()) {
|
|
45
|
-
return this.handleTransferFrom(event, state);
|
|
47
|
+
return this.handleTransferFrom(event, state, log);
|
|
46
48
|
}
|
|
47
49
|
return null;
|
|
48
50
|
}
|
|
@@ -52,10 +54,17 @@ class MiroMigratorEventPool extends stateful_event_subscriber_1.StatefulEventSub
|
|
|
52
54
|
}
|
|
53
55
|
}
|
|
54
56
|
async generateState(blockNumber = 'latest') {
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
const calls = [
|
|
58
|
+
{
|
|
59
|
+
target: this.xyzAddress,
|
|
60
|
+
callData: this.xyzInterface.encodeFunctionData('balanceOf', [
|
|
61
|
+
this.migratorAddress,
|
|
62
|
+
]),
|
|
63
|
+
decodeFunction: decoders_1.uint256ToBigInt,
|
|
64
|
+
},
|
|
65
|
+
];
|
|
66
|
+
const [balance] = await this.dexHelper.multiWrapper.tryAggregate(true, calls, blockNumber);
|
|
67
|
+
return { balance: balance.returnData };
|
|
59
68
|
}
|
|
60
69
|
async getOrGenerateState(blockNumber) {
|
|
61
70
|
let state = this.getState(blockNumber);
|
|
@@ -65,12 +74,12 @@ class MiroMigratorEventPool extends stateful_event_subscriber_1.StatefulEventSub
|
|
|
65
74
|
}
|
|
66
75
|
return state;
|
|
67
76
|
}
|
|
68
|
-
async handleTransferTo(event, state) {
|
|
77
|
+
async handleTransferTo(event, state, log) {
|
|
69
78
|
return {
|
|
70
79
|
balance: state.balance + BigInt(event.args.wad),
|
|
71
80
|
};
|
|
72
81
|
}
|
|
73
|
-
async handleTransferFrom(event, state) {
|
|
82
|
+
async handleTransferFrom(event, state, log) {
|
|
74
83
|
return {
|
|
75
84
|
balance: state.balance - BigInt(event.args.wad),
|
|
76
85
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"miro-migrator-pool.js","sourceRoot":"","sources":["../../../src/dex/miro-migrator/miro-migrator-pool.ts"],"names":[],"mappings":";;;;;;AAAA,4CAA+C;AAG/C,uCAAiD;AACjD,+EAA0E;AAG1E,
|
|
1
|
+
{"version":3,"file":"miro-migrator-pool.js","sourceRoot":"","sources":["../../../src/dex/miro-migrator/miro-migrator-pool.ts"],"names":[],"mappings":";;;;;;AAAA,4CAA+C;AAG/C,uCAAiD;AACjD,+EAA0E;AAG1E,iDAAqD;AACrD,0GAA4E;AAC5E,sEAA4C;AAE5C,MAAa,qBAAsB,SAAQ,mDAAkC;IAchE;IACC;IACA;IAEA;IACA;IACA;IACA;IACA;IArBZ,QAAQ,GAMJ,EAAE,CAAC;IAEP,UAAU,CAAoB;IAE9B,mBAAmB,CAAW;IAE9B,YACW,UAAkB,EACjB,OAAe,EACf,SAAqB,EAC/B,MAAc,EACJ,eAAuB,EACvB,UAAkB,EAClB,aAAqB,EACrB,oBAA+B,IAAI,eAAS,CAAC,+BAAe,CAAC,EAC7D,eAA0B,IAAI,eAAS,CAAC,oBAAQ,CAAC;QAE3D,KAAK,CAAC,UAAU,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;QAVrC,eAAU,GAAV,UAAU,CAAQ;QACjB,YAAO,GAAP,OAAO,CAAQ;QACf,cAAS,GAAT,SAAS,CAAY;QAErB,oBAAe,GAAf,eAAe,CAAQ;QACvB,eAAU,GAAV,UAAU,CAAQ;QAClB,kBAAa,GAAb,aAAa,CAAQ;QACrB,sBAAiB,GAAjB,iBAAiB,CAA4C;QAC7D,iBAAY,GAAZ,YAAY,CAAqC;QAG3D,IAAI,CAAC,UAAU,GAAG,CAAC,GAAQ,EAAE,EAAE,CAAC,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QAChE,IAAI,CAAC,mBAAmB,GAAG,CAAC,UAAU,CAAC,CAAC;IAC1C,CAAC;IAES,KAAK,CAAC,UAAU,CACxB,KAA8B,EAC9B,GAAkB;QAElB,IAAI,CAAC;YACH,MAAM,KA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ACzC,CAAC;IAED,KAAK,CAAC,kBAAkB,CAAC,WAAmB;QAC1C,IAAI,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;QACvC,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,KAAK,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,WAAW,CAAC,CAAC;YAC9C,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;QACpC,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,KAAK,CAAC,gBAAgB,CACpB,KAAU,EACV,KAA8B,EAC9B,GAAkB;QAElB,OAAO;YACL,OAAO,EAAE,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC;SAChD,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,kBAAkB,CACtB,KAAU,EACV,KAA8B,EAC9B,GAAkB;QAElB,OAAO;YACL,OAAO,EAAE,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC;SAChD,CAAC;IACJ,CAAC;CACF;AA3GD,sDA2GC"}
|
|
@@ -126,10 +126,7 @@ class MiroMigrator extends simple_exchange_1.SimpleExchange {
|
|
|
126
126
|
];
|
|
127
127
|
}
|
|
128
128
|
getCalldataGasCost(poolPrices) {
|
|
129
|
-
return
|
|
130
|
-
CALLDATA_GAS_COST.AMOUNT +
|
|
131
|
-
CALLDATA_GAS_COST.OFFSET_SMALL +
|
|
132
|
-
CALLDATA_GAS_COST.ZERO);
|
|
129
|
+
return CALLDATA_GAS_COST.DEX_NO_PAYLOAD;
|
|
133
130
|
}
|
|
134
131
|
getAdapterParam(srcToken, destToken, srcAmount, destAmount, data, side) {
|
|
135
132
|
return {
|
|
@@ -149,6 +146,7 @@ class MiroMigrator extends simple_exchange_1.SimpleExchange {
|
|
|
149
146
|
dexFuncHasRecipient: false,
|
|
150
147
|
exchangeData: swapData,
|
|
151
148
|
targetExchange: this.migratorAddress,
|
|
149
|
+
returnAmountPos: undefined,
|
|
152
150
|
};
|
|
153
151
|
}
|
|
154
152
|
async getTopPoolsForToken(tokenAddress, limit) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"miro-migrator.js","sourceRoot":"","sources":["../../../src/dex/miro-migrator/miro-migrator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"miro-migrator.js","sourceRoot":"","sources":["../../../src/dex/miro-migrator/miro-migrator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAcA,2EAA6D;AAC7D,uCAAoD;AAGpD,mCAAkE;AAClE,wDAAoD;AACpD,qCAA8C;AAC9C,6DAAiD;AACjD,4CAA+C;AAC/C,0GAA4E;AAC5E,2CAAsE;AACtE,6DAA6D;AAE7D,MAAa,YACX,SAAQ,gCAAc;IAcX;IACA;IACA;IACA;IAEA;IAEA;IAEC;IACA;IArBH,4BAA4B,GAAG,IAAI,CAAC;IAEpC,wBAAwB,GAAG,KAAK,CAAC;IAEnC,MAAM,CAAC,kBAAkB,GAC9B,IAAA,6BAAqB,EAAC,2BAAkB,CAAC,CAAC;IAC5B,SAAS,CAAwB;IAEjD,MAAM,CAAS;IAEf,YACW,OAAgB,EAChB,MAAc,EACd,SAAqB,EACrB,kBAA0B,2BAAkB,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC;SACnE,eAAe,EACT,kBAA0B,2BAAkB,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC;SACnE,eAAe,EACT,kBAA0B,2BAAkB,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC;SACnE,eAAe,EACR,YAAY,0BAAO,CAAC,EAAE,CAAC,EACvB,oBAAoB,IAAI,eAAS,CAAC,+BAAe,CAAC;QAE5D,KAAK,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QAZhB,Y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OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC;4BAC/B,CAAC,CAAC,IAAI,CAAC,eAAe;4BACtB,CAAC,CAAC,IAAI,CAAC,eAAe;wBACxB,QAAQ,EAAE,EAAE;qBACb;iBACF;gBACD,YAAY,EAAE,UAAU,EAAE,4DAA4D;aACvF;SACF,CAAC;IACJ,CAAC;;AAvMH,oCAwMC"}
|
|
@@ -4,34 +4,39 @@ import { Address, PoolLiquidity, Token } from '../../types';
|
|
|
4
4
|
import { UniswapV2 } from './uniswap-v2';
|
|
5
5
|
type CachedPool = {
|
|
6
6
|
address: Address;
|
|
7
|
+
updatedAt: number;
|
|
7
8
|
token0: Token;
|
|
8
9
|
token1: Token;
|
|
9
10
|
};
|
|
10
11
|
type Pool = {
|
|
11
12
|
address: Address;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
token0Address: Address;
|
|
14
|
+
token0Decimals: number;
|
|
15
|
+
token1Address: Address;
|
|
16
|
+
token1Decimals: number;
|
|
17
|
+
reserve0: bigint;
|
|
18
|
+
reserve1: bigint;
|
|
19
|
+
reservesUpdatedAt: number | null;
|
|
20
|
+
updatedAt: number;
|
|
19
21
|
};
|
|
20
22
|
export declare class UniswapV2RpcPoolTracker extends UniswapV2 {
|
|
21
23
|
protected network: Network;
|
|
22
24
|
protected dexHelper: IDexHelper;
|
|
23
25
|
protected isDynamicFees: boolean;
|
|
24
26
|
private cacheKey;
|
|
27
|
+
protected allPoolsLength: number;
|
|
25
28
|
pools: Record<string, Pool>;
|
|
26
29
|
readonly isStatePollingDex = true;
|
|
27
|
-
private
|
|
30
|
+
private newPoolsUpdateInterval;
|
|
31
|
+
private poolsAgeUpdateInterval;
|
|
28
32
|
constructor(network: Network, dexKey: string, dexHelper: IDexHelper, isDynamicFees?: boolean);
|
|
29
33
|
initializePricing(): Promise<void>;
|
|
30
34
|
updatePools(initialize?: boolean): Promise<void>;
|
|
31
35
|
initPools(fromIndex: number, toIndex: number): Promise<void>;
|
|
32
36
|
getCachedPools(fromIndex: number, toIndex: number): Promise<void>;
|
|
33
37
|
updatePoolState(): Promise<void>;
|
|
34
|
-
|
|
38
|
+
updatePoolsAge(): Promise<void>;
|
|
39
|
+
getOnChainPoolsLength(): Promise<number>;
|
|
35
40
|
fetchPools(fromIndex: number, toIndex: number): Promise<Record<string, CachedPool>>;
|
|
36
41
|
updatePoolsReserves(pools: Pool[]): Promise<void>;
|
|
37
42
|
getTopPoolsForToken(tokenAddress: Address, limit: number): Promise<PoolLiquidity[]>;
|
|
@@ -5,9 +5,14 @@ const abi_1 = require("@ethersproject/abi");
|
|
|
5
5
|
const constants_1 = require("../../constants");
|
|
6
6
|
const decoders_1 = require("../../lib/decoders");
|
|
7
7
|
const uniswap_v2_1 = require("./uniswap-v2");
|
|
8
|
-
|
|
9
|
-
const
|
|
8
|
+
// also used to check if reserves should be updated
|
|
9
|
+
const UPDATE_NEW_POOLS_INTERVAL = 10 * 60 * 1000; // 10 minutes
|
|
10
|
+
const UPDATE_POOLS_AGE_INTERVAL = 24 * 60 * 60 * 1000; // 24 hours
|
|
11
|
+
const WRITE_BATCH_SIZE = 1_000;
|
|
10
12
|
const READ_BATCH_SIZE = 10_000;
|
|
13
|
+
// pool is valid if last update was less than 180 days ago, otherwise pool is considered stale and will not be used on pt
|
|
14
|
+
const VALID_POOLS_AGE = 1000 * 60 * 60 * 24 * 180; // 180 days
|
|
15
|
+
const MAX_RESERVES_POOLS_UPDATE = 5_000;
|
|
11
16
|
const FactoryABI = [
|
|
12
17
|
{
|
|
13
18
|
constant: true,
|
|
@@ -104,28 +109,44 @@ class UniswapV2RpcPoolTracker extends uniswap_v2_1.UniswapV2 {
|
|
|
104
109
|
dexHelper;
|
|
105
110
|
isDynamicFees;
|
|
106
111
|
cacheKey;
|
|
112
|
+
allPoolsLength = 0;
|
|
107
113
|
pools = {};
|
|
108
114
|
isStatePollingDex = true;
|
|
109
|
-
|
|
115
|
+
newPoolsUpdateInterval = null;
|
|
116
|
+
poolsAgeUpdateInterval = null;
|
|
110
117
|
constructor(network, dexKey, dexHelper, isDynamicFees = false) {
|
|
111
118
|
super(network, dexKey, dexHelper, isDynamicFees);
|
|
112
119
|
this.network = network;
|
|
113
120
|
this.dexHelper = dexHelper;
|
|
114
121
|
this.isDynamicFees = isDynamicFees;
|
|
115
122
|
this.cacheKey =
|
|
116
|
-
`${constants_1.CACHE_PREFIX}_${this.
|
|
123
|
+
`${constants_1.CACHE_PREFIX}_${this.network}_${this.dexKey}_pools`.toLowerCase();
|
|
117
124
|
}
|
|
118
125
|
async initializePricing() {
|
|
119
126
|
this.logger.info(`PancakeSwapV2: Initializing pools for ${this.dexKey} on ${this.network}...`);
|
|
120
127
|
if (!this.dexHelper.config.isSlave) {
|
|
121
128
|
await this.updatePools(true);
|
|
122
|
-
this.
|
|
123
|
-
|
|
124
|
-
|
|
129
|
+
await this.updatePoolsAge();
|
|
130
|
+
this.newPoolsUpdateInterval = setInterval(async () => {
|
|
131
|
+
try {
|
|
132
|
+
await this.updatePools();
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
this.logger.error(`Error updating new pools for ${this.dexKey} on ${this.network}: ${error}`);
|
|
136
|
+
}
|
|
137
|
+
}, UPDATE_NEW_POOLS_INTERVAL);
|
|
138
|
+
this.poolsAgeUpdateInterval = setInterval(async () => {
|
|
139
|
+
try {
|
|
140
|
+
await this.updatePoolsAge();
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
this.logger.error(`Error updating pools age for ${this.dexKey} on ${this.network}: ${error}`);
|
|
144
|
+
}
|
|
145
|
+
}, UPDATE_POOLS_AGE_INTERVAL);
|
|
125
146
|
}
|
|
126
147
|
}
|
|
127
148
|
async updatePools(initialize = false) {
|
|
128
|
-
const allPools = await this.
|
|
149
|
+
const allPools = await this.getOnChainPoolsLength();
|
|
129
150
|
const allCachedPools = await this.dexHelper.cache.hlen(this.cacheKey);
|
|
130
151
|
const missingPools = allPools - allCachedPools;
|
|
131
152
|
if (!initialize && missingPools > 1_000) {
|
|
@@ -137,9 +158,9 @@ class UniswapV2RpcPoolTracker extends uniswap_v2_1.UniswapV2 {
|
|
|
137
158
|
}
|
|
138
159
|
async initPools(fromIndex, toIndex) {
|
|
139
160
|
this.logger.info(`Initializing pools from ${fromIndex} to ${toIndex}...`);
|
|
140
|
-
for (let i = fromIndex; i < toIndex; i +=
|
|
141
|
-
this.logger.info(`Fetching pools from ${i} to ${Math.min(i +
|
|
142
|
-
const fetchedPools = await this.fetchPools(i, Math.min(i +
|
|
161
|
+
for (let i = fromIndex; i < toIndex; i += WRITE_BATCH_SIZE) {
|
|
162
|
+
this.logger.info(`Fetching pools from ${i} to ${Math.min(i + WRITE_BATCH_SIZE, toIndex)}`);
|
|
163
|
+
const fetchedPools = await this.fetchPools(i, Math.min(i + WRITE_BATCH_SIZE, toIndex));
|
|
143
164
|
const pools = Object.fromEntries(Object.entries(fetchedPools).map(([key, value]) => [
|
|
144
165
|
key,
|
|
145
166
|
JSON.stringify(value),
|
|
@@ -157,30 +178,92 @@ class UniswapV2RpcPoolTracker extends uniswap_v2_1.UniswapV2 {
|
|
|
157
178
|
}
|
|
158
179
|
this.logger.info(`Getting cached pools from ${batchStart} to ${batchEnd}`);
|
|
159
180
|
const pools = await this.dexHelper.cache.hmget(this.cacheKey, keys);
|
|
181
|
+
const minValidUpdatedAt = Date.now() - VALID_POOLS_AGE;
|
|
160
182
|
pools.forEach((pool, idx) => {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
183
|
+
if (pool) {
|
|
184
|
+
const index = batchStart + idx;
|
|
185
|
+
const parsedPool = JSON.parse(pool);
|
|
186
|
+
if (parsedPool && parsedPool.updatedAt > minValidUpdatedAt) {
|
|
187
|
+
this.pools[index] = {
|
|
188
|
+
address: parsedPool.address,
|
|
189
|
+
token0Address: parsedPool.token0.address,
|
|
190
|
+
token0Decimals: parsedPool.token0.decimals,
|
|
191
|
+
token1Address: parsedPool.token1.address,
|
|
192
|
+
token1Decimals: parsedPool.token1.decimals,
|
|
193
|
+
updatedAt: parsedPool.updatedAt,
|
|
166
194
|
reserve0: 0n,
|
|
167
195
|
reserve1: 0n,
|
|
168
|
-
|
|
169
|
-
}
|
|
196
|
+
reservesUpdatedAt: null,
|
|
197
|
+
};
|
|
170
198
|
}
|
|
171
|
-
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
this.logger.warn(`Pool with index ${batchStart + idx} not found in cache`);
|
|
202
|
+
}
|
|
203
|
+
this.allPoolsLength++;
|
|
172
204
|
});
|
|
173
205
|
}
|
|
174
206
|
}
|
|
175
207
|
async updatePoolState() {
|
|
176
208
|
await this.updatePools();
|
|
177
|
-
const allPools =
|
|
209
|
+
const allPools = this.allPoolsLength;
|
|
178
210
|
const allCachedPools = await this.dexHelper.cache.hlen(this.cacheKey);
|
|
179
211
|
if (allPools < allCachedPools) {
|
|
180
212
|
await this.getCachedPools(allPools, allCachedPools);
|
|
181
213
|
}
|
|
182
214
|
}
|
|
183
|
-
async
|
|
215
|
+
async updatePoolsAge() {
|
|
216
|
+
this.logger.info(`Starting update pools age for ${this.dexKey} on ${this.network}...`);
|
|
217
|
+
const allPools = this.allPoolsLength;
|
|
218
|
+
const allCachedPools = await this.dexHelper.cache.hlen(this.cacheKey);
|
|
219
|
+
if (allPools < allCachedPools) {
|
|
220
|
+
await this.getCachedPools(allPools, allCachedPools);
|
|
221
|
+
}
|
|
222
|
+
const poolsIndexes = Object.keys(this.pools);
|
|
223
|
+
this.logger.info(`Updating pools age for ${poolsIndexes.length} pools on ${this.network}...`);
|
|
224
|
+
// batch poolsCalldata and process in chunks to avoid huge amounts of multi-calls
|
|
225
|
+
for (let batchStart = 0; batchStart < poolsIndexes.length; batchStart += WRITE_BATCH_SIZE) {
|
|
226
|
+
const batchEnd = Math.min(batchStart + WRITE_BATCH_SIZE, poolsIndexes.length);
|
|
227
|
+
const batchIndexes = poolsIndexes.slice(batchStart, batchEnd);
|
|
228
|
+
this.logger.info(`Updating pools age for ${batchStart}-${batchEnd} pools on ${this.network}...`);
|
|
229
|
+
const batchCalldata = batchIndexes.map(poolIndex => ({
|
|
230
|
+
target: this.pools[poolIndex].address,
|
|
231
|
+
callData: factoryIface.encodeFunctionData('getReserves', []),
|
|
232
|
+
decodeFunction: (result) => {
|
|
233
|
+
return (0, decoders_1.generalDecoder)(result, ['uint112', 'uint112', 'uint32'], 0, res => Number(res[2]));
|
|
234
|
+
},
|
|
235
|
+
}));
|
|
236
|
+
const batchPoolsData = await this.dexHelper.multiWrapper.tryAggregate(true, batchCalldata, undefined,
|
|
237
|
+
// as calls are small, should be affordable to use a larger batch size
|
|
238
|
+
WRITE_BATCH_SIZE);
|
|
239
|
+
const poolsToUpdate = {};
|
|
240
|
+
for (let i = 0; i < batchPoolsData.length; i++) {
|
|
241
|
+
const poolIndex = batchIndexes[i];
|
|
242
|
+
const updatedAt = batchPoolsData[i].returnData * 1000;
|
|
243
|
+
if (updatedAt !== this.pools[poolIndex].updatedAt) {
|
|
244
|
+
this.pools[poolIndex].updatedAt = updatedAt;
|
|
245
|
+
const pool = {
|
|
246
|
+
address: this.pools[poolIndex].address,
|
|
247
|
+
updatedAt,
|
|
248
|
+
token0: {
|
|
249
|
+
address: this.pools[poolIndex].token0Address,
|
|
250
|
+
decimals: this.pools[poolIndex].token0Decimals,
|
|
251
|
+
},
|
|
252
|
+
token1: {
|
|
253
|
+
address: this.pools[poolIndex].token1Address,
|
|
254
|
+
decimals: this.pools[poolIndex].token1Decimals,
|
|
255
|
+
},
|
|
256
|
+
};
|
|
257
|
+
poolsToUpdate[poolIndex] = JSON.stringify(pool);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (Object.keys(poolsToUpdate).length > 0) {
|
|
261
|
+
await this.dexHelper.cache.hmset(this.cacheKey, poolsToUpdate);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
this.logger.info(`Finished update pools age for ${poolsIndexes.length} pools on ${this.network}.`);
|
|
265
|
+
}
|
|
266
|
+
async getOnChainPoolsLength() {
|
|
184
267
|
const allPoolsCallData = {
|
|
185
268
|
target: this.factoryAddress,
|
|
186
269
|
callData: factoryIface.encodeFunctionData('allPairsLength', []),
|
|
@@ -214,13 +297,19 @@ class UniswapV2RpcPoolTracker extends uniswap_v2_1.UniswapV2 {
|
|
|
214
297
|
target: poolResult.returnData,
|
|
215
298
|
callData: factoryIface.encodeFunctionData('token1', []),
|
|
216
299
|
decodeFunction: decoders_1.addressDecode,
|
|
300
|
+
}, {
|
|
301
|
+
target: poolResult.returnData,
|
|
302
|
+
callData: factoryIface.encodeFunctionData('getReserves', []),
|
|
303
|
+
decodeFunction: (result) => {
|
|
304
|
+
return (0, decoders_1.generalDecoder)(result, ['uint112', 'uint112', 'uint32'], 0, res => Number(res[2]));
|
|
305
|
+
},
|
|
217
306
|
});
|
|
218
307
|
}
|
|
219
308
|
const poolsData = await this.dexHelper.multiWrapper.tryAggregate(true, poolsCalldata);
|
|
220
309
|
const tokensSet = new Set();
|
|
221
310
|
for (let i = 0; i < allPoolsResults.length; i++) {
|
|
222
|
-
const token0 = poolsData[i *
|
|
223
|
-
const token1 = poolsData[i *
|
|
311
|
+
const token0 = poolsData[i * 3].returnData.toLowerCase();
|
|
312
|
+
const token1 = poolsData[i * 3 + 1].returnData.toLowerCase();
|
|
224
313
|
tokensSet.add(token0.toLowerCase());
|
|
225
314
|
tokensSet.add(token1.toLowerCase());
|
|
226
315
|
}
|
|
@@ -242,11 +331,12 @@ class UniswapV2RpcPoolTracker extends uniswap_v2_1.UniswapV2 {
|
|
|
242
331
|
const pools = {};
|
|
243
332
|
for (let i = 0; i < allPoolsResults.length; i++) {
|
|
244
333
|
const poolAddress = allPoolsResults[i].returnData.toLowerCase();
|
|
245
|
-
const token0 = poolsData[i *
|
|
246
|
-
const token1 = poolsData[i *
|
|
247
|
-
|
|
334
|
+
const token0 = poolsData[i * 3].returnData.toLowerCase();
|
|
335
|
+
const token1 = poolsData[i * 3 + 1].returnData.toLowerCase();
|
|
336
|
+
const updatedAt = poolsData[i * 3 + 2].returnData;
|
|
248
337
|
pools[i + fromIndex] = {
|
|
249
338
|
address: poolAddress,
|
|
339
|
+
updatedAt: updatedAt * 1000,
|
|
250
340
|
token0: {
|
|
251
341
|
address: token0,
|
|
252
342
|
decimals: decimals[token0],
|
|
@@ -266,50 +356,60 @@ class UniswapV2RpcPoolTracker extends uniswap_v2_1.UniswapV2 {
|
|
|
266
356
|
target: pool.address,
|
|
267
357
|
callData: factoryIface.encodeFunctionData('getReserves', []),
|
|
268
358
|
decodeFunction: (result) => {
|
|
269
|
-
return (0, decoders_1.generalDecoder)(result, ['uint112', 'uint112'], [0n, 0n], res => [BigInt(res[0]), BigInt(res[1])]);
|
|
359
|
+
return (0, decoders_1.generalDecoder)(result, ['uint112', 'uint112', 'uint32'], [0n, 0n, 0], res => [BigInt(res[0]), BigInt(res[1]), Number(res[2])]);
|
|
270
360
|
},
|
|
271
361
|
});
|
|
272
362
|
}
|
|
273
363
|
const results = await this.dexHelper.multiWrapper.tryAggregate(true, callData);
|
|
274
364
|
for (let i = 0; i < pools.length; i++) {
|
|
275
|
-
const [reserve0, reserve1] = results[i].returnData;
|
|
365
|
+
const [reserve0, reserve1, updatedAt] = results[i].returnData;
|
|
276
366
|
const pool = pools[i];
|
|
277
|
-
pool.
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
};
|
|
367
|
+
pool.updatedAt = updatedAt * 1000; // convert to ms
|
|
368
|
+
pool.reserve0 = reserve0;
|
|
369
|
+
pool.reserve1 = reserve1;
|
|
370
|
+
pool.reservesUpdatedAt = Date.now();
|
|
282
371
|
}
|
|
283
372
|
}
|
|
284
373
|
async getTopPoolsForToken(tokenAddress, limit) {
|
|
285
374
|
const token = tokenAddress.toLowerCase();
|
|
286
|
-
|
|
375
|
+
// sort by updateAt with the assumption that pools with good liquidity are updated more frequently
|
|
376
|
+
let pools = Object.values(this.pools)
|
|
377
|
+
.filter(pool => pool.token0Address === token || pool.token1Address === token)
|
|
378
|
+
.sort((a, b) => {
|
|
379
|
+
return b.updatedAt - a.updatedAt;
|
|
380
|
+
});
|
|
287
381
|
if (pools.length === 0) {
|
|
288
382
|
return [];
|
|
289
383
|
}
|
|
290
384
|
const now = Date.now();
|
|
291
|
-
const poolsToUpdate = pools
|
|
292
|
-
|
|
385
|
+
const poolsToUpdate = pools
|
|
386
|
+
.filter(pool => !pool.reservesUpdatedAt ||
|
|
387
|
+
now - pool.reservesUpdatedAt > UPDATE_NEW_POOLS_INTERVAL)
|
|
388
|
+
.slice(0, MAX_RESERVES_POOLS_UPDATE);
|
|
293
389
|
if (poolsToUpdate.length > 0) {
|
|
294
|
-
|
|
390
|
+
try {
|
|
391
|
+
this.logger.info(`Started updating reserves for ${poolsToUpdate.length} pools for token ${token} on ${this.network}...`);
|
|
392
|
+
await this.updatePoolsReserves(poolsToUpdate);
|
|
393
|
+
this.logger.info(`Finished updating reserves for ${poolsToUpdate.length} pools for token ${token} on ${this.network}...`);
|
|
394
|
+
}
|
|
395
|
+
catch (error) {
|
|
396
|
+
this.logger.error(`Error updating reserves for pools for token ${token} on ${this.network}: ${error}`);
|
|
397
|
+
return [];
|
|
398
|
+
}
|
|
295
399
|
}
|
|
296
400
|
pools = pools
|
|
297
401
|
.sort((a, b) => {
|
|
298
|
-
const aReserve = token === a.
|
|
299
|
-
|
|
300
|
-
: a.reserves.reserve1;
|
|
301
|
-
const bReserve = token === b.token0.address
|
|
302
|
-
? b.reserves.reserve0
|
|
303
|
-
: b.reserves.reserve1;
|
|
402
|
+
const aReserve = token === a.token0Address ? a.reserve0 : a.reserve1;
|
|
403
|
+
const bReserve = token === b.token0Address ? b.reserve0 : b.reserve1;
|
|
304
404
|
return Number(bReserve - aReserve);
|
|
305
405
|
})
|
|
306
406
|
.slice(0, limit);
|
|
307
407
|
const tokensAmounts = pools
|
|
308
408
|
.map(pool => {
|
|
309
|
-
const token0 = pool.
|
|
310
|
-
const token1 = pool.
|
|
311
|
-
const reserve0 = pool.
|
|
312
|
-
const reserve1 = pool.
|
|
409
|
+
const token0 = pool.token0Address;
|
|
410
|
+
const token1 = pool.token1Address;
|
|
411
|
+
const reserve0 = pool.reserve0;
|
|
412
|
+
const reserve1 = pool.reserve1;
|
|
313
413
|
return [
|
|
314
414
|
[token0, reserve0],
|
|
315
415
|
[token1, reserve1],
|
|
@@ -318,7 +418,15 @@ class UniswapV2RpcPoolTracker extends uniswap_v2_1.UniswapV2 {
|
|
|
318
418
|
.flat();
|
|
319
419
|
const usdTokenAmounts = await this.dexHelper.getUsdTokenAmounts(tokensAmounts);
|
|
320
420
|
const poolsWithLiquidity = pools.map((pool, i) => {
|
|
321
|
-
const connectorToken = token === pool.
|
|
421
|
+
const connectorToken = token === pool.token0Address
|
|
422
|
+
? {
|
|
423
|
+
address: pool.token1Address,
|
|
424
|
+
decimals: pool.token1Decimals,
|
|
425
|
+
}
|
|
426
|
+
: {
|
|
427
|
+
address: pool.token0Address,
|
|
428
|
+
decimals: pool.token0Decimals,
|
|
429
|
+
};
|
|
322
430
|
let token0ReserveUSD = usdTokenAmounts[i * 2];
|
|
323
431
|
let token1ReserveUSD = usdTokenAmounts[i * 2 + 1];
|
|
324
432
|
// fallback to non-empty usd reserves
|
|
@@ -340,9 +448,13 @@ class UniswapV2RpcPoolTracker extends uniswap_v2_1.UniswapV2 {
|
|
|
340
448
|
.slice(0, limit);
|
|
341
449
|
}
|
|
342
450
|
releaseResources() {
|
|
343
|
-
if (this.
|
|
344
|
-
clearInterval(this.
|
|
345
|
-
this.
|
|
451
|
+
if (this.newPoolsUpdateInterval) {
|
|
452
|
+
clearInterval(this.newPoolsUpdateInterval);
|
|
453
|
+
this.newPoolsUpdateInterval = null;
|
|
454
|
+
}
|
|
455
|
+
if (this.poolsAgeUpdateInterval) {
|
|
456
|
+
clearInterval(this.poolsAgeUpdateInterval);
|
|
457
|
+
this.poolsAgeUpdateInterval = null;
|
|
346
458
|
}
|
|
347
459
|
}
|
|
348
460
|
}
|