@meteora-ag/dlmm 1.0.6 → 1.0.7-abc1234.0

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/index.d.ts CHANGED
@@ -2103,6 +2103,31 @@ type LbClmm = {
2103
2103
  "type": "u64";
2104
2104
  }
2105
2105
  ];
2106
+ },
2107
+ {
2108
+ "name": "setMaxSwappedAmount";
2109
+ "accounts": [
2110
+ {
2111
+ "name": "lbPair";
2112
+ "isMut": true;
2113
+ "isSigner": false;
2114
+ },
2115
+ {
2116
+ "name": "admin";
2117
+ "isMut": true;
2118
+ "isSigner": true;
2119
+ }
2120
+ ];
2121
+ "args": [
2122
+ {
2123
+ "name": "swapCapDeactivateSlot";
2124
+ "type": "u64";
2125
+ },
2126
+ {
2127
+ "name": "maxSwappedAmount";
2128
+ "type": "u64";
2129
+ }
2130
+ ];
2106
2131
  }
2107
2132
  ];
2108
2133
  "accounts": [
@@ -2386,6 +2411,20 @@ type LbClmm = {
2386
2411
  ];
2387
2412
  "type": "u64";
2388
2413
  },
2414
+ {
2415
+ "name": "swapCapDeactivateSlot";
2416
+ "docs": [
2417
+ "Last slot until pool remove max_swapped_amount for buying"
2418
+ ];
2419
+ "type": "u64";
2420
+ },
2421
+ {
2422
+ "name": "maxSwappedAmount";
2423
+ "docs": [
2424
+ "Max X swapped amount user can swap from y to x between activation_slot and last_sloi"
2425
+ ];
2426
+ "type": "u64";
2427
+ },
2389
2428
  {
2390
2429
  "name": "reserved";
2391
2430
  "docs": [
@@ -2394,7 +2433,7 @@ type LbClmm = {
2394
2433
  "type": {
2395
2434
  "array": [
2396
2435
  "u8",
2397
- 80
2436
+ 64
2398
2437
  ];
2399
2438
  };
2400
2439
  }
@@ -4432,12 +4471,12 @@ interface TokenReserve {
4432
4471
  decimal: number;
4433
4472
  }
4434
4473
  type ClmmProgram = Program<LbClmm>;
4435
- type LbPairAccount = IdlAccounts<LbClmm>["lbPair"];
4436
- type LbPairAccountsStruct = ProgramAccount<IdlAccounts<LbClmm>["lbPair"]>;
4474
+ type LbPair = IdlAccounts<LbClmm>["lbPair"];
4475
+ type LbPairAccount = ProgramAccount<IdlAccounts<LbClmm>["lbPair"]>;
4437
4476
  type Bin = IdlTypes<LbClmm>["Bin"];
4438
4477
  type BinArray = IdlAccounts<LbClmm>["binArray"];
4439
4478
  type BinArrayAccount = ProgramAccount<IdlAccounts<LbClmm>["binArray"]>;
4440
- type PositionAccount = IdlAccounts<LbClmm>["position"];
4479
+ type Position = IdlAccounts<LbClmm>["position"];
4441
4480
  type vParameters = IdlAccounts<LbClmm>["lbPair"]["vParameters"];
4442
4481
  type sParameters = IdlAccounts<LbClmm>["lbPair"]["parameters"];
4443
4482
  type BinLiquidityDistribution = IdlTypes<LbClmm>["BinLiquidityDistribution"];
@@ -4446,17 +4485,17 @@ type BinArrayBitmapExtensionAccount = ProgramAccount<IdlAccounts<LbClmm>["binArr
4446
4485
  type BinArrayBitmapExtension = IdlAccounts<LbClmm>["binArrayBitmapExtension"];
4447
4486
  type LiquidityParameterByWeight = IdlTypes<LbClmm>["LiquidityParameterByWeight"];
4448
4487
  type LiquidityOneSideParameter = IdlTypes<LbClmm>["LiquidityOneSideParameter"];
4449
- interface Position {
4488
+ interface LbPosition {
4450
4489
  publicKey: PublicKey;
4451
4490
  positionData: PositionData;
4452
4491
  version: PositionVersion;
4453
4492
  }
4454
4493
  interface PositionInfo {
4455
4494
  publicKey: PublicKey;
4456
- lbPair: LbPairAccount;
4495
+ lbPair: LbPair;
4457
4496
  tokenX: TokenReserve;
4458
4497
  tokenY: TokenReserve;
4459
- lbPairPositionsData: Array<Position>;
4498
+ lbPairPositionsData: Array<LbPosition>;
4460
4499
  }
4461
4500
  interface FeeInfo {
4462
4501
  baseFeeRatePercentage: Decimal;
@@ -4532,6 +4571,7 @@ interface BinLiquidity {
4532
4571
  pricePerToken: string;
4533
4572
  }
4534
4573
  interface SwapQuote {
4574
+ consumedInAmount: BN;
4535
4575
  outAmount: BN;
4536
4576
  fee: BN;
4537
4577
  protocolFee: BN;
@@ -4541,7 +4581,7 @@ interface SwapQuote {
4541
4581
  }
4542
4582
  interface IAccountsCache {
4543
4583
  binArrays: Map<String, BinArray>;
4544
- lbPair: LbPairAccount;
4584
+ lbPair: LbPair;
4545
4585
  }
4546
4586
  interface PositionBinData {
4547
4587
  binId: number;
@@ -4608,12 +4648,12 @@ type Opt = {
4608
4648
  declare class DLMM {
4609
4649
  pubkey: PublicKey;
4610
4650
  program: ClmmProgram;
4611
- lbPair: LbPairAccount;
4651
+ lbPair: LbPair;
4612
4652
  binArrayBitmapExtension: BinArrayBitmapExtensionAccount | null;
4613
4653
  tokenX: TokenReserve;
4614
4654
  tokenY: TokenReserve;
4615
4655
  private opt?;
4616
- constructor(pubkey: PublicKey, program: ClmmProgram, lbPair: LbPairAccount, binArrayBitmapExtension: BinArrayBitmapExtensionAccount | null, tokenX: TokenReserve, tokenY: TokenReserve, opt?: Opt);
4656
+ constructor(pubkey: PublicKey, program: ClmmProgram, lbPair: LbPair, binArrayBitmapExtension: BinArrayBitmapExtensionAccount | null, tokenX: TokenReserve, tokenY: TokenReserve, opt?: Opt);
4617
4657
  /** Static public method */
4618
4658
  /**
4619
4659
  * The function `getLbPairs` retrieves a list of LB pair accounts using a connection and optional
@@ -4623,9 +4663,9 @@ declare class DLMM {
4623
4663
  * @param {Opt} [opt] - The `opt` parameter is an optional object that contains additional options
4624
4664
  * for the function. It can have the following properties:
4625
4665
  * @returns The function `getLbPairs` returns a Promise that resolves to an array of
4626
- * `LbPairAccountsStruct` objects.
4666
+ * `LbPairAccount` objects.
4627
4667
  */
4628
- static getLbPairs(connection: Connection, opt?: Opt): Promise<LbPairAccountsStruct[]>;
4668
+ static getLbPairs(connection: Connection, opt?: Opt): Promise<LbPairAccount[]>;
4629
4669
  /**
4630
4670
  * The `create` function is a static method that creates a new instance of the `DLMM` class
4631
4671
  * @param {Connection} connection - The `connection` parameter is an instance of the `Connection`
@@ -4800,7 +4840,7 @@ declare class DLMM {
4800
4840
  binId: any;
4801
4841
  price: string;
4802
4842
  };
4803
- userPositions: Array<Position>;
4843
+ userPositions: Array<LbPosition>;
4804
4844
  }>;
4805
4845
  /**
4806
4846
  * The function `initializePositionAndAddLiquidityByWeight` function is used to initializes a position and adds liquidity
@@ -4853,8 +4893,25 @@ declare class DLMM {
4853
4893
  */
4854
4894
  closePosition({ owner, position, }: {
4855
4895
  owner: PublicKey;
4856
- position: Position;
4896
+ position: LbPosition;
4857
4897
  }): Promise<Transaction>;
4898
+ /**
4899
+ * The `swapQuoteWithCap` function returns a quote for a swap in permission pool
4900
+ * @param
4901
+ * - `inAmount`: Amount of lamport to swap in
4902
+ * - `swapForY`: Swap token X to Y when it is true, else reversed.
4903
+ * - `allowedSlipage`: Allowed slippage for the swap. Expressed in BPS. To convert from slippage percentage to BPS unit: SLIPPAGE_PERCENTAGE * 100
4904
+ * - `maxSwappedAmount`: Max swapped amount
4905
+ * @returns {SwapQuote}
4906
+ * - `consumedInAmount`: Amount of lamport to swap in
4907
+ * - `outAmount`: Amount of lamport to swap out
4908
+ * - `fee`: Fee amount
4909
+ * - `protocolFee`: Protocol fee amount
4910
+ * - `minOutAmount`: Minimum amount of lamport to swap out
4911
+ * - `priceImpact`: Price impact of the swap
4912
+ * - `binArraysPubkey`: Array of bin arrays involved in the swap
4913
+ */
4914
+ swapQuoteWithCap(inAmount: BN, swapForY: boolean, allowedSlippage: BN, maxSwappedAmount: BN, binArrays: BinArrayAccount[]): SwapQuote;
4858
4915
  /**
4859
4916
  * The `swapQuote` function returns a quote for a swap
4860
4917
  * @param
@@ -4862,6 +4919,7 @@ declare class DLMM {
4862
4919
  * - `swapForY`: Swap token X to Y when it is true, else reversed.
4863
4920
  * - `allowedSlipage`: Allowed slippage for the swap. Expressed in BPS. To convert from slippage percentage to BPS unit: SLIPPAGE_PERCENTAGE * 100
4864
4921
  * @returns {SwapQuote}
4922
+ * - `consumedInAmount`: Amount of lamport to swap in
4865
4923
  * - `outAmount`: Amount of lamport to swap out
4866
4924
  * - `fee`: Fee amount
4867
4925
  * - `protocolFee`: Protocol fee amount
@@ -4892,7 +4950,7 @@ declare class DLMM {
4892
4950
  */
4893
4951
  claimLMReward({ owner, position, }: {
4894
4952
  owner: PublicKey;
4895
- position: Position;
4953
+ position: LbPosition;
4896
4954
  }): Promise<Transaction>;
4897
4955
  /**
4898
4956
  * The `claimAllLMRewards` function is used to claim all liquidity mining rewards for a given owner
@@ -4904,7 +4962,7 @@ declare class DLMM {
4904
4962
  */
4905
4963
  claimAllLMRewards({ owner, positions, }: {
4906
4964
  owner: PublicKey;
4907
- positions: Position[];
4965
+ positions: LbPosition[];
4908
4966
  }): Promise<Transaction[]>;
4909
4967
  /**
4910
4968
  * The function `claimSwapFee` is used to claim swap fees for a specific position owned by a specific owner.
@@ -4915,7 +4973,7 @@ declare class DLMM {
4915
4973
  */
4916
4974
  claimSwapFee({ owner, position, }: {
4917
4975
  owner: PublicKey;
4918
- position: Position;
4976
+ position: LbPosition;
4919
4977
  }): Promise<Transaction>;
4920
4978
  /**
4921
4979
  * The `claimAllSwapFee` function to claim swap fees for multiple positions owned by a specific owner.
@@ -4926,7 +4984,7 @@ declare class DLMM {
4926
4984
  */
4927
4985
  claimAllSwapFee({ owner, positions, }: {
4928
4986
  owner: PublicKey;
4929
- positions: Position[];
4987
+ positions: LbPosition[];
4930
4988
  }): Promise<Transaction[]>;
4931
4989
  /**
4932
4990
  * The function `claimAllRewardsByPosition` allows a user to claim all rewards for a specific
@@ -4938,7 +4996,7 @@ declare class DLMM {
4938
4996
  */
4939
4997
  claimAllRewardsByPosition({ owner, position, }: {
4940
4998
  owner: PublicKey;
4941
- position: Position;
4999
+ position: LbPosition;
4942
5000
  }): Promise<Transaction[]>;
4943
5001
  /**
4944
5002
  * The `claimAllRewards` function to claim swap fees and LM rewards for multiple positions owned by a specific owner.
@@ -4949,7 +5007,7 @@ declare class DLMM {
4949
5007
  */
4950
5008
  claimAllRewards({ owner, positions, }: {
4951
5009
  owner: PublicKey;
4952
- positions: Position[];
5010
+ positions: LbPosition[];
4953
5011
  }): Promise<Transaction[]>;
4954
5012
  /** Private static method */
4955
5013
  private static getBinArrays;
@@ -4983,8 +5041,8 @@ declare function binIdToBinArrayIndex(binId: BN): BN;
4983
5041
  declare function getBinArrayLowerUpperBinId(binArrayIndex: BN): BN[];
4984
5042
  declare function isBinIdWithinBinArray(activeId: BN, binArrayIndex: BN): boolean;
4985
5043
  declare function getBinFromBinArray(binId: number, binArray: BinArray): Bin;
4986
- declare function findNextBinArrayIndexWithLiquidity(swapForY: boolean, activeId: BN, lbPairState: LbPairAccount, binArrayBitmapExtension: BinArrayBitmapExtension | null): BN | null;
4987
- declare function findNextBinArrayWithLiquidity(swapForY: boolean, activeBinId: BN, lbPairState: LbPairAccount, binArrayBitmapExtension: BinArrayBitmapExtension, binArrays: BinArrayAccount[]): BinArrayAccount | null;
5044
+ declare function findNextBinArrayIndexWithLiquidity(swapForY: boolean, activeId: BN, lbPairState: LbPair, binArrayBitmapExtension: BinArrayBitmapExtension | null): BN | null;
5045
+ declare function findNextBinArrayWithLiquidity(swapForY: boolean, activeBinId: BN, lbPairState: LbPair, binArrayBitmapExtension: BinArrayBitmapExtension, binArrays: BinArrayAccount[]): BinArrayAccount | null;
4988
5046
 
4989
5047
  declare function getPriceOfBinByBinId(binId: number, binStep: number): Decimal;
4990
5048
  /** private */
@@ -5050,6 +5108,13 @@ declare function getTotalFee(binStep: number, sParameter: sParameters, vParamete
5050
5108
  declare function computeFee(binStep: number, sParameter: sParameters, vParameter: vParameters, inAmount: BN): BN;
5051
5109
  declare function computeFeeFromAmount(binStep: number, sParameter: sParameters, vParameter: vParameters, inAmountWithFees: BN): BN;
5052
5110
  declare function computeProtocolFee(feeAmount: BN, sParameter: sParameters): BN;
5111
+ declare function swapQuoteAtBinWithCap(bin: Bin, binStep: number, sParameter: sParameters, vParameter: vParameters, inAmount: BN, swapForY: boolean, remainingCap: BN): {
5112
+ isReachCap: boolean;
5113
+ amountIn: BN;
5114
+ amountOut: BN;
5115
+ fee: BN;
5116
+ protocolFee: BN;
5117
+ };
5053
5118
  declare function swapQuoteAtBin(bin: Bin, binStep: number, sParameter: sParameters, vParameter: vParameters, inAmount: BN, swapForY: boolean): {
5054
5119
  amountIn: BN;
5055
5120
  amountOut: BN;
@@ -5111,6 +5176,8 @@ declare function chunkedFetchMultiplePoolAccount(program: ClmmProgram, pks: Publ
5111
5176
  whitelistedWallet: PublicKey[];
5112
5177
  baseKey: PublicKey;
5113
5178
  activationSlot: BN;
5179
+ swapCapDeactivateSlot: BN;
5180
+ maxSwappedAmount: BN;
5114
5181
  reserved: number[];
5115
5182
  }[]>;
5116
5183
  declare function chunkedFetchMultipleBinArrayBitmapExtensionAccount(program: ClmmProgram, pks: PublicKey[], chunkSize?: number): Promise<{
@@ -5166,4 +5233,4 @@ declare const MAX_CLAIM_ALL_ALLOWED = 3;
5166
5233
  declare const MAX_BIN_LENGTH_ALLOWED_IN_ONE_TX = 26;
5167
5234
  declare const MAX_ACTIVE_BIN_SLIPPAGE = 3;
5168
5235
 
5169
- export { ADMIN, BASIS_POINT_MAX, BIN_ARRAY_BITMAP_SIZE, Bin, BinAndAmount, BinArray, BinArrayAccount, BinArrayBitmapExtension, BinArrayBitmapExtensionAccount, BinLiquidity, BinLiquidityDistribution, BinLiquidityReduction, BitmapType, ClmmProgram, DLMMError, EXTENSION_BINARRAY_BITMAP_SIZE, EmissionRate, FEE_PRECISION, FeeInfo, GetOrCreateATAResponse, IAccountsCache, IDL, LBCLMM_PROGRAM_IDS, LMRewards, LbClmm, LbPairAccount, LbPairAccountsStruct, LiquidityOneSideParameter, LiquidityParameterByWeight, MAX_ACTIVE_BIN_SLIPPAGE, MAX_BIN_ARRAY_SIZE, MAX_BIN_LENGTH_ALLOWED_IN_ONE_TX, MAX_BIN_PER_POSITION, MAX_CLAIM_ALL_ALLOWED, MAX_FEE_RATE, Network, PRECISION, ParabolicParameter, Position, PositionAccount, PositionBinData, PositionData, PositionInfo, PositionVersion, SCALE, SCALE_OFFSET, SIMULATION_USER, SpotParameter, StrategyParameters, StrategyType, SwapFee, SwapParams, SwapQuote, TInitializePositionAndAddLiquidityParams, TokenReserve, binIdToBinArrayIndex, calculateBidAskDistribution, calculateNormalDistribution, calculateSpotDistribution, chunkedFetchMultipleBinArrayBitmapExtensionAccount, chunkedFetchMultiplePoolAccount, chunkedGetMultipleAccountInfos, chunks, computeBudgetIx, computeFee, computeFeeFromAmount, computeProtocolFee, DLMM as default, deriveBinArray, deriveBinArrayBitmapExtension, deriveLbPair, deriveOracle, derivePosition, derivePresetParameter, deriveReserve, findNextBinArrayIndexWithLiquidity, findNextBinArrayWithLiquidity, fromStrategyParamstoWeightDistribution, getBaseFee, getBinArrayLowerUpperBinId, getBinFromBinArray, getOrCreateATAInstruction, getOutAmount, getPriceOfBinByBinId, getTokenBalance, getTokenDecimals, getTotalFee, getVariableFee, isBinIdWithinBinArray, isOverflowDefaultBinArrayBitmap, parabolicParameter, parseLogs, sParameters, spotParameter, swapQuoteAtBin, toStrategyParameters, toWeightDistribution, unwrapSOLInstruction, vParameters, wrapSOLInstruction };
5236
+ export { ADMIN, BASIS_POINT_MAX, BIN_ARRAY_BITMAP_SIZE, Bin, BinAndAmount, BinArray, BinArrayAccount, BinArrayBitmapExtension, BinArrayBitmapExtensionAccount, BinLiquidity, BinLiquidityDistribution, BinLiquidityReduction, BitmapType, ClmmProgram, DLMMError, EXTENSION_BINARRAY_BITMAP_SIZE, EmissionRate, FEE_PRECISION, FeeInfo, GetOrCreateATAResponse, IAccountsCache, IDL, LBCLMM_PROGRAM_IDS, LMRewards, LbClmm, LbPair, LbPairAccount, LbPosition, LiquidityOneSideParameter, LiquidityParameterByWeight, MAX_ACTIVE_BIN_SLIPPAGE, MAX_BIN_ARRAY_SIZE, MAX_BIN_LENGTH_ALLOWED_IN_ONE_TX, MAX_BIN_PER_POSITION, MAX_CLAIM_ALL_ALLOWED, MAX_FEE_RATE, Network, PRECISION, ParabolicParameter, Position, PositionBinData, PositionData, PositionInfo, PositionVersion, SCALE, SCALE_OFFSET, SIMULATION_USER, SpotParameter, StrategyParameters, StrategyType, SwapFee, SwapParams, SwapQuote, TInitializePositionAndAddLiquidityParams, TokenReserve, binIdToBinArrayIndex, calculateBidAskDistribution, calculateNormalDistribution, calculateSpotDistribution, chunkedFetchMultipleBinArrayBitmapExtensionAccount, chunkedFetchMultiplePoolAccount, chunkedGetMultipleAccountInfos, chunks, computeBudgetIx, computeFee, computeFeeFromAmount, computeProtocolFee, DLMM as default, deriveBinArray, deriveBinArrayBitmapExtension, deriveLbPair, deriveOracle, derivePosition, derivePresetParameter, deriveReserve, findNextBinArrayIndexWithLiquidity, findNextBinArrayWithLiquidity, fromStrategyParamstoWeightDistribution, getBaseFee, getBinArrayLowerUpperBinId, getBinFromBinArray, getOrCreateATAInstruction, getOutAmount, getPriceOfBinByBinId, getTokenBalance, getTokenDecimals, getTotalFee, getVariableFee, isBinIdWithinBinArray, isOverflowDefaultBinArrayBitmap, parabolicParameter, parseLogs, sParameters, spotParameter, swapQuoteAtBin, swapQuoteAtBinWithCap, toStrategyParameters, toWeightDistribution, unwrapSOLInstruction, vParameters, wrapSOLInstruction };
package/dist/index.js CHANGED
@@ -2107,6 +2107,31 @@ var IDL = {
2107
2107
  "type": "u64"
2108
2108
  }
2109
2109
  ]
2110
+ },
2111
+ {
2112
+ "name": "setMaxSwappedAmount",
2113
+ "accounts": [
2114
+ {
2115
+ "name": "lbPair",
2116
+ "isMut": true,
2117
+ "isSigner": false
2118
+ },
2119
+ {
2120
+ "name": "admin",
2121
+ "isMut": true,
2122
+ "isSigner": true
2123
+ }
2124
+ ],
2125
+ "args": [
2126
+ {
2127
+ "name": "swapCapDeactivateSlot",
2128
+ "type": "u64"
2129
+ },
2130
+ {
2131
+ "name": "maxSwappedAmount",
2132
+ "type": "u64"
2133
+ }
2134
+ ]
2110
2135
  }
2111
2136
  ],
2112
2137
  "accounts": [
@@ -2390,6 +2415,20 @@ var IDL = {
2390
2415
  ],
2391
2416
  "type": "u64"
2392
2417
  },
2418
+ {
2419
+ "name": "swapCapDeactivateSlot",
2420
+ "docs": [
2421
+ "Last slot until pool remove max_swapped_amount for buying"
2422
+ ],
2423
+ "type": "u64"
2424
+ },
2425
+ {
2426
+ "name": "maxSwappedAmount",
2427
+ "docs": [
2428
+ "Max X swapped amount user can swap from y to x between activation_slot and last_sloi"
2429
+ ],
2430
+ "type": "u64"
2431
+ },
2393
2432
  {
2394
2433
  "name": "reserved",
2395
2434
  "docs": [
@@ -2398,7 +2437,7 @@ var IDL = {
2398
2437
  "type": {
2399
2438
  "array": [
2400
2439
  "u8",
2401
- 80
2440
+ 64
2402
2441
  ]
2403
2442
  }
2404
2443
  }
@@ -5346,6 +5385,48 @@ function computeFeeFromAmount(binStep, sParameter, vParameter, inAmountWithFees)
5346
5385
  function computeProtocolFee(feeAmount, sParameter) {
5347
5386
  return feeAmount.mul(new (0, _anchor.BN)(sParameter.protocolShare)).div(new (0, _anchor.BN)(BASIS_POINT_MAX));
5348
5387
  }
5388
+ function swapQuoteAtBinWithCap(bin, binStep, sParameter, vParameter, inAmount, swapForY, remainingCap) {
5389
+ let maxAmountOut;
5390
+ if (swapForY) {
5391
+ maxAmountOut = bin.amountY;
5392
+ } else {
5393
+ maxAmountOut = bin.amountX;
5394
+ }
5395
+ if (maxAmountOut.lt(remainingCap)) {
5396
+ const { amountIn: amountIn2, amountOut: amountOut2, fee: fee2, protocolFee: protocolFee2 } = swapQuoteAtBin(
5397
+ bin,
5398
+ binStep,
5399
+ sParameter,
5400
+ vParameter,
5401
+ inAmount,
5402
+ swapForY
5403
+ );
5404
+ return {
5405
+ isReachCap: false,
5406
+ amountIn: amountIn2,
5407
+ amountOut: amountOut2,
5408
+ fee: fee2,
5409
+ protocolFee: protocolFee2
5410
+ };
5411
+ }
5412
+ let amountInWithCap = getAmountIn(remainingCap, bin.price, swapForY);
5413
+ let actualAmountIn = inAmount.gt(amountInWithCap) ? amountInWithCap : inAmount;
5414
+ const { amountIn, amountOut, fee, protocolFee } = swapQuoteAtBin(
5415
+ bin,
5416
+ binStep,
5417
+ sParameter,
5418
+ vParameter,
5419
+ actualAmountIn,
5420
+ swapForY
5421
+ );
5422
+ return {
5423
+ isReachCap: true,
5424
+ amountIn,
5425
+ amountOut,
5426
+ fee,
5427
+ protocolFee
5428
+ };
5429
+ }
5349
5430
  function swapQuoteAtBin(bin, binStep, sParameter, vParameter, inAmount, swapForY) {
5350
5431
  if (swapForY && bin.amountY.isZero()) {
5351
5432
  return {
@@ -5398,6 +5479,13 @@ function swapQuoteAtBin(bin, binStep, sParameter, vParameter, inAmount, swapForY
5398
5479
  protocolFee
5399
5480
  };
5400
5481
  }
5482
+ function getAmountIn(amountOut, price, swapForY) {
5483
+ if (swapForY) {
5484
+ return shlDiv(amountOut, price, SCALE_OFFSET, 1 /* Down */);
5485
+ } else {
5486
+ return mulShr(amountOut, price, SCALE_OFFSET, 1 /* Down */);
5487
+ }
5488
+ }
5401
5489
 
5402
5490
  // src/dlmm/helpers/index.ts
5403
5491
  function chunks(array, size) {
@@ -5518,6 +5606,7 @@ var _bytes = require('@coral-xyz/anchor/dist/cjs/utils/bytes');
5518
5606
 
5519
5607
 
5520
5608
 
5609
+
5521
5610
  var DLMM = class {
5522
5611
  constructor(pubkey, program, lbPair, binArrayBitmapExtension, tokenX, tokenY, opt) {
5523
5612
  this.pubkey = pubkey;
@@ -5537,7 +5626,7 @@ var DLMM = class {
5537
5626
  * @param {Opt} [opt] - The `opt` parameter is an optional object that contains additional options
5538
5627
  * for the function. It can have the following properties:
5539
5628
  * @returns The function `getLbPairs` returns a Promise that resolves to an array of
5540
- * `LbPairAccountsStruct` objects.
5629
+ * `LbPairAccount` objects.
5541
5630
  */
5542
5631
  static async getLbPairs(connection, opt) {
5543
5632
  const provider = new (0, _anchor.AnchorProvider)(
@@ -5685,11 +5774,17 @@ var DLMM = class {
5685
5774
  );
5686
5775
  }
5687
5776
  }
5688
- const reservePublicKeys = Array.from(lbPairArraysMap.values()).map(({ reserveX, reserveY }) => [reserveX, reserveY]).flat();
5689
- const reserveAccountsInfo = await chunkedGetMultipleAccountInfos(
5690
- program.provider.connection,
5691
- reservePublicKeys
5692
- );
5777
+ const reservePublicKeys = Array.from(lbPairArraysMap.values()).map(({ reserveX, reserveY, tokenXMint, tokenYMint }) => [
5778
+ reserveX,
5779
+ reserveY,
5780
+ tokenXMint,
5781
+ tokenYMint
5782
+ ]).flat();
5783
+ const tokenMintPublicKeys = Array.from(lbPairArraysMap.values()).map(({ tokenXMint, tokenYMint }) => [tokenXMint, tokenYMint]).flat();
5784
+ const reserveAndTokenMintAccountsInfo = await chunkedGetMultipleAccountInfos(program.provider.connection, [
5785
+ ...reservePublicKeys,
5786
+ ...tokenMintPublicKeys
5787
+ ]);
5693
5788
  const lbClmmImpl = await Promise.all(
5694
5789
  dlmmList.map(async (lbPair, index) => {
5695
5790
  const lbPairState = lbPairArraysMap.get(lbPair.toBase58());
@@ -5706,18 +5801,22 @@ var DLMM = class {
5706
5801
  publicKey: binArrayBitmapExtensionPubkey
5707
5802
  };
5708
5803
  }
5709
- const reserveXAccountInfo = reserveAccountsInfo[index * 2];
5710
- const reserveYAccountInfo = reserveAccountsInfo[index * 2 + 1];
5804
+ const reserveXAccountInfo = reserveAndTokenMintAccountsInfo[index * 2];
5805
+ const reserveYAccountInfo = reserveAndTokenMintAccountsInfo[index * 2 + 1];
5806
+ const tokenXMintAccountInfo = reserveAndTokenMintAccountsInfo[reservePublicKeys.length + index * 2];
5807
+ const tokenYMintAccountInfo = reserveAndTokenMintAccountsInfo[reservePublicKeys.length + index * 2 + 1];
5711
5808
  if (!reserveXAccountInfo || !reserveYAccountInfo)
5712
5809
  throw new Error(
5713
5810
  `Reserve account for LB Pair ${lbPair.toBase58()} not found`
5714
5811
  );
5715
5812
  const reserveXBalance = _spltoken.AccountLayout.decode(reserveXAccountInfo.data);
5716
5813
  const reserveYBalance = _spltoken.AccountLayout.decode(reserveYAccountInfo.data);
5717
- const [tokenXDecimal, tokenYDecimal] = await Promise.all([
5718
- getTokenDecimals(program.provider.connection, lbPairState.tokenXMint),
5719
- getTokenDecimals(program.provider.connection, lbPairState.tokenYMint)
5720
- ]);
5814
+ const tokenXDecimal = _spltoken.MintLayout.decode(
5815
+ tokenXMintAccountInfo.data
5816
+ ).decimals;
5817
+ const tokenYDecimal = _spltoken.MintLayout.decode(
5818
+ tokenYMintAccountInfo.data
5819
+ ).decimals;
5721
5820
  const tokenX = {
5722
5821
  publicKey: lbPairState.tokenXMint,
5723
5822
  reserve: lbPairState.reserveX,
@@ -7292,6 +7391,114 @@ var DLMM = class {
7292
7391
  feePayer: owner
7293
7392
  }).add(closePositionTx);
7294
7393
  }
7394
+ /**
7395
+ * The `swapQuoteWithCap` function returns a quote for a swap in permission pool
7396
+ * @param
7397
+ * - `inAmount`: Amount of lamport to swap in
7398
+ * - `swapForY`: Swap token X to Y when it is true, else reversed.
7399
+ * - `allowedSlipage`: Allowed slippage for the swap. Expressed in BPS. To convert from slippage percentage to BPS unit: SLIPPAGE_PERCENTAGE * 100
7400
+ * - `maxSwappedAmount`: Max swapped amount
7401
+ * @returns {SwapQuote}
7402
+ * - `consumedInAmount`: Amount of lamport to swap in
7403
+ * - `outAmount`: Amount of lamport to swap out
7404
+ * - `fee`: Fee amount
7405
+ * - `protocolFee`: Protocol fee amount
7406
+ * - `minOutAmount`: Minimum amount of lamport to swap out
7407
+ * - `priceImpact`: Price impact of the swap
7408
+ * - `binArraysPubkey`: Array of bin arrays involved in the swap
7409
+ */
7410
+ swapQuoteWithCap(inAmount, swapForY, allowedSlippage, maxSwappedAmount, binArrays) {
7411
+ const currentTimestamp = Date.now() / 1e3;
7412
+ let inAmountLeft = inAmount;
7413
+ let vParameterClone = Object.assign({}, this.lbPair.vParameters);
7414
+ let activeId = new (0, _anchor.BN)(this.lbPair.activeId);
7415
+ const binStep = this.lbPair.binStep;
7416
+ const sParameters2 = this.lbPair.parameters;
7417
+ this.updateReference(
7418
+ activeId.toNumber(),
7419
+ vParameterClone,
7420
+ sParameters2,
7421
+ currentTimestamp
7422
+ );
7423
+ let startBin = null;
7424
+ let binArraysForSwap = /* @__PURE__ */ new Map();
7425
+ let actualOutAmount = new (0, _anchor.BN)(0);
7426
+ let feeAmount = new (0, _anchor.BN)(0);
7427
+ let protocolFeeAmount = new (0, _anchor.BN)(0);
7428
+ while (!inAmountLeft.isZero()) {
7429
+ let binArrayAccountToSwap = findNextBinArrayWithLiquidity(
7430
+ swapForY,
7431
+ activeId,
7432
+ this.lbPair,
7433
+ _optionalChain([this, 'access', _53 => _53.binArrayBitmapExtension, 'optionalAccess', _54 => _54.account]),
7434
+ binArrays
7435
+ );
7436
+ if (binArrayAccountToSwap == null) {
7437
+ throw new Error("Insufficient liquidity");
7438
+ }
7439
+ binArraysForSwap.set(binArrayAccountToSwap.publicKey, true);
7440
+ this.updateVolatilityAccumulator(
7441
+ vParameterClone,
7442
+ sParameters2,
7443
+ activeId.toNumber()
7444
+ );
7445
+ if (isBinIdWithinBinArray(activeId, binArrayAccountToSwap.account.index)) {
7446
+ const bin = getBinFromBinArray(
7447
+ activeId.toNumber(),
7448
+ binArrayAccountToSwap.account
7449
+ );
7450
+ const { isReachCap, amountIn, amountOut, fee, protocolFee } = swapQuoteAtBinWithCap(
7451
+ bin,
7452
+ binStep,
7453
+ sParameters2,
7454
+ vParameterClone,
7455
+ inAmountLeft,
7456
+ swapForY,
7457
+ maxSwappedAmount.sub(actualOutAmount)
7458
+ );
7459
+ if (!amountIn.isZero()) {
7460
+ inAmountLeft = inAmountLeft.sub(amountIn);
7461
+ actualOutAmount = actualOutAmount.add(amountOut);
7462
+ feeAmount = feeAmount.add(fee);
7463
+ protocolFeeAmount = protocolFee.add(protocolFee);
7464
+ if (!startBin) {
7465
+ startBin = bin;
7466
+ }
7467
+ }
7468
+ if (isReachCap) {
7469
+ break;
7470
+ }
7471
+ }
7472
+ if (!inAmountLeft.isZero()) {
7473
+ if (swapForY) {
7474
+ activeId = activeId.sub(new (0, _anchor.BN)(1));
7475
+ } else {
7476
+ activeId = activeId.add(new (0, _anchor.BN)(1));
7477
+ }
7478
+ }
7479
+ }
7480
+ if (!startBin)
7481
+ throw new Error("Invalid start bin");
7482
+ inAmount = inAmount.sub(inAmountLeft);
7483
+ const outAmountWithoutSlippage = getOutAmount(
7484
+ startBin,
7485
+ inAmount.sub(
7486
+ computeFeeFromAmount(binStep, sParameters2, vParameterClone, inAmount)
7487
+ ),
7488
+ swapForY
7489
+ );
7490
+ const priceImpact = new (0, _decimaljs2.default)(actualOutAmount.toString()).sub(new (0, _decimaljs2.default)(outAmountWithoutSlippage.toString())).div(new (0, _decimaljs2.default)(outAmountWithoutSlippage.toString())).mul(new (0, _decimaljs2.default)(100));
7491
+ const minOutAmount = actualOutAmount.mul(new (0, _anchor.BN)(BASIS_POINT_MAX).sub(allowedSlippage)).div(new (0, _anchor.BN)(BASIS_POINT_MAX));
7492
+ return {
7493
+ consumedInAmount: inAmount,
7494
+ outAmount: actualOutAmount,
7495
+ fee: feeAmount,
7496
+ protocolFee: protocolFeeAmount,
7497
+ minOutAmount,
7498
+ priceImpact,
7499
+ binArraysPubkey: [...binArraysForSwap.keys()]
7500
+ };
7501
+ }
7295
7502
  /**
7296
7503
  * The `swapQuote` function returns a quote for a swap
7297
7504
  * @param
@@ -7299,6 +7506,7 @@ var DLMM = class {
7299
7506
  * - `swapForY`: Swap token X to Y when it is true, else reversed.
7300
7507
  * - `allowedSlipage`: Allowed slippage for the swap. Expressed in BPS. To convert from slippage percentage to BPS unit: SLIPPAGE_PERCENTAGE * 100
7301
7508
  * @returns {SwapQuote}
7509
+ * - `consumedInAmount`: Amount of lamport to swap in
7302
7510
  * - `outAmount`: Amount of lamport to swap out
7303
7511
  * - `fee`: Fee amount
7304
7512
  * - `protocolFee`: Protocol fee amount
@@ -7329,7 +7537,7 @@ var DLMM = class {
7329
7537
  swapForY,
7330
7538
  activeId,
7331
7539
  this.lbPair,
7332
- _optionalChain([this, 'access', _53 => _53.binArrayBitmapExtension, 'optionalAccess', _54 => _54.account]),
7540
+ _optionalChain([this, 'access', _55 => _55.binArrayBitmapExtension, 'optionalAccess', _56 => _56.account]),
7333
7541
  binArrays
7334
7542
  );
7335
7543
  if (binArrayAccountToSwap == null) {
@@ -7384,6 +7592,7 @@ var DLMM = class {
7384
7592
  const priceImpact = new (0, _decimaljs2.default)(actualOutAmount.toString()).sub(new (0, _decimaljs2.default)(outAmountWithoutSlippage.toString())).div(new (0, _decimaljs2.default)(outAmountWithoutSlippage.toString())).mul(new (0, _decimaljs2.default)(100));
7385
7593
  const minOutAmount = actualOutAmount.mul(new (0, _anchor.BN)(BASIS_POINT_MAX).sub(allowedSlippage)).div(new (0, _anchor.BN)(BASIS_POINT_MAX));
7386
7594
  return {
7595
+ consumedInAmount: inAmount,
7387
7596
  outAmount: actualOutAmount,
7388
7597
  fee: feeAmount,
7389
7598
  protocolFee: protocolFeeAmount,
@@ -7801,13 +8010,13 @@ var DLMM = class {
7801
8010
  }
7802
8011
  return { feeX, feeY };
7803
8012
  }
7804
- static async processPosition(program, version, lbPair, onChainTimestamp, positionAccount, baseTokenDecimal, quoteTokenDecimal, lowerBinArray, upperBinArray) {
8013
+ static async processPosition(program, version, lbPair, onChainTimestamp, position, baseTokenDecimal, quoteTokenDecimal, lowerBinArray, upperBinArray) {
7805
8014
  const {
7806
8015
  lowerBinId,
7807
8016
  upperBinId,
7808
8017
  liquidityShares: posShares,
7809
8018
  lastUpdatedAt
7810
- } = positionAccount;
8019
+ } = position;
7811
8020
  const bins = this.getBinsBetweenLowerAndUpperBound(
7812
8021
  lbPair,
7813
8022
  lowerBinId,
@@ -7851,7 +8060,7 @@ var DLMM = class {
7851
8060
  const { feeX, feeY } = await this.getClaimableSwapFee(
7852
8061
  program,
7853
8062
  version,
7854
- positionAccount,
8063
+ position,
7855
8064
  lowerBinArray,
7856
8065
  upperBinArray
7857
8066
  );
@@ -7860,7 +8069,7 @@ var DLMM = class {
7860
8069
  version,
7861
8070
  lbPair,
7862
8071
  onChainTimestamp,
7863
- positionAccount,
8072
+ position,
7864
8073
  lowerBinArray,
7865
8074
  upperBinArray
7866
8075
  );
@@ -8206,7 +8415,7 @@ var DLMMError = class extends Error {
8206
8415
  const anchorError = _anchor.AnchorError.parse(
8207
8416
  JSON.parse(JSON.stringify(error)).logs
8208
8417
  );
8209
- if (_optionalChain([anchorError, 'optionalAccess', _55 => _55.program, 'access', _56 => _56.toBase58, 'call', _57 => _57()]) === LBCLMM_PROGRAM_IDS["mainnet-beta"]) {
8418
+ if (_optionalChain([anchorError, 'optionalAccess', _57 => _57.program, 'access', _58 => _58.toBase58, 'call', _59 => _59()]) === LBCLMM_PROGRAM_IDS["mainnet-beta"]) {
8210
8419
  _errorCode = anchorError.error.errorCode.number;
8211
8420
  _errorName = anchorError.error.errorCode.code;
8212
8421
  _errorMessage = anchorError.error.errorMessage;
@@ -8294,5 +8503,6 @@ var src_default = DLMM;
8294
8503
 
8295
8504
 
8296
8505
 
8297
- exports.ADMIN = ADMIN; exports.BASIS_POINT_MAX = BASIS_POINT_MAX; exports.BIN_ARRAY_BITMAP_SIZE = BIN_ARRAY_BITMAP_SIZE; exports.BitmapType = BitmapType; exports.DLMMError = DLMMError; exports.EXTENSION_BINARRAY_BITMAP_SIZE = EXTENSION_BINARRAY_BITMAP_SIZE; exports.FEE_PRECISION = FEE_PRECISION; exports.IDL = IDL; exports.LBCLMM_PROGRAM_IDS = LBCLMM_PROGRAM_IDS; exports.MAX_ACTIVE_BIN_SLIPPAGE = MAX_ACTIVE_BIN_SLIPPAGE; exports.MAX_BIN_ARRAY_SIZE = MAX_BIN_ARRAY_SIZE; exports.MAX_BIN_LENGTH_ALLOWED_IN_ONE_TX = MAX_BIN_LENGTH_ALLOWED_IN_ONE_TX; exports.MAX_BIN_PER_POSITION = MAX_BIN_PER_POSITION; exports.MAX_CLAIM_ALL_ALLOWED = MAX_CLAIM_ALL_ALLOWED; exports.MAX_FEE_RATE = MAX_FEE_RATE; exports.Network = Network; exports.PRECISION = PRECISION; exports.PositionVersion = PositionVersion; exports.SCALE = SCALE; exports.SCALE_OFFSET = SCALE_OFFSET; exports.SIMULATION_USER = SIMULATION_USER; exports.StrategyType = StrategyType; exports.binIdToBinArrayIndex = binIdToBinArrayIndex; exports.calculateBidAskDistribution = calculateBidAskDistribution; exports.calculateNormalDistribution = calculateNormalDistribution; exports.calculateSpotDistribution = calculateSpotDistribution; exports.chunkedFetchMultipleBinArrayBitmapExtensionAccount = chunkedFetchMultipleBinArrayBitmapExtensionAccount; exports.chunkedFetchMultiplePoolAccount = chunkedFetchMultiplePoolAccount; exports.chunkedGetMultipleAccountInfos = chunkedGetMultipleAccountInfos; exports.chunks = chunks; exports.computeBudgetIx = computeBudgetIx; exports.computeFee = computeFee; exports.computeFeeFromAmount = computeFeeFromAmount; exports.computeProtocolFee = computeProtocolFee; exports.default = src_default; exports.deriveBinArray = deriveBinArray; exports.deriveBinArrayBitmapExtension = deriveBinArrayBitmapExtension; exports.deriveLbPair = deriveLbPair; exports.deriveOracle = deriveOracle; exports.derivePosition = derivePosition; exports.derivePresetParameter = derivePresetParameter; exports.deriveReserve = deriveReserve; exports.findNextBinArrayIndexWithLiquidity = findNextBinArrayIndexWithLiquidity; exports.findNextBinArrayWithLiquidity = findNextBinArrayWithLiquidity; exports.fromStrategyParamstoWeightDistribution = fromStrategyParamstoWeightDistribution; exports.getBaseFee = getBaseFee; exports.getBinArrayLowerUpperBinId = getBinArrayLowerUpperBinId; exports.getBinFromBinArray = getBinFromBinArray; exports.getOrCreateATAInstruction = getOrCreateATAInstruction; exports.getOutAmount = getOutAmount; exports.getPriceOfBinByBinId = getPriceOfBinByBinId; exports.getTokenBalance = getTokenBalance; exports.getTokenDecimals = getTokenDecimals; exports.getTotalFee = getTotalFee; exports.getVariableFee = getVariableFee; exports.isBinIdWithinBinArray = isBinIdWithinBinArray; exports.isOverflowDefaultBinArrayBitmap = isOverflowDefaultBinArrayBitmap; exports.parabolicParameter = parabolicParameter; exports.parseLogs = parseLogs; exports.spotParameter = spotParameter; exports.swapQuoteAtBin = swapQuoteAtBin; exports.toStrategyParameters = toStrategyParameters; exports.toWeightDistribution = toWeightDistribution; exports.unwrapSOLInstruction = unwrapSOLInstruction; exports.wrapSOLInstruction = wrapSOLInstruction;
8506
+
8507
+ exports.ADMIN = ADMIN; exports.BASIS_POINT_MAX = BASIS_POINT_MAX; exports.BIN_ARRAY_BITMAP_SIZE = BIN_ARRAY_BITMAP_SIZE; exports.BitmapType = BitmapType; exports.DLMMError = DLMMError; exports.EXTENSION_BINARRAY_BITMAP_SIZE = EXTENSION_BINARRAY_BITMAP_SIZE; exports.FEE_PRECISION = FEE_PRECISION; exports.IDL = IDL; exports.LBCLMM_PROGRAM_IDS = LBCLMM_PROGRAM_IDS; exports.MAX_ACTIVE_BIN_SLIPPAGE = MAX_ACTIVE_BIN_SLIPPAGE; exports.MAX_BIN_ARRAY_SIZE = MAX_BIN_ARRAY_SIZE; exports.MAX_BIN_LENGTH_ALLOWED_IN_ONE_TX = MAX_BIN_LENGTH_ALLOWED_IN_ONE_TX; exports.MAX_BIN_PER_POSITION = MAX_BIN_PER_POSITION; exports.MAX_CLAIM_ALL_ALLOWED = MAX_CLAIM_ALL_ALLOWED; exports.MAX_FEE_RATE = MAX_FEE_RATE; exports.Network = Network; exports.PRECISION = PRECISION; exports.PositionVersion = PositionVersion; exports.SCALE = SCALE; exports.SCALE_OFFSET = SCALE_OFFSET; exports.SIMULATION_USER = SIMULATION_USER; exports.StrategyType = StrategyType; exports.binIdToBinArrayIndex = binIdToBinArrayIndex; exports.calculateBidAskDistribution = calculateBidAskDistribution; exports.calculateNormalDistribution = calculateNormalDistribution; exports.calculateSpotDistribution = calculateSpotDistribution; exports.chunkedFetchMultipleBinArrayBitmapExtensionAccount = chunkedFetchMultipleBinArrayBitmapExtensionAccount; exports.chunkedFetchMultiplePoolAccount = chunkedFetchMultiplePoolAccount; exports.chunkedGetMultipleAccountInfos = chunkedGetMultipleAccountInfos; exports.chunks = chunks; exports.computeBudgetIx = computeBudgetIx; exports.computeFee = computeFee; exports.computeFeeFromAmount = computeFeeFromAmount; exports.computeProtocolFee = computeProtocolFee; exports.default = src_default; exports.deriveBinArray = deriveBinArray; exports.deriveBinArrayBitmapExtension = deriveBinArrayBitmapExtension; exports.deriveLbPair = deriveLbPair; exports.deriveOracle = deriveOracle; exports.derivePosition = derivePosition; exports.derivePresetParameter = derivePresetParameter; exports.deriveReserve = deriveReserve; exports.findNextBinArrayIndexWithLiquidity = findNextBinArrayIndexWithLiquidity; exports.findNextBinArrayWithLiquidity = findNextBinArrayWithLiquidity; exports.fromStrategyParamstoWeightDistribution = fromStrategyParamstoWeightDistribution; exports.getBaseFee = getBaseFee; exports.getBinArrayLowerUpperBinId = getBinArrayLowerUpperBinId; exports.getBinFromBinArray = getBinFromBinArray; exports.getOrCreateATAInstruction = getOrCreateATAInstruction; exports.getOutAmount = getOutAmount; exports.getPriceOfBinByBinId = getPriceOfBinByBinId; exports.getTokenBalance = getTokenBalance; exports.getTokenDecimals = getTokenDecimals; exports.getTotalFee = getTotalFee; exports.getVariableFee = getVariableFee; exports.isBinIdWithinBinArray = isBinIdWithinBinArray; exports.isOverflowDefaultBinArrayBitmap = isOverflowDefaultBinArrayBitmap; exports.parabolicParameter = parabolicParameter; exports.parseLogs = parseLogs; exports.spotParameter = spotParameter; exports.swapQuoteAtBin = swapQuoteAtBin; exports.swapQuoteAtBinWithCap = swapQuoteAtBinWithCap; exports.toStrategyParameters = toStrategyParameters; exports.toWeightDistribution = toWeightDistribution; exports.unwrapSOLInstruction = unwrapSOLInstruction; exports.wrapSOLInstruction = wrapSOLInstruction;
8298
8508
  //# sourceMappingURL=index.js.map