@meteora-ag/dlmm 1.0.50 → 1.0.52-rc.10
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 +113 -22
- package/dist/index.js +344 -50
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +343 -49
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -2048,6 +2048,73 @@ var IDL = {
|
|
|
2048
2048
|
],
|
|
2049
2049
|
"args": []
|
|
2050
2050
|
},
|
|
2051
|
+
{
|
|
2052
|
+
"name": "removeLiquiditySingleSide",
|
|
2053
|
+
"accounts": [
|
|
2054
|
+
{
|
|
2055
|
+
"name": "position",
|
|
2056
|
+
"isMut": true,
|
|
2057
|
+
"isSigner": false
|
|
2058
|
+
},
|
|
2059
|
+
{
|
|
2060
|
+
"name": "lbPair",
|
|
2061
|
+
"isMut": true,
|
|
2062
|
+
"isSigner": false
|
|
2063
|
+
},
|
|
2064
|
+
{
|
|
2065
|
+
"name": "binArrayBitmapExtension",
|
|
2066
|
+
"isMut": true,
|
|
2067
|
+
"isSigner": false,
|
|
2068
|
+
"isOptional": true
|
|
2069
|
+
},
|
|
2070
|
+
{
|
|
2071
|
+
"name": "userToken",
|
|
2072
|
+
"isMut": true,
|
|
2073
|
+
"isSigner": false
|
|
2074
|
+
},
|
|
2075
|
+
{
|
|
2076
|
+
"name": "reserve",
|
|
2077
|
+
"isMut": true,
|
|
2078
|
+
"isSigner": false
|
|
2079
|
+
},
|
|
2080
|
+
{
|
|
2081
|
+
"name": "tokenMint",
|
|
2082
|
+
"isMut": false,
|
|
2083
|
+
"isSigner": false
|
|
2084
|
+
},
|
|
2085
|
+
{
|
|
2086
|
+
"name": "binArrayLower",
|
|
2087
|
+
"isMut": true,
|
|
2088
|
+
"isSigner": false
|
|
2089
|
+
},
|
|
2090
|
+
{
|
|
2091
|
+
"name": "binArrayUpper",
|
|
2092
|
+
"isMut": true,
|
|
2093
|
+
"isSigner": false
|
|
2094
|
+
},
|
|
2095
|
+
{
|
|
2096
|
+
"name": "sender",
|
|
2097
|
+
"isMut": false,
|
|
2098
|
+
"isSigner": true
|
|
2099
|
+
},
|
|
2100
|
+
{
|
|
2101
|
+
"name": "tokenProgram",
|
|
2102
|
+
"isMut": false,
|
|
2103
|
+
"isSigner": false
|
|
2104
|
+
},
|
|
2105
|
+
{
|
|
2106
|
+
"name": "eventAuthority",
|
|
2107
|
+
"isMut": false,
|
|
2108
|
+
"isSigner": false
|
|
2109
|
+
},
|
|
2110
|
+
{
|
|
2111
|
+
"name": "program",
|
|
2112
|
+
"isMut": false,
|
|
2113
|
+
"isSigner": false
|
|
2114
|
+
}
|
|
2115
|
+
],
|
|
2116
|
+
"args": []
|
|
2117
|
+
},
|
|
2051
2118
|
{
|
|
2052
2119
|
"name": "togglePairStatus",
|
|
2053
2120
|
"accounts": [
|
|
@@ -3724,38 +3791,38 @@ var IDL = {
|
|
|
3724
3791
|
}
|
|
3725
3792
|
},
|
|
3726
3793
|
{
|
|
3727
|
-
"name": "
|
|
3794
|
+
"name": "FeeParameter",
|
|
3728
3795
|
"type": {
|
|
3729
3796
|
"kind": "struct",
|
|
3730
3797
|
"fields": [
|
|
3731
3798
|
{
|
|
3732
|
-
"name": "
|
|
3733
|
-
"
|
|
3799
|
+
"name": "protocolShare",
|
|
3800
|
+
"docs": [
|
|
3801
|
+
"Portion of swap fees retained by the protocol by controlling protocol_share parameter. protocol_swap_fee = protocol_share * total_swap_fee"
|
|
3802
|
+
],
|
|
3803
|
+
"type": "u16"
|
|
3734
3804
|
},
|
|
3735
3805
|
{
|
|
3736
|
-
"name": "
|
|
3806
|
+
"name": "baseFactor",
|
|
3807
|
+
"docs": [
|
|
3808
|
+
"Base factor for base fee rate"
|
|
3809
|
+
],
|
|
3737
3810
|
"type": "u16"
|
|
3738
3811
|
}
|
|
3739
3812
|
]
|
|
3740
3813
|
}
|
|
3741
3814
|
},
|
|
3742
3815
|
{
|
|
3743
|
-
"name": "
|
|
3816
|
+
"name": "BinLiquidityReduction",
|
|
3744
3817
|
"type": {
|
|
3745
3818
|
"kind": "struct",
|
|
3746
3819
|
"fields": [
|
|
3747
3820
|
{
|
|
3748
|
-
"name": "
|
|
3749
|
-
"
|
|
3750
|
-
"Portion of swap fees retained by the protocol by controlling protocol_share parameter. protocol_swap_fee = protocol_share * total_swap_fee"
|
|
3751
|
-
],
|
|
3752
|
-
"type": "u16"
|
|
3821
|
+
"name": "binId",
|
|
3822
|
+
"type": "i32"
|
|
3753
3823
|
},
|
|
3754
3824
|
{
|
|
3755
|
-
"name": "
|
|
3756
|
-
"docs": [
|
|
3757
|
-
"Base factor for base fee rate"
|
|
3758
|
-
],
|
|
3825
|
+
"name": "bpsToRemove",
|
|
3759
3826
|
"type": "u16"
|
|
3760
3827
|
}
|
|
3761
3828
|
]
|
|
@@ -6475,6 +6542,24 @@ function findNextBinArrayWithLiquidity(swapForY, activeBinId, lbPairState, binAr
|
|
|
6475
6542
|
}
|
|
6476
6543
|
return binArrayAccount;
|
|
6477
6544
|
}
|
|
6545
|
+
function getBinArraysRequiredByPositionRange(pair, fromBinId, toBinId, programId) {
|
|
6546
|
+
const [minBinId, maxBinId] = fromBinId.lt(toBinId) ? [fromBinId, toBinId] : [toBinId, fromBinId];
|
|
6547
|
+
const positionCount = getPositionCount(minBinId, maxBinId);
|
|
6548
|
+
const binArrays = /* @__PURE__ */ new Map();
|
|
6549
|
+
for (let i = 0; i < positionCount.toNumber(); i++) {
|
|
6550
|
+
const lowerBinId = minBinId.add(MAX_BIN_PER_POSITION.mul(new BN6(i)));
|
|
6551
|
+
const lowerBinArrayIndex = binIdToBinArrayIndex(lowerBinId);
|
|
6552
|
+
const upperBinArrayIndex = lowerBinArrayIndex.add(new BN6(1));
|
|
6553
|
+
const [lowerBinArray] = deriveBinArray(pair, lowerBinArrayIndex, programId);
|
|
6554
|
+
const [upperBinArray] = deriveBinArray(pair, upperBinArrayIndex, programId);
|
|
6555
|
+
binArrays.set(lowerBinArray.toBase58(), lowerBinArrayIndex);
|
|
6556
|
+
binArrays.set(upperBinArray.toBase58(), upperBinArrayIndex);
|
|
6557
|
+
}
|
|
6558
|
+
return Array.from(binArrays, ([key, index]) => ({
|
|
6559
|
+
key: new PublicKey3(key),
|
|
6560
|
+
index
|
|
6561
|
+
}));
|
|
6562
|
+
}
|
|
6478
6563
|
|
|
6479
6564
|
// src/dlmm/helpers/fee.ts
|
|
6480
6565
|
import { BN as BN7 } from "@coral-xyz/anchor";
|
|
@@ -8485,17 +8570,6 @@ var DLMM = class {
|
|
|
8485
8570
|
);
|
|
8486
8571
|
return activeBinState;
|
|
8487
8572
|
}
|
|
8488
|
-
/**
|
|
8489
|
-
* The function get the price of a bin based on its bin ID.
|
|
8490
|
-
* @param {number} binId - The `binId` parameter is a number that represents the ID of a bin.
|
|
8491
|
-
* @returns {number} the calculated price of a bin based on the provided binId.
|
|
8492
|
-
*/
|
|
8493
|
-
getPriceOfBinByBinId(binId) {
|
|
8494
|
-
const binStepNum = new Decimal4(this.lbPair.binStep).div(
|
|
8495
|
-
new Decimal4(BASIS_POINT_MAX)
|
|
8496
|
-
);
|
|
8497
|
-
return new Decimal4(1).add(new Decimal4(binStepNum)).pow(new Decimal4(binId)).toString();
|
|
8498
|
-
}
|
|
8499
8573
|
/**
|
|
8500
8574
|
* The function get bin ID based on a given price and a boolean flag indicating whether to
|
|
8501
8575
|
* round down or up.
|
|
@@ -9586,6 +9660,76 @@ var DLMM = class {
|
|
|
9586
9660
|
}).add(removeLiquidityTx);
|
|
9587
9661
|
}
|
|
9588
9662
|
}
|
|
9663
|
+
async removeLiquiditySingleSide({
|
|
9664
|
+
user,
|
|
9665
|
+
position,
|
|
9666
|
+
binIds,
|
|
9667
|
+
removeLiquidityForY = false
|
|
9668
|
+
}) {
|
|
9669
|
+
const { lbPair, lowerBinId, owner, feeOwner } = await this.program.account.positionV2.fetch(position);
|
|
9670
|
+
const { reserveX, reserveY, tokenXMint, tokenYMint } = await this.program.account.lbPair.fetch(lbPair);
|
|
9671
|
+
const lowerBinArrayIndex = binIdToBinArrayIndex(new BN9(lowerBinId));
|
|
9672
|
+
const upperBinArrayIndex = lowerBinArrayIndex.add(new BN9(1));
|
|
9673
|
+
const [binArrayLower] = deriveBinArray(
|
|
9674
|
+
lbPair,
|
|
9675
|
+
lowerBinArrayIndex,
|
|
9676
|
+
this.program.programId
|
|
9677
|
+
);
|
|
9678
|
+
const [binArrayUpper] = deriveBinArray(
|
|
9679
|
+
lbPair,
|
|
9680
|
+
upperBinArrayIndex,
|
|
9681
|
+
this.program.programId
|
|
9682
|
+
);
|
|
9683
|
+
const preInstructions = [];
|
|
9684
|
+
const setComputeUnitLimitIx = computeBudgetIx();
|
|
9685
|
+
preInstructions.push(setComputeUnitLimitIx);
|
|
9686
|
+
const { ataPubKey: userToken, ix: createPayerTokenIx } = removeLiquidityForY ? await getOrCreateATAInstruction(
|
|
9687
|
+
this.program.provider.connection,
|
|
9688
|
+
this.tokenY.publicKey,
|
|
9689
|
+
owner,
|
|
9690
|
+
user
|
|
9691
|
+
) : await getOrCreateATAInstruction(
|
|
9692
|
+
this.program.provider.connection,
|
|
9693
|
+
this.tokenX.publicKey,
|
|
9694
|
+
owner,
|
|
9695
|
+
user
|
|
9696
|
+
);
|
|
9697
|
+
createPayerTokenIx && preInstructions.push(createPayerTokenIx);
|
|
9698
|
+
const postInstructions = [];
|
|
9699
|
+
if ([
|
|
9700
|
+
this.tokenX.publicKey.toBase58(),
|
|
9701
|
+
this.tokenY.publicKey.toBase58()
|
|
9702
|
+
].includes(NATIVE_MINT2.toBase58())) {
|
|
9703
|
+
const closeWrappedSOLIx = await unwrapSOLInstruction(user);
|
|
9704
|
+
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
9705
|
+
}
|
|
9706
|
+
const minBinId = Math.min(...binIds);
|
|
9707
|
+
const maxBinId = Math.max(...binIds);
|
|
9708
|
+
const minBinArrayIndex = binIdToBinArrayIndex(new BN9(minBinId));
|
|
9709
|
+
const maxBinArrayIndex = binIdToBinArrayIndex(new BN9(maxBinId));
|
|
9710
|
+
const useExtension = isOverflowDefaultBinArrayBitmap(minBinArrayIndex) || isOverflowDefaultBinArrayBitmap(maxBinArrayIndex);
|
|
9711
|
+
const binArrayBitmapExtension = useExtension ? deriveBinArrayBitmapExtension(this.pubkey, this.program.programId)[0] : null;
|
|
9712
|
+
const reserve = removeLiquidityForY ? reserveY : reserveX;
|
|
9713
|
+
const tokenMint = removeLiquidityForY ? tokenYMint : tokenXMint;
|
|
9714
|
+
const removeLiquiditySingleSideTx = await this.program.methods.removeLiquiditySingleSide().accounts({
|
|
9715
|
+
position,
|
|
9716
|
+
lbPair,
|
|
9717
|
+
binArrayBitmapExtension,
|
|
9718
|
+
userToken,
|
|
9719
|
+
reserve,
|
|
9720
|
+
tokenMint,
|
|
9721
|
+
binArrayLower,
|
|
9722
|
+
binArrayUpper,
|
|
9723
|
+
sender: user,
|
|
9724
|
+
tokenProgram: TOKEN_PROGRAM_ID2
|
|
9725
|
+
}).preInstructions(preInstructions).transaction();
|
|
9726
|
+
const { blockhash, lastValidBlockHeight } = await this.program.provider.connection.getLatestBlockhash("confirmed");
|
|
9727
|
+
return new Transaction({
|
|
9728
|
+
blockhash,
|
|
9729
|
+
lastValidBlockHeight,
|
|
9730
|
+
feePayer: user
|
|
9731
|
+
}).add(removeLiquiditySingleSideTx);
|
|
9732
|
+
}
|
|
9589
9733
|
/**
|
|
9590
9734
|
* The `closePosition` function closes a position
|
|
9591
9735
|
* @param
|
|
@@ -9704,9 +9848,15 @@ var DLMM = class {
|
|
|
9704
9848
|
}
|
|
9705
9849
|
}
|
|
9706
9850
|
}
|
|
9707
|
-
const startPrice =
|
|
9708
|
-
|
|
9709
|
-
|
|
9851
|
+
const startPrice = getPriceOfBinByBinId(
|
|
9852
|
+
startBinId.toNumber(),
|
|
9853
|
+
this.lbPair.binStep
|
|
9854
|
+
);
|
|
9855
|
+
const endPrice = getPriceOfBinByBinId(
|
|
9856
|
+
activeId.toNumber(),
|
|
9857
|
+
this.lbPair.binStep
|
|
9858
|
+
);
|
|
9859
|
+
const priceImpact = startPrice.sub(endPrice).abs().div(startPrice).mul(new Decimal4(100));
|
|
9710
9860
|
const maxInAmount = actualInAmount.mul(new BN9(BASIS_POINT_MAX).add(allowedSlippage)).div(new BN9(BASIS_POINT_MAX));
|
|
9711
9861
|
return {
|
|
9712
9862
|
inAmount: actualInAmount,
|
|
@@ -9817,6 +9967,10 @@ var DLMM = class {
|
|
|
9817
9967
|
);
|
|
9818
9968
|
const priceImpact = new Decimal4(actualOutAmount.toString()).sub(new Decimal4(outAmountWithoutSlippage.toString())).div(new Decimal4(outAmountWithoutSlippage.toString())).mul(new Decimal4(100));
|
|
9819
9969
|
const minOutAmount = actualOutAmount.mul(new BN9(BASIS_POINT_MAX).sub(allowedSlippage)).div(new BN9(BASIS_POINT_MAX));
|
|
9970
|
+
const endPrice = getPriceOfBinByBinId(
|
|
9971
|
+
activeId.toNumber(),
|
|
9972
|
+
this.lbPair.binStep
|
|
9973
|
+
);
|
|
9820
9974
|
return {
|
|
9821
9975
|
consumedInAmount: inAmount,
|
|
9822
9976
|
outAmount: actualOutAmount,
|
|
@@ -9824,7 +9978,8 @@ var DLMM = class {
|
|
|
9824
9978
|
protocolFee: protocolFeeAmount,
|
|
9825
9979
|
minOutAmount,
|
|
9826
9980
|
priceImpact,
|
|
9827
|
-
binArraysPubkey: [...binArraysForSwap.keys()]
|
|
9981
|
+
binArraysPubkey: [...binArraysForSwap.keys()],
|
|
9982
|
+
endPrice
|
|
9828
9983
|
};
|
|
9829
9984
|
}
|
|
9830
9985
|
async swapExactOut({
|
|
@@ -10113,7 +10268,9 @@ var DLMM = class {
|
|
|
10113
10268
|
positions
|
|
10114
10269
|
}) {
|
|
10115
10270
|
const claimAllTxs = (await Promise.all(
|
|
10116
|
-
positions.filter(
|
|
10271
|
+
positions.filter(
|
|
10272
|
+
({ positionData: { rewardOne, rewardTwo } }) => !rewardOne.isZero() || !rewardTwo.isZero()
|
|
10273
|
+
).map(async (position, idx) => {
|
|
10117
10274
|
return await this.createClaimBuildMethod({
|
|
10118
10275
|
owner,
|
|
10119
10276
|
position,
|
|
@@ -10197,7 +10354,9 @@ var DLMM = class {
|
|
|
10197
10354
|
positions
|
|
10198
10355
|
}) {
|
|
10199
10356
|
const claimAllTxs = (await Promise.all(
|
|
10200
|
-
positions.filter(
|
|
10357
|
+
positions.filter(
|
|
10358
|
+
({ positionData: { feeX, feeY } }) => !feeX.isZero() || !feeY.isZero()
|
|
10359
|
+
).map(async (position, idx, positions2) => {
|
|
10201
10360
|
return await this.createClaimSwapFeeMethod({
|
|
10202
10361
|
owner,
|
|
10203
10362
|
position,
|
|
@@ -10492,6 +10651,34 @@ var DLMM = class {
|
|
|
10492
10651
|
addLiquidityIxs
|
|
10493
10652
|
};
|
|
10494
10653
|
}
|
|
10654
|
+
/**
|
|
10655
|
+
* Initializes bin arrays for the given bin array indexes if it wasn't initialized.
|
|
10656
|
+
*
|
|
10657
|
+
* @param {BN[]} binArrayIndexes - An array of bin array indexes to initialize.
|
|
10658
|
+
* @param {PublicKey} funder - The public key of the funder.
|
|
10659
|
+
* @return {Promise<TransactionInstruction[]>} An array of transaction instructions to initialize the bin arrays.
|
|
10660
|
+
*/
|
|
10661
|
+
async initializeBinArrays(binArrayIndexes, funder) {
|
|
10662
|
+
const ixs = [];
|
|
10663
|
+
for (const idx of binArrayIndexes) {
|
|
10664
|
+
const [binArray] = deriveBinArray(
|
|
10665
|
+
this.pubkey,
|
|
10666
|
+
idx,
|
|
10667
|
+
this.program.programId
|
|
10668
|
+
);
|
|
10669
|
+
const binArrayAccount = await this.program.provider.connection.getAccountInfo(binArray);
|
|
10670
|
+
if (binArrayAccount === null) {
|
|
10671
|
+
ixs.push(
|
|
10672
|
+
await this.program.methods.initializeBinArray(idx).accounts({
|
|
10673
|
+
binArray,
|
|
10674
|
+
funder,
|
|
10675
|
+
lbPair: this.pubkey
|
|
10676
|
+
}).instruction()
|
|
10677
|
+
);
|
|
10678
|
+
}
|
|
10679
|
+
}
|
|
10680
|
+
return ixs;
|
|
10681
|
+
}
|
|
10495
10682
|
/**
|
|
10496
10683
|
*
|
|
10497
10684
|
* @param
|
|
@@ -10591,7 +10778,9 @@ var DLMM = class {
|
|
|
10591
10778
|
);
|
|
10592
10779
|
createATAAccAndIx.forEach(({ ix }) => ix && preInstructions.push(ix));
|
|
10593
10780
|
const claimAllSwapFeeTxs = (await Promise.all(
|
|
10594
|
-
positions.filter(
|
|
10781
|
+
positions.filter(
|
|
10782
|
+
({ positionData: { feeX, feeY } }) => !feeX.isZero() || !feeY.isZero()
|
|
10783
|
+
).map(async (position) => {
|
|
10595
10784
|
return await this.createClaimSwapFeeMethod({
|
|
10596
10785
|
owner,
|
|
10597
10786
|
position,
|
|
@@ -10601,7 +10790,9 @@ var DLMM = class {
|
|
|
10601
10790
|
})
|
|
10602
10791
|
)).flat();
|
|
10603
10792
|
const claimAllLMTxs = (await Promise.all(
|
|
10604
|
-
positions.filter(
|
|
10793
|
+
positions.filter(
|
|
10794
|
+
({ positionData: { rewardOne, rewardTwo } }) => !rewardOne.isZero() || !rewardTwo.isZero()
|
|
10795
|
+
).map(async (position) => {
|
|
10605
10796
|
return await this.createClaimBuildMethod({
|
|
10606
10797
|
owner,
|
|
10607
10798
|
position,
|
|
@@ -10769,6 +10960,108 @@ var DLMM = class {
|
|
|
10769
10960
|
Number(binPriceWithLastLiquidity) / (2 ** 64 - 1)
|
|
10770
10961
|
);
|
|
10771
10962
|
}
|
|
10963
|
+
getAmountOutWithdrawSingleSide(maxLiquidityShare, price, bin, isWithdrawForY) {
|
|
10964
|
+
const amountX = mulDiv(
|
|
10965
|
+
maxLiquidityShare,
|
|
10966
|
+
bin.amountX,
|
|
10967
|
+
bin.liquiditySupply,
|
|
10968
|
+
1 /* Down */
|
|
10969
|
+
);
|
|
10970
|
+
const amountY = mulDiv(
|
|
10971
|
+
maxLiquidityShare,
|
|
10972
|
+
bin.amountY,
|
|
10973
|
+
bin.liquiditySupply,
|
|
10974
|
+
1 /* Down */
|
|
10975
|
+
);
|
|
10976
|
+
const amount0 = isWithdrawForY ? amountX : amountY;
|
|
10977
|
+
const amount1 = isWithdrawForY ? amountY : amountX;
|
|
10978
|
+
const remainAmountX = bin.amountX.sub(amountX);
|
|
10979
|
+
const remainAmountY = bin.amountY.sub(amountY);
|
|
10980
|
+
if (amount0.eq(new BN9(0))) {
|
|
10981
|
+
return {
|
|
10982
|
+
withdrawAmount: amount1
|
|
10983
|
+
};
|
|
10984
|
+
}
|
|
10985
|
+
let maxAmountOut = isWithdrawForY ? remainAmountY : remainAmountX;
|
|
10986
|
+
let maxAmountIn = isWithdrawForY ? shlDiv(remainAmountY, price, SCALE_OFFSET, 0 /* Up */) : mulShr(remainAmountX, price, SCALE_OFFSET, 0 /* Up */);
|
|
10987
|
+
let maxFee = computeFee(
|
|
10988
|
+
this.lbPair.binStep,
|
|
10989
|
+
this.lbPair.parameters,
|
|
10990
|
+
this.lbPair.vParameters,
|
|
10991
|
+
maxAmountIn
|
|
10992
|
+
);
|
|
10993
|
+
maxAmountIn = maxAmountIn.add(maxFee);
|
|
10994
|
+
if (amount0.gt(maxAmountIn)) {
|
|
10995
|
+
return {
|
|
10996
|
+
withdrawAmount: maxAmountOut
|
|
10997
|
+
};
|
|
10998
|
+
}
|
|
10999
|
+
const fee = computeFeeFromAmount(
|
|
11000
|
+
this.lbPair.binStep,
|
|
11001
|
+
this.lbPair.parameters,
|
|
11002
|
+
this.lbPair.vParameters,
|
|
11003
|
+
amount0
|
|
11004
|
+
);
|
|
11005
|
+
const amount0AfterFee = amount0.sub(fee);
|
|
11006
|
+
const amountOut = isWithdrawForY ? mulShr(price, amount0AfterFee, SCALE_OFFSET, 1 /* Down */) : shlDiv(amount0AfterFee, price, SCALE_OFFSET, 1 /* Down */);
|
|
11007
|
+
return {
|
|
11008
|
+
withdrawAmount: amount1.add(amountOut.gt(maxAmountOut) ? maxAmountOut : amountOut)
|
|
11009
|
+
};
|
|
11010
|
+
}
|
|
11011
|
+
async getWithdrawSingleSideAmount(positionPubkey, isWithdrawForY) {
|
|
11012
|
+
let totalWithdrawAmount = new BN9(0);
|
|
11013
|
+
let lowerBinArray;
|
|
11014
|
+
let upperBinArray;
|
|
11015
|
+
const position = await this.program.account.positionV2.fetch(
|
|
11016
|
+
positionPubkey
|
|
11017
|
+
);
|
|
11018
|
+
const lowerBinArrayIdx = binIdToBinArrayIndex(new BN9(position.lowerBinId));
|
|
11019
|
+
const [lowerBinArrayPubKey] = deriveBinArray(
|
|
11020
|
+
position.lbPair,
|
|
11021
|
+
lowerBinArrayIdx,
|
|
11022
|
+
this.program.programId
|
|
11023
|
+
);
|
|
11024
|
+
const upperBinArrayIdx = lowerBinArrayIdx.add(new BN9(1));
|
|
11025
|
+
const [upperBinArrayPubKey] = deriveBinArray(
|
|
11026
|
+
position.lbPair,
|
|
11027
|
+
upperBinArrayIdx,
|
|
11028
|
+
this.program.programId
|
|
11029
|
+
);
|
|
11030
|
+
[lowerBinArray, upperBinArray] = await this.program.account.binArray.fetchMultiple([
|
|
11031
|
+
lowerBinArrayPubKey,
|
|
11032
|
+
upperBinArrayPubKey
|
|
11033
|
+
]);
|
|
11034
|
+
if (!lowerBinArray || !upperBinArray)
|
|
11035
|
+
throw new Error("BinArray not found");
|
|
11036
|
+
for (let idx = 0; idx < position.liquidityShares.length; idx++) {
|
|
11037
|
+
const shareToRemove = position.liquidityShares[idx];
|
|
11038
|
+
if (shareToRemove.eq(new BN9(0))) {
|
|
11039
|
+
continue;
|
|
11040
|
+
}
|
|
11041
|
+
const binId = new BN9(position.lowerBinId).add(new BN9(idx));
|
|
11042
|
+
const binArrayIndex = binIdToBinArrayIndex(binId);
|
|
11043
|
+
const binArray = binArrayIndex.eq(lowerBinArrayIdx) ? lowerBinArray : upperBinArray;
|
|
11044
|
+
const bin = getBinFromBinArray(binId.toNumber(), binArray);
|
|
11045
|
+
if (isWithdrawForY) {
|
|
11046
|
+
if (binId.gt(new BN9(this.lbPair.activeId))) {
|
|
11047
|
+
break;
|
|
11048
|
+
}
|
|
11049
|
+
} else {
|
|
11050
|
+
if (binId.lt(new BN9(this.lbPair.activeId))) {
|
|
11051
|
+
continue;
|
|
11052
|
+
}
|
|
11053
|
+
}
|
|
11054
|
+
const price = getQPriceFromId(binId, new BN9(this.lbPair.binStep));
|
|
11055
|
+
const { withdrawAmount } = this.getAmountOutWithdrawSingleSide(
|
|
11056
|
+
shareToRemove,
|
|
11057
|
+
price,
|
|
11058
|
+
bin,
|
|
11059
|
+
isWithdrawForY
|
|
11060
|
+
);
|
|
11061
|
+
totalWithdrawAmount = totalWithdrawAmount.add(withdrawAmount);
|
|
11062
|
+
}
|
|
11063
|
+
return totalWithdrawAmount;
|
|
11064
|
+
}
|
|
10772
11065
|
/** Private static method */
|
|
10773
11066
|
static async getBinArrays(program, lbPairPubkey) {
|
|
10774
11067
|
return program.account.binArray.all([
|
|
@@ -10995,10 +11288,10 @@ var DLMM = class {
|
|
|
10995
11288
|
binArray.bins.forEach((bin, idx) => {
|
|
10996
11289
|
const binId = lowerBinIdForBinArray.toNumber() + idx;
|
|
10997
11290
|
if (binId >= lowerBinId && binId <= upperBinId) {
|
|
10998
|
-
const pricePerLamport =
|
|
10999
|
-
|
|
11000
|
-
|
|
11001
|
-
);
|
|
11291
|
+
const pricePerLamport = getPriceOfBinByBinId(
|
|
11292
|
+
binId,
|
|
11293
|
+
lbPair.binStep
|
|
11294
|
+
).toString();
|
|
11002
11295
|
bins.push({
|
|
11003
11296
|
binId,
|
|
11004
11297
|
xAmount: bin.amountX,
|
|
@@ -11019,10 +11312,10 @@ var DLMM = class {
|
|
|
11019
11312
|
binArray.bins.forEach((bin, idx) => {
|
|
11020
11313
|
const binId = lowerBinIdForBinArray.toNumber() + idx;
|
|
11021
11314
|
if (binId >= lowerBinId && binId <= upperBinId) {
|
|
11022
|
-
const pricePerLamport =
|
|
11023
|
-
|
|
11024
|
-
|
|
11025
|
-
);
|
|
11315
|
+
const pricePerLamport = getPriceOfBinByBinId(
|
|
11316
|
+
binId,
|
|
11317
|
+
lbPair.binStep
|
|
11318
|
+
).toString();
|
|
11026
11319
|
bins.push({
|
|
11027
11320
|
binId,
|
|
11028
11321
|
xAmount: bin.amountX,
|
|
@@ -11038,10 +11331,6 @@ var DLMM = class {
|
|
|
11038
11331
|
}
|
|
11039
11332
|
return bins;
|
|
11040
11333
|
}
|
|
11041
|
-
static getPriceOfBinByBinId(binStep, binId) {
|
|
11042
|
-
const binStepNum = new Decimal4(binStep).div(new Decimal4(BASIS_POINT_MAX));
|
|
11043
|
-
return new Decimal4(1).add(new Decimal4(binStepNum)).pow(new Decimal4(binId)).toString();
|
|
11044
|
-
}
|
|
11045
11334
|
/** Private method */
|
|
11046
11335
|
processXYAmountDistribution(xYAmountDistribution) {
|
|
11047
11336
|
let currentBinId = null;
|
|
@@ -11080,8 +11369,6 @@ var DLMM = class {
|
|
|
11080
11369
|
const [lowerBinId2, upperBinId2] = getBinArrayLowerUpperBinId(lowerBinArrayIndex);
|
|
11081
11370
|
const binArrayBins = [];
|
|
11082
11371
|
for (let i = lowerBinId2.toNumber(); i <= upperBinId2.toNumber(); i++) {
|
|
11083
|
-
const binId = new BN9(i);
|
|
11084
|
-
const pricePerLamport = this.getPriceOfBinByBinId(binId.toNumber());
|
|
11085
11372
|
binArrayBins.push({
|
|
11086
11373
|
amountX: new BN9(0),
|
|
11087
11374
|
amountY: new BN9(0),
|
|
@@ -11106,7 +11393,10 @@ var DLMM = class {
|
|
|
11106
11393
|
binArray.bins.forEach((bin, idx) => {
|
|
11107
11394
|
const binId = lowerBinIdForBinArray.toNumber() + idx;
|
|
11108
11395
|
if (binId >= lowerBinId && binId <= upperBinId) {
|
|
11109
|
-
const pricePerLamport =
|
|
11396
|
+
const pricePerLamport = getPriceOfBinByBinId(
|
|
11397
|
+
binId,
|
|
11398
|
+
this.lbPair.binStep
|
|
11399
|
+
).toString();
|
|
11110
11400
|
bins.push({
|
|
11111
11401
|
binId,
|
|
11112
11402
|
xAmount: bin.amountX,
|
|
@@ -11147,7 +11437,10 @@ var DLMM = class {
|
|
|
11147
11437
|
binArray.bins.forEach((bin, idx) => {
|
|
11148
11438
|
const binId = lowerBinIdForBinArray.toNumber() + idx;
|
|
11149
11439
|
if (binId >= lowerBinId && binId <= upperBinId) {
|
|
11150
|
-
const pricePerLamport =
|
|
11440
|
+
const pricePerLamport = getPriceOfBinByBinId(
|
|
11441
|
+
binId,
|
|
11442
|
+
this.lbPair.binStep
|
|
11443
|
+
).toString();
|
|
11151
11444
|
bins.push({
|
|
11152
11445
|
binId,
|
|
11153
11446
|
xAmount: bin.amountX,
|
|
@@ -11448,6 +11741,7 @@ export {
|
|
|
11448
11741
|
fromWeightDistributionToAmountOneSide,
|
|
11449
11742
|
getBaseFee,
|
|
11450
11743
|
getBinArrayLowerUpperBinId,
|
|
11744
|
+
getBinArraysRequiredByPositionRange,
|
|
11451
11745
|
getBinFromBinArray,
|
|
11452
11746
|
getOrCreateATAInstruction,
|
|
11453
11747
|
getOutAmount,
|