@mento-protocol/mento-sdk 3.2.6 → 3.2.7
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/package.json +7 -1
- package/dist/cache/routes.d.ts +0 -13
- package/dist/cache/routes.js +0 -14649
- package/dist/cache/tokens.d.ts +0 -68
- package/dist/cache/tokens.js +0 -488
- package/dist/core/abis/activePool.d.ts +0 -2
- package/dist/core/abis/activePool.js +0 -14
- package/dist/core/abis/addressesRegistry.d.ts +0 -2
- package/dist/core/abis/addressesRegistry.js +0 -26
- package/dist/core/abis/bipoolmanager.d.ts +0 -34
- package/dist/core/abis/bipoolmanager.js +0 -72
- package/dist/core/abis/borrowerOperations.d.ts +0 -9
- package/dist/core/abis/borrowerOperations.js +0 -89
- package/dist/core/abis/breakerbox.d.ts +0 -13
- package/dist/core/abis/breakerbox.js +0 -8
- package/dist/core/abis/broker.d.ts +0 -2
- package/dist/core/abis/broker.js +0 -9
- package/dist/core/abis/erc20.d.ts +0 -9
- package/dist/core/abis/erc20.js +0 -21
- package/dist/core/abis/fpmm.d.ts +0 -270
- package/dist/core/abis/fpmm.js +0 -49
- package/dist/core/abis/fpmmFactory.d.ts +0 -85
- package/dist/core/abis/fpmmFactory.js +0 -26
- package/dist/core/abis/hintHelpers.d.ts +0 -2
- package/dist/core/abis/hintHelpers.js +0 -14
- package/dist/core/abis/index.d.ts +0 -22
- package/dist/core/abis/index.js +0 -38
- package/dist/core/abis/liquidityStrategy.d.ts +0 -132
- package/dist/core/abis/liquidityStrategy.js +0 -10
- package/dist/core/abis/multiTroveGetter.d.ts +0 -8
- package/dist/core/abis/multiTroveGetter.js +0 -15
- package/dist/core/abis/priceFeed.d.ts +0 -7
- package/dist/core/abis/priceFeed.js +0 -16
- package/dist/core/abis/pricingmodule.d.ts +0 -2
- package/dist/core/abis/pricingmodule.js +0 -6
- package/dist/core/abis/reserve.d.ts +0 -3
- package/dist/core/abis/reserve.js +0 -18
- package/dist/core/abis/router.d.ts +0 -521
- package/dist/core/abis/router.js +0 -45
- package/dist/core/abis/sortedTroves.d.ts +0 -2
- package/dist/core/abis/sortedTroves.js +0 -15
- package/dist/core/abis/systemParams.d.ts +0 -2
- package/dist/core/abis/systemParams.js +0 -14
- package/dist/core/abis/troveManager.d.ts +0 -2
- package/dist/core/abis/troveManager.js +0 -27
- package/dist/core/abis/troveNFT.d.ts +0 -2
- package/dist/core/abis/troveNFT.js +0 -9
- package/dist/core/abis/virtualPool.d.ts +0 -50
- package/dist/core/abis/virtualPool.js +0 -11
- package/dist/core/abis/virtualPoolFactory.d.ts +0 -59
- package/dist/core/abis/virtualPoolFactory.js +0 -17
- package/dist/core/constants/addresses.d.ts +0 -18
- package/dist/core/constants/addresses.js +0 -113
- package/dist/core/constants/borrowConstants.d.ts +0 -10
- package/dist/core/constants/borrowConstants.js +0 -16
- package/dist/core/constants/borrowRegistries.d.ts +0 -7
- package/dist/core/constants/borrowRegistries.js +0 -34
- package/dist/core/constants/chainId.d.ts +0 -7
- package/dist/core/constants/chainId.js +0 -11
- package/dist/core/constants/contractNames.d.ts +0 -21
- package/dist/core/constants/contractNames.js +0 -24
- package/dist/core/constants/index.d.ts +0 -6
- package/dist/core/constants/index.js +0 -22
- package/dist/core/errors/base.d.ts +0 -8
- package/dist/core/errors/base.js +0 -17
- package/dist/core/errors/index.d.ts +0 -4
- package/dist/core/errors/index.js +0 -20
- package/dist/core/errors/oracle.d.ts +0 -9
- package/dist/core/errors/oracle.js +0 -15
- package/dist/core/errors/router.d.ts +0 -14
- package/dist/core/errors/router.js +0 -24
- package/dist/core/types/borrow.d.ts +0 -87
- package/dist/core/types/borrow.js +0 -3
- package/dist/core/types/contractAddresses.d.ts +0 -42
- package/dist/core/types/contractAddresses.js +0 -3
- package/dist/core/types/index.d.ts +0 -10
- package/dist/core/types/index.js +0 -26
- package/dist/core/types/liquidity.d.ts +0 -194
- package/dist/core/types/liquidity.js +0 -3
- package/dist/core/types/pool.d.ts +0 -208
- package/dist/core/types/pool.js +0 -14
- package/dist/core/types/provider.d.ts +0 -45
- package/dist/core/types/provider.js +0 -3
- package/dist/core/types/route.d.ts +0 -62
- package/dist/core/types/route.js +0 -3
- package/dist/core/types/token.d.ts +0 -21
- package/dist/core/types/token.js +0 -3
- package/dist/core/types/tradingLimits.d.ts +0 -91
- package/dist/core/types/tradingLimits.js +0 -3
- package/dist/core/types/tradingMode.d.ts +0 -24
- package/dist/core/types/tradingMode.js +0 -31
- package/dist/core/types/transaction.d.ts +0 -45
- package/dist/core/types/transaction.js +0 -3
- package/dist/esm/cache/routes.js +0 -14644
- package/dist/esm/cache/tokens.js +0 -480
- package/dist/esm/core/abis/activePool.js +0 -10
- package/dist/esm/core/abis/addressesRegistry.js +0 -22
- package/dist/esm/core/abis/bipoolmanager.js +0 -68
- package/dist/esm/core/abis/borrowerOperations.js +0 -85
- package/dist/esm/core/abis/breakerbox.js +0 -4
- package/dist/esm/core/abis/broker.js +0 -5
- package/dist/esm/core/abis/erc20.js +0 -17
- package/dist/esm/core/abis/fpmm.js +0 -45
- package/dist/esm/core/abis/fpmmFactory.js +0 -22
- package/dist/esm/core/abis/hintHelpers.js +0 -10
- package/dist/esm/core/abis/index.js +0 -21
- package/dist/esm/core/abis/liquidityStrategy.js +0 -6
- package/dist/esm/core/abis/multiTroveGetter.js +0 -11
- package/dist/esm/core/abis/priceFeed.js +0 -12
- package/dist/esm/core/abis/pricingmodule.js +0 -2
- package/dist/esm/core/abis/reserve.js +0 -14
- package/dist/esm/core/abis/router.js +0 -41
- package/dist/esm/core/abis/sortedTroves.js +0 -11
- package/dist/esm/core/abis/systemParams.js +0 -10
- package/dist/esm/core/abis/troveManager.js +0 -23
- package/dist/esm/core/abis/troveNFT.js +0 -5
- package/dist/esm/core/abis/virtualPool.js +0 -7
- package/dist/esm/core/abis/virtualPoolFactory.js +0 -13
- package/dist/esm/core/constants/addresses.js +0 -107
- package/dist/esm/core/constants/borrowConstants.js +0 -12
- package/dist/esm/core/constants/borrowRegistries.js +0 -29
- package/dist/esm/core/constants/chainId.js +0 -7
- package/dist/esm/core/constants/contractNames.js +0 -20
- package/dist/esm/core/constants/index.js +0 -5
- package/dist/esm/core/errors/base.js +0 -12
- package/dist/esm/core/errors/index.js +0 -3
- package/dist/esm/core/errors/oracle.js +0 -10
- package/dist/esm/core/errors/router.js +0 -18
- package/dist/esm/core/types/borrow.js +0 -1
- package/dist/esm/core/types/contractAddresses.js +0 -1
- package/dist/esm/core/types/index.js +0 -9
- package/dist/esm/core/types/liquidity.js +0 -1
- package/dist/esm/core/types/pool.js +0 -10
- package/dist/esm/core/types/provider.js +0 -1
- package/dist/esm/core/types/route.js +0 -1
- package/dist/esm/core/types/token.js +0 -1
- package/dist/esm/core/types/tradingLimits.js +0 -1
- package/dist/esm/core/types/tradingMode.js +0 -26
- package/dist/esm/core/types/transaction.js +0 -1
- package/dist/esm/index.js +0 -139
- package/dist/esm/package.json +0 -1
- package/dist/esm/services/borrow/BorrowService.js +0 -455
- package/dist/esm/services/borrow/borrowHelpers.js +0 -3
- package/dist/esm/services/borrow/borrowMath.js +0 -127
- package/dist/esm/services/borrow/index.js +0 -3
- package/dist/esm/services/borrow/internal/borrowApprovalService.js +0 -48
- package/dist/esm/services/borrow/internal/borrowContextStore.js +0 -35
- package/dist/esm/services/borrow/internal/borrowErc20.js +0 -38
- package/dist/esm/services/borrow/internal/borrowHints.js +0 -27
- package/dist/esm/services/borrow/internal/borrowPositionParser.js +0 -82
- package/dist/esm/services/borrow/internal/borrowReadService.js +0 -271
- package/dist/esm/services/borrow/internal/borrowRegistryReader.js +0 -108
- package/dist/esm/services/borrow/internal/borrowTransactionService.js +0 -271
- package/dist/esm/services/borrow/internal/borrowTypes.js +0 -1
- package/dist/esm/services/borrow/internal/borrowValidation.js +0 -89
- package/dist/esm/services/index.js +0 -8
- package/dist/esm/services/liquidity/LiquidityService.js +0 -163
- package/dist/esm/services/liquidity/basicLiquidity.js +0 -162
- package/dist/esm/services/liquidity/index.js +0 -1
- package/dist/esm/services/liquidity/liquidityHelpers.js +0 -95
- package/dist/esm/services/liquidity/rebalance.js +0 -59
- package/dist/esm/services/liquidity/zapHelpers.js +0 -120
- package/dist/esm/services/liquidity/zapIn.js +0 -112
- package/dist/esm/services/liquidity/zapOut.js +0 -248
- package/dist/esm/services/pools/PoolService.js +0 -204
- package/dist/esm/services/pools/index.js +0 -1
- package/dist/esm/services/pools/poolDetails.js +0 -209
- package/dist/esm/services/pools/poolDiscovery.js +0 -112
- package/dist/esm/services/pools/rebalancePreview.js +0 -181
- package/dist/esm/services/quotes/QuoteService.js +0 -85
- package/dist/esm/services/quotes/index.js +0 -1
- package/dist/esm/services/routes/RouteService.js +0 -268
- package/dist/esm/services/routes/index.js +0 -1
- package/dist/esm/services/swap/SwapService.js +0 -247
- package/dist/esm/services/swap/index.js +0 -1
- package/dist/esm/services/tokens/index.js +0 -1
- package/dist/esm/services/tokens/tokenService.js +0 -285
- package/dist/esm/services/trading/TradingLimitsService.js +0 -154
- package/dist/esm/services/trading/TradingService.js +0 -222
- package/dist/esm/services/trading/index.js +0 -2
- package/dist/esm/utils/chainConfig.js +0 -118
- package/dist/esm/utils/costUtils.js +0 -56
- package/dist/esm/utils/deadline.js +0 -22
- package/dist/esm/utils/index.js +0 -9
- package/dist/esm/utils/multicall.js +0 -47
- package/dist/esm/utils/pathEncoder.js +0 -69
- package/dist/esm/utils/rateFeed.js +0 -23
- package/dist/esm/utils/retry.js +0 -24
- package/dist/esm/utils/routeUtils.js +0 -361
- package/dist/esm/utils/routes.js +0 -2
- package/dist/esm/utils/sortUtils.js +0 -33
- package/dist/esm/utils/tokens.js +0 -2
- package/dist/esm/utils/tradingLimits.js +0 -163
- package/dist/esm/utils/validation.js +0 -30
- package/dist/index.d.ts +0 -101
- package/dist/index.js +0 -158
- package/dist/services/borrow/BorrowService.d.ts +0 -381
- package/dist/services/borrow/BorrowService.js +0 -460
- package/dist/services/borrow/borrowHelpers.d.ts +0 -4
- package/dist/services/borrow/borrowHelpers.js +0 -13
- package/dist/services/borrow/borrowMath.d.ts +0 -21
- package/dist/services/borrow/borrowMath.js +0 -137
- package/dist/services/borrow/index.d.ts +0 -4
- package/dist/services/borrow/index.js +0 -20
- package/dist/services/borrow/internal/borrowApprovalService.d.ts +0 -14
- package/dist/services/borrow/internal/borrowApprovalService.js +0 -53
- package/dist/services/borrow/internal/borrowContextStore.d.ts +0 -11
- package/dist/services/borrow/internal/borrowContextStore.js +0 -40
- package/dist/services/borrow/internal/borrowErc20.d.ts +0 -5
- package/dist/services/borrow/internal/borrowErc20.js +0 -43
- package/dist/services/borrow/internal/borrowHints.d.ts +0 -7
- package/dist/services/borrow/internal/borrowHints.js +0 -31
- package/dist/services/borrow/internal/borrowPositionParser.d.ts +0 -4
- package/dist/services/borrow/internal/borrowPositionParser.js +0 -87
- package/dist/services/borrow/internal/borrowReadService.d.ts +0 -31
- package/dist/services/borrow/internal/borrowReadService.js +0 -276
- package/dist/services/borrow/internal/borrowRegistryReader.d.ts +0 -5
- package/dist/services/borrow/internal/borrowRegistryReader.js +0 -113
- package/dist/services/borrow/internal/borrowTransactionService.d.ts +0 -23
- package/dist/services/borrow/internal/borrowTransactionService.js +0 -276
- package/dist/services/borrow/internal/borrowTypes.d.ts +0 -15
- package/dist/services/borrow/internal/borrowTypes.js +0 -3
- package/dist/services/borrow/internal/borrowValidation.d.ts +0 -14
- package/dist/services/borrow/internal/borrowValidation.js +0 -104
- package/dist/services/index.d.ts +0 -9
- package/dist/services/index.js +0 -25
- package/dist/services/liquidity/LiquidityService.d.ts +0 -139
- package/dist/services/liquidity/LiquidityService.js +0 -168
- package/dist/services/liquidity/basicLiquidity.d.ts +0 -11
- package/dist/services/liquidity/basicLiquidity.js +0 -172
- package/dist/services/liquidity/index.d.ts +0 -2
- package/dist/services/liquidity/index.js +0 -18
- package/dist/services/liquidity/liquidityHelpers.d.ts +0 -19
- package/dist/services/liquidity/liquidityHelpers.js +0 -104
- package/dist/services/liquidity/rebalance.d.ts +0 -6
- package/dist/services/liquidity/rebalance.js +0 -64
- package/dist/services/liquidity/zapHelpers.d.ts +0 -66
- package/dist/services/liquidity/zapHelpers.js +0 -129
- package/dist/services/liquidity/zapIn.d.ts +0 -18
- package/dist/services/liquidity/zapIn.js +0 -119
- package/dist/services/liquidity/zapOut.d.ts +0 -9
- package/dist/services/liquidity/zapOut.js +0 -255
- package/dist/services/pools/PoolService.d.ts +0 -69
- package/dist/services/pools/PoolService.js +0 -209
- package/dist/services/pools/index.d.ts +0 -2
- package/dist/services/pools/index.js +0 -18
- package/dist/services/pools/poolDetails.d.ts +0 -13
- package/dist/services/pools/poolDetails.js +0 -216
- package/dist/services/pools/poolDiscovery.d.ts +0 -12
- package/dist/services/pools/poolDiscovery.js +0 -117
- package/dist/services/pools/rebalancePreview.d.ts +0 -5
- package/dist/services/pools/rebalancePreview.js +0 -186
- package/dist/services/quotes/QuoteService.d.ts +0 -51
- package/dist/services/quotes/QuoteService.js +0 -91
- package/dist/services/quotes/index.d.ts +0 -2
- package/dist/services/quotes/index.js +0 -18
- package/dist/services/routes/RouteService.d.ts +0 -117
- package/dist/services/routes/RouteService.js +0 -306
- package/dist/services/routes/index.d.ts +0 -2
- package/dist/services/routes/index.js +0 -18
- package/dist/services/swap/SwapService.d.ts +0 -198
- package/dist/services/swap/SwapService.js +0 -252
- package/dist/services/swap/index.d.ts +0 -2
- package/dist/services/swap/index.js +0 -18
- package/dist/services/tokens/index.d.ts +0 -2
- package/dist/services/tokens/index.js +0 -18
- package/dist/services/tokens/tokenService.d.ts +0 -55
- package/dist/services/tokens/tokenService.js +0 -290
- package/dist/services/trading/TradingLimitsService.d.ts +0 -38
- package/dist/services/trading/TradingLimitsService.js +0 -159
- package/dist/services/trading/TradingService.d.ts +0 -115
- package/dist/services/trading/TradingService.js +0 -227
- package/dist/services/trading/index.d.ts +0 -3
- package/dist/services/trading/index.js +0 -19
- package/dist/utils/chainConfig.d.ts +0 -16
- package/dist/utils/chainConfig.js +0 -123
- package/dist/utils/costUtils.d.ts +0 -12
- package/dist/utils/costUtils.js +0 -60
- package/dist/utils/deadline.d.ts +0 -21
- package/dist/utils/deadline.js +0 -26
- package/dist/utils/index.d.ts +0 -10
- package/dist/utils/index.js +0 -26
- package/dist/utils/multicall.d.ts +0 -30
- package/dist/utils/multicall.js +0 -52
- package/dist/utils/pathEncoder.d.ts +0 -34
- package/dist/utils/pathEncoder.js +0 -73
- package/dist/utils/rateFeed.d.ts +0 -18
- package/dist/utils/rateFeed.js +0 -27
- package/dist/utils/retry.d.ts +0 -12
- package/dist/utils/retry.js +0 -28
- package/dist/utils/routeUtils.d.ts +0 -295
- package/dist/utils/routeUtils.js +0 -371
- package/dist/utils/routes.d.ts +0 -3
- package/dist/utils/routes.js +0 -8
- package/dist/utils/sortUtils.d.ts +0 -24
- package/dist/utils/sortUtils.js +0 -39
- package/dist/utils/tokens.d.ts +0 -2
- package/dist/utils/tokens.js +0 -13
- package/dist/utils/tradingLimits.d.ts +0 -41
- package/dist/utils/tradingLimits.js +0 -171
- package/dist/utils/validation.d.ts +0 -19
- package/dist/utils/validation.js +0 -34
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
import { ROUTER_ABI } from '../../core/abis';
|
|
2
|
-
import { getContractAddress } from '../../core/constants';
|
|
3
|
-
import { RouteNotFoundError, ZapOutRouteNotViableError } from '../../core/errors';
|
|
4
|
-
import { validateAddress } from '../../utils/validation';
|
|
5
|
-
import { encodeRoutePath } from '../../utils/pathEncoder';
|
|
6
|
-
import { buildApprovalParams, getAllowance, calculateMinAmount, getPoolInfo } from './liquidityHelpers';
|
|
7
|
-
import { encodeZapOutCall, findZapOutRoutes } from './zapHelpers';
|
|
8
|
-
const INSUFFICIENT_LIQUIDITY_SELECTOR = '0xbb55fd27';
|
|
9
|
-
const MAX_ROUTE_CANDIDATES_PER_LEG = 8;
|
|
10
|
-
const MAX_ROUTE_COMBINATIONS = 48;
|
|
11
|
-
const ROUTE_SIMULATION_BATCH_SIZE = 6;
|
|
12
|
-
export async function buildZapOutTransactionInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, recipient, owner, options) {
|
|
13
|
-
const prepared = await prepareZapOutInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, recipient, owner, options);
|
|
14
|
-
return {
|
|
15
|
-
approval: prepared.approval ?? null,
|
|
16
|
-
zapOut: prepared.details,
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
export async function buildZapOutParamsInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, recipient, options) {
|
|
20
|
-
const prepared = await prepareZapOutInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, recipient, undefined, options);
|
|
21
|
-
return prepared.details;
|
|
22
|
-
}
|
|
23
|
-
export async function quoteZapOutInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, options) {
|
|
24
|
-
const prepared = await prepareZapOutInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, tokenOut, undefined, options);
|
|
25
|
-
return prepared.quote;
|
|
26
|
-
}
|
|
27
|
-
export async function prepareZapOutInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, recipient, owner, options) {
|
|
28
|
-
if (owner) {
|
|
29
|
-
validateAddress(owner, 'owner');
|
|
30
|
-
}
|
|
31
|
-
const [context, currentAllowance] = await Promise.all([
|
|
32
|
-
prepareZapOutContextInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, recipient, options),
|
|
33
|
-
owner ? getAllowance(publicClient, poolAddress, owner, chainId) : Promise.resolve(null),
|
|
34
|
-
]);
|
|
35
|
-
let details = context.details;
|
|
36
|
-
const approval = owner && currentAllowance !== null && currentAllowance < liquidity
|
|
37
|
-
? { token: poolAddress, amount: liquidity, params: buildApprovalParams(chainId, poolAddress, liquidity) }
|
|
38
|
-
: owner
|
|
39
|
-
? null
|
|
40
|
-
: undefined;
|
|
41
|
-
if (owner && currentAllowance !== null && currentAllowance >= liquidity) {
|
|
42
|
-
const ownerAddr = owner;
|
|
43
|
-
const routerAddress = getContractAddress(chainId, 'Router');
|
|
44
|
-
try {
|
|
45
|
-
await simulateZapOut(publicClient, ownerAddr, routerAddress, details.params.data);
|
|
46
|
-
}
|
|
47
|
-
catch (error) {
|
|
48
|
-
if (!isInsufficientLiquidityError(error)) {
|
|
49
|
-
throw error;
|
|
50
|
-
}
|
|
51
|
-
details = await findViableZapOutDetails(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, ownerAddr, options);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
return {
|
|
55
|
-
routesA: details.routesA,
|
|
56
|
-
routesB: details.routesB,
|
|
57
|
-
quote: {
|
|
58
|
-
amountOutFromA: details.zapParams.amountOutMinA,
|
|
59
|
-
amountOutFromB: details.zapParams.amountOutMinB,
|
|
60
|
-
amountAMin: details.zapParams.amountAMin,
|
|
61
|
-
amountBMin: details.zapParams.amountBMin,
|
|
62
|
-
estimatedMinTokenOut: details.estimatedMinTokenOut,
|
|
63
|
-
},
|
|
64
|
-
approval,
|
|
65
|
-
details,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
async function prepareZapOutContextInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, recipient, options) {
|
|
69
|
-
validateAddress(poolAddress, 'poolAddress');
|
|
70
|
-
validateAddress(tokenOut, 'tokenOut');
|
|
71
|
-
validateAddress(recipient, 'recipient');
|
|
72
|
-
const { token0, token1, factoryAddr } = await getPoolInfo(poolService, poolAddress);
|
|
73
|
-
const { routesA, routesB } = await findZapOutRoutes(routeService, token0, token1, tokenOut);
|
|
74
|
-
const details = await buildZapOutDetailsForRoutes(publicClient, chainId, poolAddress, tokenOut, liquidity, token0, token1, factoryAddr, routesA, routesB, options);
|
|
75
|
-
return {
|
|
76
|
-
routesA,
|
|
77
|
-
routesB,
|
|
78
|
-
quote: {
|
|
79
|
-
amountOutFromA: details.zapParams.amountOutMinA,
|
|
80
|
-
amountOutFromB: details.zapParams.amountOutMinB,
|
|
81
|
-
amountAMin: details.zapParams.amountAMin,
|
|
82
|
-
amountBMin: details.zapParams.amountBMin,
|
|
83
|
-
estimatedMinTokenOut: details.estimatedMinTokenOut,
|
|
84
|
-
},
|
|
85
|
-
details,
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
async function buildZapOutDetailsForRoutes(publicClient, chainId, poolAddress, tokenOut, liquidity, token0, token1, factoryAddr, routesA, routesB, options) {
|
|
89
|
-
const routerAddress = getContractAddress(chainId, 'Router');
|
|
90
|
-
const [amountOutMinA, amountOutMinB, amountAMin, amountBMin] = (await publicClient.readContract({
|
|
91
|
-
address: routerAddress,
|
|
92
|
-
abi: ROUTER_ABI,
|
|
93
|
-
functionName: 'generateZapOutParams',
|
|
94
|
-
args: [token0, token1, factoryAddr, liquidity, routesA, routesB],
|
|
95
|
-
}));
|
|
96
|
-
const finalAmountAMin = calculateMinAmount(amountAMin, options.slippageTolerance);
|
|
97
|
-
const finalAmountBMin = calculateMinAmount(amountBMin, options.slippageTolerance);
|
|
98
|
-
const finalAmountOutMinA = calculateMinAmount(amountOutMinA, options.slippageTolerance);
|
|
99
|
-
const finalAmountOutMinB = calculateMinAmount(amountOutMinB, options.slippageTolerance);
|
|
100
|
-
const zapParams = {
|
|
101
|
-
tokenA: token0,
|
|
102
|
-
tokenB: token1,
|
|
103
|
-
factory: factoryAddr,
|
|
104
|
-
amountAMin: finalAmountAMin,
|
|
105
|
-
amountBMin: finalAmountBMin,
|
|
106
|
-
amountOutMinA: finalAmountOutMinA,
|
|
107
|
-
amountOutMinB: finalAmountOutMinB,
|
|
108
|
-
};
|
|
109
|
-
const data = encodeZapOutCall(tokenOut, liquidity, zapParams, routesA, routesB);
|
|
110
|
-
return {
|
|
111
|
-
params: {
|
|
112
|
-
to: routerAddress,
|
|
113
|
-
data,
|
|
114
|
-
value: '0',
|
|
115
|
-
},
|
|
116
|
-
poolAddress,
|
|
117
|
-
tokenOut,
|
|
118
|
-
liquidity,
|
|
119
|
-
routesA,
|
|
120
|
-
routesB,
|
|
121
|
-
zapParams,
|
|
122
|
-
estimatedMinTokenOut: finalAmountOutMinA + finalAmountOutMinB,
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
async function findViableZapOutDetails(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, owner, options) {
|
|
126
|
-
const { token0, token1, factoryAddr } = await getPoolInfo(poolService, poolAddress);
|
|
127
|
-
const routerAddress = getContractAddress(chainId, 'Router');
|
|
128
|
-
const allRoutes = await routeService.getRoutes({ cached: false, returnAllRoutes: true });
|
|
129
|
-
const [routesAOptions, routesBOptions] = await Promise.all([
|
|
130
|
-
getEncodedRouteCandidates(routeService, token0, tokenOut, poolAddress, allRoutes),
|
|
131
|
-
getEncodedRouteCandidates(routeService, token1, tokenOut, poolAddress, allRoutes),
|
|
132
|
-
]);
|
|
133
|
-
const routeCombinations = [];
|
|
134
|
-
outer: for (const routesA of routesAOptions) {
|
|
135
|
-
for (const routesB of routesBOptions) {
|
|
136
|
-
routeCombinations.push({ routesA, routesB });
|
|
137
|
-
if (routeCombinations.length >= MAX_ROUTE_COMBINATIONS) {
|
|
138
|
-
break outer;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
let best = null;
|
|
143
|
-
for (let index = 0; index < routeCombinations.length; index += ROUTE_SIMULATION_BATCH_SIZE) {
|
|
144
|
-
const batch = routeCombinations.slice(index, index + ROUTE_SIMULATION_BATCH_SIZE);
|
|
145
|
-
const candidates = await Promise.all(batch.map(async ({ routesA, routesB }) => {
|
|
146
|
-
try {
|
|
147
|
-
const candidate = await buildZapOutDetailsForRoutes(publicClient, chainId, poolAddress, tokenOut, liquidity, token0, token1, factoryAddr, routesA, routesB, options);
|
|
148
|
-
await simulateZapOut(publicClient, owner, routerAddress, candidate.params.data);
|
|
149
|
-
return candidate;
|
|
150
|
-
}
|
|
151
|
-
catch {
|
|
152
|
-
return null;
|
|
153
|
-
}
|
|
154
|
-
}));
|
|
155
|
-
for (const candidate of candidates) {
|
|
156
|
-
if (candidate && (!best || candidate.estimatedMinTokenOut > best.estimatedMinTokenOut)) {
|
|
157
|
-
best = candidate;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
if (!best) {
|
|
162
|
-
throw new ZapOutRouteNotViableError(poolAddress, tokenOut);
|
|
163
|
-
}
|
|
164
|
-
return best;
|
|
165
|
-
}
|
|
166
|
-
async function getEncodedRouteCandidates(routeService, tokenIn, tokenOut, sourcePoolAddress, allRoutes) {
|
|
167
|
-
if (tokenIn.toLowerCase() === tokenOut.toLowerCase()) {
|
|
168
|
-
return [[]];
|
|
169
|
-
}
|
|
170
|
-
const rawCandidates = [];
|
|
171
|
-
try {
|
|
172
|
-
rawCandidates.push(await routeService.findRoute(tokenIn, tokenOut));
|
|
173
|
-
}
|
|
174
|
-
catch {
|
|
175
|
-
// Continue; we'll try the broader route set next.
|
|
176
|
-
}
|
|
177
|
-
const pairCandidates = (allRoutes ?? await routeService.getRoutes({ cached: false, returnAllRoutes: true })).filter((route) => {
|
|
178
|
-
const a0 = route.tokens[0].address.toLowerCase();
|
|
179
|
-
const a1 = route.tokens[1].address.toLowerCase();
|
|
180
|
-
const t0 = tokenIn.toLowerCase();
|
|
181
|
-
const t1 = tokenOut.toLowerCase();
|
|
182
|
-
return (a0 === t0 && a1 === t1) || (a0 === t1 && a1 === t0);
|
|
183
|
-
});
|
|
184
|
-
rawCandidates.push(...pairCandidates);
|
|
185
|
-
if (rawCandidates.length === 0) {
|
|
186
|
-
throw new RouteNotFoundError(tokenIn, tokenOut);
|
|
187
|
-
}
|
|
188
|
-
rawCandidates.sort((routeA, routeB) => {
|
|
189
|
-
const routeAUsesSourcePool = routeUsesPool(routeA, sourcePoolAddress) ? 1 : 0;
|
|
190
|
-
const routeBUsesSourcePool = routeUsesPool(routeB, sourcePoolAddress) ? 1 : 0;
|
|
191
|
-
if (routeAUsesSourcePool !== routeBUsesSourcePool)
|
|
192
|
-
return routeAUsesSourcePool - routeBUsesSourcePool;
|
|
193
|
-
if (routeA.path.length !== routeB.path.length)
|
|
194
|
-
return routeA.path.length - routeB.path.length;
|
|
195
|
-
return 0;
|
|
196
|
-
});
|
|
197
|
-
const encodedRoutes = [];
|
|
198
|
-
const seen = new Set();
|
|
199
|
-
for (const route of rawCandidates) {
|
|
200
|
-
try {
|
|
201
|
-
const encoded = encodeRoutePath(route.path, tokenIn, tokenOut);
|
|
202
|
-
const key = JSON.stringify(encoded);
|
|
203
|
-
if (seen.has(key))
|
|
204
|
-
continue;
|
|
205
|
-
seen.add(key);
|
|
206
|
-
encodedRoutes.push(encoded);
|
|
207
|
-
if (encodedRoutes.length >= MAX_ROUTE_CANDIDATES_PER_LEG) {
|
|
208
|
-
break;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
catch {
|
|
212
|
-
// Invalid path encoding for this direction; skip.
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
if (encodedRoutes.length === 0) {
|
|
216
|
-
throw new RouteNotFoundError(tokenIn, tokenOut);
|
|
217
|
-
}
|
|
218
|
-
return encodedRoutes;
|
|
219
|
-
}
|
|
220
|
-
function routeUsesPool(route, poolAddress) {
|
|
221
|
-
const normalizedPool = poolAddress.toLowerCase();
|
|
222
|
-
return route.path.some((hop) => hop.poolAddr.toLowerCase() === normalizedPool);
|
|
223
|
-
}
|
|
224
|
-
async function simulateZapOut(publicClient, owner, routerAddress, data) {
|
|
225
|
-
await publicClient.call({
|
|
226
|
-
account: owner,
|
|
227
|
-
to: routerAddress,
|
|
228
|
-
data,
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
function isInsufficientLiquidityError(error) {
|
|
232
|
-
const message = extractErrorMessage(error).toLowerCase();
|
|
233
|
-
return (message.includes(INSUFFICIENT_LIQUIDITY_SELECTOR) ||
|
|
234
|
-
message.includes('insufficientliquidity'));
|
|
235
|
-
}
|
|
236
|
-
function extractErrorMessage(error) {
|
|
237
|
-
if (!(error instanceof Error)) {
|
|
238
|
-
return String(error);
|
|
239
|
-
}
|
|
240
|
-
const typed = error;
|
|
241
|
-
const parts = [
|
|
242
|
-
typed.message,
|
|
243
|
-
typed.shortMessage,
|
|
244
|
-
typed.details,
|
|
245
|
-
typed.cause instanceof Error ? typed.cause.message : undefined,
|
|
246
|
-
].filter((part) => Boolean(part));
|
|
247
|
-
return parts.join(' | ');
|
|
248
|
-
}
|
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
import { PoolType } from '../../core/types';
|
|
2
|
-
import { fetchFPMMPools, fetchVirtualPools } from './poolDiscovery';
|
|
3
|
-
import { fetchFPMMPoolDetailsBatch, fetchVirtualPoolDetailsBatch, } from './poolDetails';
|
|
4
|
-
import { fetchPoolRebalancePreview, fetchPoolRebalancePreviewBatch, } from './rebalancePreview';
|
|
5
|
-
/**
|
|
6
|
-
* Service for discovering liquidity pools in the Mento protocol.
|
|
7
|
-
* Aggregates pools from multiple factory contracts (FPMM and VirtualPool).
|
|
8
|
-
*/
|
|
9
|
-
export class PoolService {
|
|
10
|
-
constructor(publicClient, chainId) {
|
|
11
|
-
this.publicClient = publicClient;
|
|
12
|
-
this.chainId = chainId;
|
|
13
|
-
this.poolsCache = null;
|
|
14
|
-
this.discoveryWarnings = [];
|
|
15
|
-
this.poolsPromise = null;
|
|
16
|
-
this.poolDetailsCache = new Map();
|
|
17
|
-
this.poolDetailPromises = new Map();
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Returns any warnings from the last pool discovery operation.
|
|
21
|
-
* Useful for debugging when some factories fail but others succeed.
|
|
22
|
-
*/
|
|
23
|
-
getDiscoveryWarnings() {
|
|
24
|
-
return [...this.discoveryWarnings];
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Fetches all pools available in the protocol from both FPMM and Virtual pool factories
|
|
28
|
-
* Results are cached in memory for the service instance lifetime
|
|
29
|
-
*
|
|
30
|
-
* @returns Array of all pools available in the protocol
|
|
31
|
-
* @throws {Error} If no pools can be discovered from any factory
|
|
32
|
-
*
|
|
33
|
-
* @example
|
|
34
|
-
* ```typescript
|
|
35
|
-
* const pools = await poolService.getPools()
|
|
36
|
-
* console.log(`Found ${pools.length} pools`)
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
async getPools() {
|
|
40
|
-
if (this.poolsCache) {
|
|
41
|
-
return this.poolsCache;
|
|
42
|
-
}
|
|
43
|
-
if (this.poolsPromise) {
|
|
44
|
-
return this.poolsPromise;
|
|
45
|
-
}
|
|
46
|
-
this.poolsPromise = this.loadPools();
|
|
47
|
-
try {
|
|
48
|
-
return await this.poolsPromise;
|
|
49
|
-
}
|
|
50
|
-
finally {
|
|
51
|
-
this.poolsPromise = null;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
async loadPools() {
|
|
55
|
-
const warnings = [];
|
|
56
|
-
const settled = await Promise.allSettled([
|
|
57
|
-
fetchFPMMPools(this.publicClient, this.chainId),
|
|
58
|
-
fetchVirtualPools(this.publicClient, this.chainId),
|
|
59
|
-
]);
|
|
60
|
-
const pools = [];
|
|
61
|
-
const [fpmmResult, virtualResult] = settled;
|
|
62
|
-
if (fpmmResult.status === 'fulfilled') {
|
|
63
|
-
pools.push(...fpmmResult.value);
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
warnings.push(`Failed to fetch FPMM pools: ${fpmmResult.reason instanceof Error ? fpmmResult.reason.message : String(fpmmResult.reason)}`);
|
|
67
|
-
}
|
|
68
|
-
if (virtualResult.status === 'fulfilled') {
|
|
69
|
-
pools.push(...virtualResult.value);
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
warnings.push(`Failed to fetch Virtual pools: ${virtualResult.reason instanceof Error ? virtualResult.reason.message : String(virtualResult.reason)}`);
|
|
73
|
-
}
|
|
74
|
-
this.discoveryWarnings = warnings;
|
|
75
|
-
// Only throw if NO pools were discovered from any factory
|
|
76
|
-
if (pools.length === 0) {
|
|
77
|
-
throw new Error('Failed to discover any pools from any factory. ' +
|
|
78
|
-
'All pool factory queries failed. Check network connectivity and RPC endpoint.');
|
|
79
|
-
}
|
|
80
|
-
this.poolsCache = pools;
|
|
81
|
-
return pools;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Fetches enriched on-chain details for a specific pool by address.
|
|
85
|
-
* Resolves the pool type from the discovery cache, then fetches
|
|
86
|
-
* pool-type-specific data (pricing, fees, rebalancing for FPMM; reserves and spread for Virtual).
|
|
87
|
-
*
|
|
88
|
-
* @param poolAddr - The deployed pool contract address
|
|
89
|
-
* @returns Enriched pool details (FPMMPoolDetails or VirtualPoolDetails)
|
|
90
|
-
* @throws {Error} If the pool address is not found in any known factory
|
|
91
|
-
* @throws {Error} If on-chain calls fail
|
|
92
|
-
*
|
|
93
|
-
* @example
|
|
94
|
-
* ```typescript
|
|
95
|
-
* const details = await poolService.getPoolDetails('0x...')
|
|
96
|
-
* if (details.poolType === 'FPMM') {
|
|
97
|
-
* console.log(details.pricing.oraclePrice)
|
|
98
|
-
* console.log(details.rebalancing.inBand)
|
|
99
|
-
* } else {
|
|
100
|
-
* console.log(details.spreadPercent)
|
|
101
|
-
* }
|
|
102
|
-
* ```
|
|
103
|
-
*/
|
|
104
|
-
async getPoolDetails(poolAddr) {
|
|
105
|
-
const [details] = await this.getPoolDetailsBatch([poolAddr]);
|
|
106
|
-
return details;
|
|
107
|
-
}
|
|
108
|
-
async getPoolRebalancePreview(poolAddr) {
|
|
109
|
-
const details = await this.getPoolDetails(poolAddr);
|
|
110
|
-
return fetchPoolRebalancePreview(this.publicClient, details);
|
|
111
|
-
}
|
|
112
|
-
async getPoolRebalancePreviewBatch(poolAddresses) {
|
|
113
|
-
const details = await this.getPoolDetailsBatch(poolAddresses);
|
|
114
|
-
return fetchPoolRebalancePreviewBatch(this.publicClient, details);
|
|
115
|
-
}
|
|
116
|
-
async getPoolDetailsBatch(poolAddresses) {
|
|
117
|
-
const pools = await this.getPools();
|
|
118
|
-
const targets = poolAddresses
|
|
119
|
-
? poolAddresses.map((poolAddress) => {
|
|
120
|
-
const pool = pools.find((candidate) => candidate.poolAddr.toLowerCase() === poolAddress.toLowerCase());
|
|
121
|
-
if (!pool) {
|
|
122
|
-
throw new Error(`Pool not found: ${poolAddress}. Ensure the address is a valid pool discovered by getPools().`);
|
|
123
|
-
}
|
|
124
|
-
return pool;
|
|
125
|
-
})
|
|
126
|
-
: pools;
|
|
127
|
-
const results = new Array(targets.length);
|
|
128
|
-
const pendingResults = [];
|
|
129
|
-
const missingTargets = [];
|
|
130
|
-
for (const [index, pool] of targets.entries()) {
|
|
131
|
-
const key = pool.poolAddr.toLowerCase();
|
|
132
|
-
const cached = this.poolDetailsCache.get(key);
|
|
133
|
-
if (cached) {
|
|
134
|
-
results[index] = cached;
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
const inFlight = this.poolDetailPromises.get(key);
|
|
138
|
-
if (inFlight) {
|
|
139
|
-
pendingResults.push(inFlight.then((detail) => {
|
|
140
|
-
results[index] = detail;
|
|
141
|
-
}));
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
missingTargets.push({ pool, index, key });
|
|
145
|
-
}
|
|
146
|
-
if (missingTargets.length > 0) {
|
|
147
|
-
const grouped = {
|
|
148
|
-
fpmm: missingTargets.filter(({ pool }) => pool.poolType === PoolType.FPMM),
|
|
149
|
-
virtual: missingTargets.filter(({ pool }) => pool.poolType !== PoolType.FPMM),
|
|
150
|
-
};
|
|
151
|
-
const createdPromises = new Map();
|
|
152
|
-
const createdPendingResults = [];
|
|
153
|
-
for (const target of missingTargets) {
|
|
154
|
-
const deferred = createDeferred();
|
|
155
|
-
this.poolDetailPromises.set(target.key, deferred.promise);
|
|
156
|
-
createdPromises.set(target.key, deferred);
|
|
157
|
-
const pendingResult = deferred.promise.then((detail) => {
|
|
158
|
-
results[target.index] = detail;
|
|
159
|
-
});
|
|
160
|
-
pendingResults.push(pendingResult);
|
|
161
|
-
createdPendingResults.push(pendingResult);
|
|
162
|
-
}
|
|
163
|
-
try {
|
|
164
|
-
const [fpmmDetails, virtualDetails] = await Promise.all([
|
|
165
|
-
fetchFPMMPoolDetailsBatch(this.publicClient, this.chainId, grouped.fpmm.map(({ pool }) => pool)),
|
|
166
|
-
fetchVirtualPoolDetailsBatch(this.publicClient, grouped.virtual.map(({ pool }) => pool)),
|
|
167
|
-
]);
|
|
168
|
-
for (const [groupIndex, detail] of fpmmDetails.entries()) {
|
|
169
|
-
const target = grouped.fpmm[groupIndex];
|
|
170
|
-
this.poolDetailsCache.set(target.key, detail);
|
|
171
|
-
createdPromises.get(target.key)?.resolve(detail);
|
|
172
|
-
}
|
|
173
|
-
for (const [groupIndex, detail] of virtualDetails.entries()) {
|
|
174
|
-
const target = grouped.virtual[groupIndex];
|
|
175
|
-
this.poolDetailsCache.set(target.key, detail);
|
|
176
|
-
createdPromises.get(target.key)?.resolve(detail);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
catch (error) {
|
|
180
|
-
for (const target of missingTargets) {
|
|
181
|
-
createdPromises.get(target.key)?.reject(error);
|
|
182
|
-
}
|
|
183
|
-
await Promise.allSettled(createdPendingResults);
|
|
184
|
-
throw error;
|
|
185
|
-
}
|
|
186
|
-
finally {
|
|
187
|
-
for (const target of missingTargets) {
|
|
188
|
-
this.poolDetailPromises.delete(target.key);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
await Promise.all(pendingResults);
|
|
193
|
-
return results;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
function createDeferred() {
|
|
197
|
-
let resolve;
|
|
198
|
-
let reject;
|
|
199
|
-
const promise = new Promise((resolvePromise, rejectPromise) => {
|
|
200
|
-
resolve = resolvePromise;
|
|
201
|
-
reject = rejectPromise;
|
|
202
|
-
});
|
|
203
|
-
return { promise, resolve, reject };
|
|
204
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./PoolService";
|
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
import { tryGetContractAddress } from '../../core/constants';
|
|
2
|
-
import { FPMM_ABI, VIRTUAL_POOL_ABI } from '../../core/abis';
|
|
3
|
-
import { getAddress } from 'viem';
|
|
4
|
-
import { multicall } from '../../utils/multicall';
|
|
5
|
-
const FPMM_FIXED_RESULT_COUNT = 8;
|
|
6
|
-
const VIRTUAL_RESULT_COUNT = 3;
|
|
7
|
-
/**
|
|
8
|
-
* Fetches enriched details for an FPMM pool
|
|
9
|
-
*/
|
|
10
|
-
export async function fetchFPMMPoolDetails(publicClient, chainId, pool) {
|
|
11
|
-
const [details] = await fetchFPMMPoolDetailsBatch(publicClient, chainId, [pool]);
|
|
12
|
-
return details;
|
|
13
|
-
}
|
|
14
|
-
export async function fetchFPMMPoolDetailsBatch(publicClient, chainId, pools) {
|
|
15
|
-
if (pools.length === 0) {
|
|
16
|
-
return [];
|
|
17
|
-
}
|
|
18
|
-
const openLiquidityStrategy = getOpenLiquidityStrategy(chainId);
|
|
19
|
-
const contracts = pools.flatMap((pool) => buildFPMMContracts(pool, openLiquidityStrategy));
|
|
20
|
-
const results = await multicall(publicClient, contracts);
|
|
21
|
-
const strategyCheckCount = openLiquidityStrategy ? 1 : 0;
|
|
22
|
-
const perPoolResultCount = FPMM_FIXED_RESULT_COUNT + strategyCheckCount + 1;
|
|
23
|
-
return pools.map((pool, index) => {
|
|
24
|
-
const offset = index * perPoolResultCount;
|
|
25
|
-
const poolResults = results.slice(offset, offset + perPoolResultCount);
|
|
26
|
-
return parseFPMMPoolDetails(pool, openLiquidityStrategy, poolResults);
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
function buildFPMMContracts(pool, openLiquidityStrategy) {
|
|
30
|
-
const address = pool.poolAddr;
|
|
31
|
-
return [
|
|
32
|
-
{ address, abi: FPMM_ABI, functionName: 'getReserves' },
|
|
33
|
-
{ address, abi: FPMM_ABI, functionName: 'decimals0' },
|
|
34
|
-
{ address, abi: FPMM_ABI, functionName: 'decimals1' },
|
|
35
|
-
{ address, abi: FPMM_ABI, functionName: 'lpFee' },
|
|
36
|
-
{ address, abi: FPMM_ABI, functionName: 'protocolFee' },
|
|
37
|
-
{ address, abi: FPMM_ABI, functionName: 'rebalanceIncentive' },
|
|
38
|
-
{ address, abi: FPMM_ABI, functionName: 'rebalanceThresholdAbove' },
|
|
39
|
-
{ address, abi: FPMM_ABI, functionName: 'rebalanceThresholdBelow' },
|
|
40
|
-
...(openLiquidityStrategy
|
|
41
|
-
? [{
|
|
42
|
-
address,
|
|
43
|
-
abi: FPMM_ABI,
|
|
44
|
-
functionName: 'liquidityStrategy',
|
|
45
|
-
args: [openLiquidityStrategy],
|
|
46
|
-
}]
|
|
47
|
-
: []),
|
|
48
|
-
{ address, abi: FPMM_ABI, functionName: 'getRebalancingState' },
|
|
49
|
-
];
|
|
50
|
-
}
|
|
51
|
-
function parseFPMMPoolDetails(pool, openLiquidityStrategy, results) {
|
|
52
|
-
try {
|
|
53
|
-
const reservesRes = results[0];
|
|
54
|
-
const decimals0Res = results[1];
|
|
55
|
-
const decimals1Res = results[2];
|
|
56
|
-
const lpFeeRes = results[3];
|
|
57
|
-
const protocolFeeRes = results[4];
|
|
58
|
-
const rebalanceIncentiveRes = results[5];
|
|
59
|
-
const thresholdAboveRes = results[6];
|
|
60
|
-
const thresholdBelowRes = results[7];
|
|
61
|
-
if (!reservesRes ||
|
|
62
|
-
!decimals0Res ||
|
|
63
|
-
!decimals1Res ||
|
|
64
|
-
!lpFeeRes ||
|
|
65
|
-
!protocolFeeRes ||
|
|
66
|
-
!rebalanceIncentiveRes ||
|
|
67
|
-
!thresholdAboveRes ||
|
|
68
|
-
!thresholdBelowRes ||
|
|
69
|
-
reservesRes.status === 'failure' ||
|
|
70
|
-
decimals0Res.status === 'failure' ||
|
|
71
|
-
decimals1Res.status === 'failure' ||
|
|
72
|
-
lpFeeRes.status === 'failure' ||
|
|
73
|
-
protocolFeeRes.status === 'failure' ||
|
|
74
|
-
rebalanceIncentiveRes.status === 'failure' ||
|
|
75
|
-
thresholdAboveRes.status === 'failure' ||
|
|
76
|
-
thresholdBelowRes.status === 'failure') {
|
|
77
|
-
throw new Error('One or more core pool reads failed');
|
|
78
|
-
}
|
|
79
|
-
const [reserve0, reserve1, blockTimestampLast] = reservesRes.result;
|
|
80
|
-
const lpFeeBps = lpFeeRes.result;
|
|
81
|
-
const protocolFeeBps = protocolFeeRes.result;
|
|
82
|
-
const rebalanceIncentiveBps = rebalanceIncentiveRes.result;
|
|
83
|
-
const thresholdAboveBps = thresholdAboveRes.result;
|
|
84
|
-
const thresholdBelowBps = thresholdBelowRes.result;
|
|
85
|
-
const strategyCheckCount = openLiquidityStrategy ? 1 : 0;
|
|
86
|
-
const openStrategyResult = strategyCheckCount > 0 ? results[FPMM_FIXED_RESULT_COUNT] : null;
|
|
87
|
-
const liquidityStrategy = openLiquidityStrategy &&
|
|
88
|
-
openStrategyResult?.status === 'success' &&
|
|
89
|
-
openStrategyResult.result === true
|
|
90
|
-
? openLiquidityStrategy
|
|
91
|
-
: null;
|
|
92
|
-
const rebalancingRes = results[FPMM_FIXED_RESULT_COUNT + strategyCheckCount];
|
|
93
|
-
let pricing = null;
|
|
94
|
-
let inBand = null;
|
|
95
|
-
if (rebalancingRes?.status === 'success') {
|
|
96
|
-
const [oraclePriceNum, oraclePriceDen, reservePriceNum, reservePriceDen, reservePriceAboveOraclePrice, rebalanceThreshold, priceDifference,] = rebalancingRes.result;
|
|
97
|
-
pricing = {
|
|
98
|
-
oraclePriceNum,
|
|
99
|
-
oraclePriceDen,
|
|
100
|
-
oraclePrice: Number(oraclePriceNum) / Number(oraclePriceDen),
|
|
101
|
-
reservePriceNum,
|
|
102
|
-
reservePriceDen,
|
|
103
|
-
reservePrice: Number(reservePriceNum) / Number(reservePriceDen),
|
|
104
|
-
priceDifferenceBps: priceDifference,
|
|
105
|
-
priceDifferencePercent: Number(priceDifference) / 100,
|
|
106
|
-
reservePriceAboveOraclePrice,
|
|
107
|
-
};
|
|
108
|
-
inBand = priceDifference < BigInt(rebalanceThreshold);
|
|
109
|
-
}
|
|
110
|
-
return {
|
|
111
|
-
...pool,
|
|
112
|
-
poolType: 'FPMM',
|
|
113
|
-
scalingFactor0: decimals0Res.result,
|
|
114
|
-
scalingFactor1: decimals1Res.result,
|
|
115
|
-
reserve0,
|
|
116
|
-
reserve1,
|
|
117
|
-
blockTimestampLast,
|
|
118
|
-
pricing,
|
|
119
|
-
fees: {
|
|
120
|
-
lpFeeBps,
|
|
121
|
-
lpFeePercent: Number(lpFeeBps) / 100,
|
|
122
|
-
protocolFeeBps,
|
|
123
|
-
protocolFeePercent: Number(protocolFeeBps) / 100,
|
|
124
|
-
totalFeePercent: (Number(lpFeeBps) + Number(protocolFeeBps)) / 100,
|
|
125
|
-
},
|
|
126
|
-
rebalancing: {
|
|
127
|
-
rebalanceIncentiveBps,
|
|
128
|
-
rebalanceIncentivePercent: Number(rebalanceIncentiveBps) / 100,
|
|
129
|
-
rebalanceThresholdAboveBps: thresholdAboveBps,
|
|
130
|
-
rebalanceThresholdAbovePercent: Number(thresholdAboveBps) / 100,
|
|
131
|
-
rebalanceThresholdBelowBps: thresholdBelowBps,
|
|
132
|
-
rebalanceThresholdBelowPercent: Number(thresholdBelowBps) / 100,
|
|
133
|
-
inBand,
|
|
134
|
-
liquidityStrategy,
|
|
135
|
-
},
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
catch (error) {
|
|
139
|
-
throw new Error(`Failed to fetch FPMM pool details for ${pool.poolAddr}: ${error.message}`);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Fetches enriched details for a Virtual pool
|
|
144
|
-
*/
|
|
145
|
-
export async function fetchVirtualPoolDetails(publicClient, pool) {
|
|
146
|
-
const [details] = await fetchVirtualPoolDetailsBatch(publicClient, [pool]);
|
|
147
|
-
return details;
|
|
148
|
-
}
|
|
149
|
-
export async function fetchVirtualPoolDetailsBatch(publicClient, pools) {
|
|
150
|
-
if (pools.length === 0) {
|
|
151
|
-
return [];
|
|
152
|
-
}
|
|
153
|
-
const contracts = pools.flatMap((pool) => buildVirtualContracts(pool));
|
|
154
|
-
const results = await multicall(publicClient, contracts);
|
|
155
|
-
return pools.map((pool, index) => {
|
|
156
|
-
const offset = index * VIRTUAL_RESULT_COUNT;
|
|
157
|
-
const poolResults = results.slice(offset, offset + VIRTUAL_RESULT_COUNT);
|
|
158
|
-
return parseVirtualPoolDetails(pool, poolResults);
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
function buildVirtualContracts(pool) {
|
|
162
|
-
const address = pool.poolAddr;
|
|
163
|
-
return [
|
|
164
|
-
{ address, abi: VIRTUAL_POOL_ABI, functionName: 'getReserves' },
|
|
165
|
-
{ address, abi: VIRTUAL_POOL_ABI, functionName: 'protocolFee' },
|
|
166
|
-
{ address, abi: VIRTUAL_POOL_ABI, functionName: 'metadata' },
|
|
167
|
-
];
|
|
168
|
-
}
|
|
169
|
-
function parseVirtualPoolDetails(pool, results) {
|
|
170
|
-
try {
|
|
171
|
-
if (results.length !== VIRTUAL_RESULT_COUNT ||
|
|
172
|
-
results[0].status === 'failure' ||
|
|
173
|
-
results[1].status === 'failure' ||
|
|
174
|
-
results[2].status === 'failure') {
|
|
175
|
-
throw new Error('One or more virtual pool reads failed');
|
|
176
|
-
}
|
|
177
|
-
const [reserve0, reserve1, blockTimestampLast] = results[0].result;
|
|
178
|
-
const [dec0, dec1] = results[2].result;
|
|
179
|
-
const spreadBps = results[1].result;
|
|
180
|
-
return {
|
|
181
|
-
...pool,
|
|
182
|
-
poolType: 'Virtual',
|
|
183
|
-
scalingFactor0: dec0,
|
|
184
|
-
scalingFactor1: dec1,
|
|
185
|
-
reserve0,
|
|
186
|
-
reserve1,
|
|
187
|
-
blockTimestampLast,
|
|
188
|
-
spreadBps,
|
|
189
|
-
spreadPercent: Number(spreadBps) / 100,
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
catch (error) {
|
|
193
|
-
throw new Error(`Failed to fetch Virtual pool details for ${pool.poolAddr}: ${error.message}`);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Returns the configured Open Liquidity Strategy for the given chain.
|
|
198
|
-
*/
|
|
199
|
-
function getOpenLiquidityStrategy(chainId) {
|
|
200
|
-
const strategyAddress = tryGetContractAddress(chainId, 'OpenLiquidityStrategy');
|
|
201
|
-
if (!strategyAddress)
|
|
202
|
-
return null;
|
|
203
|
-
try {
|
|
204
|
-
return getAddress(strategyAddress);
|
|
205
|
-
}
|
|
206
|
-
catch {
|
|
207
|
-
return null;
|
|
208
|
-
}
|
|
209
|
-
}
|