@mento-protocol/mento-sdk 3.2.7 → 3.2.8
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/dist/cache/routes.d.ts +13 -0
- package/dist/cache/routes.js +14649 -0
- package/dist/cache/tokens.d.ts +68 -0
- package/dist/cache/tokens.js +488 -0
- package/dist/core/abis/activePool.d.ts +2 -0
- package/dist/core/abis/activePool.js +14 -0
- package/dist/core/abis/addressesRegistry.d.ts +2 -0
- package/dist/core/abis/addressesRegistry.js +26 -0
- package/dist/core/abis/bipoolmanager.d.ts +34 -0
- package/dist/core/abis/bipoolmanager.js +72 -0
- package/dist/core/abis/borrowerOperations.d.ts +9 -0
- package/dist/core/abis/borrowerOperations.js +89 -0
- package/dist/core/abis/breakerbox.d.ts +13 -0
- package/dist/core/abis/breakerbox.js +8 -0
- package/dist/core/abis/broker.d.ts +2 -0
- package/dist/core/abis/broker.js +9 -0
- package/dist/core/abis/erc20.d.ts +9 -0
- package/dist/core/abis/erc20.js +21 -0
- package/dist/core/abis/fpmm.d.ts +270 -0
- package/dist/core/abis/fpmm.js +49 -0
- package/dist/core/abis/fpmmFactory.d.ts +85 -0
- package/dist/core/abis/fpmmFactory.js +26 -0
- package/dist/core/abis/hintHelpers.d.ts +2 -0
- package/dist/core/abis/hintHelpers.js +14 -0
- package/dist/core/abis/index.d.ts +22 -0
- package/dist/core/abis/index.js +38 -0
- package/dist/core/abis/liquidityStrategy.d.ts +132 -0
- package/dist/core/abis/liquidityStrategy.js +10 -0
- package/dist/core/abis/multiTroveGetter.d.ts +8 -0
- package/dist/core/abis/multiTroveGetter.js +15 -0
- package/dist/core/abis/priceFeed.d.ts +7 -0
- package/dist/core/abis/priceFeed.js +16 -0
- package/dist/core/abis/pricingmodule.d.ts +2 -0
- package/dist/core/abis/pricingmodule.js +6 -0
- package/dist/core/abis/reserve.d.ts +3 -0
- package/dist/core/abis/reserve.js +18 -0
- package/dist/core/abis/router.d.ts +521 -0
- package/dist/core/abis/router.js +45 -0
- package/dist/core/abis/sortedTroves.d.ts +2 -0
- package/dist/core/abis/sortedTroves.js +15 -0
- package/dist/core/abis/systemParams.d.ts +2 -0
- package/dist/core/abis/systemParams.js +14 -0
- package/dist/core/abis/troveManager.d.ts +2 -0
- package/dist/core/abis/troveManager.js +27 -0
- package/dist/core/abis/troveNFT.d.ts +2 -0
- package/dist/core/abis/troveNFT.js +9 -0
- package/dist/core/abis/virtualPool.d.ts +50 -0
- package/dist/core/abis/virtualPool.js +11 -0
- package/dist/core/abis/virtualPoolFactory.d.ts +59 -0
- package/dist/core/abis/virtualPoolFactory.js +17 -0
- package/dist/core/constants/addresses.d.ts +18 -0
- package/dist/core/constants/addresses.js +113 -0
- package/dist/core/constants/borrowConstants.d.ts +10 -0
- package/dist/core/constants/borrowConstants.js +16 -0
- package/dist/core/constants/borrowRegistries.d.ts +7 -0
- package/dist/core/constants/borrowRegistries.js +34 -0
- package/dist/core/constants/chainId.d.ts +7 -0
- package/dist/core/constants/chainId.js +11 -0
- package/dist/core/constants/contractNames.d.ts +21 -0
- package/dist/core/constants/contractNames.js +24 -0
- package/dist/core/constants/index.d.ts +6 -0
- package/dist/core/constants/index.js +22 -0
- package/dist/core/errors/base.d.ts +8 -0
- package/dist/core/errors/base.js +17 -0
- package/dist/core/errors/index.d.ts +4 -0
- package/dist/core/errors/index.js +20 -0
- package/dist/core/errors/oracle.d.ts +9 -0
- package/dist/core/errors/oracle.js +15 -0
- package/dist/core/errors/router.d.ts +14 -0
- package/dist/core/errors/router.js +24 -0
- package/dist/core/types/borrow.d.ts +87 -0
- package/dist/core/types/borrow.js +3 -0
- package/dist/core/types/contractAddresses.d.ts +42 -0
- package/dist/core/types/contractAddresses.js +3 -0
- package/dist/core/types/index.d.ts +10 -0
- package/dist/core/types/index.js +26 -0
- package/dist/core/types/liquidity.d.ts +194 -0
- package/dist/core/types/liquidity.js +3 -0
- package/dist/core/types/pool.d.ts +208 -0
- package/dist/core/types/pool.js +14 -0
- package/dist/core/types/provider.d.ts +45 -0
- package/dist/core/types/provider.js +3 -0
- package/dist/core/types/route.d.ts +62 -0
- package/dist/core/types/route.js +3 -0
- package/dist/core/types/token.d.ts +21 -0
- package/dist/core/types/token.js +3 -0
- package/dist/core/types/tradingLimits.d.ts +91 -0
- package/dist/core/types/tradingLimits.js +3 -0
- package/dist/core/types/tradingMode.d.ts +24 -0
- package/dist/core/types/tradingMode.js +31 -0
- package/dist/core/types/transaction.d.ts +45 -0
- package/dist/core/types/transaction.js +3 -0
- package/dist/esm/cache/routes.js +14644 -0
- package/dist/esm/cache/tokens.js +480 -0
- package/dist/esm/core/abis/activePool.js +10 -0
- package/dist/esm/core/abis/addressesRegistry.js +22 -0
- package/dist/esm/core/abis/bipoolmanager.js +68 -0
- package/dist/esm/core/abis/borrowerOperations.js +85 -0
- package/dist/esm/core/abis/breakerbox.js +4 -0
- package/dist/esm/core/abis/broker.js +5 -0
- package/dist/esm/core/abis/erc20.js +17 -0
- package/dist/esm/core/abis/fpmm.js +45 -0
- package/dist/esm/core/abis/fpmmFactory.js +22 -0
- package/dist/esm/core/abis/hintHelpers.js +10 -0
- package/dist/esm/core/abis/index.js +21 -0
- package/dist/esm/core/abis/liquidityStrategy.js +6 -0
- package/dist/esm/core/abis/multiTroveGetter.js +11 -0
- package/dist/esm/core/abis/priceFeed.js +12 -0
- package/dist/esm/core/abis/pricingmodule.js +2 -0
- package/dist/esm/core/abis/reserve.js +14 -0
- package/dist/esm/core/abis/router.js +41 -0
- package/dist/esm/core/abis/sortedTroves.js +11 -0
- package/dist/esm/core/abis/systemParams.js +10 -0
- package/dist/esm/core/abis/troveManager.js +23 -0
- package/dist/esm/core/abis/troveNFT.js +5 -0
- package/dist/esm/core/abis/virtualPool.js +7 -0
- package/dist/esm/core/abis/virtualPoolFactory.js +13 -0
- package/dist/esm/core/constants/addresses.js +107 -0
- package/dist/esm/core/constants/borrowConstants.js +12 -0
- package/dist/esm/core/constants/borrowRegistries.js +29 -0
- package/dist/esm/core/constants/chainId.js +7 -0
- package/dist/esm/core/constants/contractNames.js +20 -0
- package/dist/esm/core/constants/index.js +5 -0
- package/dist/esm/core/errors/base.js +12 -0
- package/dist/esm/core/errors/index.js +3 -0
- package/dist/esm/core/errors/oracle.js +10 -0
- package/dist/esm/core/errors/router.js +18 -0
- package/dist/esm/core/types/borrow.js +1 -0
- package/dist/esm/core/types/contractAddresses.js +1 -0
- package/dist/esm/core/types/index.js +9 -0
- package/dist/esm/core/types/liquidity.js +1 -0
- package/dist/esm/core/types/pool.js +10 -0
- package/dist/esm/core/types/provider.js +1 -0
- package/dist/esm/core/types/route.js +1 -0
- package/dist/esm/core/types/token.js +1 -0
- package/dist/esm/core/types/tradingLimits.js +1 -0
- package/dist/esm/core/types/tradingMode.js +26 -0
- package/dist/esm/core/types/transaction.js +1 -0
- package/dist/esm/index.js +139 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/services/borrow/BorrowService.js +455 -0
- package/dist/esm/services/borrow/borrowHelpers.js +3 -0
- package/dist/esm/services/borrow/borrowMath.js +127 -0
- package/dist/esm/services/borrow/index.js +3 -0
- package/dist/esm/services/borrow/internal/borrowApprovalService.js +48 -0
- package/dist/esm/services/borrow/internal/borrowContextStore.js +35 -0
- package/dist/esm/services/borrow/internal/borrowErc20.js +38 -0
- package/dist/esm/services/borrow/internal/borrowHints.js +27 -0
- package/dist/esm/services/borrow/internal/borrowPositionParser.js +82 -0
- package/dist/esm/services/borrow/internal/borrowReadService.js +271 -0
- package/dist/esm/services/borrow/internal/borrowRegistryReader.js +108 -0
- package/dist/esm/services/borrow/internal/borrowTransactionService.js +271 -0
- package/dist/esm/services/borrow/internal/borrowTypes.js +1 -0
- package/dist/esm/services/borrow/internal/borrowValidation.js +89 -0
- package/dist/esm/services/index.js +8 -0
- package/dist/esm/services/liquidity/LiquidityService.js +163 -0
- package/dist/esm/services/liquidity/basicLiquidity.js +162 -0
- package/dist/esm/services/liquidity/index.js +1 -0
- package/dist/esm/services/liquidity/liquidityHelpers.js +95 -0
- package/dist/esm/services/liquidity/rebalance.js +59 -0
- package/dist/esm/services/liquidity/zapHelpers.js +181 -0
- package/dist/esm/services/liquidity/zapIn.js +131 -0
- package/dist/esm/services/liquidity/zapOut.js +248 -0
- package/dist/esm/services/pools/PoolService.js +204 -0
- package/dist/esm/services/pools/index.js +1 -0
- package/dist/esm/services/pools/poolDetails.js +209 -0
- package/dist/esm/services/pools/poolDiscovery.js +112 -0
- package/dist/esm/services/pools/rebalancePreview.js +181 -0
- package/dist/esm/services/quotes/QuoteService.js +85 -0
- package/dist/esm/services/quotes/index.js +1 -0
- package/dist/esm/services/routes/RouteService.js +268 -0
- package/dist/esm/services/routes/index.js +1 -0
- package/dist/esm/services/swap/SwapService.js +247 -0
- package/dist/esm/services/swap/index.js +1 -0
- package/dist/esm/services/tokens/index.js +1 -0
- package/dist/esm/services/tokens/tokenService.js +285 -0
- package/dist/esm/services/trading/TradingLimitsService.js +154 -0
- package/dist/esm/services/trading/TradingService.js +222 -0
- package/dist/esm/services/trading/index.js +2 -0
- package/dist/esm/utils/chainConfig.js +118 -0
- package/dist/esm/utils/costUtils.js +56 -0
- package/dist/esm/utils/deadline.js +22 -0
- package/dist/esm/utils/index.js +9 -0
- package/dist/esm/utils/multicall.js +47 -0
- package/dist/esm/utils/pathEncoder.js +69 -0
- package/dist/esm/utils/rateFeed.js +23 -0
- package/dist/esm/utils/retry.js +24 -0
- package/dist/esm/utils/routeUtils.js +361 -0
- package/dist/esm/utils/routes.js +2 -0
- package/dist/esm/utils/sortUtils.js +33 -0
- package/dist/esm/utils/tokens.js +2 -0
- package/dist/esm/utils/tradingLimits.js +163 -0
- package/dist/esm/utils/validation.js +30 -0
- package/dist/index.d.ts +101 -0
- package/dist/index.js +158 -0
- package/dist/services/borrow/BorrowService.d.ts +381 -0
- package/dist/services/borrow/BorrowService.js +460 -0
- package/dist/services/borrow/borrowHelpers.d.ts +4 -0
- package/dist/services/borrow/borrowHelpers.js +13 -0
- package/dist/services/borrow/borrowMath.d.ts +21 -0
- package/dist/services/borrow/borrowMath.js +137 -0
- package/dist/services/borrow/index.d.ts +4 -0
- package/dist/services/borrow/index.js +20 -0
- package/dist/services/borrow/internal/borrowApprovalService.d.ts +14 -0
- package/dist/services/borrow/internal/borrowApprovalService.js +53 -0
- package/dist/services/borrow/internal/borrowContextStore.d.ts +11 -0
- package/dist/services/borrow/internal/borrowContextStore.js +40 -0
- package/dist/services/borrow/internal/borrowErc20.d.ts +5 -0
- package/dist/services/borrow/internal/borrowErc20.js +43 -0
- package/dist/services/borrow/internal/borrowHints.d.ts +7 -0
- package/dist/services/borrow/internal/borrowHints.js +31 -0
- package/dist/services/borrow/internal/borrowPositionParser.d.ts +4 -0
- package/dist/services/borrow/internal/borrowPositionParser.js +87 -0
- package/dist/services/borrow/internal/borrowReadService.d.ts +31 -0
- package/dist/services/borrow/internal/borrowReadService.js +276 -0
- package/dist/services/borrow/internal/borrowRegistryReader.d.ts +5 -0
- package/dist/services/borrow/internal/borrowRegistryReader.js +113 -0
- package/dist/services/borrow/internal/borrowTransactionService.d.ts +23 -0
- package/dist/services/borrow/internal/borrowTransactionService.js +276 -0
- package/dist/services/borrow/internal/borrowTypes.d.ts +15 -0
- package/dist/services/borrow/internal/borrowTypes.js +3 -0
- package/dist/services/borrow/internal/borrowValidation.d.ts +14 -0
- package/dist/services/borrow/internal/borrowValidation.js +104 -0
- package/dist/services/index.d.ts +9 -0
- package/dist/services/index.js +25 -0
- package/dist/services/liquidity/LiquidityService.d.ts +139 -0
- package/dist/services/liquidity/LiquidityService.js +168 -0
- package/dist/services/liquidity/basicLiquidity.d.ts +11 -0
- package/dist/services/liquidity/basicLiquidity.js +172 -0
- package/dist/services/liquidity/index.d.ts +2 -0
- package/dist/services/liquidity/index.js +18 -0
- package/dist/services/liquidity/liquidityHelpers.d.ts +19 -0
- package/dist/services/liquidity/liquidityHelpers.js +104 -0
- package/dist/services/liquidity/rebalance.d.ts +6 -0
- package/dist/services/liquidity/rebalance.js +64 -0
- package/dist/services/liquidity/zapHelpers.d.ts +100 -0
- package/dist/services/liquidity/zapHelpers.js +192 -0
- package/dist/services/liquidity/zapIn.d.ts +18 -0
- package/dist/services/liquidity/zapIn.js +138 -0
- package/dist/services/liquidity/zapOut.d.ts +9 -0
- package/dist/services/liquidity/zapOut.js +255 -0
- package/dist/services/pools/PoolService.d.ts +69 -0
- package/dist/services/pools/PoolService.js +209 -0
- package/dist/services/pools/index.d.ts +2 -0
- package/dist/services/pools/index.js +18 -0
- package/dist/services/pools/poolDetails.d.ts +13 -0
- package/dist/services/pools/poolDetails.js +216 -0
- package/dist/services/pools/poolDiscovery.d.ts +12 -0
- package/dist/services/pools/poolDiscovery.js +117 -0
- package/dist/services/pools/rebalancePreview.d.ts +5 -0
- package/dist/services/pools/rebalancePreview.js +186 -0
- package/dist/services/quotes/QuoteService.d.ts +51 -0
- package/dist/services/quotes/QuoteService.js +91 -0
- package/dist/services/quotes/index.d.ts +2 -0
- package/dist/services/quotes/index.js +18 -0
- package/dist/services/routes/RouteService.d.ts +117 -0
- package/dist/services/routes/RouteService.js +306 -0
- package/dist/services/routes/index.d.ts +2 -0
- package/dist/services/routes/index.js +18 -0
- package/dist/services/swap/SwapService.d.ts +198 -0
- package/dist/services/swap/SwapService.js +252 -0
- package/dist/services/swap/index.d.ts +2 -0
- package/dist/services/swap/index.js +18 -0
- package/dist/services/tokens/index.d.ts +2 -0
- package/dist/services/tokens/index.js +18 -0
- package/dist/services/tokens/tokenService.d.ts +55 -0
- package/dist/services/tokens/tokenService.js +290 -0
- package/dist/services/trading/TradingLimitsService.d.ts +38 -0
- package/dist/services/trading/TradingLimitsService.js +159 -0
- package/dist/services/trading/TradingService.d.ts +115 -0
- package/dist/services/trading/TradingService.js +227 -0
- package/dist/services/trading/index.d.ts +3 -0
- package/dist/services/trading/index.js +19 -0
- package/dist/utils/chainConfig.d.ts +16 -0
- package/dist/utils/chainConfig.js +123 -0
- package/dist/utils/costUtils.d.ts +12 -0
- package/dist/utils/costUtils.js +60 -0
- package/dist/utils/deadline.d.ts +21 -0
- package/dist/utils/deadline.js +26 -0
- package/dist/utils/index.d.ts +10 -0
- package/dist/utils/index.js +26 -0
- package/dist/utils/multicall.d.ts +30 -0
- package/dist/utils/multicall.js +52 -0
- package/dist/utils/pathEncoder.d.ts +34 -0
- package/dist/utils/pathEncoder.js +73 -0
- package/dist/utils/rateFeed.d.ts +18 -0
- package/dist/utils/rateFeed.js +27 -0
- package/dist/utils/retry.d.ts +12 -0
- package/dist/utils/retry.js +28 -0
- package/dist/utils/routeUtils.d.ts +295 -0
- package/dist/utils/routeUtils.js +371 -0
- package/dist/utils/routes.d.ts +3 -0
- package/dist/utils/routes.js +8 -0
- package/dist/utils/sortUtils.d.ts +24 -0
- package/dist/utils/sortUtils.js +39 -0
- package/dist/utils/tokens.d.ts +2 -0
- package/dist/utils/tokens.js +13 -0
- package/dist/utils/tradingLimits.d.ts +41 -0
- package/dist/utils/tradingLimits.js +171 -0
- package/dist/utils/validation.d.ts +19 -0
- package/dist/utils/validation.js +34 -0
- package/package.json +1 -1
|
@@ -0,0 +1,64 @@
|
|
|
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
|
|
@@ -0,0 +1,100 @@
|
|
|
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
|
+
* Off-chain mirror of Router.sol's quoteAddLiquidity / _quoteZapLiquidity logic.
|
|
52
|
+
*
|
|
53
|
+
* Given desired amounts and pool reserves, returns the (amountA, amountB) the
|
|
54
|
+
* router will actually deposit when adding liquidity. Used to predict the exact
|
|
55
|
+
* `amountAMin` / `amountBMin` the contract will check against post-swap reserves.
|
|
56
|
+
*
|
|
57
|
+
* @param amountADesired - Desired amount of tokenA
|
|
58
|
+
* @param amountBDesired - Desired amount of tokenB
|
|
59
|
+
* @param reserveA - Reserve of tokenA at the moment liquidity is added
|
|
60
|
+
* @param reserveB - Reserve of tokenB at the moment liquidity is added
|
|
61
|
+
*/
|
|
62
|
+
export declare function quoteAddLiquidityFromReserves(amountADesired: bigint, amountBDesired: bigint, reserveA: bigint, reserveB: bigint): {
|
|
63
|
+
amountA: bigint;
|
|
64
|
+
amountB: bigint;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Computes the net delta a single-hop zap swap applies to a target pool's
|
|
68
|
+
* reserves. Returns `{delta0, delta1}` to add to the pool's pre-swap reserves
|
|
69
|
+
* to obtain the reserves the router will see when it runs `_quoteZapLiquidity`.
|
|
70
|
+
*
|
|
71
|
+
* Only single-hop routes whose factory matches the target pool's factory and
|
|
72
|
+
* whose `(from, to)` are `(token0, token1)` (in either direction) are
|
|
73
|
+
* considered. Multi-hop routes and routes through other pools have no effect
|
|
74
|
+
* on the target pool's reserves and return `{0, 0}`.
|
|
75
|
+
*
|
|
76
|
+
* Note: Multi-hop routes that traverse the target pool as an intermediate hop
|
|
77
|
+
* are intentionally not handled here — they are uncommon for single-sided zaps
|
|
78
|
+
* and would require per-hop amounts from `getAmountsOut`.
|
|
79
|
+
*/
|
|
80
|
+
export declare function computeTargetPoolImpact(routes: RouterRoute[], amountIn: bigint, amountOut: bigint, token0: Address, token1: Address, factoryAddr: Address): {
|
|
81
|
+
delta0: bigint;
|
|
82
|
+
delta1: bigint;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Estimates minimum LP tokens from zap in amounts.
|
|
86
|
+
*
|
|
87
|
+
* This is a conservative lower-bound estimate. The inputs are slippage-adjusted
|
|
88
|
+
* minimums, not expected amounts. Actual LP tokens minted on-chain may be higher
|
|
89
|
+
* because the router uses balanceOf(address(this)) after swaps, which can exceed
|
|
90
|
+
* the pre-calculated minimums.
|
|
91
|
+
*
|
|
92
|
+
* @param amountOutA - Minimum amount of token0 after swap (slippage-adjusted)
|
|
93
|
+
* @param amountOutB - Minimum amount of token1 after swap (slippage-adjusted)
|
|
94
|
+
* @param reserve0 - Current reserve of token0 in pool
|
|
95
|
+
* @param reserve1 - Current reserve of token1 in pool
|
|
96
|
+
* @param totalSupply - Total LP token supply
|
|
97
|
+
* @returns Conservative estimate of minimum LP tokens to be minted
|
|
98
|
+
*/
|
|
99
|
+
export declare function estimateLiquidityFromZapIn(amountOutA: bigint, amountOutB: bigint, reserve0: bigint, reserve1: bigint, totalSupply: bigint): bigint;
|
|
100
|
+
//# sourceMappingURL=zapHelpers.d.ts.map
|
|
@@ -0,0 +1,192 @@
|
|
|
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.quoteAddLiquidityFromReserves = quoteAddLiquidityFromReserves;
|
|
9
|
+
exports.computeTargetPoolImpact = computeTargetPoolImpact;
|
|
10
|
+
exports.estimateLiquidityFromZapIn = estimateLiquidityFromZapIn;
|
|
11
|
+
const viem_1 = require("viem");
|
|
12
|
+
const abis_1 = require("../../core/abis");
|
|
13
|
+
const pathEncoder_1 = require("../../utils/pathEncoder");
|
|
14
|
+
// ========== ENCODING FUNCTIONS ==========
|
|
15
|
+
/**
|
|
16
|
+
* Encodes the zapIn function call for the Router contract
|
|
17
|
+
*/
|
|
18
|
+
function encodeZapInCall(tokenIn, amountInA, amountInB, zapParams, routesA, routesB, recipient) {
|
|
19
|
+
return (0, viem_1.encodeFunctionData)({
|
|
20
|
+
abi: abis_1.ROUTER_ABI,
|
|
21
|
+
functionName: 'zapIn',
|
|
22
|
+
args: [tokenIn, amountInA, amountInB, zapParams, routesA, routesB, recipient],
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Encodes the zapOut function call for the Router contract
|
|
27
|
+
*/
|
|
28
|
+
function encodeZapOutCall(tokenOut, liquidity, zapParams, routesA, routesB) {
|
|
29
|
+
return (0, viem_1.encodeFunctionData)({
|
|
30
|
+
abi: abis_1.ROUTER_ABI,
|
|
31
|
+
functionName: 'zapOut',
|
|
32
|
+
args: [tokenOut, liquidity, zapParams, routesA, routesB],
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
// ========== ROUTING FUNCTIONS ==========
|
|
36
|
+
/**
|
|
37
|
+
* Finds routes for zap in operations (from tokenIn to both pool tokens)
|
|
38
|
+
*
|
|
39
|
+
* @param routeService - Route service for finding swap paths
|
|
40
|
+
* @param tokenIn - Input token address
|
|
41
|
+
* @param token0 - Pool's token0 address
|
|
42
|
+
* @param token1 - Pool's token1 address
|
|
43
|
+
* @returns Routes from tokenIn to token0 and token1
|
|
44
|
+
*/
|
|
45
|
+
async function findZapInRoutes(routeService, tokenIn, token0, token1) {
|
|
46
|
+
const [routeA, routeB] = await Promise.all([
|
|
47
|
+
tokenIn.toLowerCase() !== token0.toLowerCase()
|
|
48
|
+
? routeService.findRoute(tokenIn, token0)
|
|
49
|
+
: Promise.resolve(null),
|
|
50
|
+
tokenIn.toLowerCase() !== token1.toLowerCase()
|
|
51
|
+
? routeService.findRoute(tokenIn, token1)
|
|
52
|
+
: Promise.resolve(null),
|
|
53
|
+
]);
|
|
54
|
+
const routesA = routeA ? (0, pathEncoder_1.encodeRoutePath)(routeA.path, tokenIn, token0) : [];
|
|
55
|
+
const routesB = routeB ? (0, pathEncoder_1.encodeRoutePath)(routeB.path, tokenIn, token1) : [];
|
|
56
|
+
return { routesA, routesB };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Finds routes for zap out operations (from both pool tokens to tokenOut)
|
|
60
|
+
*
|
|
61
|
+
* @param routeService - Route service for finding swap paths
|
|
62
|
+
* @param token0 - Pool's token0 address
|
|
63
|
+
* @param token1 - Pool's token1 address
|
|
64
|
+
* @param tokenOut - Output token address
|
|
65
|
+
* @returns Routes from token0 and token1 to tokenOut
|
|
66
|
+
*/
|
|
67
|
+
async function findZapOutRoutes(routeService, token0, token1, tokenOut) {
|
|
68
|
+
const [routeA, routeB] = await Promise.all([
|
|
69
|
+
token0.toLowerCase() !== tokenOut.toLowerCase()
|
|
70
|
+
? routeService.findRoute(token0, tokenOut)
|
|
71
|
+
: Promise.resolve(null),
|
|
72
|
+
token1.toLowerCase() !== tokenOut.toLowerCase()
|
|
73
|
+
? routeService.findRoute(token1, tokenOut)
|
|
74
|
+
: Promise.resolve(null),
|
|
75
|
+
]);
|
|
76
|
+
const routesA = routeA ? (0, pathEncoder_1.encodeRoutePath)(routeA.path, token0, tokenOut) : [];
|
|
77
|
+
const routesB = routeB ? (0, pathEncoder_1.encodeRoutePath)(routeB.path, token1, tokenOut) : [];
|
|
78
|
+
return { routesA, routesB };
|
|
79
|
+
}
|
|
80
|
+
// ========== CALCULATION FUNCTIONS ==========
|
|
81
|
+
/**
|
|
82
|
+
* Splits an amount into two parts based on a ratio
|
|
83
|
+
*
|
|
84
|
+
* @param amountIn - Total input amount
|
|
85
|
+
* @param splitRatio - Ratio for splitting (0-1, e.g., 0.5 for 50/50)
|
|
86
|
+
* @returns Split amounts for each part
|
|
87
|
+
*/
|
|
88
|
+
function splitAmount(amountIn, splitRatio) {
|
|
89
|
+
if (splitRatio < 0 || splitRatio > 1) {
|
|
90
|
+
throw new Error('Split ratio must be between 0 and 1');
|
|
91
|
+
}
|
|
92
|
+
const amountA = (amountIn * BigInt(Math.floor(splitRatio * 10000))) / 10000n;
|
|
93
|
+
const amountB = amountIn - amountA;
|
|
94
|
+
return { amountA, amountB };
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Off-chain mirror of Router.sol's quoteAddLiquidity / _quoteZapLiquidity logic.
|
|
98
|
+
*
|
|
99
|
+
* Given desired amounts and pool reserves, returns the (amountA, amountB) the
|
|
100
|
+
* router will actually deposit when adding liquidity. Used to predict the exact
|
|
101
|
+
* `amountAMin` / `amountBMin` the contract will check against post-swap reserves.
|
|
102
|
+
*
|
|
103
|
+
* @param amountADesired - Desired amount of tokenA
|
|
104
|
+
* @param amountBDesired - Desired amount of tokenB
|
|
105
|
+
* @param reserveA - Reserve of tokenA at the moment liquidity is added
|
|
106
|
+
* @param reserveB - Reserve of tokenB at the moment liquidity is added
|
|
107
|
+
*/
|
|
108
|
+
function quoteAddLiquidityFromReserves(amountADesired, amountBDesired, reserveA, reserveB) {
|
|
109
|
+
if (reserveA === 0n && reserveB === 0n) {
|
|
110
|
+
return { amountA: amountADesired, amountB: amountBDesired };
|
|
111
|
+
}
|
|
112
|
+
if (reserveA === 0n || reserveB === 0n) {
|
|
113
|
+
// Mirrors Router.sol's InsufficientLiquidity revert. Caller can fall back.
|
|
114
|
+
return { amountA: 0n, amountB: 0n };
|
|
115
|
+
}
|
|
116
|
+
const amountBOptimal = (amountADesired * reserveB) / reserveA;
|
|
117
|
+
if (amountBOptimal <= amountBDesired) {
|
|
118
|
+
return { amountA: amountADesired, amountB: amountBOptimal };
|
|
119
|
+
}
|
|
120
|
+
const amountAOptimal = (amountBDesired * reserveA) / reserveB;
|
|
121
|
+
return { amountA: amountAOptimal, amountB: amountBDesired };
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Computes the net delta a single-hop zap swap applies to a target pool's
|
|
125
|
+
* reserves. Returns `{delta0, delta1}` to add to the pool's pre-swap reserves
|
|
126
|
+
* to obtain the reserves the router will see when it runs `_quoteZapLiquidity`.
|
|
127
|
+
*
|
|
128
|
+
* Only single-hop routes whose factory matches the target pool's factory and
|
|
129
|
+
* whose `(from, to)` are `(token0, token1)` (in either direction) are
|
|
130
|
+
* considered. Multi-hop routes and routes through other pools have no effect
|
|
131
|
+
* on the target pool's reserves and return `{0, 0}`.
|
|
132
|
+
*
|
|
133
|
+
* Note: Multi-hop routes that traverse the target pool as an intermediate hop
|
|
134
|
+
* are intentionally not handled here — they are uncommon for single-sided zaps
|
|
135
|
+
* and would require per-hop amounts from `getAmountsOut`.
|
|
136
|
+
*/
|
|
137
|
+
function computeTargetPoolImpact(routes, amountIn, amountOut, token0, token1, factoryAddr) {
|
|
138
|
+
if (routes.length !== 1) {
|
|
139
|
+
return { delta0: 0n, delta1: 0n };
|
|
140
|
+
}
|
|
141
|
+
const route = routes[0];
|
|
142
|
+
if (route.factory.toLowerCase() !== factoryAddr.toLowerCase()) {
|
|
143
|
+
return { delta0: 0n, delta1: 0n };
|
|
144
|
+
}
|
|
145
|
+
const fromLower = route.from.toLowerCase();
|
|
146
|
+
const toLower = route.to.toLowerCase();
|
|
147
|
+
const t0 = token0.toLowerCase();
|
|
148
|
+
const t1 = token1.toLowerCase();
|
|
149
|
+
if (fromLower === t0 && toLower === t1) {
|
|
150
|
+
return { delta0: amountIn, delta1: -amountOut };
|
|
151
|
+
}
|
|
152
|
+
if (fromLower === t1 && toLower === t0) {
|
|
153
|
+
return { delta0: -amountOut, delta1: amountIn };
|
|
154
|
+
}
|
|
155
|
+
return { delta0: 0n, delta1: 0n };
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Estimates minimum LP tokens from zap in amounts.
|
|
159
|
+
*
|
|
160
|
+
* This is a conservative lower-bound estimate. The inputs are slippage-adjusted
|
|
161
|
+
* minimums, not expected amounts. Actual LP tokens minted on-chain may be higher
|
|
162
|
+
* because the router uses balanceOf(address(this)) after swaps, which can exceed
|
|
163
|
+
* the pre-calculated minimums.
|
|
164
|
+
*
|
|
165
|
+
* @param amountOutA - Minimum amount of token0 after swap (slippage-adjusted)
|
|
166
|
+
* @param amountOutB - Minimum amount of token1 after swap (slippage-adjusted)
|
|
167
|
+
* @param reserve0 - Current reserve of token0 in pool
|
|
168
|
+
* @param reserve1 - Current reserve of token1 in pool
|
|
169
|
+
* @param totalSupply - Total LP token supply
|
|
170
|
+
* @returns Conservative estimate of minimum LP tokens to be minted
|
|
171
|
+
*/
|
|
172
|
+
function estimateLiquidityFromZapIn(amountOutA, amountOutB, reserve0, reserve1, totalSupply) {
|
|
173
|
+
if (totalSupply === 0n) {
|
|
174
|
+
// First liquidity provision - use geometric mean with BigInt sqrt
|
|
175
|
+
// to avoid precision loss from Number() conversion on 18-decimal values
|
|
176
|
+
const product = amountOutA * amountOutB;
|
|
177
|
+
if (product === 0n)
|
|
178
|
+
return 0n;
|
|
179
|
+
let x = product;
|
|
180
|
+
let y = (x + 1n) / 2n;
|
|
181
|
+
while (y < x) {
|
|
182
|
+
x = y;
|
|
183
|
+
y = (x + product / x) / 2n;
|
|
184
|
+
}
|
|
185
|
+
return x;
|
|
186
|
+
}
|
|
187
|
+
// Existing pool - calculate based on smaller ratio
|
|
188
|
+
const liquidityA = (amountOutA * totalSupply) / reserve0;
|
|
189
|
+
const liquidityB = (amountOutB * totalSupply) / reserve1;
|
|
190
|
+
return liquidityA < liquidityB ? liquidityA : liquidityB;
|
|
191
|
+
}
|
|
192
|
+
//# sourceMappingURL=zapHelpers.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
|
@@ -0,0 +1,138 @@
|
|
|
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
|
+
// Re-quote amountAMin / amountBMin against POST-swap reserves.
|
|
76
|
+
//
|
|
77
|
+
// `generateZapInParams` calls `quoteAddLiquidity` with the pool's *current*
|
|
78
|
+
// reserves, but on-chain `_quoteZapLiquidity` runs *after* the zap's internal
|
|
79
|
+
// swap, which moves the same pool when tokenIn is one of the pool tokens.
|
|
80
|
+
// Applying user slippage on top of a pre-swap minimum makes the contract
|
|
81
|
+
// reject any non-trivial amount because the deterministic price impact of
|
|
82
|
+
// the swap alone exceeds the slippage budget. Predict the post-swap reserves
|
|
83
|
+
// and re-derive the minimums so that user slippage only covers real drift
|
|
84
|
+
// between quote-time and execution.
|
|
85
|
+
const impactA = (0, zapHelpers_1.computeTargetPoolImpact)(routesA, amountInA, amountOutMinA, token0, token1, factoryAddr);
|
|
86
|
+
const impactB = (0, zapHelpers_1.computeTargetPoolImpact)(routesB, amountInB, amountOutMinB, token0, token1, factoryAddr);
|
|
87
|
+
const projectedReserveA = poolSnapshot.reserve0 + impactA.delta0 + impactB.delta0;
|
|
88
|
+
const projectedReserveB = poolSnapshot.reserve1 + impactA.delta1 + impactB.delta1;
|
|
89
|
+
const useReserveA = projectedReserveA > 0n ? projectedReserveA : poolSnapshot.reserve0;
|
|
90
|
+
const useReserveB = projectedReserveB > 0n ? projectedReserveB : poolSnapshot.reserve1;
|
|
91
|
+
const projected = (0, zapHelpers_1.quoteAddLiquidityFromReserves)(amountOutMinA, amountOutMinB, useReserveA, useReserveB);
|
|
92
|
+
const baselineAmountAMin = projected.amountA > 0n ? projected.amountA : amountAMin;
|
|
93
|
+
const baselineAmountBMin = projected.amountB > 0n ? projected.amountB : amountBMin;
|
|
94
|
+
const finalAmountAMin = (0, liquidityHelpers_1.calculateMinAmount)(baselineAmountAMin, options.slippageTolerance);
|
|
95
|
+
const finalAmountBMin = (0, liquidityHelpers_1.calculateMinAmount)(baselineAmountBMin, options.slippageTolerance);
|
|
96
|
+
const finalAmountOutMinA = (0, liquidityHelpers_1.calculateMinAmount)(amountOutMinA, options.slippageTolerance);
|
|
97
|
+
const finalAmountOutMinB = (0, liquidityHelpers_1.calculateMinAmount)(amountOutMinB, options.slippageTolerance);
|
|
98
|
+
const expectedLiquidity = (0, zapHelpers_1.estimateLiquidityFromZapIn)(finalAmountOutMinA, finalAmountOutMinB, poolSnapshot.reserve0, poolSnapshot.reserve1, poolSnapshot.totalSupply);
|
|
99
|
+
const quote = {
|
|
100
|
+
amountOutFromA: finalAmountOutMinA,
|
|
101
|
+
amountOutFromB: finalAmountOutMinB,
|
|
102
|
+
amountAMin: finalAmountAMin,
|
|
103
|
+
amountBMin: finalAmountBMin,
|
|
104
|
+
estimatedMinLiquidity: expectedLiquidity,
|
|
105
|
+
};
|
|
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.encodeZapInCall)(tokenIn, amountInA, amountInB, zapParams, routesA, routesB, recipient);
|
|
116
|
+
return {
|
|
117
|
+
routesA,
|
|
118
|
+
routesB,
|
|
119
|
+
quote,
|
|
120
|
+
details: {
|
|
121
|
+
params: {
|
|
122
|
+
to: routerAddress,
|
|
123
|
+
data,
|
|
124
|
+
value: '0',
|
|
125
|
+
},
|
|
126
|
+
poolAddress,
|
|
127
|
+
tokenIn,
|
|
128
|
+
amountIn,
|
|
129
|
+
amountInA,
|
|
130
|
+
amountInB,
|
|
131
|
+
routesA,
|
|
132
|
+
routesB,
|
|
133
|
+
zapParams,
|
|
134
|
+
estimatedMinLiquidity: expectedLiquidity,
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=zapIn.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|