@paraswap/dex-lib 2.16.0 → 2.16.1-generate-state-latest.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/composed-event-subscriber.d.ts +2 -2
- package/build/composed-event-subscriber.js +9 -4
- package/build/composed-event-subscriber.js.map +1 -1
- package/build/dex/aave-v1/aave-v1.d.ts +1 -1
- package/build/dex/aave-v1/aave-v1.js +1 -1
- package/build/dex/aave-v1/aave-v1.js.map +1 -1
- package/build/dex/aave-v2/aave-v2.d.ts +1 -1
- package/build/dex/aave-v2/aave-v2.js +1 -1
- package/build/dex/aave-v2/aave-v2.js.map +1 -1
- package/build/dex/aave-v3/aave-v3.d.ts +1 -1
- package/build/dex/aave-v3/aave-v3.js +1 -1
- package/build/dex/aave-v3/aave-v3.js.map +1 -1
- package/build/dex/aave-v3/tokens/arbitrum.json +50 -0
- package/build/dex/aave-v3/tokens/avalanche.json +74 -0
- package/build/dex/aave-v3/tokens/fantom.json +62 -0
- package/build/dex/aave-v3/tokens/optimism.json +50 -0
- package/build/dex/aave-v3/tokens/polygon.json +116 -0
- package/build/dex/aave-v3/utils.d.ts +1 -1
- package/build/dex/aave-v3/utils.js.map +1 -1
- package/build/dex/balancer-v1/balancer-v1-pool.d.ts +2 -2
- package/build/dex/balancer-v1/balancer-v1-pool.js +5 -1
- package/build/dex/balancer-v1/balancer-v1-pool.js.map +1 -1
- package/build/dex/balancer-v1/balancer-v1.d.ts +1 -1
- package/build/dex/balancer-v1/balancer-v1.js +7 -4
- package/build/dex/balancer-v1/balancer-v1.js.map +1 -1
- package/build/dex/balancer-v1/utils.d.ts +3 -1
- package/build/dex/balancer-v1/utils.js +18 -5
- package/build/dex/balancer-v1/utils.js.map +1 -1
- package/build/dex/balancer-v2/balancer-v2.d.ts +10 -5
- package/build/dex/balancer-v2/balancer-v2.js +15 -10
- package/build/dex/balancer-v2/balancer-v2.js.map +1 -1
- package/build/dex/camelot/camelot.d.ts +2 -2
- package/build/dex/camelot/camelot.js +15 -6
- package/build/dex/camelot/camelot.js.map +1 -1
- package/build/dex/curve-v1/curve-v1.d.ts +1 -1
- package/build/dex/curve-v1/curve-v1.js +5 -8
- package/build/dex/curve-v1/curve-v1.js.map +1 -1
- package/build/dex/curve-v1/pools/curve-metapool.d.ts +2 -2
- package/build/dex/curve-v1/pools/curve-metapool.js +12 -4
- package/build/dex/curve-v1/pools/curve-metapool.js.map +1 -1
- package/build/dex/curve-v1/pools/curve-pool.d.ts +3 -3
- package/build/dex/curve-v1/pools/curve-pool.js +13 -4
- package/build/dex/curve-v1/pools/curve-pool.js.map +1 -1
- package/build/dex/curve-v1/pools/getstate-multicall.d.ts +6 -1
- package/build/dex/curve-v1/pools/getstate-multicall.js +32 -25
- package/build/dex/curve-v1/pools/getstate-multicall.js.map +1 -1
- package/build/dex/curve-v1-factory/curve-v1-factory.d.ts +3 -3
- package/build/dex/curve-v1-factory/curve-v1-factory.js +4 -4
- package/build/dex/curve-v1-factory/curve-v1-factory.js.map +1 -1
- package/build/dex/curve-v1-factory/curve-v1-pool-manager.d.ts +1 -1
- package/build/dex/curve-v1-factory/curve-v1-pool-manager.js +1 -1
- package/build/dex/curve-v1-factory/curve-v1-pool-manager.js.map +1 -1
- package/build/dex/curve-v1-factory/state-polling-pools/custom-pool-polling.d.ts +1 -1
- package/build/dex/curve-v1-factory/state-polling-pools/custom-pool-polling.js.map +1 -1
- package/build/dex/curve-v1-factory/state-polling-pools/factory-pool-polling.d.ts +1 -1
- package/build/dex/curve-v1-factory/state-polling-pools/factory-pool-polling.js.map +1 -1
- package/build/dex/curve-v1-factory/state-polling-pools/polling-manager.d.ts +4 -4
- package/build/dex/curve-v1-factory/state-polling-pools/polling-manager.js +5 -15
- package/build/dex/curve-v1-factory/state-polling-pools/polling-manager.js.map +1 -1
- package/build/dex/curve-v1-factory/state-polling-pools/pool-polling-base.d.ts +1 -1
- package/build/dex/curve-v1-factory/types.d.ts +1 -1
- package/build/dex/generic-rfq/generic-rfq.d.ts +1 -1
- package/build/dex/generic-rfq/generic-rfq.js +1 -1
- package/build/dex/generic-rfq/generic-rfq.js.map +1 -1
- package/build/dex/generic-rfq/utils.js +1 -1
- package/build/dex/generic-rfq/utils.js.map +1 -1
- package/build/dex/gmx/gmx.d.ts +1 -1
- package/build/dex/gmx/gmx.js +1 -1
- package/build/dex/gmx/gmx.js.map +1 -1
- package/build/dex/gmx/pool.js +5 -4
- package/build/dex/gmx/pool.js.map +1 -1
- package/build/dex/idex.d.ts +1 -1
- package/build/dex/jarvis-v6/jarvis-v6-events.d.ts +4 -3
- package/build/dex/jarvis-v6/jarvis-v6-events.js +8 -4
- package/build/dex/jarvis-v6/jarvis-v6-events.js.map +1 -1
- package/build/dex/jarvis-v6/jarvis-v6.d.ts +1 -1
- package/build/dex/jarvis-v6/jarvis-v6.js +12 -8
- package/build/dex/jarvis-v6/jarvis-v6.js.map +1 -1
- package/build/dex/jarvis-v6/utils.d.ts +2 -1
- package/build/dex/jarvis-v6/utils.js +15 -6
- package/build/dex/jarvis-v6/utils.js.map +1 -1
- package/build/dex/kyberdmm/kyberdmm.js +4 -1
- package/build/dex/kyberdmm/kyberdmm.js.map +1 -1
- package/build/dex/kyberdmm/pool.d.ts +2 -2
- package/build/dex/kyberdmm/pool.js +21 -20
- package/build/dex/kyberdmm/pool.js.map +1 -1
- package/build/dex/maker-psm/maker-psm.d.ts +3 -3
- package/build/dex/maker-psm/maker-psm.js +35 -27
- package/build/dex/maker-psm/maker-psm.js.map +1 -1
- package/build/dex/maverick-v1/maverick-v1-pool.d.ts +3 -5
- package/build/dex/maverick-v1/maverick-v1-pool.js +27 -38
- package/build/dex/maverick-v1/maverick-v1-pool.js.map +1 -1
- package/build/dex/maverick-v1/maverick-v1.d.ts +2 -2
- package/build/dex/maverick-v1/maverick-v1.js +8 -8
- package/build/dex/maverick-v1/maverick-v1.js.map +1 -1
- package/build/dex/nerve/nerve-pool.d.ts +2 -2
- package/build/dex/nerve/nerve-pool.js +28 -25
- package/build/dex/nerve/nerve-pool.js.map +1 -1
- package/build/dex/nerve/nerve.d.ts +2 -2
- package/build/dex/nerve/nerve.js +5 -5
- package/build/dex/nerve/nerve.js.map +1 -1
- package/build/dex/platypus/platypus.d.ts +3 -3
- package/build/dex/platypus/platypus.js +6 -5
- package/build/dex/platypus/platypus.js.map +1 -1
- package/build/dex/swaap-v1/config.d.ts +1 -1
- package/build/dex/swaap-v1/config.js +2 -2
- package/build/dex/swaap-v1/swaap-v1-pool.d.ts +3 -3
- package/build/dex/swaap-v1/swaap-v1-pool.js +15 -9
- package/build/dex/swaap-v1/swaap-v1-pool.js.map +1 -1
- package/build/dex/swaap-v1/swaap-v1.d.ts +2 -2
- package/build/dex/swaap-v1/swaap-v1.js +11 -8
- package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
- package/build/dex/synthetix/synthetix-state.js +1 -1
- package/build/dex/synthetix/synthetix-state.js.map +1 -1
- package/build/dex/synthetix/synthetix.d.ts +1 -1
- package/build/dex/synthetix/synthetix.js +5 -4
- package/build/dex/synthetix/synthetix.js.map +1 -1
- package/build/dex/test-dex/config.d.ts +4 -0
- package/build/dex/test-dex/config.js +15 -0
- package/build/dex/test-dex/config.js.map +1 -0
- package/build/dex/test-dex/test-dex-pool.d.ts +39 -0
- package/build/dex/test-dex/test-dex-pool.js +63 -0
- package/build/dex/test-dex/test-dex-pool.js.map +1 -0
- package/build/dex/test-dex/test-dex.d.ts +37 -0
- package/build/dex/test-dex/test-dex.js +126 -0
- package/build/dex/test-dex/test-dex.js.map +1 -0
- package/build/dex/test-dex/types.d.ts +6 -0
- package/build/dex/test-dex/types.js +3 -0
- package/build/dex/test-dex/types.js.map +1 -0
- package/build/dex/uniswap-v2/uniswap-v2.d.ts +2 -2
- package/build/dex/uniswap-v2/uniswap-v2.js +15 -10
- package/build/dex/uniswap-v2/uniswap-v2.js.map +1 -1
- package/build/dex/uniswap-v3/uniswap-v3-pool.d.ts +3 -3
- package/build/dex/uniswap-v3/uniswap-v3-pool.js +33 -27
- package/build/dex/uniswap-v3/uniswap-v3-pool.js.map +1 -1
- package/build/dex/uniswap-v3/uniswap-v3.d.ts +2 -2
- package/build/dex/uniswap-v3/uniswap-v3.js +4 -3
- package/build/dex/uniswap-v3/uniswap-v3.js.map +1 -1
- package/build/dex/woo-fi-v2/woo-fi-math.d.ts +16 -0
- package/build/dex/woo-fi-v2/woo-fi-math.js +137 -0
- package/build/dex/woo-fi-v2/woo-fi-math.js.map +1 -0
- package/build/dex/woo-fi-v2/woo-fi-v2.d.ts +1 -1
- package/build/dex/woo-fi-v2/woo-fi-v2.js +1 -1
- package/build/dex/woo-fi-v2/woo-fi-v2.js.map +1 -1
- package/build/implementations/local-paraswap-sdk.js +1 -2
- package/build/implementations/local-paraswap-sdk.js.map +1 -1
- package/build/lib/generics-events-subscribers/erc20-event-subscriber.d.ts +2 -2
- package/build/lib/generics-events-subscribers/erc20-event-subscriber.js +17 -12
- package/build/lib/generics-events-subscribers/erc20-event-subscriber.js.map +1 -1
- package/build/lib/multi-wrapper.d.ts +5 -1
- package/build/lib/multi-wrapper.js +21 -2
- package/build/lib/multi-wrapper.js.map +1 -1
- package/build/lib/tokens/balancer-fetcher.d.ts +5 -1
- package/build/lib/tokens/balancer-fetcher.js +7 -9
- package/build/lib/tokens/balancer-fetcher.js.map +1 -1
- package/build/pricing-helper.d.ts +1 -1
- package/build/pricing-helper.js +1 -1
- package/build/pricing-helper.js.map +1 -1
- package/build/stateful-event-subscriber.d.ts +7 -3
- package/build/stateful-event-subscriber.js +25 -22
- package/build/stateful-event-subscriber.js.map +1 -1
- package/build/utils.d.ts +2 -2
- package/build/utils.js +1 -1
- package/build/utils.js.map +1 -1
- package/dex-template/__DexName__.ts.template +3 -1
- package/package.json +1 -1
- package/src/composed-event-subscriber.ts +24 -13
- package/src/dex/aave-v1/aave-v1.ts +1 -1
- package/src/dex/aave-v2/aave-v2.ts +1 -1
- package/src/dex/aave-v3/aave-v3.ts +3 -1
- package/src/dex/aave-v3/utils.ts +3 -3
- package/src/dex/balancer-v1/balancer-v1-pool.ts +18 -9
- package/src/dex/balancer-v1/balancer-v1.ts +13 -8
- package/src/dex/balancer-v1/utils.ts +36 -7
- package/src/dex/balancer-v2/balancer-v2.ts +42 -25
- package/src/dex/camelot/camelot.ts +29 -10
- package/src/dex/curve-v1/curve-v1.ts +11 -11
- package/src/dex/curve-v1/pools/curve-metapool.ts +23 -7
- package/src/dex/curve-v1/pools/curve-pool.ts +29 -8
- package/src/dex/curve-v1/pools/getstate-multicall.ts +83 -56
- package/src/dex/curve-v1-factory/curve-v1-factory.ts +4 -4
- package/src/dex/curve-v1-factory/curve-v1-pool-manager.ts +1 -1
- package/src/dex/curve-v1-factory/state-polling-pools/custom-pool-polling.ts +1 -1
- package/src/dex/curve-v1-factory/state-polling-pools/factory-pool-polling.ts +1 -1
- package/src/dex/curve-v1-factory/state-polling-pools/polling-manager.ts +5 -16
- package/src/dex/curve-v1-factory/state-polling-pools/pool-polling-base.ts +1 -1
- package/src/dex/curve-v1-factory/types.ts +1 -1
- package/src/dex/generic-rfq/generic-rfq.ts +3 -1
- package/src/dex/generic-rfq/utils.ts +1 -1
- package/src/dex/gmx/gmx.ts +1 -1
- package/src/dex/gmx/pool.ts +9 -4
- package/src/dex/idex.ts +1 -1
- package/src/dex/jarvis-v6/jarvis-v6-events.ts +17 -6
- package/src/dex/jarvis-v6/jarvis-v6.ts +17 -15
- package/src/dex/jarvis-v6/utils.ts +21 -9
- package/src/dex/kyberdmm/kyberdmm.ts +4 -1
- package/src/dex/kyberdmm/pool.ts +32 -24
- package/src/dex/maker-psm/maker-psm.ts +69 -47
- package/src/dex/maverick-v1/maverick-v1-pool.ts +50 -38
- package/src/dex/maverick-v1/maverick-v1.ts +12 -8
- package/src/dex/nerve/nerve-pool.ts +42 -30
- package/src/dex/nerve/nerve.ts +9 -5
- package/src/dex/platypus/platypus.ts +11 -7
- package/src/dex/swaap-v1/config.ts +1 -1
- package/src/dex/swaap-v1/swaap-v1-pool.ts +23 -14
- package/src/dex/swaap-v1/swaap-v1.ts +21 -14
- package/src/dex/synthetix/synthetix-state.ts +3 -3
- package/src/dex/synthetix/synthetix.ts +5 -4
- package/src/dex/uniswap-v2/uniswap-v2.ts +27 -13
- package/src/dex/uniswap-v3/uniswap-v3-pool.ts +43 -39
- package/src/dex/uniswap-v3/uniswap-v3.ts +5 -4
- package/src/dex/woo-fi-v2/woo-fi-v2.ts +3 -1
- package/src/implementations/local-paraswap-sdk.ts +1 -2
- package/src/lib/generics-events-subscribers/erc20-event-subscriber.ts +29 -18
- package/src/lib/multi-wrapper.ts +41 -2
- package/src/lib/tokens/balancer-fetcher.ts +23 -19
- package/src/pricing-helper.ts +5 -2
- package/src/stateful-event-subscriber.ts +35 -28
- package/src/utils.ts +3 -4
- package/tests/utils.test.ts +44 -0
|
@@ -93,22 +93,21 @@ export class JarvisV6
|
|
|
93
93
|
// pricing service. It is intended to setup the integration
|
|
94
94
|
// for pricing requests. It is optional for a DEX to
|
|
95
95
|
// implement this function
|
|
96
|
-
async initializePricing(blockNumber: number) {
|
|
97
|
-
const poolStates =
|
|
98
|
-
this.dexHelper,
|
|
99
|
-
this.poolConfigs,
|
|
100
|
-
blockNumber,
|
|
101
|
-
{
|
|
96
|
+
async initializePricing(blockNumber: number | 'latest' = 'latest') {
|
|
97
|
+
const { blockNumber: _blockNumber, state: poolStates } =
|
|
98
|
+
await getOnChainState(this.dexHelper, this.poolConfigs, blockNumber, {
|
|
102
99
|
poolInterface: this.poolInterface,
|
|
103
100
|
priceFeedContract: this.priceFeedContract,
|
|
104
|
-
}
|
|
105
|
-
);
|
|
101
|
+
});
|
|
106
102
|
|
|
107
103
|
await Promise.all(
|
|
108
104
|
this.poolConfigs.map(async (pool, index) => {
|
|
109
105
|
const eventPool = this.eventPools[pool.address.toLowerCase()];
|
|
110
|
-
await eventPool.initialize(
|
|
111
|
-
|
|
106
|
+
await eventPool.initialize(_blockNumber, {
|
|
107
|
+
stateWithBn: {
|
|
108
|
+
state: poolStates[index],
|
|
109
|
+
blockNumber: _blockNumber,
|
|
110
|
+
},
|
|
112
111
|
});
|
|
113
112
|
}),
|
|
114
113
|
);
|
|
@@ -145,10 +144,13 @@ export class JarvisV6
|
|
|
145
144
|
blockNumber: number,
|
|
146
145
|
): Promise<PoolState> {
|
|
147
146
|
const eventState = pool.getState(blockNumber);
|
|
148
|
-
if (eventState)
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
147
|
+
if (eventState) {
|
|
148
|
+
return eventState;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const onChainStateWithBn = await pool.generateState(blockNumber);
|
|
152
|
+
pool.setState(onChainStateWithBn.state, onChainStateWithBn.blockNumber);
|
|
153
|
+
return onChainStateWithBn.state;
|
|
152
154
|
}
|
|
153
155
|
|
|
154
156
|
// Returns pool prices for amounts.
|
|
@@ -162,7 +164,7 @@ export class JarvisV6
|
|
|
162
164
|
amounts: bigint[],
|
|
163
165
|
side: SwapSide,
|
|
164
166
|
blockNumber: number,
|
|
165
|
-
limitPools?: string[], //
|
|
167
|
+
limitPools?: string[], // unused since DEX is constant price, safe to reuse pools
|
|
166
168
|
): Promise<null | ExchangePrices<JarvisV6Data>> {
|
|
167
169
|
if (side !== SwapSide.SELL) return null;
|
|
168
170
|
|
|
@@ -6,6 +6,7 @@ import { getBigIntPow } from '../../utils';
|
|
|
6
6
|
import { JarvisSwapFunctions, PoolConfig, PoolState } from './types';
|
|
7
7
|
import { Interface } from '@ethersproject/abi';
|
|
8
8
|
import { Contract } from 'web3-eth-contract';
|
|
9
|
+
import { StateWithBlock } from '../../stateful-event-subscriber';
|
|
9
10
|
|
|
10
11
|
export const THIRTY_MINUTES = 60 * 30;
|
|
11
12
|
|
|
@@ -18,27 +19,38 @@ export async function getOnChainState(
|
|
|
18
19
|
priceFeedContract,
|
|
19
20
|
poolInterface,
|
|
20
21
|
}: { priceFeedContract: Contract; poolInterface: Interface },
|
|
21
|
-
): Promise<PoolState[]
|
|
22
|
+
): Promise<StateWithBlock<PoolState[]>> {
|
|
23
|
+
const _blockNumber =
|
|
24
|
+
blockNumber === 'latest'
|
|
25
|
+
? await dexHelper.web3Provider.eth.getBlockNumber()
|
|
26
|
+
: blockNumber;
|
|
27
|
+
|
|
28
|
+
// These two calls can not be packed into multicall because Jarvis contract
|
|
29
|
+
// is callable either from Jarvis or EOA. If you try to call from Multicall,
|
|
30
|
+
// it will fail
|
|
22
31
|
const [pricesFeedValues, poolFeePercentages] = await Promise.all([
|
|
23
32
|
_getPricesFeedValues(
|
|
24
33
|
dexHelper,
|
|
25
34
|
poolConfigs,
|
|
26
|
-
|
|
35
|
+
_blockNumber,
|
|
27
36
|
priceFeedContract,
|
|
28
37
|
),
|
|
29
|
-
_getPoolFeePercentages(dexHelper, poolConfigs,
|
|
38
|
+
_getPoolFeePercentages(dexHelper, poolConfigs, _blockNumber, poolInterface),
|
|
30
39
|
]);
|
|
31
40
|
|
|
32
|
-
return
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
41
|
+
return {
|
|
42
|
+
blockNumber: _blockNumber,
|
|
43
|
+
state: poolConfigs.map((_, index) => ({
|
|
44
|
+
priceFeed: pricesFeedValues[index],
|
|
45
|
+
pool: poolFeePercentages[index],
|
|
46
|
+
})),
|
|
47
|
+
};
|
|
36
48
|
}
|
|
37
49
|
|
|
38
50
|
async function _getPoolFeePercentages(
|
|
39
51
|
dexHelper: IDexHelper,
|
|
40
52
|
poolConfigs: PoolConfig[],
|
|
41
|
-
blockNumber: number
|
|
53
|
+
blockNumber: number,
|
|
42
54
|
poolInterface: Interface,
|
|
43
55
|
) {
|
|
44
56
|
const multiContract = dexHelper.multiContract;
|
|
@@ -65,7 +77,7 @@ async function _getPoolFeePercentages(
|
|
|
65
77
|
async function _getPricesFeedValues(
|
|
66
78
|
dexHelper: IDexHelper,
|
|
67
79
|
poolConfigs: PoolConfig[],
|
|
68
|
-
blockNumber: number
|
|
80
|
+
blockNumber: number,
|
|
69
81
|
priceFeedContract: Contract,
|
|
70
82
|
) {
|
|
71
83
|
const pairList = poolConfigs.map(pool =>
|
|
@@ -251,7 +251,10 @@ export class KyberDmm
|
|
|
251
251
|
pair.pools[poolAddress] = pool;
|
|
252
252
|
pool.addressesSubscribed.push(poolAddress);
|
|
253
253
|
await pool.initialize(blockNumber, {
|
|
254
|
-
|
|
254
|
+
stateWithBn: {
|
|
255
|
+
state: poolData,
|
|
256
|
+
blockNumber,
|
|
257
|
+
},
|
|
255
258
|
});
|
|
256
259
|
}
|
|
257
260
|
}
|
package/src/dex/kyberdmm/pool.ts
CHANGED
|
@@ -3,10 +3,13 @@ import { DeepReadonly } from 'ts-essentials';
|
|
|
3
3
|
import kyberDmmPoolABI from '../../abi/kyberdmm/kyber-dmm-pool.abi.json';
|
|
4
4
|
import { getFee, getRFactor } from './fee-formula';
|
|
5
5
|
import { KyberDmmAbiEvents, TradeInfo } from './types';
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
StatefulEventSubscriber,
|
|
8
|
+
StateWithBlock,
|
|
9
|
+
} from '../../stateful-event-subscriber';
|
|
7
10
|
import { Address, BlockHeader, Log, Logger, Token } from '../../types';
|
|
8
11
|
import { IDexHelper } from '../../dex-helper/idex-helper';
|
|
9
|
-
import {
|
|
12
|
+
import { blockAndTryAggregate } from '../../utils';
|
|
10
13
|
|
|
11
14
|
export type KyberDmmPools = { [poolAddress: string]: KyberDmmPool };
|
|
12
15
|
|
|
@@ -113,7 +116,6 @@ export class KyberDmmPool extends StatefulEventSubscriber<KyberDmmPoolState> {
|
|
|
113
116
|
},
|
|
114
117
|
};
|
|
115
118
|
}
|
|
116
|
-
return null;
|
|
117
119
|
}
|
|
118
120
|
|
|
119
121
|
isAmpPool(): boolean {
|
|
@@ -122,10 +124,12 @@ export class KyberDmmPool extends StatefulEventSubscriber<KyberDmmPoolState> {
|
|
|
122
124
|
|
|
123
125
|
async generateState(
|
|
124
126
|
blockNumber: number | 'latest' = 'latest',
|
|
125
|
-
): Promise<
|
|
126
|
-
const
|
|
127
|
-
await
|
|
128
|
-
|
|
127
|
+
): Promise<StateWithBlock<KyberDmmPoolState>> {
|
|
128
|
+
const { results: data, blockNumber: _blockNumber } =
|
|
129
|
+
await blockAndTryAggregate(
|
|
130
|
+
true,
|
|
131
|
+
this.dexHelper.multiContract,
|
|
132
|
+
[
|
|
129
133
|
{
|
|
130
134
|
target: this.poolAddress,
|
|
131
135
|
callData: iface.encodeFunctionData('getTradeInfo', []),
|
|
@@ -134,18 +138,19 @@ export class KyberDmmPool extends StatefulEventSubscriber<KyberDmmPoolState> {
|
|
|
134
138
|
target: this.poolAddress,
|
|
135
139
|
callData: iface.encodeFunctionData('getVolumeTrendData', []),
|
|
136
140
|
},
|
|
137
|
-
]
|
|
138
|
-
|
|
141
|
+
],
|
|
142
|
+
blockNumber,
|
|
143
|
+
);
|
|
139
144
|
|
|
140
145
|
const [reserves0, reserves1, vReserves0, vReserves1] = coder
|
|
141
|
-
.decode(['uint256', 'uint256', 'uint256', 'uint256'], data
|
|
146
|
+
.decode(['uint256', 'uint256', 'uint256', 'uint256'], data[0].returnData)
|
|
142
147
|
.map(a => BigInt(a.toString()));
|
|
143
148
|
|
|
144
149
|
const [shortEMA, longEMA, , lastTradeBlock] = coder
|
|
145
|
-
.decode(['uint256', 'uint256', 'uint128', 'uint256'], data
|
|
150
|
+
.decode(['uint256', 'uint256', 'uint128', 'uint256'], data[1].returnData)
|
|
146
151
|
.map(a => BigInt(a.toString()));
|
|
147
152
|
|
|
148
|
-
if (blockNumber
|
|
153
|
+
if (blockNumber === 'latest')
|
|
149
154
|
blockNumber = await this.dexHelper.web3Provider.eth.getBlockNumber();
|
|
150
155
|
|
|
151
156
|
const prevBlockData: { returnData: any[] } =
|
|
@@ -166,19 +171,22 @@ export class KyberDmmPool extends StatefulEventSubscriber<KyberDmmPoolState> {
|
|
|
166
171
|
.map(a => BigInt(a.toString()));
|
|
167
172
|
|
|
168
173
|
return {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
174
|
+
blockNumber,
|
|
175
|
+
state: {
|
|
176
|
+
trendData: {
|
|
177
|
+
shortEMA,
|
|
178
|
+
longEMA,
|
|
179
|
+
lastBlockVolume,
|
|
180
|
+
lastTradeBlock,
|
|
181
|
+
},
|
|
182
|
+
reserves: {
|
|
183
|
+
reserves0,
|
|
184
|
+
reserves1,
|
|
185
|
+
vReserves0,
|
|
186
|
+
vReserves1,
|
|
187
|
+
},
|
|
188
|
+
ampBps: this.ampBps,
|
|
180
189
|
},
|
|
181
|
-
ampBps: this.ampBps,
|
|
182
190
|
};
|
|
183
191
|
}
|
|
184
192
|
}
|
|
@@ -14,8 +14,15 @@ import {
|
|
|
14
14
|
} from '../../types';
|
|
15
15
|
import { SwapSide, Network } from '../../constants';
|
|
16
16
|
import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
|
|
17
|
-
import {
|
|
18
|
-
|
|
17
|
+
import {
|
|
18
|
+
StatefulEventSubscriber,
|
|
19
|
+
StateWithBlock,
|
|
20
|
+
} from '../../stateful-event-subscriber';
|
|
21
|
+
import {
|
|
22
|
+
getDexKeysWithNetwork,
|
|
23
|
+
getBigIntPow,
|
|
24
|
+
blockAndTryAggregate,
|
|
25
|
+
} from '../../utils';
|
|
19
26
|
import { IDex } from '../../dex/idex';
|
|
20
27
|
import { IDexHelper } from '../../dex-helper/idex-helper';
|
|
21
28
|
import { MakerPsmData, PoolState, PoolConfig } from './types';
|
|
@@ -37,7 +44,7 @@ async function getOnChainState(
|
|
|
37
44
|
poolConfigs: PoolConfig[],
|
|
38
45
|
vatAddress: Address,
|
|
39
46
|
blockNumber: number | 'latest',
|
|
40
|
-
): Promise<PoolState[]
|
|
47
|
+
): Promise<StateWithBlock<PoolState[]>> {
|
|
41
48
|
const callData = poolConfigs
|
|
42
49
|
.map(c => [
|
|
43
50
|
{
|
|
@@ -55,30 +62,35 @@ async function getOnChainState(
|
|
|
55
62
|
])
|
|
56
63
|
.flat();
|
|
57
64
|
|
|
58
|
-
const res =
|
|
59
|
-
|
|
60
|
-
.call({}, blockNumber);
|
|
65
|
+
const { blockNumber: _blockNumber, results: res } =
|
|
66
|
+
await blockAndTryAggregate(true, multiContract, callData, blockNumber);
|
|
61
67
|
|
|
62
68
|
let i = 0;
|
|
63
|
-
return
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
69
|
+
return {
|
|
70
|
+
blockNumber: _blockNumber,
|
|
71
|
+
state: poolConfigs.map(c => {
|
|
72
|
+
const tin = bigIntify(
|
|
73
|
+
psmInterface.decodeFunctionResult('tin', res[i++].returnData)[0],
|
|
74
|
+
);
|
|
75
|
+
const tout = bigIntify(
|
|
76
|
+
psmInterface.decodeFunctionResult('tout', res[i++].returnData)[0],
|
|
77
|
+
);
|
|
78
|
+
const ilks = vatInterface.decodeFunctionResult(
|
|
79
|
+
'ilks',
|
|
80
|
+
res[i++].returnData,
|
|
81
|
+
);
|
|
82
|
+
const Art = bigIntify(ilks.Art);
|
|
83
|
+
const line = bigIntify(ilks.line);
|
|
84
|
+
const rate = bigIntify(ilks.rate);
|
|
85
|
+
return {
|
|
86
|
+
tin,
|
|
87
|
+
tout,
|
|
88
|
+
Art,
|
|
89
|
+
line,
|
|
90
|
+
rate,
|
|
91
|
+
};
|
|
92
|
+
}),
|
|
93
|
+
};
|
|
82
94
|
}
|
|
83
95
|
|
|
84
96
|
export class MakerPsmEventPool extends StatefulEventSubscriber<PoolState> {
|
|
@@ -174,15 +186,20 @@ export class MakerPsmEventPool extends StatefulEventSubscriber<PoolState> {
|
|
|
174
186
|
* should be generated
|
|
175
187
|
* @returns state of the event subscriber at blocknumber
|
|
176
188
|
*/
|
|
177
|
-
async generateState(
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
)
|
|
189
|
+
async generateState(
|
|
190
|
+
blockNumber: number | 'latest',
|
|
191
|
+
): Promise<StateWithBlock<PoolState>> {
|
|
192
|
+
const { blockNumber: _blockNumber, state } = await getOnChainState(
|
|
193
|
+
this.dexHelper.multiContract,
|
|
194
|
+
[this.poolConfig],
|
|
195
|
+
this.vatAddress,
|
|
196
|
+
blockNumber,
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
return {
|
|
200
|
+
blockNumber: _blockNumber,
|
|
201
|
+
state: state[0],
|
|
202
|
+
};
|
|
186
203
|
}
|
|
187
204
|
}
|
|
188
205
|
|
|
@@ -223,18 +240,22 @@ export class MakerPsm extends SimpleExchange implements IDex<MakerPsmData> {
|
|
|
223
240
|
);
|
|
224
241
|
}
|
|
225
242
|
|
|
226
|
-
async initializePricing(blockNumber: number) {
|
|
227
|
-
const poolStates =
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
243
|
+
async initializePricing(blockNumber: number | 'latest' = 'latest') {
|
|
244
|
+
const { blockNumber: _blockNumber, state: poolStates } =
|
|
245
|
+
await getOnChainState(
|
|
246
|
+
this.dexHelper.multiContract,
|
|
247
|
+
this.poolConfigs,
|
|
248
|
+
this.vatAddress,
|
|
249
|
+
blockNumber,
|
|
250
|
+
);
|
|
233
251
|
await Promise.all(
|
|
234
252
|
this.poolConfigs.map(async (p, i) => {
|
|
235
253
|
const eventPool = this.eventPools[p.gem.address.toLowerCase()];
|
|
236
|
-
await eventPool.initialize(
|
|
237
|
-
|
|
254
|
+
await eventPool.initialize(_blockNumber, {
|
|
255
|
+
stateWithBn: {
|
|
256
|
+
state: poolStates[i],
|
|
257
|
+
blockNumber: _blockNumber,
|
|
258
|
+
},
|
|
238
259
|
});
|
|
239
260
|
}),
|
|
240
261
|
);
|
|
@@ -276,9 +297,10 @@ export class MakerPsm extends SimpleExchange implements IDex<MakerPsmData> {
|
|
|
276
297
|
): Promise<PoolState> {
|
|
277
298
|
const eventState = pool.getState(blockNumber);
|
|
278
299
|
if (eventState) return eventState;
|
|
279
|
-
const
|
|
280
|
-
pool.setState(
|
|
281
|
-
|
|
300
|
+
const onChainStateWithBn = await pool.generateState(blockNumber);
|
|
301
|
+
pool.setState(onChainStateWithBn.state, onChainStateWithBn.blockNumber);
|
|
302
|
+
|
|
303
|
+
return onChainStateWithBn.state;
|
|
282
304
|
}
|
|
283
305
|
|
|
284
306
|
computePrices(
|
|
@@ -523,7 +545,7 @@ export class MakerPsm extends SimpleExchange implements IDex<MakerPsmData> {
|
|
|
523
545
|
: [];
|
|
524
546
|
if (!validPoolConfigs.length) return [];
|
|
525
547
|
|
|
526
|
-
const poolStates = await getOnChainState(
|
|
548
|
+
const { state: poolStates } = await getOnChainState(
|
|
527
549
|
this.dexHelper.multiContract,
|
|
528
550
|
validPoolConfigs,
|
|
529
551
|
this.vatAddress,
|
|
@@ -1,24 +1,21 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import { Interface } from '@ethersproject/abi';
|
|
3
|
+
import { DeepReadonly } from 'ts-essentials';
|
|
3
4
|
import { Log, Logger, Address } from '../../types';
|
|
4
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
StatefulEventSubscriber,
|
|
7
|
+
StateWithBlock,
|
|
8
|
+
} from '../../stateful-event-subscriber';
|
|
5
9
|
import { IDexHelper } from '../../dex-helper/idex-helper';
|
|
6
|
-
import { Bin,
|
|
10
|
+
import { Bin, PoolState } from './types';
|
|
7
11
|
import { Token } from '../../types';
|
|
8
|
-
|
|
9
|
-
import { MaverickV1Config } from './config';
|
|
10
|
-
import PoolABI from '../../abi/maverick-v1/pool.json';
|
|
11
|
-
import PoolInspectorABI from '../../abi/maverick-v1/pool-inspector.json';
|
|
12
|
-
|
|
13
|
-
import { Contract } from 'web3-eth-contract';
|
|
14
|
-
import { AbiItem } from 'web3-utils';
|
|
15
12
|
import { MaverickBinMap } from './maverick-math/maverick-bin-map';
|
|
16
13
|
import { MaverickPoolMath } from './maverick-math/maverick-pool-math';
|
|
17
14
|
import { MMath } from './maverick-math/maverick-basic-math';
|
|
18
|
-
|
|
19
|
-
import * as _ from 'lodash';
|
|
20
15
|
import { BI_POWS } from '../../bigint-constants';
|
|
21
|
-
import { getBigIntPow } from '../../utils';
|
|
16
|
+
import { blockAndTryAggregate, getBigIntPow } from '../../utils';
|
|
17
|
+
import PoolABI from '../../abi/maverick-v1/pool.json';
|
|
18
|
+
import PoolInspectorABI from '../../abi/maverick-v1/pool-inspector.json';
|
|
22
19
|
|
|
23
20
|
(BigInt.prototype as any).toJSON = function () {
|
|
24
21
|
return this.toString();
|
|
@@ -32,8 +29,7 @@ export class MaverickV1EventPool extends StatefulEventSubscriber<PoolState> {
|
|
|
32
29
|
logDecoder: (log: Log) => any;
|
|
33
30
|
|
|
34
31
|
addressesSubscribed: string[];
|
|
35
|
-
|
|
36
|
-
poolInspectorContract: Contract;
|
|
32
|
+
poolInspectorInterface: Interface;
|
|
37
33
|
poolMath: MaverickPoolMath;
|
|
38
34
|
|
|
39
35
|
constructor(
|
|
@@ -68,15 +64,7 @@ export class MaverickV1EventPool extends StatefulEventSubscriber<PoolState> {
|
|
|
68
64
|
|
|
69
65
|
this.addressesSubscribed = [address];
|
|
70
66
|
|
|
71
|
-
this.
|
|
72
|
-
PoolInspectorABI as AbiItem[],
|
|
73
|
-
this.inspectorAddress,
|
|
74
|
-
);
|
|
75
|
-
|
|
76
|
-
this.poolContract = new this.dexHelper.web3Provider.eth.Contract(
|
|
77
|
-
PoolABI as AbiItem[],
|
|
78
|
-
this.address,
|
|
79
|
-
);
|
|
67
|
+
this.poolInspectorInterface = new Interface(PoolInspectorABI);
|
|
80
68
|
|
|
81
69
|
this.poolMath = new MaverickPoolMath(
|
|
82
70
|
parentName,
|
|
@@ -128,16 +116,37 @@ export class MaverickV1EventPool extends StatefulEventSubscriber<PoolState> {
|
|
|
128
116
|
* should be generated
|
|
129
117
|
* @returns state of the event subscriber at blocknumber
|
|
130
118
|
*/
|
|
131
|
-
async generateState(
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
119
|
+
async generateState(
|
|
120
|
+
blockNumber: number | 'latest',
|
|
121
|
+
): Promise<Readonly<StateWithBlock<PoolState>>> {
|
|
122
|
+
const { blockNumber: _blockNumber, results } = await blockAndTryAggregate(
|
|
123
|
+
true,
|
|
124
|
+
this.dexHelper.multiContract,
|
|
125
|
+
[
|
|
126
|
+
{
|
|
127
|
+
target: this.inspectorAddress,
|
|
128
|
+
callData: this.poolInspectorInterface.encodeFunctionData(
|
|
129
|
+
'getActiveBins',
|
|
130
|
+
[this.address, 0, 0],
|
|
131
|
+
),
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
target: this.address,
|
|
135
|
+
callData: this.poolInterface.encodeFunctionData('getState', []),
|
|
136
|
+
},
|
|
137
|
+
],
|
|
139
138
|
blockNumber,
|
|
140
139
|
);
|
|
140
|
+
|
|
141
|
+
const rawBins = this.poolInspectorInterface.decodeFunctionResult(
|
|
142
|
+
'getActiveBins',
|
|
143
|
+
results[0].returnData,
|
|
144
|
+
);
|
|
145
|
+
const rawState = this.poolInterface.decodeFunctionResult(
|
|
146
|
+
'getState',
|
|
147
|
+
results[1].returnData,
|
|
148
|
+
);
|
|
149
|
+
|
|
141
150
|
let binPositions: { [tick: string]: { [kind: string]: bigint } } = {};
|
|
142
151
|
let bins: { [id: string]: Bin } = {};
|
|
143
152
|
let binMap: { [id: string]: bigint } = {};
|
|
@@ -164,11 +173,14 @@ export class MaverickV1EventPool extends StatefulEventSubscriber<PoolState> {
|
|
|
164
173
|
});
|
|
165
174
|
|
|
166
175
|
return {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
176
|
+
blockNumber: await this.dexHelper.web3Provider.eth.getBlockNumber(),
|
|
177
|
+
state: {
|
|
178
|
+
activeTick: BigInt(rawState.activeTick),
|
|
179
|
+
binCounter: BigInt(rawState.binCounter),
|
|
180
|
+
bins: bins,
|
|
181
|
+
binPositions: binPositions,
|
|
182
|
+
binMap: binMap,
|
|
183
|
+
},
|
|
172
184
|
};
|
|
173
185
|
}
|
|
174
186
|
|
|
@@ -76,7 +76,7 @@ export class MaverickV1
|
|
|
76
76
|
MaverickV1Config[this.dexKey][this.network].routerAddress;
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
async setupEventPools(blockNumber: number) {
|
|
79
|
+
async setupEventPools(blockNumber: number | 'latest' = 'latest') {
|
|
80
80
|
const pools = await this.fetchAllSubgraphPools();
|
|
81
81
|
await Promise.all(
|
|
82
82
|
pools.map(async (pool: any) => {
|
|
@@ -102,13 +102,14 @@ export class MaverickV1
|
|
|
102
102
|
MaverickV1Config[this.dexKey][this.network].poolInspectorAddress,
|
|
103
103
|
this.logger,
|
|
104
104
|
);
|
|
105
|
-
const
|
|
105
|
+
const { state: generatedState, blockNumber: generatedBlockNumber } =
|
|
106
|
+
await eventPool.generateState(blockNumber);
|
|
106
107
|
if (blockNumber) {
|
|
107
|
-
eventPool.setState(
|
|
108
|
+
eventPool.setState(generatedState, generatedBlockNumber);
|
|
108
109
|
this.dexHelper.blockManager.subscribeToLogs(
|
|
109
110
|
eventPool,
|
|
110
111
|
eventPool.addressesSubscribed,
|
|
111
|
-
|
|
112
|
+
generatedBlockNumber,
|
|
112
113
|
);
|
|
113
114
|
}
|
|
114
115
|
this.pools[eventPool.address] = eventPool;
|
|
@@ -120,7 +121,7 @@ export class MaverickV1
|
|
|
120
121
|
// pricing service. It is intended to setup the integration
|
|
121
122
|
// for pricing requests. It is optional for a DEX to
|
|
122
123
|
// implement this function
|
|
123
|
-
async initializePricing(blockNumber: number) {
|
|
124
|
+
async initializePricing(blockNumber: number | 'latest' = 'latest') {
|
|
124
125
|
await this.setupEventPools(blockNumber);
|
|
125
126
|
}
|
|
126
127
|
|
|
@@ -224,8 +225,11 @@ export class MaverickV1
|
|
|
224
225
|
try {
|
|
225
226
|
let state = pool.getState(blockNumber);
|
|
226
227
|
if (state === null) {
|
|
227
|
-
|
|
228
|
-
|
|
228
|
+
const {
|
|
229
|
+
state: generatedState,
|
|
230
|
+
blockNumber: generatedBlockNumber,
|
|
231
|
+
} = await pool.generateState(blockNumber);
|
|
232
|
+
pool.setState(generatedState, generatedBlockNumber);
|
|
229
233
|
}
|
|
230
234
|
if (state === null) {
|
|
231
235
|
this.logger.debug(
|
|
@@ -266,7 +270,7 @@ export class MaverickV1
|
|
|
266
270
|
let gasCosts: number[] = dataList.map(
|
|
267
271
|
([d, t]: [BigInt, number]) => {
|
|
268
272
|
if (d == 0n) return 0;
|
|
269
|
-
// I think it is reasonable estimation assuming "kind" gas cost is almost
|
|
273
|
+
// I think it is reasonable estimation assuming "kind" gas cost is almost every time around 1
|
|
270
274
|
return (
|
|
271
275
|
MAV_V1_BASE_GAS_COST +
|
|
272
276
|
(MAV_V1_TICK_GAS_COST + MAV_V1_KIND_GAS_COST) * t
|