@orca-so/whirlpools-sdk
Advanced tools
Comparing version 0.12.5 to 0.12.6
@@ -49,2 +49,9 @@ /// <reference types="bn.js" /> | ||
*/ | ||
export declare function decreaseLiquidityQuoteByLiquidityWithParams(param: DecreaseLiquidityQuoteParam): DecreaseLiquidityQuote; | ||
export declare function decreaseLiquidityQuoteByLiquidityWithParams(params: DecreaseLiquidityQuoteParam): DecreaseLiquidityQuote; | ||
/** | ||
* Get an estimated quote on the minimum tokens receivable based on the desired withdraw liquidity value. | ||
* This version calculates slippage based on price percentage movement, rather than setting the percentage threshold based on token estimates. | ||
* @param params DecreaseLiquidityQuoteParam | ||
* @returns A DecreaseLiquidityQuote object detailing the tokenMin & liquidity values to use when calling decrease-liquidity-ix. | ||
*/ | ||
export declare function decreaseLiquidityQuoteByLiquidityWithParamsUsingPriceSlippage(params: DecreaseLiquidityQuoteParam): DecreaseLiquidityQuote; |
@@ -6,3 +6,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.decreaseLiquidityQuoteByLiquidityWithParams = exports.decreaseLiquidityQuoteByLiquidity = void 0; | ||
exports.decreaseLiquidityQuoteByLiquidityWithParamsUsingPriceSlippage = exports.decreaseLiquidityQuoteByLiquidityWithParams = exports.decreaseLiquidityQuoteByLiquidity = void 0; | ||
const anchor_1 = require("@coral-xyz/anchor"); | ||
const common_sdk_1 = require("@orca-so/common-sdk"); | ||
@@ -43,42 +44,56 @@ const tiny_invariant_1 = __importDefault(require("tiny-invariant")); | ||
*/ | ||
function decreaseLiquidityQuoteByLiquidityWithParams(param) { | ||
(0, tiny_invariant_1.default)(public_1.TickUtil.checkTickInBounds(param.tickLowerIndex), "tickLowerIndex is out of bounds."); | ||
(0, tiny_invariant_1.default)(public_1.TickUtil.checkTickInBounds(param.tickUpperIndex), "tickUpperIndex is out of bounds."); | ||
(0, tiny_invariant_1.default)(public_1.TickUtil.checkTickInBounds(param.tickCurrentIndex), "tickCurrentIndex is out of bounds."); | ||
const positionStatus = position_util_1.PositionUtil.getStrictPositionStatus(param.sqrtPrice, param.tickLowerIndex, param.tickUpperIndex); | ||
switch (positionStatus) { | ||
case position_util_1.PositionStatus.BelowRange: | ||
return quotePositionBelowRange(param); | ||
case position_util_1.PositionStatus.InRange: | ||
return quotePositionInRange(param); | ||
case position_util_1.PositionStatus.AboveRange: | ||
return quotePositionAboveRange(param); | ||
default: | ||
throw new Error(`type ${positionStatus} is an unknown PositionStatus`); | ||
function decreaseLiquidityQuoteByLiquidityWithParams(params) { | ||
(0, tiny_invariant_1.default)(public_1.TickUtil.checkTickInBounds(params.tickLowerIndex), "tickLowerIndex is out of bounds."); | ||
(0, tiny_invariant_1.default)(public_1.TickUtil.checkTickInBounds(params.tickUpperIndex), "tickUpperIndex is out of bounds."); | ||
(0, tiny_invariant_1.default)(public_1.TickUtil.checkTickInBounds(params.tickCurrentIndex), "tickCurrentIndex is out of bounds."); | ||
if (params.liquidity.eq(common_sdk_1.ZERO)) { | ||
return { | ||
tokenMinA: common_sdk_1.ZERO, | ||
tokenMinB: common_sdk_1.ZERO, | ||
liquidityAmount: common_sdk_1.ZERO, | ||
tokenEstA: common_sdk_1.ZERO, | ||
tokenEstB: common_sdk_1.ZERO, | ||
}; | ||
} | ||
} | ||
exports.decreaseLiquidityQuoteByLiquidityWithParams = decreaseLiquidityQuoteByLiquidityWithParams; | ||
function quotePositionBelowRange(param) { | ||
const { tickLowerIndex, tickUpperIndex, liquidity, slippageTolerance } = param; | ||
const sqrtPriceLowerX64 = public_1.PriceMath.tickIndexToSqrtPriceX64(tickLowerIndex); | ||
const sqrtPriceUpperX64 = public_1.PriceMath.tickIndexToSqrtPriceX64(tickUpperIndex); | ||
const tokenEstA = (0, position_util_1.getTokenAFromLiquidity)(liquidity, sqrtPriceLowerX64, sqrtPriceUpperX64, false); | ||
const tokenMinA = (0, position_util_1.adjustForSlippage)(tokenEstA, slippageTolerance, false); | ||
const { tokenEstA, tokenEstB } = getTokenEstimatesFromLiquidity(params); | ||
const [tokenMinA, tokenMinB] = [tokenEstA, tokenEstB].map((tokenEst) => (0, position_util_1.adjustForSlippage)(tokenEst, params.slippageTolerance, false)); | ||
return { | ||
tokenMinA, | ||
tokenMinB: common_sdk_1.ZERO, | ||
tokenMinB, | ||
tokenEstA, | ||
tokenEstB: common_sdk_1.ZERO, | ||
liquidityAmount: liquidity, | ||
tokenEstB, | ||
liquidityAmount: params.liquidity, | ||
}; | ||
} | ||
function quotePositionInRange(param) { | ||
const { sqrtPrice, tickLowerIndex, tickUpperIndex, liquidity, slippageTolerance } = param; | ||
const sqrtPriceX64 = sqrtPrice; | ||
const sqrtPriceLowerX64 = public_1.PriceMath.tickIndexToSqrtPriceX64(tickLowerIndex); | ||
const sqrtPriceUpperX64 = public_1.PriceMath.tickIndexToSqrtPriceX64(tickUpperIndex); | ||
const tokenEstA = (0, position_util_1.getTokenAFromLiquidity)(liquidity, sqrtPriceX64, sqrtPriceUpperX64, false); | ||
const tokenMinA = (0, position_util_1.adjustForSlippage)(tokenEstA, slippageTolerance, false); | ||
const tokenEstB = (0, position_util_1.getTokenBFromLiquidity)(liquidity, sqrtPriceLowerX64, sqrtPriceX64, false); | ||
const tokenMinB = (0, position_util_1.adjustForSlippage)(tokenEstB, slippageTolerance, false); | ||
exports.decreaseLiquidityQuoteByLiquidityWithParams = decreaseLiquidityQuoteByLiquidityWithParams; | ||
/** | ||
* Get an estimated quote on the minimum tokens receivable based on the desired withdraw liquidity value. | ||
* This version calculates slippage based on price percentage movement, rather than setting the percentage threshold based on token estimates. | ||
* @param params DecreaseLiquidityQuoteParam | ||
* @returns A DecreaseLiquidityQuote object detailing the tokenMin & liquidity values to use when calling decrease-liquidity-ix. | ||
*/ | ||
function decreaseLiquidityQuoteByLiquidityWithParamsUsingPriceSlippage(params) { | ||
if (params.liquidity.eq(common_sdk_1.ZERO)) { | ||
return { | ||
tokenMinA: common_sdk_1.ZERO, | ||
tokenMinB: common_sdk_1.ZERO, | ||
liquidityAmount: common_sdk_1.ZERO, | ||
tokenEstA: common_sdk_1.ZERO, | ||
tokenEstB: common_sdk_1.ZERO, | ||
}; | ||
} | ||
const { tokenEstA, tokenEstB } = getTokenEstimatesFromLiquidity(params); | ||
const { lowerBound: [sLowerSqrtPrice, sLowerIndex], upperBound: [sUpperSqrtPrice, sUpperIndex], } = public_1.PriceMath.getSlippageBoundForSqrtPrice(params.sqrtPrice, params.slippageTolerance); | ||
const { tokenEstA: tokenEstALower, tokenEstB: tokenEstBLower } = getTokenEstimatesFromLiquidity({ | ||
...params, | ||
sqrtPrice: sLowerSqrtPrice, | ||
tickCurrentIndex: sLowerIndex, | ||
}); | ||
const { tokenEstA: tokenEstAUpper, tokenEstB: tokenEstBUpper } = getTokenEstimatesFromLiquidity({ | ||
...params, | ||
sqrtPrice: sUpperSqrtPrice, | ||
tickCurrentIndex: sUpperIndex, | ||
}); | ||
const tokenMinA = anchor_1.BN.min(anchor_1.BN.min(tokenEstA, tokenEstALower), tokenEstAUpper); | ||
const tokenMinB = anchor_1.BN.min(anchor_1.BN.min(tokenEstB, tokenEstBLower), tokenEstBUpper); | ||
return { | ||
@@ -89,18 +104,27 @@ tokenMinA, | ||
tokenEstB, | ||
liquidityAmount: liquidity, | ||
liquidityAmount: params.liquidity, | ||
}; | ||
} | ||
function quotePositionAboveRange(param) { | ||
const { tickLowerIndex, tickUpperIndex, liquidity, slippageTolerance: slippageTolerance } = param; | ||
const sqrtPriceLowerX64 = public_1.PriceMath.tickIndexToSqrtPriceX64(tickLowerIndex); | ||
const sqrtPriceUpperX64 = public_1.PriceMath.tickIndexToSqrtPriceX64(tickUpperIndex); | ||
const tokenEstB = (0, position_util_1.getTokenBFromLiquidity)(liquidity, sqrtPriceLowerX64, sqrtPriceUpperX64, false); | ||
const tokenMinB = (0, position_util_1.adjustForSlippage)(tokenEstB, slippageTolerance, false); | ||
return { | ||
tokenMinA: common_sdk_1.ZERO, | ||
tokenMinB, | ||
tokenEstA: common_sdk_1.ZERO, | ||
tokenEstB, | ||
liquidityAmount: liquidity, | ||
}; | ||
exports.decreaseLiquidityQuoteByLiquidityWithParamsUsingPriceSlippage = decreaseLiquidityQuoteByLiquidityWithParamsUsingPriceSlippage; | ||
function getTokenEstimatesFromLiquidity(params) { | ||
const { liquidity, tickLowerIndex, tickUpperIndex, sqrtPrice } = params; | ||
if (liquidity.eq(common_sdk_1.ZERO)) { | ||
throw new Error("liquidity must be greater than 0"); | ||
} | ||
let tokenEstA = common_sdk_1.ZERO; | ||
let tokenEstB = common_sdk_1.ZERO; | ||
const lowerSqrtPrice = public_1.PriceMath.tickIndexToSqrtPriceX64(tickLowerIndex); | ||
const upperSqrtPrice = public_1.PriceMath.tickIndexToSqrtPriceX64(tickUpperIndex); | ||
const positionStatus = position_util_1.PositionUtil.getStrictPositionStatus(sqrtPrice, tickLowerIndex, tickUpperIndex); | ||
if (positionStatus === position_util_1.PositionStatus.BelowRange) { | ||
tokenEstA = (0, position_util_1.getTokenAFromLiquidity)(liquidity, lowerSqrtPrice, upperSqrtPrice, false); | ||
} | ||
else if (positionStatus === position_util_1.PositionStatus.InRange) { | ||
tokenEstA = (0, position_util_1.getTokenAFromLiquidity)(liquidity, sqrtPrice, upperSqrtPrice, false); | ||
tokenEstB = (0, position_util_1.getTokenBFromLiquidity)(liquidity, lowerSqrtPrice, sqrtPrice, false); | ||
} | ||
else { | ||
tokenEstB = (0, position_util_1.getTokenBFromLiquidity)(liquidity, lowerSqrtPrice, upperSqrtPrice, false); | ||
} | ||
return { tokenEstA, tokenEstB }; | ||
} |
{ | ||
"name": "@orca-so/whirlpools-sdk", | ||
"version": "0.12.5", | ||
"version": "0.12.6", | ||
"description": "Typescript SDK to interact with Orca's Whirlpool program.", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
988161
20382