@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,64 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.buildRebalanceParamsInternal = buildRebalanceParamsInternal;
|
|
4
|
-
exports.buildRebalanceTransactionInternal = buildRebalanceTransactionInternal;
|
|
5
|
-
const viem_1 = require("viem");
|
|
6
|
-
const abis_1 = require("../../core/abis");
|
|
7
|
-
const validation_1 = require("../../utils/validation");
|
|
8
|
-
const liquidityHelpers_1 = require("./liquidityHelpers");
|
|
9
|
-
function getRebalanceUnavailableError(poolAddress) {
|
|
10
|
-
return new Error(`Pool ${poolAddress} is not currently rebalanceable or does not have a supported liquidity strategy.`);
|
|
11
|
-
}
|
|
12
|
-
function assertRebalanceActionAmounts(poolAddress, amountRequired, amountTransferred) {
|
|
13
|
-
if (amountRequired <= 0n || amountTransferred <= 0n) {
|
|
14
|
-
throw new Error(`Rebalance action for pool ${poolAddress} has zero amounts and cannot be executed.`);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
async function buildRebalanceParamsInternal(publicClient, chainId, poolService, poolAddress) {
|
|
18
|
-
(0, validation_1.validateAddress)(poolAddress, 'poolAddress');
|
|
19
|
-
const preview = await poolService.getPoolRebalancePreview(poolAddress);
|
|
20
|
-
if (!preview) {
|
|
21
|
-
throw getRebalanceUnavailableError(poolAddress);
|
|
22
|
-
}
|
|
23
|
-
assertRebalanceActionAmounts(poolAddress, preview.amountRequired.amount, preview.amountTransferred.amount);
|
|
24
|
-
const data = (0, viem_1.encodeFunctionData)({
|
|
25
|
-
abi: abis_1.LIQUIDITY_STRATEGY_ABI,
|
|
26
|
-
functionName: 'rebalance',
|
|
27
|
-
args: [poolAddress],
|
|
28
|
-
});
|
|
29
|
-
return {
|
|
30
|
-
params: {
|
|
31
|
-
to: preview.strategyAddress,
|
|
32
|
-
data,
|
|
33
|
-
value: '0',
|
|
34
|
-
},
|
|
35
|
-
poolAddress,
|
|
36
|
-
strategyAddress: preview.strategyAddress,
|
|
37
|
-
inputToken: preview.inputToken,
|
|
38
|
-
outputToken: preview.outputToken,
|
|
39
|
-
amountRequired: preview.amountRequired.amount,
|
|
40
|
-
expectedAmountTransferred: preview.amountTransferred.amount,
|
|
41
|
-
expectedProtocolIncentive: preview.protocolIncentive.amount,
|
|
42
|
-
expectedLiquiditySourceIncentive: preview.liquiditySourceIncentive.amount,
|
|
43
|
-
approvalToken: preview.approvalToken,
|
|
44
|
-
approvalSpender: preview.approvalSpender,
|
|
45
|
-
approvalAmount: preview.approvalAmount,
|
|
46
|
-
direction: preview.direction,
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
async function buildRebalanceTransactionInternal(publicClient, chainId, poolService, poolAddress, owner) {
|
|
50
|
-
(0, validation_1.validateAddress)(owner, 'owner');
|
|
51
|
-
const rebalance = await buildRebalanceParamsInternal(publicClient, chainId, poolService, poolAddress);
|
|
52
|
-
const approvalToken = rebalance.approvalToken;
|
|
53
|
-
const approvalSpender = rebalance.approvalSpender;
|
|
54
|
-
const currentAllowance = await (0, liquidityHelpers_1.getAllowance)(publicClient, approvalToken, owner, chainId, approvalSpender);
|
|
55
|
-
const approval = currentAllowance < rebalance.approvalAmount
|
|
56
|
-
? {
|
|
57
|
-
token: rebalance.approvalToken,
|
|
58
|
-
amount: rebalance.approvalAmount,
|
|
59
|
-
params: (0, liquidityHelpers_1.buildApprovalParams)(chainId, approvalToken, rebalance.approvalAmount, approvalSpender),
|
|
60
|
-
}
|
|
61
|
-
: null;
|
|
62
|
-
return { approval, rebalance };
|
|
63
|
-
}
|
|
64
|
-
//# sourceMappingURL=rebalance.js.map
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { Address } from 'viem';
|
|
2
|
-
import { RouteService } from '../routes';
|
|
3
|
-
import { ZapParams } from '../../core/types';
|
|
4
|
-
import { RouterRoute } from '../../utils/pathEncoder';
|
|
5
|
-
/**
|
|
6
|
-
* Encodes the zapIn function call for the Router contract
|
|
7
|
-
*/
|
|
8
|
-
export declare function encodeZapInCall(tokenIn: Address, amountInA: bigint, amountInB: bigint, zapParams: ZapParams, routesA: RouterRoute[], routesB: RouterRoute[], recipient: Address): string;
|
|
9
|
-
/**
|
|
10
|
-
* Encodes the zapOut function call for the Router contract
|
|
11
|
-
*/
|
|
12
|
-
export declare function encodeZapOutCall(tokenOut: Address, liquidity: bigint, zapParams: ZapParams, routesA: RouterRoute[], routesB: RouterRoute[]): string;
|
|
13
|
-
/**
|
|
14
|
-
* Finds routes for zap in operations (from tokenIn to both pool tokens)
|
|
15
|
-
*
|
|
16
|
-
* @param routeService - Route service for finding swap paths
|
|
17
|
-
* @param tokenIn - Input token address
|
|
18
|
-
* @param token0 - Pool's token0 address
|
|
19
|
-
* @param token1 - Pool's token1 address
|
|
20
|
-
* @returns Routes from tokenIn to token0 and token1
|
|
21
|
-
*/
|
|
22
|
-
export declare function findZapInRoutes(routeService: RouteService, tokenIn: string, token0: string, token1: string): Promise<{
|
|
23
|
-
routesA: RouterRoute[];
|
|
24
|
-
routesB: RouterRoute[];
|
|
25
|
-
}>;
|
|
26
|
-
/**
|
|
27
|
-
* Finds routes for zap out operations (from both pool tokens to tokenOut)
|
|
28
|
-
*
|
|
29
|
-
* @param routeService - Route service for finding swap paths
|
|
30
|
-
* @param token0 - Pool's token0 address
|
|
31
|
-
* @param token1 - Pool's token1 address
|
|
32
|
-
* @param tokenOut - Output token address
|
|
33
|
-
* @returns Routes from token0 and token1 to tokenOut
|
|
34
|
-
*/
|
|
35
|
-
export declare function findZapOutRoutes(routeService: RouteService, token0: string, token1: string, tokenOut: string): Promise<{
|
|
36
|
-
routesA: RouterRoute[];
|
|
37
|
-
routesB: RouterRoute[];
|
|
38
|
-
}>;
|
|
39
|
-
/**
|
|
40
|
-
* Splits an amount into two parts based on a ratio
|
|
41
|
-
*
|
|
42
|
-
* @param amountIn - Total input amount
|
|
43
|
-
* @param splitRatio - Ratio for splitting (0-1, e.g., 0.5 for 50/50)
|
|
44
|
-
* @returns Split amounts for each part
|
|
45
|
-
*/
|
|
46
|
-
export declare function splitAmount(amountIn: bigint, splitRatio: number): {
|
|
47
|
-
amountA: bigint;
|
|
48
|
-
amountB: bigint;
|
|
49
|
-
};
|
|
50
|
-
/**
|
|
51
|
-
* Estimates minimum LP tokens from zap in amounts.
|
|
52
|
-
*
|
|
53
|
-
* This is a conservative lower-bound estimate. The inputs are slippage-adjusted
|
|
54
|
-
* minimums, not expected amounts. Actual LP tokens minted on-chain may be higher
|
|
55
|
-
* because the router uses balanceOf(address(this)) after swaps, which can exceed
|
|
56
|
-
* the pre-calculated minimums.
|
|
57
|
-
*
|
|
58
|
-
* @param amountOutA - Minimum amount of token0 after swap (slippage-adjusted)
|
|
59
|
-
* @param amountOutB - Minimum amount of token1 after swap (slippage-adjusted)
|
|
60
|
-
* @param reserve0 - Current reserve of token0 in pool
|
|
61
|
-
* @param reserve1 - Current reserve of token1 in pool
|
|
62
|
-
* @param totalSupply - Total LP token supply
|
|
63
|
-
* @returns Conservative estimate of minimum LP tokens to be minted
|
|
64
|
-
*/
|
|
65
|
-
export declare function estimateLiquidityFromZapIn(amountOutA: bigint, amountOutB: bigint, reserve0: bigint, reserve1: bigint, totalSupply: bigint): bigint;
|
|
66
|
-
//# sourceMappingURL=zapHelpers.d.ts.map
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.encodeZapInCall = encodeZapInCall;
|
|
4
|
-
exports.encodeZapOutCall = encodeZapOutCall;
|
|
5
|
-
exports.findZapInRoutes = findZapInRoutes;
|
|
6
|
-
exports.findZapOutRoutes = findZapOutRoutes;
|
|
7
|
-
exports.splitAmount = splitAmount;
|
|
8
|
-
exports.estimateLiquidityFromZapIn = estimateLiquidityFromZapIn;
|
|
9
|
-
const viem_1 = require("viem");
|
|
10
|
-
const abis_1 = require("../../core/abis");
|
|
11
|
-
const pathEncoder_1 = require("../../utils/pathEncoder");
|
|
12
|
-
// ========== ENCODING FUNCTIONS ==========
|
|
13
|
-
/**
|
|
14
|
-
* Encodes the zapIn function call for the Router contract
|
|
15
|
-
*/
|
|
16
|
-
function encodeZapInCall(tokenIn, amountInA, amountInB, zapParams, routesA, routesB, recipient) {
|
|
17
|
-
return (0, viem_1.encodeFunctionData)({
|
|
18
|
-
abi: abis_1.ROUTER_ABI,
|
|
19
|
-
functionName: 'zapIn',
|
|
20
|
-
args: [tokenIn, amountInA, amountInB, zapParams, routesA, routesB, recipient],
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Encodes the zapOut function call for the Router contract
|
|
25
|
-
*/
|
|
26
|
-
function encodeZapOutCall(tokenOut, liquidity, zapParams, routesA, routesB) {
|
|
27
|
-
return (0, viem_1.encodeFunctionData)({
|
|
28
|
-
abi: abis_1.ROUTER_ABI,
|
|
29
|
-
functionName: 'zapOut',
|
|
30
|
-
args: [tokenOut, liquidity, zapParams, routesA, routesB],
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
// ========== ROUTING FUNCTIONS ==========
|
|
34
|
-
/**
|
|
35
|
-
* Finds routes for zap in operations (from tokenIn to both pool tokens)
|
|
36
|
-
*
|
|
37
|
-
* @param routeService - Route service for finding swap paths
|
|
38
|
-
* @param tokenIn - Input token address
|
|
39
|
-
* @param token0 - Pool's token0 address
|
|
40
|
-
* @param token1 - Pool's token1 address
|
|
41
|
-
* @returns Routes from tokenIn to token0 and token1
|
|
42
|
-
*/
|
|
43
|
-
async function findZapInRoutes(routeService, tokenIn, token0, token1) {
|
|
44
|
-
const [routeA, routeB] = await Promise.all([
|
|
45
|
-
tokenIn.toLowerCase() !== token0.toLowerCase()
|
|
46
|
-
? routeService.findRoute(tokenIn, token0)
|
|
47
|
-
: Promise.resolve(null),
|
|
48
|
-
tokenIn.toLowerCase() !== token1.toLowerCase()
|
|
49
|
-
? routeService.findRoute(tokenIn, token1)
|
|
50
|
-
: Promise.resolve(null),
|
|
51
|
-
]);
|
|
52
|
-
const routesA = routeA ? (0, pathEncoder_1.encodeRoutePath)(routeA.path, tokenIn, token0) : [];
|
|
53
|
-
const routesB = routeB ? (0, pathEncoder_1.encodeRoutePath)(routeB.path, tokenIn, token1) : [];
|
|
54
|
-
return { routesA, routesB };
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Finds routes for zap out operations (from both pool tokens to tokenOut)
|
|
58
|
-
*
|
|
59
|
-
* @param routeService - Route service for finding swap paths
|
|
60
|
-
* @param token0 - Pool's token0 address
|
|
61
|
-
* @param token1 - Pool's token1 address
|
|
62
|
-
* @param tokenOut - Output token address
|
|
63
|
-
* @returns Routes from token0 and token1 to tokenOut
|
|
64
|
-
*/
|
|
65
|
-
async function findZapOutRoutes(routeService, token0, token1, tokenOut) {
|
|
66
|
-
const [routeA, routeB] = await Promise.all([
|
|
67
|
-
token0.toLowerCase() !== tokenOut.toLowerCase()
|
|
68
|
-
? routeService.findRoute(token0, tokenOut)
|
|
69
|
-
: Promise.resolve(null),
|
|
70
|
-
token1.toLowerCase() !== tokenOut.toLowerCase()
|
|
71
|
-
? routeService.findRoute(token1, tokenOut)
|
|
72
|
-
: Promise.resolve(null),
|
|
73
|
-
]);
|
|
74
|
-
const routesA = routeA ? (0, pathEncoder_1.encodeRoutePath)(routeA.path, token0, tokenOut) : [];
|
|
75
|
-
const routesB = routeB ? (0, pathEncoder_1.encodeRoutePath)(routeB.path, token1, tokenOut) : [];
|
|
76
|
-
return { routesA, routesB };
|
|
77
|
-
}
|
|
78
|
-
// ========== CALCULATION FUNCTIONS ==========
|
|
79
|
-
/**
|
|
80
|
-
* Splits an amount into two parts based on a ratio
|
|
81
|
-
*
|
|
82
|
-
* @param amountIn - Total input amount
|
|
83
|
-
* @param splitRatio - Ratio for splitting (0-1, e.g., 0.5 for 50/50)
|
|
84
|
-
* @returns Split amounts for each part
|
|
85
|
-
*/
|
|
86
|
-
function splitAmount(amountIn, splitRatio) {
|
|
87
|
-
if (splitRatio < 0 || splitRatio > 1) {
|
|
88
|
-
throw new Error('Split ratio must be between 0 and 1');
|
|
89
|
-
}
|
|
90
|
-
const amountA = (amountIn * BigInt(Math.floor(splitRatio * 10000))) / 10000n;
|
|
91
|
-
const amountB = amountIn - amountA;
|
|
92
|
-
return { amountA, amountB };
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Estimates minimum LP tokens from zap in amounts.
|
|
96
|
-
*
|
|
97
|
-
* This is a conservative lower-bound estimate. The inputs are slippage-adjusted
|
|
98
|
-
* minimums, not expected amounts. Actual LP tokens minted on-chain may be higher
|
|
99
|
-
* because the router uses balanceOf(address(this)) after swaps, which can exceed
|
|
100
|
-
* the pre-calculated minimums.
|
|
101
|
-
*
|
|
102
|
-
* @param amountOutA - Minimum amount of token0 after swap (slippage-adjusted)
|
|
103
|
-
* @param amountOutB - Minimum amount of token1 after swap (slippage-adjusted)
|
|
104
|
-
* @param reserve0 - Current reserve of token0 in pool
|
|
105
|
-
* @param reserve1 - Current reserve of token1 in pool
|
|
106
|
-
* @param totalSupply - Total LP token supply
|
|
107
|
-
* @returns Conservative estimate of minimum LP tokens to be minted
|
|
108
|
-
*/
|
|
109
|
-
function estimateLiquidityFromZapIn(amountOutA, amountOutB, reserve0, reserve1, totalSupply) {
|
|
110
|
-
if (totalSupply === 0n) {
|
|
111
|
-
// First liquidity provision - use geometric mean with BigInt sqrt
|
|
112
|
-
// to avoid precision loss from Number() conversion on 18-decimal values
|
|
113
|
-
const product = amountOutA * amountOutB;
|
|
114
|
-
if (product === 0n)
|
|
115
|
-
return 0n;
|
|
116
|
-
let x = product;
|
|
117
|
-
let y = (x + 1n) / 2n;
|
|
118
|
-
while (y < x) {
|
|
119
|
-
x = y;
|
|
120
|
-
y = (x + product / x) / 2n;
|
|
121
|
-
}
|
|
122
|
-
return x;
|
|
123
|
-
}
|
|
124
|
-
// Existing pool - calculate based on smaller ratio
|
|
125
|
-
const liquidityA = (amountOutA * totalSupply) / reserve0;
|
|
126
|
-
const liquidityB = (amountOutB * totalSupply) / reserve1;
|
|
127
|
-
return liquidityA < liquidityB ? liquidityA : liquidityB;
|
|
128
|
-
}
|
|
129
|
-
//# sourceMappingURL=zapHelpers.js.map
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { PublicClient } from 'viem';
|
|
2
|
-
import { PoolService } from '../pools';
|
|
3
|
-
import { RouteService } from '../routes';
|
|
4
|
-
import { LiquidityOptions, PreparedZapIn, ZapInQuote, ZapInDetails, ZapInTransaction } from '../../core/types';
|
|
5
|
-
/**
|
|
6
|
-
* Builds a complete zap in transaction including approval if needed
|
|
7
|
-
*/
|
|
8
|
-
export declare function buildZapInTransactionInternal(publicClient: PublicClient, chainId: number, poolService: PoolService, routeService: RouteService, poolAddress: string, tokenIn: string, amountIn: bigint, amountInSplit: number, recipient: string, owner: string, options: LiquidityOptions): Promise<ZapInTransaction>;
|
|
9
|
-
/**
|
|
10
|
-
* Builds zap in transaction parameters without checking approval
|
|
11
|
-
*/
|
|
12
|
-
export declare function buildZapInParamsInternal(publicClient: PublicClient, chainId: number, poolService: PoolService, routeService: RouteService, poolAddress: string, tokenIn: string, amountIn: bigint, amountInSplit: number, recipient: string, options: LiquidityOptions): Promise<ZapInDetails>;
|
|
13
|
-
/**
|
|
14
|
-
* Quotes a zap in operation (read-only)
|
|
15
|
-
*/
|
|
16
|
-
export declare function quoteZapInInternal(publicClient: PublicClient, chainId: number, poolService: PoolService, routeService: RouteService, poolAddress: string, tokenIn: string, amountIn: bigint, amountInSplit: number, options: LiquidityOptions): Promise<ZapInQuote>;
|
|
17
|
-
export declare function prepareZapInInternal(publicClient: PublicClient, chainId: number, poolService: PoolService, routeService: RouteService, poolAddress: string, tokenIn: string, amountIn: bigint, amountInSplit: number, recipient: string, owner: string | undefined, options: LiquidityOptions): Promise<PreparedZapIn>;
|
|
18
|
-
//# sourceMappingURL=zapIn.d.ts.map
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.buildZapInTransactionInternal = buildZapInTransactionInternal;
|
|
4
|
-
exports.buildZapInParamsInternal = buildZapInParamsInternal;
|
|
5
|
-
exports.quoteZapInInternal = quoteZapInInternal;
|
|
6
|
-
exports.prepareZapInInternal = prepareZapInInternal;
|
|
7
|
-
const abis_1 = require("../../core/abis");
|
|
8
|
-
const constants_1 = require("../../core/constants");
|
|
9
|
-
const validation_1 = require("../../utils/validation");
|
|
10
|
-
const liquidityHelpers_1 = require("./liquidityHelpers");
|
|
11
|
-
const zapHelpers_1 = require("./zapHelpers");
|
|
12
|
-
// ========== ZAP IN OPERATIONS ==========
|
|
13
|
-
/**
|
|
14
|
-
* Builds a complete zap in transaction including approval if needed
|
|
15
|
-
*/
|
|
16
|
-
async function buildZapInTransactionInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenIn, amountIn, amountInSplit, recipient, owner, options) {
|
|
17
|
-
const prepared = await prepareZapInInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenIn, amountIn, amountInSplit, recipient, owner, options);
|
|
18
|
-
return {
|
|
19
|
-
approval: prepared.approval ?? null,
|
|
20
|
-
zapIn: prepared.details,
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Builds zap in transaction parameters without checking approval
|
|
25
|
-
*/
|
|
26
|
-
async function buildZapInParamsInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenIn, amountIn, amountInSplit, recipient, options) {
|
|
27
|
-
const prepared = await prepareZapInInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenIn, amountIn, amountInSplit, recipient, undefined, options);
|
|
28
|
-
return prepared.details;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Quotes a zap in operation (read-only)
|
|
32
|
-
*/
|
|
33
|
-
async function quoteZapInInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenIn, amountIn, amountInSplit, options) {
|
|
34
|
-
const prepared = await prepareZapInInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenIn, amountIn, amountInSplit, tokenIn, undefined, options);
|
|
35
|
-
return prepared.quote;
|
|
36
|
-
}
|
|
37
|
-
async function prepareZapInInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenIn, amountIn, amountInSplit, recipient, owner, options) {
|
|
38
|
-
if (owner) {
|
|
39
|
-
(0, validation_1.validateAddress)(owner, 'owner');
|
|
40
|
-
}
|
|
41
|
-
const [context, currentAllowance] = await Promise.all([
|
|
42
|
-
prepareZapInContextInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenIn, amountIn, amountInSplit, recipient, options),
|
|
43
|
-
owner ? (0, liquidityHelpers_1.getAllowance)(publicClient, tokenIn, owner, chainId) : Promise.resolve(null),
|
|
44
|
-
]);
|
|
45
|
-
const approval = owner && currentAllowance !== null && currentAllowance < amountIn
|
|
46
|
-
? { token: tokenIn, amount: amountIn, params: (0, liquidityHelpers_1.buildApprovalParams)(chainId, tokenIn, amountIn) }
|
|
47
|
-
: owner
|
|
48
|
-
? null
|
|
49
|
-
: undefined;
|
|
50
|
-
return {
|
|
51
|
-
routesA: context.routesA,
|
|
52
|
-
routesB: context.routesB,
|
|
53
|
-
quote: context.quote,
|
|
54
|
-
approval,
|
|
55
|
-
details: context.details,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
async function prepareZapInContextInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenIn, amountIn, amountInSplit, recipient, options) {
|
|
59
|
-
(0, validation_1.validateAddress)(poolAddress, 'poolAddress');
|
|
60
|
-
(0, validation_1.validateAddress)(tokenIn, 'tokenIn');
|
|
61
|
-
(0, validation_1.validateAddress)(recipient, 'recipient');
|
|
62
|
-
const { token0, token1, factoryAddr } = await (0, liquidityHelpers_1.getPoolInfo)(poolService, poolAddress);
|
|
63
|
-
const { amountA: amountInA, amountB: amountInB } = (0, zapHelpers_1.splitAmount)(amountIn, amountInSplit);
|
|
64
|
-
const [{ routesA, routesB }, poolSnapshot] = await Promise.all([
|
|
65
|
-
(0, zapHelpers_1.findZapInRoutes)(routeService, tokenIn, token0, token1),
|
|
66
|
-
(0, liquidityHelpers_1.getPoolSnapshot)(publicClient, poolAddress),
|
|
67
|
-
]);
|
|
68
|
-
const routerAddress = (0, constants_1.getContractAddress)(chainId, 'Router');
|
|
69
|
-
const [amountOutMinA, amountOutMinB, amountAMin, amountBMin] = (await publicClient.readContract({
|
|
70
|
-
address: routerAddress,
|
|
71
|
-
abi: abis_1.ROUTER_ABI,
|
|
72
|
-
functionName: 'generateZapInParams',
|
|
73
|
-
args: [token0, token1, factoryAddr, amountInA, amountInB, routesA, routesB],
|
|
74
|
-
}));
|
|
75
|
-
const finalAmountAMin = (0, liquidityHelpers_1.calculateMinAmount)(amountAMin, options.slippageTolerance);
|
|
76
|
-
const finalAmountBMin = (0, liquidityHelpers_1.calculateMinAmount)(amountBMin, options.slippageTolerance);
|
|
77
|
-
const finalAmountOutMinA = (0, liquidityHelpers_1.calculateMinAmount)(amountOutMinA, options.slippageTolerance);
|
|
78
|
-
const finalAmountOutMinB = (0, liquidityHelpers_1.calculateMinAmount)(amountOutMinB, options.slippageTolerance);
|
|
79
|
-
const expectedLiquidity = (0, zapHelpers_1.estimateLiquidityFromZapIn)(finalAmountOutMinA, finalAmountOutMinB, poolSnapshot.reserve0, poolSnapshot.reserve1, poolSnapshot.totalSupply);
|
|
80
|
-
const quote = {
|
|
81
|
-
amountOutFromA: finalAmountOutMinA,
|
|
82
|
-
amountOutFromB: finalAmountOutMinB,
|
|
83
|
-
amountAMin: finalAmountAMin,
|
|
84
|
-
amountBMin: finalAmountBMin,
|
|
85
|
-
estimatedMinLiquidity: expectedLiquidity,
|
|
86
|
-
};
|
|
87
|
-
const zapParams = {
|
|
88
|
-
tokenA: token0,
|
|
89
|
-
tokenB: token1,
|
|
90
|
-
factory: factoryAddr,
|
|
91
|
-
amountAMin: finalAmountAMin,
|
|
92
|
-
amountBMin: finalAmountBMin,
|
|
93
|
-
amountOutMinA: finalAmountOutMinA,
|
|
94
|
-
amountOutMinB: finalAmountOutMinB,
|
|
95
|
-
};
|
|
96
|
-
const data = (0, zapHelpers_1.encodeZapInCall)(tokenIn, amountInA, amountInB, zapParams, routesA, routesB, recipient);
|
|
97
|
-
return {
|
|
98
|
-
routesA,
|
|
99
|
-
routesB,
|
|
100
|
-
quote,
|
|
101
|
-
details: {
|
|
102
|
-
params: {
|
|
103
|
-
to: routerAddress,
|
|
104
|
-
data,
|
|
105
|
-
value: '0',
|
|
106
|
-
},
|
|
107
|
-
poolAddress,
|
|
108
|
-
tokenIn,
|
|
109
|
-
amountIn,
|
|
110
|
-
amountInA,
|
|
111
|
-
amountInB,
|
|
112
|
-
routesA,
|
|
113
|
-
routesB,
|
|
114
|
-
zapParams,
|
|
115
|
-
estimatedMinLiquidity: expectedLiquidity,
|
|
116
|
-
},
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
//# sourceMappingURL=zapIn.js.map
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { PublicClient } from 'viem';
|
|
2
|
-
import { PoolService } from '../pools';
|
|
3
|
-
import { RouteService } from '../routes';
|
|
4
|
-
import { LiquidityOptions, PreparedZapOut, ZapOutQuote, ZapOutDetails, ZapOutTransaction } from '../../core/types';
|
|
5
|
-
export declare function buildZapOutTransactionInternal(publicClient: PublicClient, chainId: number, poolService: PoolService, routeService: RouteService, poolAddress: string, tokenOut: string, liquidity: bigint, recipient: string, owner: string, options: LiquidityOptions): Promise<ZapOutTransaction>;
|
|
6
|
-
export declare function buildZapOutParamsInternal(publicClient: PublicClient, chainId: number, poolService: PoolService, routeService: RouteService, poolAddress: string, tokenOut: string, liquidity: bigint, recipient: string, options: LiquidityOptions): Promise<ZapOutDetails>;
|
|
7
|
-
export declare function quoteZapOutInternal(publicClient: PublicClient, chainId: number, poolService: PoolService, routeService: RouteService, poolAddress: string, tokenOut: string, liquidity: bigint, options: LiquidityOptions): Promise<ZapOutQuote>;
|
|
8
|
-
export declare function prepareZapOutInternal(publicClient: PublicClient, chainId: number, poolService: PoolService, routeService: RouteService, poolAddress: string, tokenOut: string, liquidity: bigint, recipient: string, owner: string | undefined, options: LiquidityOptions): Promise<PreparedZapOut>;
|
|
9
|
-
//# sourceMappingURL=zapOut.d.ts.map
|
|
@@ -1,255 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.buildZapOutTransactionInternal = buildZapOutTransactionInternal;
|
|
4
|
-
exports.buildZapOutParamsInternal = buildZapOutParamsInternal;
|
|
5
|
-
exports.quoteZapOutInternal = quoteZapOutInternal;
|
|
6
|
-
exports.prepareZapOutInternal = prepareZapOutInternal;
|
|
7
|
-
const abis_1 = require("../../core/abis");
|
|
8
|
-
const constants_1 = require("../../core/constants");
|
|
9
|
-
const errors_1 = require("../../core/errors");
|
|
10
|
-
const validation_1 = require("../../utils/validation");
|
|
11
|
-
const pathEncoder_1 = require("../../utils/pathEncoder");
|
|
12
|
-
const liquidityHelpers_1 = require("./liquidityHelpers");
|
|
13
|
-
const zapHelpers_1 = require("./zapHelpers");
|
|
14
|
-
const INSUFFICIENT_LIQUIDITY_SELECTOR = '0xbb55fd27';
|
|
15
|
-
const MAX_ROUTE_CANDIDATES_PER_LEG = 8;
|
|
16
|
-
const MAX_ROUTE_COMBINATIONS = 48;
|
|
17
|
-
const ROUTE_SIMULATION_BATCH_SIZE = 6;
|
|
18
|
-
async function buildZapOutTransactionInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, recipient, owner, options) {
|
|
19
|
-
const prepared = await prepareZapOutInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, recipient, owner, options);
|
|
20
|
-
return {
|
|
21
|
-
approval: prepared.approval ?? null,
|
|
22
|
-
zapOut: prepared.details,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
async function buildZapOutParamsInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, recipient, options) {
|
|
26
|
-
const prepared = await prepareZapOutInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, recipient, undefined, options);
|
|
27
|
-
return prepared.details;
|
|
28
|
-
}
|
|
29
|
-
async function quoteZapOutInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, options) {
|
|
30
|
-
const prepared = await prepareZapOutInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, tokenOut, undefined, options);
|
|
31
|
-
return prepared.quote;
|
|
32
|
-
}
|
|
33
|
-
async function prepareZapOutInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, recipient, owner, options) {
|
|
34
|
-
if (owner) {
|
|
35
|
-
(0, validation_1.validateAddress)(owner, 'owner');
|
|
36
|
-
}
|
|
37
|
-
const [context, currentAllowance] = await Promise.all([
|
|
38
|
-
prepareZapOutContextInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, recipient, options),
|
|
39
|
-
owner ? (0, liquidityHelpers_1.getAllowance)(publicClient, poolAddress, owner, chainId) : Promise.resolve(null),
|
|
40
|
-
]);
|
|
41
|
-
let details = context.details;
|
|
42
|
-
const approval = owner && currentAllowance !== null && currentAllowance < liquidity
|
|
43
|
-
? { token: poolAddress, amount: liquidity, params: (0, liquidityHelpers_1.buildApprovalParams)(chainId, poolAddress, liquidity) }
|
|
44
|
-
: owner
|
|
45
|
-
? null
|
|
46
|
-
: undefined;
|
|
47
|
-
if (owner && currentAllowance !== null && currentAllowance >= liquidity) {
|
|
48
|
-
const ownerAddr = owner;
|
|
49
|
-
const routerAddress = (0, constants_1.getContractAddress)(chainId, 'Router');
|
|
50
|
-
try {
|
|
51
|
-
await simulateZapOut(publicClient, ownerAddr, routerAddress, details.params.data);
|
|
52
|
-
}
|
|
53
|
-
catch (error) {
|
|
54
|
-
if (!isInsufficientLiquidityError(error)) {
|
|
55
|
-
throw error;
|
|
56
|
-
}
|
|
57
|
-
details = await findViableZapOutDetails(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, ownerAddr, options);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return {
|
|
61
|
-
routesA: details.routesA,
|
|
62
|
-
routesB: details.routesB,
|
|
63
|
-
quote: {
|
|
64
|
-
amountOutFromA: details.zapParams.amountOutMinA,
|
|
65
|
-
amountOutFromB: details.zapParams.amountOutMinB,
|
|
66
|
-
amountAMin: details.zapParams.amountAMin,
|
|
67
|
-
amountBMin: details.zapParams.amountBMin,
|
|
68
|
-
estimatedMinTokenOut: details.estimatedMinTokenOut,
|
|
69
|
-
},
|
|
70
|
-
approval,
|
|
71
|
-
details,
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
async function prepareZapOutContextInternal(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, recipient, options) {
|
|
75
|
-
(0, validation_1.validateAddress)(poolAddress, 'poolAddress');
|
|
76
|
-
(0, validation_1.validateAddress)(tokenOut, 'tokenOut');
|
|
77
|
-
(0, validation_1.validateAddress)(recipient, 'recipient');
|
|
78
|
-
const { token0, token1, factoryAddr } = await (0, liquidityHelpers_1.getPoolInfo)(poolService, poolAddress);
|
|
79
|
-
const { routesA, routesB } = await (0, zapHelpers_1.findZapOutRoutes)(routeService, token0, token1, tokenOut);
|
|
80
|
-
const details = await buildZapOutDetailsForRoutes(publicClient, chainId, poolAddress, tokenOut, liquidity, token0, token1, factoryAddr, routesA, routesB, options);
|
|
81
|
-
return {
|
|
82
|
-
routesA,
|
|
83
|
-
routesB,
|
|
84
|
-
quote: {
|
|
85
|
-
amountOutFromA: details.zapParams.amountOutMinA,
|
|
86
|
-
amountOutFromB: details.zapParams.amountOutMinB,
|
|
87
|
-
amountAMin: details.zapParams.amountAMin,
|
|
88
|
-
amountBMin: details.zapParams.amountBMin,
|
|
89
|
-
estimatedMinTokenOut: details.estimatedMinTokenOut,
|
|
90
|
-
},
|
|
91
|
-
details,
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
async function buildZapOutDetailsForRoutes(publicClient, chainId, poolAddress, tokenOut, liquidity, token0, token1, factoryAddr, routesA, routesB, options) {
|
|
95
|
-
const routerAddress = (0, constants_1.getContractAddress)(chainId, 'Router');
|
|
96
|
-
const [amountOutMinA, amountOutMinB, amountAMin, amountBMin] = (await publicClient.readContract({
|
|
97
|
-
address: routerAddress,
|
|
98
|
-
abi: abis_1.ROUTER_ABI,
|
|
99
|
-
functionName: 'generateZapOutParams',
|
|
100
|
-
args: [token0, token1, factoryAddr, liquidity, routesA, routesB],
|
|
101
|
-
}));
|
|
102
|
-
const finalAmountAMin = (0, liquidityHelpers_1.calculateMinAmount)(amountAMin, options.slippageTolerance);
|
|
103
|
-
const finalAmountBMin = (0, liquidityHelpers_1.calculateMinAmount)(amountBMin, options.slippageTolerance);
|
|
104
|
-
const finalAmountOutMinA = (0, liquidityHelpers_1.calculateMinAmount)(amountOutMinA, options.slippageTolerance);
|
|
105
|
-
const finalAmountOutMinB = (0, liquidityHelpers_1.calculateMinAmount)(amountOutMinB, options.slippageTolerance);
|
|
106
|
-
const zapParams = {
|
|
107
|
-
tokenA: token0,
|
|
108
|
-
tokenB: token1,
|
|
109
|
-
factory: factoryAddr,
|
|
110
|
-
amountAMin: finalAmountAMin,
|
|
111
|
-
amountBMin: finalAmountBMin,
|
|
112
|
-
amountOutMinA: finalAmountOutMinA,
|
|
113
|
-
amountOutMinB: finalAmountOutMinB,
|
|
114
|
-
};
|
|
115
|
-
const data = (0, zapHelpers_1.encodeZapOutCall)(tokenOut, liquidity, zapParams, routesA, routesB);
|
|
116
|
-
return {
|
|
117
|
-
params: {
|
|
118
|
-
to: routerAddress,
|
|
119
|
-
data,
|
|
120
|
-
value: '0',
|
|
121
|
-
},
|
|
122
|
-
poolAddress,
|
|
123
|
-
tokenOut,
|
|
124
|
-
liquidity,
|
|
125
|
-
routesA,
|
|
126
|
-
routesB,
|
|
127
|
-
zapParams,
|
|
128
|
-
estimatedMinTokenOut: finalAmountOutMinA + finalAmountOutMinB,
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
async function findViableZapOutDetails(publicClient, chainId, poolService, routeService, poolAddress, tokenOut, liquidity, owner, options) {
|
|
132
|
-
const { token0, token1, factoryAddr } = await (0, liquidityHelpers_1.getPoolInfo)(poolService, poolAddress);
|
|
133
|
-
const routerAddress = (0, constants_1.getContractAddress)(chainId, 'Router');
|
|
134
|
-
const allRoutes = await routeService.getRoutes({ cached: false, returnAllRoutes: true });
|
|
135
|
-
const [routesAOptions, routesBOptions] = await Promise.all([
|
|
136
|
-
getEncodedRouteCandidates(routeService, token0, tokenOut, poolAddress, allRoutes),
|
|
137
|
-
getEncodedRouteCandidates(routeService, token1, tokenOut, poolAddress, allRoutes),
|
|
138
|
-
]);
|
|
139
|
-
const routeCombinations = [];
|
|
140
|
-
outer: for (const routesA of routesAOptions) {
|
|
141
|
-
for (const routesB of routesBOptions) {
|
|
142
|
-
routeCombinations.push({ routesA, routesB });
|
|
143
|
-
if (routeCombinations.length >= MAX_ROUTE_COMBINATIONS) {
|
|
144
|
-
break outer;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
let best = null;
|
|
149
|
-
for (let index = 0; index < routeCombinations.length; index += ROUTE_SIMULATION_BATCH_SIZE) {
|
|
150
|
-
const batch = routeCombinations.slice(index, index + ROUTE_SIMULATION_BATCH_SIZE);
|
|
151
|
-
const candidates = await Promise.all(batch.map(async ({ routesA, routesB }) => {
|
|
152
|
-
try {
|
|
153
|
-
const candidate = await buildZapOutDetailsForRoutes(publicClient, chainId, poolAddress, tokenOut, liquidity, token0, token1, factoryAddr, routesA, routesB, options);
|
|
154
|
-
await simulateZapOut(publicClient, owner, routerAddress, candidate.params.data);
|
|
155
|
-
return candidate;
|
|
156
|
-
}
|
|
157
|
-
catch {
|
|
158
|
-
return null;
|
|
159
|
-
}
|
|
160
|
-
}));
|
|
161
|
-
for (const candidate of candidates) {
|
|
162
|
-
if (candidate && (!best || candidate.estimatedMinTokenOut > best.estimatedMinTokenOut)) {
|
|
163
|
-
best = candidate;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
if (!best) {
|
|
168
|
-
throw new errors_1.ZapOutRouteNotViableError(poolAddress, tokenOut);
|
|
169
|
-
}
|
|
170
|
-
return best;
|
|
171
|
-
}
|
|
172
|
-
async function getEncodedRouteCandidates(routeService, tokenIn, tokenOut, sourcePoolAddress, allRoutes) {
|
|
173
|
-
if (tokenIn.toLowerCase() === tokenOut.toLowerCase()) {
|
|
174
|
-
return [[]];
|
|
175
|
-
}
|
|
176
|
-
const rawCandidates = [];
|
|
177
|
-
try {
|
|
178
|
-
rawCandidates.push(await routeService.findRoute(tokenIn, tokenOut));
|
|
179
|
-
}
|
|
180
|
-
catch {
|
|
181
|
-
// Continue; we'll try the broader route set next.
|
|
182
|
-
}
|
|
183
|
-
const pairCandidates = (allRoutes ?? await routeService.getRoutes({ cached: false, returnAllRoutes: true })).filter((route) => {
|
|
184
|
-
const a0 = route.tokens[0].address.toLowerCase();
|
|
185
|
-
const a1 = route.tokens[1].address.toLowerCase();
|
|
186
|
-
const t0 = tokenIn.toLowerCase();
|
|
187
|
-
const t1 = tokenOut.toLowerCase();
|
|
188
|
-
return (a0 === t0 && a1 === t1) || (a0 === t1 && a1 === t0);
|
|
189
|
-
});
|
|
190
|
-
rawCandidates.push(...pairCandidates);
|
|
191
|
-
if (rawCandidates.length === 0) {
|
|
192
|
-
throw new errors_1.RouteNotFoundError(tokenIn, tokenOut);
|
|
193
|
-
}
|
|
194
|
-
rawCandidates.sort((routeA, routeB) => {
|
|
195
|
-
const routeAUsesSourcePool = routeUsesPool(routeA, sourcePoolAddress) ? 1 : 0;
|
|
196
|
-
const routeBUsesSourcePool = routeUsesPool(routeB, sourcePoolAddress) ? 1 : 0;
|
|
197
|
-
if (routeAUsesSourcePool !== routeBUsesSourcePool)
|
|
198
|
-
return routeAUsesSourcePool - routeBUsesSourcePool;
|
|
199
|
-
if (routeA.path.length !== routeB.path.length)
|
|
200
|
-
return routeA.path.length - routeB.path.length;
|
|
201
|
-
return 0;
|
|
202
|
-
});
|
|
203
|
-
const encodedRoutes = [];
|
|
204
|
-
const seen = new Set();
|
|
205
|
-
for (const route of rawCandidates) {
|
|
206
|
-
try {
|
|
207
|
-
const encoded = (0, pathEncoder_1.encodeRoutePath)(route.path, tokenIn, tokenOut);
|
|
208
|
-
const key = JSON.stringify(encoded);
|
|
209
|
-
if (seen.has(key))
|
|
210
|
-
continue;
|
|
211
|
-
seen.add(key);
|
|
212
|
-
encodedRoutes.push(encoded);
|
|
213
|
-
if (encodedRoutes.length >= MAX_ROUTE_CANDIDATES_PER_LEG) {
|
|
214
|
-
break;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
catch {
|
|
218
|
-
// Invalid path encoding for this direction; skip.
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
if (encodedRoutes.length === 0) {
|
|
222
|
-
throw new errors_1.RouteNotFoundError(tokenIn, tokenOut);
|
|
223
|
-
}
|
|
224
|
-
return encodedRoutes;
|
|
225
|
-
}
|
|
226
|
-
function routeUsesPool(route, poolAddress) {
|
|
227
|
-
const normalizedPool = poolAddress.toLowerCase();
|
|
228
|
-
return route.path.some((hop) => hop.poolAddr.toLowerCase() === normalizedPool);
|
|
229
|
-
}
|
|
230
|
-
async function simulateZapOut(publicClient, owner, routerAddress, data) {
|
|
231
|
-
await publicClient.call({
|
|
232
|
-
account: owner,
|
|
233
|
-
to: routerAddress,
|
|
234
|
-
data,
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
function isInsufficientLiquidityError(error) {
|
|
238
|
-
const message = extractErrorMessage(error).toLowerCase();
|
|
239
|
-
return (message.includes(INSUFFICIENT_LIQUIDITY_SELECTOR) ||
|
|
240
|
-
message.includes('insufficientliquidity'));
|
|
241
|
-
}
|
|
242
|
-
function extractErrorMessage(error) {
|
|
243
|
-
if (!(error instanceof Error)) {
|
|
244
|
-
return String(error);
|
|
245
|
-
}
|
|
246
|
-
const typed = error;
|
|
247
|
-
const parts = [
|
|
248
|
-
typed.message,
|
|
249
|
-
typed.shortMessage,
|
|
250
|
-
typed.details,
|
|
251
|
-
typed.cause instanceof Error ? typed.cause.message : undefined,
|
|
252
|
-
].filter((part) => Boolean(part));
|
|
253
|
-
return parts.join(' | ');
|
|
254
|
-
}
|
|
255
|
-
//# sourceMappingURL=zapOut.js.map
|