Skip to content

Instantly share code, notes, and snippets.

@sparrowDom
Created April 9, 2025 19:01
Show Gist options
  • Save sparrowDom/f84cf9b1bd0c8482bac2c50c3a739db1 to your computer and use it in GitHub Desktop.
Save sparrowDom/f84cf9b1bd0c8482bac2c50c3a739db1 to your computer and use it in GitHub Desktop.
int32[] memory ticks = new int32[](1);
uint128[] memory relativeLiquidityAmounts = new uint128[](1);
ticks[0] = -1;
// all liquidity into one tick
relativeLiquidityAmounts[0] = 1e18;
uint256 maxAmountA = 1e18;
// see documentation: https://docs.mav.xyz/technical-reference/maverick-v2/v2-contracts/maverick-v2-supplemental-contracts/interfaces/imaverickv2poollens#addparamsspecification
IMaverickV2PoolLens.AddParamsSpecification memory addSpec = IMaverickV2PoolLens.AddParamsSpecification({
slippageFactorD18: 0.01e18,
numberOfPriceBreaksPerSide: 0,
targetAmount: maxAmountA,
targetIsA: false
});
int32 activeTick = mPool.getState().activeTick;
console.log("activeTick");
console.log(activeTick == -1); // true
// see documentation: https://docs.mav.xyz/technical-reference/maverick-v2/v2-contracts/maverick-v2-supplemental-contracts/maverickv2poollens#getaddliquidityparams
(bytes memory packedSqrtPriceBreaks, bytes[] memory packedArgs, , , ) = poolLens.getAddLiquidityParams(IMaverickV2PoolLens.AddParamsViewInputs({
pool: mPool,
kind: 0, // static kind
ticks: ticks,
relativeLiquidityAmounts: relativeLiquidityAmounts,
addSpec: addSpec
}));
console.log("getAddLiquidityParams called");
(uint256 amountA, uint256 amountB, , uint256 _tokenId) = liquidityManager.mintPositionNftToSender(mPool, packedSqrtPriceBreaks, packedArgs);
// Store the tokenId
tokenId = _tokenId;
@gte620v
Copy link

gte620v commented Apr 9, 2025

IPool.TickState memory tickState = pool.getTick(activeTick);
IMaverickV2PoolLens.AddParamsViewInputs memory params = IMaverickV2PoolLens.AddParamsViewInputs({
    pool: mPool,
    kind: 0, // static kind
    ticks: ticks,
    relativeLiquidityAmounts: relativeLiquidityAmounts,
    addSpec: addSpec
})
if (tickState.reserveA == 0) {
 // we only need to check targetIsA = false
 params.addSpec.maxAmount = maxAmountB;
 params.addSpec.targetIsA = false;
 (bytes memory packedSqrtPriceBreaks, bytes[] memory packedArgs, , , ) = poolLens.getAddLiquidityParams(params);
 
 } else if (tickState.reserveB == 0) {
 // we only need to check targetIsA = true
 params.addSpec.maxAmount = maxAmountA;
 params.addSpec.targetIsA = true;
 (bytes memory packedSqrtPriceBreaks, bytes[] memory packedArgs, , , ) = poolLens.getAddLiquidityParams(params);
 
 } else {
  // we need to check both
  
 params.addSpec.maxAmount = maxAmountA;
 params.addSpec.targetIsA = true;
 (bytes memory packedSqrtPriceBreaks, bytes[] memory packedArgs, , ,IMaverickV2PoolLens.TickDeltas[] memory tickDeltas ) = poolLens.getAddLiquidityParams(params);
    if (tickDeltas[0].deltaBOut > maxAmountB) {
        // we know the params didn't meet out max spec.  we are asking for more B than we want to spend.  
        // do the call again with B as the target.  
       params.addSpec.maxAmount = maxAmountB;
       params.addSpec.targetIsA = false;
       (bytes memory packedSqrtPriceBreaks, bytes[] memory packedArgs, , , ) = poolLens.getAddLiquidityParams(params);
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment