@meteora-ag/dlmm 1.6.0-rc.18 → 1.6.0-rc.19
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 +46 -1
- package/dist/index.js +1727 -1570
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1242 -1085
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -11779,1714 +11779,1861 @@ function getTokenProgramId(lbPairState) {
|
|
|
11779
11779
|
};
|
|
11780
11780
|
}
|
|
11781
11781
|
|
|
11782
|
-
// src/dlmm/helpers/
|
|
11783
|
-
|
|
11784
|
-
|
|
11785
|
-
|
|
11782
|
+
// src/dlmm/helpers/rebalance/rebalancePosition.ts
|
|
11783
|
+
|
|
11784
|
+
|
|
11785
|
+
|
|
11786
|
+
function buildBitFlagAndNegateStrategyParameters(x0, y0, deltaX, deltaY) {
|
|
11787
|
+
let bitFlag = 0;
|
|
11788
|
+
if (x0.isNeg()) {
|
|
11789
|
+
bitFlag |= 1;
|
|
11790
|
+
x0 = x0.neg();
|
|
11791
|
+
}
|
|
11792
|
+
if (y0.isNeg()) {
|
|
11793
|
+
bitFlag |= 2;
|
|
11794
|
+
y0 = y0.neg();
|
|
11795
|
+
}
|
|
11796
|
+
if (deltaX.isNeg()) {
|
|
11797
|
+
bitFlag |= 4;
|
|
11798
|
+
deltaX = deltaX.neg();
|
|
11799
|
+
}
|
|
11800
|
+
if (deltaY.isNeg()) {
|
|
11801
|
+
bitFlag |= 8;
|
|
11802
|
+
deltaY = deltaY.neg();
|
|
11803
|
+
}
|
|
11804
|
+
return {
|
|
11805
|
+
bitFlag,
|
|
11806
|
+
x0,
|
|
11807
|
+
y0,
|
|
11808
|
+
deltaX,
|
|
11809
|
+
deltaY
|
|
11810
|
+
};
|
|
11811
|
+
}
|
|
11812
|
+
function toRebalancePositionBinData(positionData) {
|
|
11813
|
+
return positionData.positionBinData.map(
|
|
11814
|
+
({
|
|
11815
|
+
binId,
|
|
11816
|
+
price,
|
|
11817
|
+
pricePerToken,
|
|
11818
|
+
positionXAmount,
|
|
11819
|
+
positionYAmount,
|
|
11820
|
+
positionFeeXAmount,
|
|
11821
|
+
positionFeeYAmount,
|
|
11822
|
+
positionRewardAmount
|
|
11823
|
+
}) => {
|
|
11824
|
+
return {
|
|
11825
|
+
binId,
|
|
11826
|
+
price,
|
|
11827
|
+
pricePerToken,
|
|
11828
|
+
amountX: new (0, _bnjs2.default)(positionXAmount),
|
|
11829
|
+
amountY: new (0, _bnjs2.default)(positionYAmount),
|
|
11830
|
+
claimableRewardAmount: positionRewardAmount.map(
|
|
11831
|
+
(amount) => new (0, _bnjs2.default)(amount)
|
|
11832
|
+
),
|
|
11833
|
+
claimableFeeXAmount: new (0, _bnjs2.default)(positionFeeXAmount),
|
|
11834
|
+
claimableFeeYAmount: new (0, _bnjs2.default)(positionFeeYAmount)
|
|
11835
|
+
};
|
|
11836
|
+
}
|
|
11786
11837
|
);
|
|
11787
11838
|
}
|
|
11788
|
-
function
|
|
11789
|
-
const
|
|
11790
|
-
|
|
11839
|
+
function getDepositBinIds(activeId, deposits) {
|
|
11840
|
+
const uniqueBinId = /* @__PURE__ */ new Set();
|
|
11841
|
+
for (const { minDeltaId, maxDeltaId } of deposits) {
|
|
11842
|
+
const minBinId = activeId.add(minDeltaId);
|
|
11843
|
+
const maxBinId = activeId.add(maxDeltaId);
|
|
11844
|
+
for (let binId = minBinId.toNumber(); binId <= maxBinId.toNumber(); binId++) {
|
|
11845
|
+
uniqueBinId.add(binId);
|
|
11846
|
+
}
|
|
11847
|
+
}
|
|
11848
|
+
const binIds = Array.from(uniqueBinId);
|
|
11849
|
+
binIds.sort((a, b) => a - b);
|
|
11850
|
+
return binIds;
|
|
11791
11851
|
}
|
|
11792
|
-
|
|
11793
|
-
|
|
11794
|
-
|
|
11795
|
-
|
|
11796
|
-
)
|
|
11797
|
-
|
|
11798
|
-
|
|
11852
|
+
function findMinMaxBinIdWithLiquidity(rebalancePositionBinData) {
|
|
11853
|
+
let minBinId = null;
|
|
11854
|
+
let maxBinId = null;
|
|
11855
|
+
for (const binData of rebalancePositionBinData) {
|
|
11856
|
+
if (binData.amountX.isZero() && binData.amountY.isZero() && binData.claimableFeeXAmount.isZero() && binData.claimableFeeYAmount.isZero() && binData.claimableRewardAmount.every((amount) => amount.isZero())) {
|
|
11857
|
+
continue;
|
|
11858
|
+
}
|
|
11859
|
+
if (minBinId == null || binData.binId < minBinId) {
|
|
11860
|
+
minBinId = binData.binId;
|
|
11861
|
+
}
|
|
11862
|
+
if (maxBinId == null || binData.binId > maxBinId) {
|
|
11863
|
+
maxBinId = binData.binId;
|
|
11864
|
+
}
|
|
11865
|
+
}
|
|
11866
|
+
return [minBinId, maxBinId];
|
|
11799
11867
|
}
|
|
11800
|
-
|
|
11801
|
-
|
|
11802
|
-
|
|
11803
|
-
|
|
11804
|
-
|
|
11805
|
-
)).flat();
|
|
11806
|
-
return accounts;
|
|
11868
|
+
function onlyDepositToBidSide(maxDeltaId, favorXInActiveBin) {
|
|
11869
|
+
if (favorXInActiveBin) {
|
|
11870
|
+
return maxDeltaId.lt(new (0, _bnjs2.default)(0));
|
|
11871
|
+
}
|
|
11872
|
+
return maxDeltaId.lte(new (0, _bnjs2.default)(0));
|
|
11807
11873
|
}
|
|
11808
|
-
function
|
|
11809
|
-
|
|
11874
|
+
function onlyDepositToAskSide(minDeltaId, favorXInActiveBin) {
|
|
11875
|
+
if (favorXInActiveBin) {
|
|
11876
|
+
return minDeltaId.gte(new (0, _bnjs2.default)(0));
|
|
11877
|
+
}
|
|
11878
|
+
return minDeltaId.gt(new (0, _bnjs2.default)(0));
|
|
11810
11879
|
}
|
|
11811
|
-
|
|
11812
|
-
const
|
|
11813
|
-
|
|
11880
|
+
function getAmountInBinsBidSide(activeId, minDeltaId, maxDeltaId, deltaY, y0) {
|
|
11881
|
+
const amountInBins = [];
|
|
11882
|
+
const minBinId = activeId.add(minDeltaId);
|
|
11883
|
+
const maxBinId = activeId.add(maxDeltaId);
|
|
11884
|
+
for (let binId = minBinId.toNumber(); binId <= maxBinId.toNumber(); binId++) {
|
|
11885
|
+
const deltaBin = activeId.toNumber() - binId;
|
|
11886
|
+
const totalDeltaY = deltaY.mul(new (0, _bnjs2.default)(deltaBin));
|
|
11887
|
+
const amountY = y0.add(totalDeltaY);
|
|
11888
|
+
amountInBins.push({
|
|
11889
|
+
binId: new (0, _bnjs2.default)(binId),
|
|
11890
|
+
amountX: new (0, _bnjs2.default)(0),
|
|
11891
|
+
amountY
|
|
11892
|
+
});
|
|
11893
|
+
}
|
|
11894
|
+
return amountInBins;
|
|
11814
11895
|
}
|
|
11815
|
-
|
|
11816
|
-
|
|
11817
|
-
const
|
|
11818
|
-
|
|
11819
|
-
|
|
11820
|
-
|
|
11821
|
-
|
|
11822
|
-
|
|
11823
|
-
|
|
11824
|
-
|
|
11825
|
-
|
|
11826
|
-
|
|
11827
|
-
|
|
11828
|
-
|
|
11829
|
-
|
|
11830
|
-
|
|
11831
|
-
|
|
11832
|
-
|
|
11833
|
-
tokenMint,
|
|
11834
|
-
programId,
|
|
11835
|
-
_spltoken.ASSOCIATED_TOKEN_PROGRAM_ID
|
|
11836
|
-
);
|
|
11837
|
-
return { ataPubKey: toAccount, ix };
|
|
11838
|
-
} else {
|
|
11839
|
-
console.error("Error::getOrCreateATAInstruction", e);
|
|
11840
|
-
throw e;
|
|
11841
|
-
}
|
|
11896
|
+
function getAmountInBinsAskSide(activeId, binStep, minDeltaId, maxDeltaId, deltaX, x0) {
|
|
11897
|
+
const binCount = maxDeltaId.sub(minDeltaId).add(new (0, _bnjs2.default)(1));
|
|
11898
|
+
const minBinId = activeId.add(minDeltaId);
|
|
11899
|
+
const maxBinId = activeId.add(maxDeltaId);
|
|
11900
|
+
const amountInBins = new Array(binCount.toNumber());
|
|
11901
|
+
const base = getQPriceBaseFactor(binStep);
|
|
11902
|
+
let inverseBasePrice = pow(base, maxBinId.neg());
|
|
11903
|
+
for (let binId = maxBinId.toNumber(); binId >= minBinId.toNumber(); binId--) {
|
|
11904
|
+
const delta = binId - activeId.toNumber();
|
|
11905
|
+
const totalDeltaX = deltaX.mul(new (0, _bnjs2.default)(delta));
|
|
11906
|
+
const amountX = x0.add(totalDeltaX).mul(inverseBasePrice).shrn(SCALE_OFFSET);
|
|
11907
|
+
const idx = binId - minBinId.toNumber();
|
|
11908
|
+
amountInBins[idx] = {
|
|
11909
|
+
binId: new (0, _bnjs2.default)(binId),
|
|
11910
|
+
amountX,
|
|
11911
|
+
amountY: new (0, _bnjs2.default)(0)
|
|
11912
|
+
};
|
|
11913
|
+
inverseBasePrice = inverseBasePrice.mul(base).shrn(SCALE_OFFSET);
|
|
11842
11914
|
}
|
|
11843
|
-
|
|
11844
|
-
async function getTokenBalance(conn, tokenAccount) {
|
|
11845
|
-
const acc = await _spltoken.getAccount.call(void 0, conn, tokenAccount);
|
|
11846
|
-
return acc.amount;
|
|
11915
|
+
return amountInBins;
|
|
11847
11916
|
}
|
|
11848
|
-
|
|
11849
|
-
if (
|
|
11850
|
-
|
|
11851
|
-
for (const event of _optionalChain([eventParser, 'optionalAccess', _41 => _41.parseLogs, 'call', _42 => _42(logs)])) {
|
|
11852
|
-
return event.data;
|
|
11917
|
+
function toAmountIntoBins(activeId, minDeltaId, maxDeltaId, deltaX, deltaY, x0, y0, binStep, favorXInActiveBin) {
|
|
11918
|
+
if (onlyDepositToBidSide(maxDeltaId, favorXInActiveBin)) {
|
|
11919
|
+
return getAmountInBinsBidSide(activeId, minDeltaId, maxDeltaId, deltaY, y0);
|
|
11853
11920
|
}
|
|
11854
|
-
|
|
11855
|
-
|
|
11856
|
-
|
|
11857
|
-
|
|
11858
|
-
|
|
11859
|
-
|
|
11860
|
-
|
|
11861
|
-
|
|
11862
|
-
}),
|
|
11863
|
-
new (0, _web3js.TransactionInstruction)({
|
|
11864
|
-
keys: [
|
|
11865
|
-
{
|
|
11866
|
-
pubkey: to,
|
|
11867
|
-
isSigner: false,
|
|
11868
|
-
isWritable: true
|
|
11869
|
-
}
|
|
11870
|
-
],
|
|
11871
|
-
data: Buffer.from(new Uint8Array([17])),
|
|
11872
|
-
programId: _spltoken.TOKEN_PROGRAM_ID
|
|
11873
|
-
})
|
|
11874
|
-
];
|
|
11875
|
-
};
|
|
11876
|
-
var unwrapSOLInstruction = async (owner, allowOwnerOffCurve = true) => {
|
|
11877
|
-
const wSolATAAccount = _spltoken.getAssociatedTokenAddressSync.call(void 0,
|
|
11878
|
-
_spltoken.NATIVE_MINT,
|
|
11879
|
-
owner,
|
|
11880
|
-
allowOwnerOffCurve
|
|
11881
|
-
);
|
|
11882
|
-
if (wSolATAAccount) {
|
|
11883
|
-
const closedWrappedSolInstruction = _spltoken.createCloseAccountInstruction.call(void 0,
|
|
11884
|
-
wSolATAAccount,
|
|
11885
|
-
owner,
|
|
11886
|
-
owner,
|
|
11887
|
-
[],
|
|
11888
|
-
_spltoken.TOKEN_PROGRAM_ID
|
|
11921
|
+
if (onlyDepositToAskSide(minDeltaId, favorXInActiveBin)) {
|
|
11922
|
+
return getAmountInBinsAskSide(
|
|
11923
|
+
activeId,
|
|
11924
|
+
binStep,
|
|
11925
|
+
minDeltaId,
|
|
11926
|
+
maxDeltaId,
|
|
11927
|
+
deltaX,
|
|
11928
|
+
x0
|
|
11889
11929
|
);
|
|
11890
|
-
return closedWrappedSolInstruction;
|
|
11891
|
-
}
|
|
11892
|
-
return null;
|
|
11893
|
-
};
|
|
11894
|
-
async function chunkedGetMultipleAccountInfos(connection, pks, chunkSize = 100) {
|
|
11895
|
-
const accountInfos = (await Promise.all(
|
|
11896
|
-
chunks(pks, chunkSize).map(
|
|
11897
|
-
(chunk) => connection.getMultipleAccountsInfo(chunk)
|
|
11898
|
-
)
|
|
11899
|
-
)).flat();
|
|
11900
|
-
return accountInfos;
|
|
11901
|
-
}
|
|
11902
|
-
var getEstimatedComputeUnitUsageWithBuffer = async (connection, instructions, feePayer, buffer) => {
|
|
11903
|
-
if (!buffer) {
|
|
11904
|
-
buffer = 0.1;
|
|
11905
11930
|
}
|
|
11906
|
-
|
|
11907
|
-
|
|
11908
|
-
|
|
11909
|
-
|
|
11910
|
-
|
|
11911
|
-
|
|
11912
|
-
|
|
11913
|
-
);
|
|
11914
|
-
let extraComputeUnitBuffer = estimatedComputeUnitUsage * buffer;
|
|
11915
|
-
if (extraComputeUnitBuffer > MAX_CU_BUFFER) {
|
|
11916
|
-
extraComputeUnitBuffer = MAX_CU_BUFFER;
|
|
11917
|
-
} else if (extraComputeUnitBuffer < MIN_CU_BUFFER) {
|
|
11918
|
-
extraComputeUnitBuffer = MIN_CU_BUFFER;
|
|
11919
|
-
}
|
|
11920
|
-
return estimatedComputeUnitUsage + extraComputeUnitBuffer;
|
|
11921
|
-
};
|
|
11922
|
-
var getEstimatedComputeUnitIxWithBuffer = async (connection, instructions, feePayer, buffer) => {
|
|
11923
|
-
const units = await getEstimatedComputeUnitUsageWithBuffer(
|
|
11924
|
-
connection,
|
|
11925
|
-
instructions,
|
|
11926
|
-
feePayer,
|
|
11927
|
-
buffer
|
|
11928
|
-
).catch((error) => {
|
|
11929
|
-
console.error("Error::getEstimatedComputeUnitUsageWithBuffer", error);
|
|
11930
|
-
return 14e5;
|
|
11931
|
-
});
|
|
11932
|
-
return _web3js.ComputeBudgetProgram.setComputeUnitLimit({ units });
|
|
11933
|
-
};
|
|
11934
|
-
function createProgram(connection, opt) {
|
|
11935
|
-
const cluster = _optionalChain([opt, 'optionalAccess', _43 => _43.cluster]) || "mainnet-beta";
|
|
11936
|
-
const provider = new (0, _anchor.AnchorProvider)(
|
|
11937
|
-
connection,
|
|
11938
|
-
{},
|
|
11939
|
-
_anchor.AnchorProvider.defaultOptions()
|
|
11931
|
+
const [bidSideEndDeltaId, askSideStartDeltaId] = favorXInActiveBin ? [-1, 0] : [0, 1];
|
|
11932
|
+
const amountInBinsBidSide = getAmountInBinsBidSide(
|
|
11933
|
+
activeId,
|
|
11934
|
+
minDeltaId,
|
|
11935
|
+
new (0, _bnjs2.default)(bidSideEndDeltaId),
|
|
11936
|
+
deltaY,
|
|
11937
|
+
y0
|
|
11940
11938
|
);
|
|
11941
|
-
|
|
11942
|
-
|
|
11943
|
-
|
|
11939
|
+
const amountInBinsAskSide = getAmountInBinsAskSide(
|
|
11940
|
+
activeId,
|
|
11941
|
+
binStep,
|
|
11942
|
+
new (0, _bnjs2.default)(askSideStartDeltaId),
|
|
11943
|
+
maxDeltaId,
|
|
11944
|
+
deltaX,
|
|
11945
|
+
x0
|
|
11944
11946
|
);
|
|
11947
|
+
return amountInBinsBidSide.concat(amountInBinsAskSide);
|
|
11945
11948
|
}
|
|
11946
|
-
function
|
|
11947
|
-
|
|
11949
|
+
function getLiquidity(x, y, price) {
|
|
11950
|
+
const px = price.mul(x);
|
|
11951
|
+
const shly = y.shln(SCALE_OFFSET);
|
|
11952
|
+
return px.add(shly);
|
|
11948
11953
|
}
|
|
11949
|
-
function
|
|
11950
|
-
|
|
11951
|
-
|
|
11952
|
-
|
|
11954
|
+
function computeCompositionFee(binStep, sParameters3, vParameters3, outAmountX, inAmountX, outAmountY, inAmountY) {
|
|
11955
|
+
if (outAmountX.gt(inAmountX)) {
|
|
11956
|
+
const delta = inAmountY.sub(outAmountY);
|
|
11957
|
+
const totalFeeRate = getTotalFee(
|
|
11958
|
+
binStep.toNumber(),
|
|
11959
|
+
sParameters3,
|
|
11960
|
+
vParameters3
|
|
11961
|
+
);
|
|
11962
|
+
const feeAmount = delta.mul(totalFeeRate);
|
|
11963
|
+
return feeAmount.mul(FEE_PRECISION.add(totalFeeRate)).div(FEE_PRECISION.pow(new (0, _bnjs2.default)(2)));
|
|
11964
|
+
}
|
|
11965
|
+
return new (0, _bnjs2.default)(0);
|
|
11953
11966
|
}
|
|
11954
|
-
function
|
|
11955
|
-
if (
|
|
11956
|
-
|
|
11967
|
+
function simulateDepositBin(binId, binStep, amountX, amountY, bin) {
|
|
11968
|
+
if (!bin) {
|
|
11969
|
+
return {
|
|
11970
|
+
amountXIntoBin: amountX,
|
|
11971
|
+
amountYIntoBin: amountY
|
|
11972
|
+
};
|
|
11957
11973
|
}
|
|
11958
|
-
|
|
11959
|
-
|
|
11974
|
+
const price = getQPriceFromId(binId, binStep);
|
|
11975
|
+
const inLiquidity = getLiquidity(amountX, amountY, price);
|
|
11976
|
+
const binLiquidity = getLiquidity(bin.amountX, bin.amountY, price);
|
|
11977
|
+
if (bin.liquiditySupply.isZero()) {
|
|
11978
|
+
return {
|
|
11979
|
+
amountXIntoBin: amountX,
|
|
11980
|
+
amountYIntoBin: amountY
|
|
11981
|
+
};
|
|
11960
11982
|
}
|
|
11961
|
-
|
|
11962
|
-
|
|
11963
|
-
|
|
11964
|
-
|
|
11965
|
-
|
|
11966
|
-
|
|
11967
|
-
|
|
11968
|
-
|
|
11969
|
-
|
|
11970
|
-
|
|
11971
|
-
|
|
11972
|
-
};
|
|
11973
|
-
};
|
|
11974
|
-
var presetParameter2BaseFactorFilter = (baseFactor) => {
|
|
11975
|
-
return {
|
|
11976
|
-
memcmp: {
|
|
11977
|
-
bytes: _bytes.bs58.encode(baseFactor.toArrayLike(Buffer, "le", 2)),
|
|
11978
|
-
offset: 8 + 2
|
|
11979
|
-
}
|
|
11980
|
-
};
|
|
11981
|
-
};
|
|
11982
|
-
var presetParameter2BaseFeePowerFactor = (baseFeePowerFactor) => {
|
|
11983
|
-
return {
|
|
11984
|
-
memcmp: {
|
|
11985
|
-
bytes: _bytes.bs58.encode(baseFeePowerFactor.toArrayLike(Buffer, "le", 1)),
|
|
11986
|
-
offset: 8 + 22
|
|
11987
|
-
}
|
|
11988
|
-
};
|
|
11989
|
-
};
|
|
11990
|
-
var binArrayLbPairFilter = (lbPair) => {
|
|
11991
|
-
return {
|
|
11992
|
-
memcmp: {
|
|
11993
|
-
bytes: lbPair.toBase58(),
|
|
11994
|
-
offset: 8 + 16
|
|
11995
|
-
}
|
|
11996
|
-
};
|
|
11997
|
-
};
|
|
11998
|
-
var positionOwnerFilter = (owner) => {
|
|
11999
|
-
return {
|
|
12000
|
-
memcmp: {
|
|
12001
|
-
bytes: owner.toBase58(),
|
|
12002
|
-
offset: 8 + 32
|
|
12003
|
-
}
|
|
12004
|
-
};
|
|
12005
|
-
};
|
|
12006
|
-
var positionLbPairFilter = (lbPair) => {
|
|
12007
|
-
return {
|
|
12008
|
-
memcmp: {
|
|
12009
|
-
bytes: _bytes.bs58.encode(lbPair.toBuffer()),
|
|
12010
|
-
offset: 8
|
|
12011
|
-
}
|
|
12012
|
-
};
|
|
12013
|
-
};
|
|
12014
|
-
var positionV2Filter = () => {
|
|
12015
|
-
return {
|
|
12016
|
-
memcmp: {
|
|
12017
|
-
bytes: _bytes.bs58.encode(Buffer.from(getAccountDiscriminator("positionV2"))),
|
|
12018
|
-
offset: 0
|
|
12019
|
-
}
|
|
12020
|
-
};
|
|
12021
|
-
};
|
|
12022
|
-
|
|
12023
|
-
// src/dlmm/helpers/positions/index.ts
|
|
12024
|
-
|
|
12025
|
-
|
|
12026
|
-
// src/dlmm/helpers/positions/wrapper.ts
|
|
12027
|
-
|
|
12028
|
-
function combineBaseAndExtendedPositionBinData(base, extended) {
|
|
12029
|
-
const combinedLiquidityShares = base.liquidityShares;
|
|
12030
|
-
const combinedRewardInfos = base.rewardInfos;
|
|
12031
|
-
const combinedFeeInfos = base.feeInfos;
|
|
12032
|
-
for (const binData of extended) {
|
|
12033
|
-
combinedLiquidityShares.push(binData.liquidityShare);
|
|
12034
|
-
combinedRewardInfos.push(binData.rewardInfo);
|
|
12035
|
-
combinedFeeInfos.push(binData.feeInfo);
|
|
11983
|
+
const liquidityShare = inLiquidity.mul(bin.liquiditySupply).div(binLiquidity);
|
|
11984
|
+
const updatedBinXAmount = bin.amountX.add(amountX);
|
|
11985
|
+
const updatedBinYAmount = bin.amountY.add(amountY);
|
|
11986
|
+
const updatedBinSupply = bin.liquiditySupply.add(liquidityShare);
|
|
11987
|
+
let amountXIntoBin = liquidityShare.mul(
|
|
11988
|
+
updatedBinXAmount.div(updatedBinSupply)
|
|
11989
|
+
);
|
|
11990
|
+
let amountYIntoBin = liquidityShare.mul(
|
|
11991
|
+
updatedBinYAmount.div(updatedBinSupply)
|
|
11992
|
+
);
|
|
11993
|
+
if (amountXIntoBin.gt(amountX)) {
|
|
12036
11994
|
}
|
|
12037
11995
|
return {
|
|
12038
|
-
|
|
12039
|
-
|
|
12040
|
-
feeInfos: combinedFeeInfos
|
|
11996
|
+
amountXIntoBin,
|
|
11997
|
+
amountYIntoBin
|
|
12041
11998
|
};
|
|
12042
11999
|
}
|
|
12043
|
-
|
|
12044
|
-
|
|
12045
|
-
|
|
12046
|
-
|
|
12000
|
+
var RebalancePosition = class {
|
|
12001
|
+
|
|
12002
|
+
|
|
12003
|
+
|
|
12004
|
+
|
|
12005
|
+
|
|
12006
|
+
|
|
12007
|
+
|
|
12008
|
+
|
|
12009
|
+
|
|
12010
|
+
|
|
12011
|
+
constructor(positionAddress, positionData, lbPair, activeBin, shouldClaimFee, shouldClaimReward, currentTimestamp) {
|
|
12012
|
+
this.address = positionAddress;
|
|
12013
|
+
this.rebalancePositionBinData = toRebalancePositionBinData(positionData);
|
|
12014
|
+
this.lowerBinId = new (0, _bnjs2.default)(positionData.lowerBinId);
|
|
12015
|
+
this.upperBinId = new (0, _bnjs2.default)(positionData.upperBinId);
|
|
12016
|
+
this.lbPair = lbPair;
|
|
12017
|
+
this.shouldClaimFee = shouldClaimFee;
|
|
12018
|
+
this.shouldClaimReward = shouldClaimReward;
|
|
12019
|
+
this.owner = positionData.owner;
|
|
12020
|
+
this.activeBin = activeBin;
|
|
12021
|
+
this.currentTimestamp = currentTimestamp;
|
|
12022
|
+
}
|
|
12023
|
+
static async create(params) {
|
|
12024
|
+
const {
|
|
12047
12025
|
program,
|
|
12048
|
-
|
|
12049
|
-
|
|
12026
|
+
positionAddress,
|
|
12027
|
+
pairAddress,
|
|
12028
|
+
positionData,
|
|
12029
|
+
shouldClaimFee,
|
|
12030
|
+
shouldClaimReward
|
|
12031
|
+
} = params;
|
|
12032
|
+
const [lbPairAccount, clockAccount] = await program.provider.connection.getMultipleAccountsInfo([
|
|
12033
|
+
pairAddress,
|
|
12034
|
+
_web3js.SYSVAR_CLOCK_PUBKEY
|
|
12035
|
+
]);
|
|
12036
|
+
const lbPair = decodeAccount(program, "lbPair", lbPairAccount.data);
|
|
12037
|
+
const clock = ClockLayout.decode(clockAccount.data);
|
|
12038
|
+
const activeBinArrayIdx = binIdToBinArrayIndex(new (0, _bnjs2.default)(lbPair.activeId));
|
|
12039
|
+
const [activeBinArrayPubkey] = deriveBinArray(
|
|
12040
|
+
pairAddress,
|
|
12041
|
+
activeBinArrayIdx,
|
|
12042
|
+
program.programId
|
|
12050
12043
|
);
|
|
12051
|
-
const
|
|
12052
|
-
|
|
12053
|
-
program,
|
|
12054
|
-
account.data.subarray(8 + POSITION_MIN_SIZE)
|
|
12044
|
+
const activeBinArrayState = await program.account.binArray.fetch(
|
|
12045
|
+
activeBinArrayPubkey
|
|
12055
12046
|
);
|
|
12056
|
-
const
|
|
12057
|
-
|
|
12058
|
-
|
|
12047
|
+
const [lowerBinId, upperBinId] = getBinArrayLowerUpperBinId(activeBinArrayIdx);
|
|
12048
|
+
const idx = getBinIdIndexInBinArray(
|
|
12049
|
+
new (0, _bnjs2.default)(lbPair.activeId),
|
|
12050
|
+
lowerBinId,
|
|
12051
|
+
upperBinId
|
|
12052
|
+
);
|
|
12053
|
+
const activeBin = activeBinArrayState[idx.toNumber()];
|
|
12054
|
+
return new RebalancePosition(
|
|
12055
|
+
positionAddress,
|
|
12056
|
+
positionData,
|
|
12057
|
+
lbPair,
|
|
12058
|
+
activeBin,
|
|
12059
|
+
shouldClaimFee,
|
|
12060
|
+
shouldClaimReward,
|
|
12061
|
+
clock.unixTimestamp
|
|
12059
12062
|
);
|
|
12060
|
-
return new PositionV2Wrapper(key, state, extended, combinedPositionBinData);
|
|
12061
|
-
} else {
|
|
12062
|
-
throw new Error("Unknown position account");
|
|
12063
12063
|
}
|
|
12064
|
-
|
|
12065
|
-
|
|
12066
|
-
|
|
12067
|
-
|
|
12068
|
-
|
|
12069
|
-
|
|
12070
|
-
|
|
12071
|
-
|
|
12072
|
-
|
|
12073
|
-
|
|
12074
|
-
|
|
12075
|
-
|
|
12076
|
-
|
|
12077
|
-
|
|
12078
|
-
feeOwner() {
|
|
12079
|
-
return this.inner.feeOwner;
|
|
12080
|
-
}
|
|
12081
|
-
lockReleasePoint() {
|
|
12082
|
-
return this.inner.lockReleasePoint;
|
|
12083
|
-
}
|
|
12084
|
-
operator() {
|
|
12085
|
-
return this.inner.operator;
|
|
12086
|
-
}
|
|
12087
|
-
totalClaimedFeeYAmount() {
|
|
12088
|
-
return this.inner.totalClaimedFeeYAmount;
|
|
12089
|
-
}
|
|
12090
|
-
totalClaimedFeeXAmount() {
|
|
12091
|
-
return this.inner.totalClaimedFeeXAmount;
|
|
12092
|
-
}
|
|
12093
|
-
lbPair() {
|
|
12094
|
-
return this.inner.lbPair;
|
|
12095
|
-
}
|
|
12096
|
-
lowerBinId() {
|
|
12097
|
-
return new (0, _bnjs2.default)(this.inner.lowerBinId);
|
|
12098
|
-
}
|
|
12099
|
-
upperBinId() {
|
|
12100
|
-
return new (0, _bnjs2.default)(this.inner.upperBinId);
|
|
12101
|
-
}
|
|
12102
|
-
liquidityShares() {
|
|
12103
|
-
return this.combinedPositionBinData.liquidityShares;
|
|
12104
|
-
}
|
|
12105
|
-
rewardInfos() {
|
|
12106
|
-
return this.combinedPositionBinData.rewardInfos;
|
|
12107
|
-
}
|
|
12108
|
-
feeInfos() {
|
|
12109
|
-
return this.combinedPositionBinData.feeInfos;
|
|
12110
|
-
}
|
|
12111
|
-
lastUpdatedAt() {
|
|
12112
|
-
return this.inner.lastUpdatedAt;
|
|
12113
|
-
}
|
|
12114
|
-
getBinArrayIndexesCoverage() {
|
|
12115
|
-
const isExtended = this.extended.length > 0;
|
|
12116
|
-
if (isExtended) {
|
|
12117
|
-
return getBinArrayIndexesCoverage(this.lowerBinId(), this.upperBinId());
|
|
12118
|
-
} else {
|
|
12119
|
-
const lowerBinArrayIndex = binIdToBinArrayIndex(this.lowerBinId());
|
|
12120
|
-
const upperBinArrayIndex = lowerBinArrayIndex.add(new (0, _bnjs2.default)(1));
|
|
12121
|
-
return [lowerBinArrayIndex, upperBinArrayIndex];
|
|
12122
|
-
}
|
|
12123
|
-
}
|
|
12124
|
-
getBinArrayKeysCoverage(programId) {
|
|
12125
|
-
return this.getBinArrayIndexesCoverage().map(
|
|
12126
|
-
(index) => deriveBinArray(this.lbPair(), index, programId)[0]
|
|
12127
|
-
);
|
|
12128
|
-
}
|
|
12129
|
-
version() {
|
|
12130
|
-
return 1 /* V2 */;
|
|
12131
|
-
}
|
|
12132
|
-
owner() {
|
|
12133
|
-
return this.inner.owner;
|
|
12134
|
-
}
|
|
12135
|
-
width() {
|
|
12136
|
-
return this.upperBinId().sub(this.lowerBinId()).add(new (0, _bnjs2.default)(1));
|
|
12137
|
-
}
|
|
12138
|
-
};
|
|
12139
|
-
|
|
12140
|
-
// src/dlmm/helpers/positions/index.ts
|
|
12141
|
-
function getBinArrayIndexesCoverage(lowerBinId, upperBinId) {
|
|
12142
|
-
const lowerBinArrayIndex = binIdToBinArrayIndex(lowerBinId);
|
|
12143
|
-
const upperBinArrayIndex = binIdToBinArrayIndex(upperBinId);
|
|
12144
|
-
const binArrayIndexes = [];
|
|
12145
|
-
for (let i = lowerBinArrayIndex.toNumber(); i <= upperBinArrayIndex.toNumber(); i++) {
|
|
12146
|
-
binArrayIndexes.push(new (0, _bnjs2.default)(i));
|
|
12147
|
-
}
|
|
12148
|
-
return binArrayIndexes;
|
|
12149
|
-
}
|
|
12150
|
-
function getBinArrayKeysCoverage2(lowerBinId, upperBinId, lbPair, programId) {
|
|
12151
|
-
const binArrayIndexes = getBinArrayIndexesCoverage(lowerBinId, upperBinId);
|
|
12152
|
-
return binArrayIndexes.map((index) => {
|
|
12153
|
-
return deriveBinArray(lbPair, index, programId)[0];
|
|
12154
|
-
});
|
|
12155
|
-
}
|
|
12156
|
-
function getBinArrayAccountMetasCoverage(lowerBinId, upperBinId, lbPair, programId) {
|
|
12157
|
-
return getBinArrayKeysCoverage2(lowerBinId, upperBinId, lbPair, programId).map(
|
|
12158
|
-
(key) => {
|
|
12159
|
-
return {
|
|
12160
|
-
pubkey: key,
|
|
12161
|
-
isSigner: false,
|
|
12162
|
-
isWritable: true
|
|
12163
|
-
};
|
|
12164
|
-
}
|
|
12165
|
-
);
|
|
12166
|
-
}
|
|
12167
|
-
function getPositionLowerUpperBinIdWithLiquidity(position) {
|
|
12168
|
-
const binWithLiquidity = position.positionBinData.filter(
|
|
12169
|
-
(b) => !new (0, _bnjs2.default)(b.binLiquidity).isZero() || !new (0, _bnjs2.default)(b.positionFeeXAmount.toString()).isZero() || !new (0, _bnjs2.default)(b.positionFeeYAmount.toString()).isZero() || !new (0, _bnjs2.default)(b.positionRewardAmount[0].toString()).isZero() || !new (0, _bnjs2.default)(b.positionRewardAmount[1].toString()).isZero()
|
|
12170
|
-
);
|
|
12171
|
-
return binWithLiquidity.length > 0 ? {
|
|
12172
|
-
lowerBinId: new (0, _bnjs2.default)(binWithLiquidity[0].binId),
|
|
12173
|
-
upperBinId: new (0, _bnjs2.default)(binWithLiquidity[binWithLiquidity.length - 1].binId)
|
|
12174
|
-
} : null;
|
|
12175
|
-
}
|
|
12176
|
-
function isPositionNoFee(position) {
|
|
12177
|
-
return position.feeX.isZero() && position.feeY.isZero();
|
|
12178
|
-
}
|
|
12179
|
-
function isPositionNoReward(position) {
|
|
12180
|
-
return position.rewardOne.isZero() && position.rewardTwo.isZero();
|
|
12181
|
-
}
|
|
12182
|
-
function chunkBinRange(minBinId, maxBinId) {
|
|
12183
|
-
const chunkedBinRange = [];
|
|
12184
|
-
let startBinId = minBinId;
|
|
12185
|
-
while (startBinId <= maxBinId) {
|
|
12186
|
-
const endBinId = Math.min(
|
|
12187
|
-
startBinId + DEFAULT_BIN_PER_POSITION.toNumber() - 1,
|
|
12188
|
-
maxBinId
|
|
12189
|
-
);
|
|
12190
|
-
chunkedBinRange.push({
|
|
12191
|
-
lowerBinId: startBinId,
|
|
12192
|
-
upperBinId: endBinId
|
|
12193
|
-
});
|
|
12194
|
-
startBinId += DEFAULT_BIN_PER_POSITION.toNumber();
|
|
12195
|
-
}
|
|
12196
|
-
return chunkedBinRange;
|
|
12197
|
-
}
|
|
12198
|
-
async function getPositionExpandRentExemption(currentMinBinId, currentMaxBinId, connection, binCountToExpand) {
|
|
12199
|
-
const currentPositionWidth = currentMaxBinId.sub(currentMinBinId).addn(1);
|
|
12200
|
-
const positionWidthAfterExpand = currentPositionWidth.add(binCountToExpand);
|
|
12201
|
-
if (positionWidthAfterExpand.lte(DEFAULT_BIN_PER_POSITION)) {
|
|
12202
|
-
return 0;
|
|
12203
|
-
} else {
|
|
12204
|
-
const binCountInExpandedBytes = positionWidthAfterExpand.sub(
|
|
12205
|
-
DEFAULT_BIN_PER_POSITION
|
|
12206
|
-
);
|
|
12207
|
-
const expandSize = binCountInExpandedBytes.toNumber() * POSITION_BIN_DATA_SIZE;
|
|
12208
|
-
const [minimumLamports, rentExemptionLamports] = await Promise.all([
|
|
12209
|
-
connection.getMinimumBalanceForRentExemption(0),
|
|
12210
|
-
connection.getMinimumBalanceForRentExemption(expandSize)
|
|
12211
|
-
]);
|
|
12212
|
-
return rentExemptionLamports - minimumLamports;
|
|
12213
|
-
}
|
|
12214
|
-
}
|
|
12215
|
-
function getExtendedPositionBinCount(minBinId, maxBinId) {
|
|
12216
|
-
const width = maxBinId.sub(minBinId).addn(1);
|
|
12217
|
-
const extended = width.sub(DEFAULT_BIN_PER_POSITION);
|
|
12218
|
-
return extended.lte(new (0, _bnjs2.default)(0)) ? new (0, _bnjs2.default)(0) : extended;
|
|
12219
|
-
}
|
|
12220
|
-
function decodeExtendedPosition(base, program, bytes) {
|
|
12221
|
-
const width = base.upperBinId - base.lowerBinId + 1;
|
|
12222
|
-
const extendedWidth = width - DEFAULT_BIN_PER_POSITION.toNumber();
|
|
12223
|
-
const extendedPosition = [];
|
|
12224
|
-
for (let i = 0; i < extendedWidth; i++) {
|
|
12225
|
-
const offset = i * POSITION_BIN_DATA_SIZE;
|
|
12226
|
-
const data = bytes.subarray(offset, offset + POSITION_BIN_DATA_SIZE);
|
|
12227
|
-
const decodedPositionBinData = program.coder.types.decode(
|
|
12228
|
-
// TODO: Find a type safe way
|
|
12229
|
-
"positionBinData",
|
|
12230
|
-
data
|
|
12231
|
-
);
|
|
12232
|
-
extendedPosition.push(decodedPositionBinData);
|
|
12233
|
-
}
|
|
12234
|
-
return extendedPosition;
|
|
12235
|
-
}
|
|
12236
|
-
|
|
12237
|
-
// src/dlmm/helpers/rebalance/rebalancePosition.ts
|
|
12238
|
-
|
|
12239
|
-
|
|
12240
|
-
|
|
12241
|
-
function buildBitFlagAndNegateStrategyParameters(x0, y0, deltaX, deltaY) {
|
|
12242
|
-
let bitFlag = 0;
|
|
12243
|
-
if (x0.isNeg()) {
|
|
12244
|
-
bitFlag |= 1;
|
|
12245
|
-
x0 = x0.neg();
|
|
12246
|
-
}
|
|
12247
|
-
if (y0.isNeg()) {
|
|
12248
|
-
bitFlag |= 2;
|
|
12249
|
-
y0 = y0.neg();
|
|
12250
|
-
}
|
|
12251
|
-
if (deltaX.isNeg()) {
|
|
12252
|
-
bitFlag |= 4;
|
|
12253
|
-
deltaX = deltaX.neg();
|
|
12254
|
-
}
|
|
12255
|
-
if (deltaY.isNeg()) {
|
|
12256
|
-
bitFlag |= 8;
|
|
12257
|
-
deltaY = deltaY.neg();
|
|
12258
|
-
}
|
|
12259
|
-
return {
|
|
12260
|
-
bitFlag,
|
|
12261
|
-
x0,
|
|
12262
|
-
y0,
|
|
12263
|
-
deltaX,
|
|
12264
|
-
deltaY
|
|
12265
|
-
};
|
|
12266
|
-
}
|
|
12267
|
-
function toRebalancePositionBinData(positionData) {
|
|
12268
|
-
return positionData.positionBinData.map(
|
|
12269
|
-
({
|
|
12270
|
-
binId,
|
|
12271
|
-
price,
|
|
12272
|
-
pricePerToken,
|
|
12273
|
-
positionXAmount,
|
|
12274
|
-
positionYAmount,
|
|
12275
|
-
positionFeeXAmount,
|
|
12276
|
-
positionFeeYAmount,
|
|
12277
|
-
positionRewardAmount
|
|
12278
|
-
}) => {
|
|
12279
|
-
return {
|
|
12280
|
-
binId,
|
|
12281
|
-
price,
|
|
12282
|
-
pricePerToken,
|
|
12283
|
-
amountX: new (0, _bnjs2.default)(positionXAmount),
|
|
12284
|
-
amountY: new (0, _bnjs2.default)(positionYAmount),
|
|
12285
|
-
claimableRewardAmount: positionRewardAmount.map(
|
|
12286
|
-
(amount) => new (0, _bnjs2.default)(amount)
|
|
12287
|
-
),
|
|
12288
|
-
claimableFeeXAmount: new (0, _bnjs2.default)(positionFeeXAmount),
|
|
12289
|
-
claimableFeeYAmount: new (0, _bnjs2.default)(positionFeeYAmount)
|
|
12290
|
-
};
|
|
12291
|
-
}
|
|
12292
|
-
);
|
|
12293
|
-
}
|
|
12294
|
-
function getDepositBinIds(activeId, deposits) {
|
|
12295
|
-
const uniqueBinId = /* @__PURE__ */ new Set();
|
|
12296
|
-
for (const { minDeltaId, maxDeltaId } of deposits) {
|
|
12297
|
-
const minBinId = activeId.add(minDeltaId);
|
|
12298
|
-
const maxBinId = activeId.add(maxDeltaId);
|
|
12299
|
-
for (let binId = minBinId.toNumber(); binId <= maxBinId.toNumber(); binId++) {
|
|
12300
|
-
uniqueBinId.add(binId);
|
|
12064
|
+
_simulateDeposit(binStep, tokenXDecimal, tokenYDecimal, deposits, simulatedWithdrawResult) {
|
|
12065
|
+
const { liquidityAndFeeXWithdrawn, liquidityAndFeeYWithdrawn } = simulatedWithdrawResult;
|
|
12066
|
+
const activeId = new (0, _bnjs2.default)(this.lbPair.activeId);
|
|
12067
|
+
const depositBinIds = getDepositBinIds(activeId, deposits);
|
|
12068
|
+
if (depositBinIds.length > 0) {
|
|
12069
|
+
const depositMinBinId = depositBinIds[0];
|
|
12070
|
+
const depositMaxBinId = depositBinIds[depositBinIds.length - 1];
|
|
12071
|
+
this._simulateResize(
|
|
12072
|
+
new (0, _bnjs2.default)(depositMinBinId),
|
|
12073
|
+
new (0, _bnjs2.default)(depositMaxBinId),
|
|
12074
|
+
binStep,
|
|
12075
|
+
tokenXDecimal,
|
|
12076
|
+
tokenYDecimal
|
|
12077
|
+
);
|
|
12301
12078
|
}
|
|
12302
|
-
|
|
12303
|
-
|
|
12304
|
-
|
|
12305
|
-
|
|
12306
|
-
|
|
12307
|
-
|
|
12308
|
-
|
|
12309
|
-
|
|
12310
|
-
|
|
12311
|
-
|
|
12312
|
-
|
|
12079
|
+
let totalAmountXDeposited = new (0, _bnjs2.default)(0);
|
|
12080
|
+
let totalAmountYDeposited = new (0, _bnjs2.default)(0);
|
|
12081
|
+
const addLiquidityParam = [];
|
|
12082
|
+
for (const {
|
|
12083
|
+
x0,
|
|
12084
|
+
y0,
|
|
12085
|
+
favorXInActiveBin,
|
|
12086
|
+
deltaX,
|
|
12087
|
+
deltaY,
|
|
12088
|
+
minDeltaId,
|
|
12089
|
+
maxDeltaId
|
|
12090
|
+
} of deposits) {
|
|
12091
|
+
const params = buildBitFlagAndNegateStrategyParameters(
|
|
12092
|
+
x0,
|
|
12093
|
+
y0,
|
|
12094
|
+
deltaX,
|
|
12095
|
+
deltaY
|
|
12096
|
+
);
|
|
12097
|
+
addLiquidityParam.push({
|
|
12098
|
+
minDeltaId: minDeltaId.toNumber(),
|
|
12099
|
+
maxDeltaId: maxDeltaId.toNumber(),
|
|
12100
|
+
x0: params.x0,
|
|
12101
|
+
y0: params.y0,
|
|
12102
|
+
deltaX: params.deltaX,
|
|
12103
|
+
deltaY: params.deltaY,
|
|
12104
|
+
bitFlag: params.bitFlag,
|
|
12105
|
+
padding: Array(16).fill(0),
|
|
12106
|
+
favorXInActiveId: favorXInActiveBin
|
|
12107
|
+
});
|
|
12108
|
+
const amountIntoBins = toAmountIntoBins(
|
|
12109
|
+
activeId,
|
|
12110
|
+
minDeltaId,
|
|
12111
|
+
maxDeltaId,
|
|
12112
|
+
deltaX,
|
|
12113
|
+
deltaY,
|
|
12114
|
+
x0,
|
|
12115
|
+
y0,
|
|
12116
|
+
binStep,
|
|
12117
|
+
favorXInActiveBin
|
|
12118
|
+
);
|
|
12119
|
+
for (const { binId, amountX, amountY } of amountIntoBins) {
|
|
12120
|
+
totalAmountXDeposited = totalAmountXDeposited.add(amountX);
|
|
12121
|
+
totalAmountYDeposited = totalAmountYDeposited.add(amountY);
|
|
12122
|
+
const idx = this.rebalancePositionBinData.findIndex(
|
|
12123
|
+
(data) => data.binId == binId.toNumber()
|
|
12124
|
+
);
|
|
12125
|
+
if (binId.eq(activeId)) {
|
|
12126
|
+
const vParameters3 = Object.assign({}, this.lbPair.vParameters);
|
|
12127
|
+
const sParameters3 = Object.assign({}, this.lbPair.parameters);
|
|
12128
|
+
DLMM.updateReference(
|
|
12129
|
+
activeId.toNumber(),
|
|
12130
|
+
vParameters3,
|
|
12131
|
+
sParameters3,
|
|
12132
|
+
this.currentTimestamp.toNumber()
|
|
12133
|
+
);
|
|
12134
|
+
DLMM.updateVolatilityAccumulator(
|
|
12135
|
+
vParameters3,
|
|
12136
|
+
sParameters3,
|
|
12137
|
+
activeId.toNumber()
|
|
12138
|
+
);
|
|
12139
|
+
const { amountXIntoBin, amountYIntoBin } = simulateDepositBin(
|
|
12140
|
+
binId,
|
|
12141
|
+
binStep,
|
|
12142
|
+
amountX,
|
|
12143
|
+
amountY,
|
|
12144
|
+
this.activeBin
|
|
12145
|
+
);
|
|
12146
|
+
const feeY = computeCompositionFee(
|
|
12147
|
+
binStep,
|
|
12148
|
+
sParameters3,
|
|
12149
|
+
vParameters3,
|
|
12150
|
+
amountXIntoBin,
|
|
12151
|
+
amountX,
|
|
12152
|
+
amountYIntoBin,
|
|
12153
|
+
amountY
|
|
12154
|
+
);
|
|
12155
|
+
const feeX = computeCompositionFee(
|
|
12156
|
+
binStep,
|
|
12157
|
+
sParameters3,
|
|
12158
|
+
vParameters3,
|
|
12159
|
+
amountYIntoBin,
|
|
12160
|
+
amountY,
|
|
12161
|
+
amountXIntoBin,
|
|
12162
|
+
amountX
|
|
12163
|
+
);
|
|
12164
|
+
const amountXIntoBinExcludeFee = amountXIntoBin.sub(feeX);
|
|
12165
|
+
const amountYIntoBinExcludeFee = amountYIntoBin.sub(feeY);
|
|
12166
|
+
this.rebalancePositionBinData[idx].amountX = this.rebalancePositionBinData[idx].amountX.add(
|
|
12167
|
+
amountXIntoBinExcludeFee
|
|
12168
|
+
);
|
|
12169
|
+
this.rebalancePositionBinData[idx].amountY = this.rebalancePositionBinData[idx].amountY.add(
|
|
12170
|
+
amountYIntoBinExcludeFee
|
|
12171
|
+
);
|
|
12172
|
+
} else {
|
|
12173
|
+
this.rebalancePositionBinData[idx].amountX = this.rebalancePositionBinData[idx].amountX.add(amountX);
|
|
12174
|
+
this.rebalancePositionBinData[idx].amountY = this.rebalancePositionBinData[idx].amountY.add(amountY);
|
|
12175
|
+
}
|
|
12176
|
+
}
|
|
12313
12177
|
}
|
|
12314
|
-
|
|
12315
|
-
|
|
12178
|
+
let actualTotalAmountXDeposited = totalAmountXDeposited;
|
|
12179
|
+
let actualTotalAmountYDeposited = totalAmountYDeposited;
|
|
12180
|
+
let actualLiquidityAndFeeXWithdrawn = liquidityAndFeeXWithdrawn;
|
|
12181
|
+
let actualLiquidityAndFeeYWithdrawn = liquidityAndFeeYWithdrawn;
|
|
12182
|
+
if (actualTotalAmountXDeposited.gt(actualLiquidityAndFeeXWithdrawn)) {
|
|
12183
|
+
actualTotalAmountXDeposited = actualTotalAmountXDeposited.sub(
|
|
12184
|
+
actualLiquidityAndFeeXWithdrawn
|
|
12185
|
+
);
|
|
12186
|
+
actualLiquidityAndFeeXWithdrawn = new (0, _bnjs2.default)(0);
|
|
12187
|
+
} else {
|
|
12188
|
+
actualLiquidityAndFeeXWithdrawn = actualLiquidityAndFeeXWithdrawn.sub(
|
|
12189
|
+
actualTotalAmountXDeposited
|
|
12190
|
+
);
|
|
12191
|
+
actualTotalAmountXDeposited = new (0, _bnjs2.default)(0);
|
|
12316
12192
|
}
|
|
12317
|
-
if (
|
|
12318
|
-
|
|
12193
|
+
if (actualTotalAmountYDeposited.gt(actualLiquidityAndFeeYWithdrawn)) {
|
|
12194
|
+
actualTotalAmountYDeposited = actualTotalAmountYDeposited.sub(
|
|
12195
|
+
actualLiquidityAndFeeYWithdrawn
|
|
12196
|
+
);
|
|
12197
|
+
actualLiquidityAndFeeYWithdrawn = new (0, _bnjs2.default)(0);
|
|
12198
|
+
} else {
|
|
12199
|
+
actualLiquidityAndFeeYWithdrawn = actualLiquidityAndFeeYWithdrawn.sub(
|
|
12200
|
+
actualTotalAmountYDeposited
|
|
12201
|
+
);
|
|
12202
|
+
actualTotalAmountYDeposited = new (0, _bnjs2.default)(0);
|
|
12319
12203
|
}
|
|
12320
|
-
}
|
|
12321
|
-
return [minBinId, maxBinId];
|
|
12322
|
-
}
|
|
12323
|
-
function onlyDepositToBidSide(maxDeltaId, favorXInActiveBin) {
|
|
12324
|
-
if (favorXInActiveBin) {
|
|
12325
|
-
return maxDeltaId.lt(new (0, _bnjs2.default)(0));
|
|
12326
|
-
}
|
|
12327
|
-
return maxDeltaId.lte(new (0, _bnjs2.default)(0));
|
|
12328
|
-
}
|
|
12329
|
-
function onlyDepositToAskSide(minDeltaId, favorXInActiveBin) {
|
|
12330
|
-
if (favorXInActiveBin) {
|
|
12331
|
-
return minDeltaId.gte(new (0, _bnjs2.default)(0));
|
|
12332
|
-
}
|
|
12333
|
-
return minDeltaId.gt(new (0, _bnjs2.default)(0));
|
|
12334
|
-
}
|
|
12335
|
-
function getAmountInBinsBidSide(activeId, minDeltaId, maxDeltaId, deltaY, y0) {
|
|
12336
|
-
const amountInBins = [];
|
|
12337
|
-
const minBinId = activeId.add(minDeltaId);
|
|
12338
|
-
const maxBinId = activeId.add(maxDeltaId);
|
|
12339
|
-
for (let binId = minBinId.toNumber(); binId <= maxBinId.toNumber(); binId++) {
|
|
12340
|
-
const deltaBin = activeId.toNumber() - binId;
|
|
12341
|
-
const totalDeltaY = deltaY.mul(new (0, _bnjs2.default)(deltaBin));
|
|
12342
|
-
const amountY = y0.add(totalDeltaY);
|
|
12343
|
-
amountInBins.push({
|
|
12344
|
-
binId: new (0, _bnjs2.default)(binId),
|
|
12345
|
-
amountX: new (0, _bnjs2.default)(0),
|
|
12346
|
-
amountY
|
|
12347
|
-
});
|
|
12348
|
-
}
|
|
12349
|
-
return amountInBins;
|
|
12350
|
-
}
|
|
12351
|
-
function getAmountInBinsAskSide(activeId, binStep, minDeltaId, maxDeltaId, deltaX, x0) {
|
|
12352
|
-
const binCount = maxDeltaId.sub(minDeltaId).add(new (0, _bnjs2.default)(1));
|
|
12353
|
-
const minBinId = activeId.add(minDeltaId);
|
|
12354
|
-
const maxBinId = activeId.add(maxDeltaId);
|
|
12355
|
-
const amountInBins = new Array(binCount.toNumber());
|
|
12356
|
-
const base = getQPriceBaseFactor(binStep);
|
|
12357
|
-
let inverseBasePrice = pow(base, maxBinId.neg());
|
|
12358
|
-
for (let binId = maxBinId.toNumber(); binId >= minBinId.toNumber(); binId--) {
|
|
12359
|
-
const delta = binId - activeId.toNumber();
|
|
12360
|
-
const totalDeltaX = deltaX.mul(new (0, _bnjs2.default)(delta));
|
|
12361
|
-
const amountX = x0.add(totalDeltaX).mul(inverseBasePrice).shrn(SCALE_OFFSET);
|
|
12362
|
-
const idx = binId - minBinId.toNumber();
|
|
12363
|
-
amountInBins[idx] = {
|
|
12364
|
-
binId: new (0, _bnjs2.default)(binId),
|
|
12365
|
-
amountX,
|
|
12366
|
-
amountY: new (0, _bnjs2.default)(0)
|
|
12367
|
-
};
|
|
12368
|
-
inverseBasePrice = inverseBasePrice.mul(base).shrn(SCALE_OFFSET);
|
|
12369
|
-
}
|
|
12370
|
-
return amountInBins;
|
|
12371
|
-
}
|
|
12372
|
-
function toAmountIntoBins(activeId, minDeltaId, maxDeltaId, deltaX, deltaY, x0, y0, binStep, favorXInActiveBin) {
|
|
12373
|
-
if (onlyDepositToBidSide(maxDeltaId, favorXInActiveBin)) {
|
|
12374
|
-
return getAmountInBinsBidSide(activeId, minDeltaId, maxDeltaId, deltaY, y0);
|
|
12375
|
-
}
|
|
12376
|
-
if (onlyDepositToAskSide(minDeltaId, favorXInActiveBin)) {
|
|
12377
|
-
return getAmountInBinsAskSide(
|
|
12378
|
-
activeId,
|
|
12379
|
-
binStep,
|
|
12380
|
-
minDeltaId,
|
|
12381
|
-
maxDeltaId,
|
|
12382
|
-
deltaX,
|
|
12383
|
-
x0
|
|
12384
|
-
);
|
|
12385
|
-
}
|
|
12386
|
-
const [bidSideEndDeltaId, askSideStartDeltaId] = favorXInActiveBin ? [-1, 0] : [0, 1];
|
|
12387
|
-
const amountInBinsBidSide = getAmountInBinsBidSide(
|
|
12388
|
-
activeId,
|
|
12389
|
-
minDeltaId,
|
|
12390
|
-
new (0, _bnjs2.default)(bidSideEndDeltaId),
|
|
12391
|
-
deltaY,
|
|
12392
|
-
y0
|
|
12393
|
-
);
|
|
12394
|
-
const amountInBinsAskSide = getAmountInBinsAskSide(
|
|
12395
|
-
activeId,
|
|
12396
|
-
binStep,
|
|
12397
|
-
new (0, _bnjs2.default)(askSideStartDeltaId),
|
|
12398
|
-
maxDeltaId,
|
|
12399
|
-
deltaX,
|
|
12400
|
-
x0
|
|
12401
|
-
);
|
|
12402
|
-
return amountInBinsBidSide.concat(amountInBinsAskSide);
|
|
12403
|
-
}
|
|
12404
|
-
function getLiquidity(x, y, price) {
|
|
12405
|
-
const px = price.mul(x);
|
|
12406
|
-
const shly = y.shln(SCALE_OFFSET);
|
|
12407
|
-
return px.add(shly);
|
|
12408
|
-
}
|
|
12409
|
-
function computeCompositionFee(binStep, sParameters3, vParameters3, outAmountX, inAmountX, outAmountY, inAmountY) {
|
|
12410
|
-
if (outAmountX.gt(inAmountX)) {
|
|
12411
|
-
const delta = inAmountY.sub(outAmountY);
|
|
12412
|
-
const totalFeeRate = getTotalFee(
|
|
12413
|
-
binStep.toNumber(),
|
|
12414
|
-
sParameters3,
|
|
12415
|
-
vParameters3
|
|
12416
|
-
);
|
|
12417
|
-
const feeAmount = delta.mul(totalFeeRate);
|
|
12418
|
-
return feeAmount.mul(FEE_PRECISION.add(totalFeeRate)).div(FEE_PRECISION.pow(new (0, _bnjs2.default)(2)));
|
|
12419
|
-
}
|
|
12420
|
-
return new (0, _bnjs2.default)(0);
|
|
12421
|
-
}
|
|
12422
|
-
function simulateDepositBin(binId, binStep, amountX, amountY, bin) {
|
|
12423
|
-
if (!bin) {
|
|
12424
|
-
return {
|
|
12425
|
-
amountXIntoBin: amountX,
|
|
12426
|
-
amountYIntoBin: amountY
|
|
12427
|
-
};
|
|
12428
|
-
}
|
|
12429
|
-
const price = getQPriceFromId(binId, binStep);
|
|
12430
|
-
const inLiquidity = getLiquidity(amountX, amountY, price);
|
|
12431
|
-
const binLiquidity = getLiquidity(bin.amountX, bin.amountY, price);
|
|
12432
|
-
if (bin.liquiditySupply.isZero()) {
|
|
12433
12204
|
return {
|
|
12434
|
-
|
|
12435
|
-
|
|
12205
|
+
result: {
|
|
12206
|
+
totalAmountXDeposited,
|
|
12207
|
+
totalAmountYDeposited,
|
|
12208
|
+
actualLiquidityAndFeeXWithdrawn,
|
|
12209
|
+
actualLiquidityAndFeeYWithdrawn,
|
|
12210
|
+
actualTotalAmountXDeposited,
|
|
12211
|
+
actualTotalAmountYDeposited
|
|
12212
|
+
},
|
|
12213
|
+
depositParams: addLiquidityParam
|
|
12436
12214
|
};
|
|
12437
12215
|
}
|
|
12438
|
-
|
|
12439
|
-
|
|
12440
|
-
|
|
12441
|
-
|
|
12442
|
-
|
|
12443
|
-
updatedBinXAmount.div(updatedBinSupply)
|
|
12444
|
-
);
|
|
12445
|
-
let amountYIntoBin = liquidityShare.mul(
|
|
12446
|
-
updatedBinYAmount.div(updatedBinSupply)
|
|
12447
|
-
);
|
|
12448
|
-
if (amountXIntoBin.gt(amountX)) {
|
|
12449
|
-
}
|
|
12450
|
-
return {
|
|
12451
|
-
amountXIntoBin,
|
|
12452
|
-
amountYIntoBin
|
|
12453
|
-
};
|
|
12454
|
-
}
|
|
12455
|
-
var RebalancePosition = class {
|
|
12456
|
-
|
|
12457
|
-
|
|
12458
|
-
|
|
12459
|
-
|
|
12460
|
-
|
|
12461
|
-
|
|
12462
|
-
|
|
12463
|
-
|
|
12464
|
-
|
|
12465
|
-
|
|
12466
|
-
constructor(positionAddress, positionData, lbPair, activeBin, shouldClaimFee, shouldClaimReward, currentTimestamp) {
|
|
12467
|
-
this.address = positionAddress;
|
|
12468
|
-
this.rebalancePositionBinData = toRebalancePositionBinData(positionData);
|
|
12469
|
-
this.lowerBinId = new (0, _bnjs2.default)(positionData.lowerBinId);
|
|
12470
|
-
this.upperBinId = new (0, _bnjs2.default)(positionData.upperBinId);
|
|
12471
|
-
this.lbPair = lbPair;
|
|
12472
|
-
this.shouldClaimFee = shouldClaimFee;
|
|
12473
|
-
this.shouldClaimReward = shouldClaimReward;
|
|
12474
|
-
this.owner = positionData.owner;
|
|
12475
|
-
this.activeBin = activeBin;
|
|
12476
|
-
this.currentTimestamp = currentTimestamp;
|
|
12477
|
-
}
|
|
12478
|
-
static async create(params) {
|
|
12479
|
-
const {
|
|
12480
|
-
program,
|
|
12481
|
-
positionAddress,
|
|
12482
|
-
pairAddress,
|
|
12483
|
-
positionData,
|
|
12484
|
-
shouldClaimFee,
|
|
12485
|
-
shouldClaimReward
|
|
12486
|
-
} = params;
|
|
12487
|
-
const [lbPairAccount, clockAccount] = await program.provider.connection.getMultipleAccountsInfo([
|
|
12488
|
-
pairAddress,
|
|
12489
|
-
_web3js.SYSVAR_CLOCK_PUBKEY
|
|
12490
|
-
]);
|
|
12491
|
-
const lbPair = decodeAccount(program, "lbPair", lbPairAccount.data);
|
|
12492
|
-
const clock = ClockLayout.decode(clockAccount.data);
|
|
12493
|
-
const activeBinArrayIdx = binIdToBinArrayIndex(new (0, _bnjs2.default)(lbPair.activeId));
|
|
12494
|
-
const [activeBinArrayPubkey] = deriveBinArray(
|
|
12495
|
-
pairAddress,
|
|
12496
|
-
activeBinArrayIdx,
|
|
12497
|
-
program.programId
|
|
12498
|
-
);
|
|
12499
|
-
const activeBinArrayState = await program.account.binArray.fetch(
|
|
12500
|
-
activeBinArrayPubkey
|
|
12216
|
+
_simulateResize(depositMinBinId, depositMaxBinId, binStep, tokenXDecimal, tokenYDecimal) {
|
|
12217
|
+
const tokenXMultiplier = new (0, _decimaljs2.default)(10 ** tokenXDecimal.toNumber());
|
|
12218
|
+
const tokenYMultiplier = new (0, _decimaljs2.default)(10 ** tokenYDecimal.toNumber());
|
|
12219
|
+
const [minBinId, maxBinId] = findMinMaxBinIdWithLiquidity(
|
|
12220
|
+
this.rebalancePositionBinData
|
|
12501
12221
|
);
|
|
12502
|
-
const
|
|
12503
|
-
|
|
12504
|
-
new (0, _bnjs2.default)(lbPair.activeId),
|
|
12505
|
-
lowerBinId,
|
|
12506
|
-
upperBinId
|
|
12222
|
+
const newMinBinId = new (0, _bnjs2.default)(
|
|
12223
|
+
Math.min(depositMinBinId.toNumber(), _nullishCoalesce(minBinId, () => ( Number.MAX_SAFE_INTEGER)))
|
|
12507
12224
|
);
|
|
12508
|
-
const
|
|
12509
|
-
|
|
12510
|
-
positionAddress,
|
|
12511
|
-
positionData,
|
|
12512
|
-
lbPair,
|
|
12513
|
-
activeBin,
|
|
12514
|
-
shouldClaimFee,
|
|
12515
|
-
shouldClaimReward,
|
|
12516
|
-
clock.unixTimestamp
|
|
12225
|
+
const newMaxBinId = new (0, _bnjs2.default)(
|
|
12226
|
+
Math.max(depositMaxBinId.toNumber(), _nullishCoalesce(maxBinId, () => ( Number.MIN_SAFE_INTEGER)))
|
|
12517
12227
|
);
|
|
12228
|
+
if (newMinBinId.lt(this.lowerBinId)) {
|
|
12229
|
+
const binCountToExpand = this.lowerBinId.sub(depositMinBinId);
|
|
12230
|
+
for (let i = 1; i <= binCountToExpand.toNumber(); i++) {
|
|
12231
|
+
const binId = this.lowerBinId.subn(i);
|
|
12232
|
+
const price = getPriceOfBinByBinId(
|
|
12233
|
+
binId.toNumber(),
|
|
12234
|
+
binStep.toNumber()
|
|
12235
|
+
);
|
|
12236
|
+
const adjustedPrice = price.mul(tokenXMultiplier).div(tokenYMultiplier);
|
|
12237
|
+
this.rebalancePositionBinData.unshift({
|
|
12238
|
+
binId: binId.toNumber(),
|
|
12239
|
+
price: adjustedPrice.toString(),
|
|
12240
|
+
pricePerToken: adjustedPrice.toString(),
|
|
12241
|
+
amountX: new (0, _bnjs2.default)(0),
|
|
12242
|
+
amountY: new (0, _bnjs2.default)(0),
|
|
12243
|
+
claimableRewardAmount: [new (0, _bnjs2.default)(0), new (0, _bnjs2.default)(0)],
|
|
12244
|
+
claimableFeeXAmount: new (0, _bnjs2.default)(0),
|
|
12245
|
+
claimableFeeYAmount: new (0, _bnjs2.default)(0)
|
|
12246
|
+
});
|
|
12247
|
+
}
|
|
12248
|
+
} else {
|
|
12249
|
+
const binCountToShrink = newMinBinId.sub(this.lowerBinId);
|
|
12250
|
+
for (let i = 1; i <= binCountToShrink.toNumber(); i++) {
|
|
12251
|
+
this.rebalancePositionBinData.shift();
|
|
12252
|
+
}
|
|
12253
|
+
}
|
|
12254
|
+
if (newMaxBinId.gt(this.upperBinId)) {
|
|
12255
|
+
const binCountToExpand = newMaxBinId.sub(this.upperBinId);
|
|
12256
|
+
for (let i = 1; i <= binCountToExpand.toNumber(); i++) {
|
|
12257
|
+
const binId = this.upperBinId.addn(i);
|
|
12258
|
+
const price = getPriceOfBinByBinId(
|
|
12259
|
+
binId.toNumber(),
|
|
12260
|
+
binStep.toNumber()
|
|
12261
|
+
);
|
|
12262
|
+
const adjustedPrice = price.mul(tokenXMultiplier).div(tokenYMultiplier);
|
|
12263
|
+
this.rebalancePositionBinData.push({
|
|
12264
|
+
binId: binId.toNumber(),
|
|
12265
|
+
price: adjustedPrice.toString(),
|
|
12266
|
+
pricePerToken: adjustedPrice.toString(),
|
|
12267
|
+
amountX: new (0, _bnjs2.default)(0),
|
|
12268
|
+
amountY: new (0, _bnjs2.default)(0),
|
|
12269
|
+
claimableRewardAmount: [new (0, _bnjs2.default)(0), new (0, _bnjs2.default)(0)],
|
|
12270
|
+
claimableFeeXAmount: new (0, _bnjs2.default)(0),
|
|
12271
|
+
claimableFeeYAmount: new (0, _bnjs2.default)(0)
|
|
12272
|
+
});
|
|
12273
|
+
}
|
|
12274
|
+
} else {
|
|
12275
|
+
const binCountToShrink = this.upperBinId.sub(newMaxBinId);
|
|
12276
|
+
for (let i = 1; i <= binCountToShrink.toNumber(); i++) {
|
|
12277
|
+
this.rebalancePositionBinData.pop();
|
|
12278
|
+
}
|
|
12279
|
+
}
|
|
12280
|
+
this.lowerBinId = newMinBinId;
|
|
12281
|
+
this.upperBinId = newMaxBinId;
|
|
12518
12282
|
}
|
|
12519
|
-
|
|
12520
|
-
|
|
12283
|
+
_simulateWithdraw(withdraws) {
|
|
12284
|
+
let liquidityAndFeeXWithdrawn = new (0, _bnjs2.default)(0);
|
|
12285
|
+
let liquidityAndFeeYWithdrawn = new (0, _bnjs2.default)(0);
|
|
12286
|
+
let rewardsAmountClaimed = [new (0, _bnjs2.default)(0), new (0, _bnjs2.default)(0)];
|
|
12521
12287
|
const activeId = new (0, _bnjs2.default)(this.lbPair.activeId);
|
|
12522
|
-
const
|
|
12523
|
-
|
|
12524
|
-
const
|
|
12525
|
-
const
|
|
12526
|
-
|
|
12527
|
-
new (0, _bnjs2.default)(depositMinBinId),
|
|
12528
|
-
new (0, _bnjs2.default)(depositMaxBinId),
|
|
12529
|
-
binStep,
|
|
12530
|
-
tokenXDecimal,
|
|
12531
|
-
tokenYDecimal
|
|
12532
|
-
);
|
|
12533
|
-
}
|
|
12534
|
-
let totalAmountXDeposited = new (0, _bnjs2.default)(0);
|
|
12535
|
-
let totalAmountYDeposited = new (0, _bnjs2.default)(0);
|
|
12536
|
-
const addLiquidityParam = [];
|
|
12537
|
-
for (const {
|
|
12538
|
-
x0,
|
|
12539
|
-
y0,
|
|
12540
|
-
favorXInActiveBin,
|
|
12541
|
-
deltaX,
|
|
12542
|
-
deltaY,
|
|
12543
|
-
minDeltaId,
|
|
12544
|
-
maxDeltaId
|
|
12545
|
-
} of deposits) {
|
|
12546
|
-
const params = buildBitFlagAndNegateStrategyParameters(
|
|
12547
|
-
x0,
|
|
12548
|
-
y0,
|
|
12549
|
-
deltaX,
|
|
12550
|
-
deltaY
|
|
12551
|
-
);
|
|
12552
|
-
addLiquidityParam.push({
|
|
12553
|
-
minDeltaId: minDeltaId.toNumber(),
|
|
12554
|
-
maxDeltaId: maxDeltaId.toNumber(),
|
|
12555
|
-
x0: params.x0,
|
|
12556
|
-
y0: params.y0,
|
|
12557
|
-
deltaX: params.deltaX,
|
|
12558
|
-
deltaY: params.deltaY,
|
|
12559
|
-
bitFlag: params.bitFlag,
|
|
12560
|
-
padding: Array(16).fill(0),
|
|
12561
|
-
favorXInActiveId: favorXInActiveBin
|
|
12562
|
-
});
|
|
12563
|
-
const amountIntoBins = toAmountIntoBins(
|
|
12564
|
-
activeId,
|
|
12565
|
-
minDeltaId,
|
|
12566
|
-
maxDeltaId,
|
|
12567
|
-
deltaX,
|
|
12568
|
-
deltaY,
|
|
12569
|
-
x0,
|
|
12570
|
-
y0,
|
|
12571
|
-
binStep,
|
|
12572
|
-
favorXInActiveBin
|
|
12288
|
+
for (const { minBinId, maxBinId, bps } of withdraws) {
|
|
12289
|
+
const fromBinId = _nullishCoalesce(minBinId, () => ( activeId));
|
|
12290
|
+
const toBinId = _nullishCoalesce(maxBinId, () => ( activeId));
|
|
12291
|
+
const binIds = binRangeToBinIdArray(fromBinId, toBinId).filter(
|
|
12292
|
+
(binId) => binId.gte(this.lowerBinId) && binId.lte(this.upperBinId)
|
|
12573
12293
|
);
|
|
12574
|
-
for (const
|
|
12575
|
-
totalAmountXDeposited = totalAmountXDeposited.add(amountX);
|
|
12576
|
-
totalAmountYDeposited = totalAmountYDeposited.add(amountY);
|
|
12294
|
+
for (const binId of binIds) {
|
|
12577
12295
|
const idx = this.rebalancePositionBinData.findIndex(
|
|
12578
|
-
(
|
|
12296
|
+
(b) => b.binId === binId.toNumber()
|
|
12579
12297
|
);
|
|
12580
|
-
|
|
12581
|
-
|
|
12582
|
-
|
|
12583
|
-
|
|
12584
|
-
|
|
12585
|
-
|
|
12586
|
-
|
|
12587
|
-
|
|
12588
|
-
|
|
12589
|
-
|
|
12590
|
-
vParameters3,
|
|
12591
|
-
sParameters3,
|
|
12592
|
-
activeId.toNumber()
|
|
12593
|
-
);
|
|
12594
|
-
const { amountXIntoBin, amountYIntoBin } = simulateDepositBin(
|
|
12595
|
-
binId,
|
|
12596
|
-
binStep,
|
|
12597
|
-
amountX,
|
|
12598
|
-
amountY,
|
|
12599
|
-
this.activeBin
|
|
12600
|
-
);
|
|
12601
|
-
const feeY = computeCompositionFee(
|
|
12602
|
-
binStep,
|
|
12603
|
-
sParameters3,
|
|
12604
|
-
vParameters3,
|
|
12605
|
-
amountXIntoBin,
|
|
12606
|
-
amountX,
|
|
12607
|
-
amountYIntoBin,
|
|
12608
|
-
amountY
|
|
12609
|
-
);
|
|
12610
|
-
const feeX = computeCompositionFee(
|
|
12611
|
-
binStep,
|
|
12612
|
-
sParameters3,
|
|
12613
|
-
vParameters3,
|
|
12614
|
-
amountYIntoBin,
|
|
12615
|
-
amountY,
|
|
12616
|
-
amountXIntoBin,
|
|
12617
|
-
amountX
|
|
12618
|
-
);
|
|
12619
|
-
const amountXIntoBinExcludeFee = amountXIntoBin.sub(feeX);
|
|
12620
|
-
const amountYIntoBinExcludeFee = amountYIntoBin.sub(feeY);
|
|
12621
|
-
this.rebalancePositionBinData[idx].amountX = this.rebalancePositionBinData[idx].amountX.add(
|
|
12622
|
-
amountXIntoBinExcludeFee
|
|
12298
|
+
const binData = this.rebalancePositionBinData[idx];
|
|
12299
|
+
const amountXWithdrawn = binData.amountX.mul(bps).divn(BASIS_POINT_MAX);
|
|
12300
|
+
const amountYWithdrawn = binData.amountY.mul(bps).divn(BASIS_POINT_MAX);
|
|
12301
|
+
liquidityAndFeeXWithdrawn = liquidityAndFeeXWithdrawn.add(amountXWithdrawn);
|
|
12302
|
+
liquidityAndFeeYWithdrawn = liquidityAndFeeYWithdrawn.add(amountYWithdrawn);
|
|
12303
|
+
binData.amountX = binData.amountX.sub(amountXWithdrawn);
|
|
12304
|
+
binData.amountY = binData.amountY.sub(amountYWithdrawn);
|
|
12305
|
+
if (this.shouldClaimFee) {
|
|
12306
|
+
liquidityAndFeeXWithdrawn = liquidityAndFeeXWithdrawn.add(
|
|
12307
|
+
binData.claimableFeeXAmount
|
|
12623
12308
|
);
|
|
12624
|
-
|
|
12625
|
-
|
|
12309
|
+
liquidityAndFeeYWithdrawn = liquidityAndFeeYWithdrawn.add(
|
|
12310
|
+
binData.claimableFeeYAmount
|
|
12626
12311
|
);
|
|
12627
|
-
|
|
12628
|
-
|
|
12629
|
-
|
|
12312
|
+
binData.claimableFeeXAmount = new (0, _bnjs2.default)(0);
|
|
12313
|
+
binData.claimableFeeYAmount = new (0, _bnjs2.default)(0);
|
|
12314
|
+
}
|
|
12315
|
+
if (this.shouldClaimReward) {
|
|
12316
|
+
for (const [idx2, amount] of binData.claimableRewardAmount.entries()) {
|
|
12317
|
+
rewardsAmountClaimed[idx2] = rewardsAmountClaimed[idx2].add(amount);
|
|
12318
|
+
binData.claimableRewardAmount[idx2] = new (0, _bnjs2.default)(0);
|
|
12319
|
+
}
|
|
12630
12320
|
}
|
|
12321
|
+
this.rebalancePositionBinData[idx] = binData;
|
|
12322
|
+
}
|
|
12323
|
+
}
|
|
12324
|
+
const withdrawParams = withdraws.map(
|
|
12325
|
+
({ minBinId, maxBinId, bps }) => {
|
|
12326
|
+
return {
|
|
12327
|
+
minBinId: minBinId ? minBinId.toNumber() : null,
|
|
12328
|
+
maxBinId: maxBinId ? maxBinId.toNumber() : null,
|
|
12329
|
+
bps: bps.toNumber(),
|
|
12330
|
+
padding: Array(16).fill(0)
|
|
12331
|
+
};
|
|
12332
|
+
}
|
|
12333
|
+
);
|
|
12334
|
+
return {
|
|
12335
|
+
result: {
|
|
12336
|
+
liquidityAndFeeXWithdrawn,
|
|
12337
|
+
liquidityAndFeeYWithdrawn,
|
|
12338
|
+
rewardsAmountClaimed
|
|
12339
|
+
},
|
|
12340
|
+
withdrawParams
|
|
12341
|
+
};
|
|
12342
|
+
}
|
|
12343
|
+
async simulateRebalance(connection, binStep, tokenXDecimal, tokenYDecimal, withdraws, deposits) {
|
|
12344
|
+
if (withdraws.length == 0 && deposits.length == 0) {
|
|
12345
|
+
throw "No rebalance action";
|
|
12346
|
+
}
|
|
12347
|
+
const activeId = new (0, _bnjs2.default)(this.lbPair.activeId);
|
|
12348
|
+
withdraws = validateAndSortRebalanceWithdraw(withdraws, activeId);
|
|
12349
|
+
deposits = validateAndSortRebalanceDeposit(deposits);
|
|
12350
|
+
const beforeWidth = getPositionWidthWithMinWidth(
|
|
12351
|
+
this.lowerBinId.toNumber(),
|
|
12352
|
+
this.upperBinId.toNumber()
|
|
12353
|
+
);
|
|
12354
|
+
const { withdrawParams, result: withdrawResult } = this._simulateWithdraw(withdraws);
|
|
12355
|
+
const { depositParams, result: depositResult } = this._simulateDeposit(
|
|
12356
|
+
binStep,
|
|
12357
|
+
tokenXDecimal,
|
|
12358
|
+
tokenYDecimal,
|
|
12359
|
+
deposits,
|
|
12360
|
+
withdrawResult
|
|
12361
|
+
);
|
|
12362
|
+
const afterWidth = getPositionWidthWithMinWidth(
|
|
12363
|
+
this.lowerBinId.toNumber(),
|
|
12364
|
+
this.upperBinId.toNumber()
|
|
12365
|
+
);
|
|
12366
|
+
const widthDelta = afterWidth - beforeWidth;
|
|
12367
|
+
let rentalCostLamports = new (0, _bnjs2.default)(0);
|
|
12368
|
+
if (widthDelta != 0) {
|
|
12369
|
+
const sizeChanges = Math.abs(widthDelta) * POSITION_BIN_DATA_SIZE;
|
|
12370
|
+
const [minimumLamports, rentExemptionLamports] = await Promise.all([
|
|
12371
|
+
connection.getMinimumBalanceForRentExemption(0),
|
|
12372
|
+
connection.getMinimumBalanceForRentExemption(sizeChanges)
|
|
12373
|
+
]);
|
|
12374
|
+
const lamportChanges = new (0, _bnjs2.default)(rentExemptionLamports).sub(
|
|
12375
|
+
new (0, _bnjs2.default)(minimumLamports)
|
|
12376
|
+
);
|
|
12377
|
+
if (widthDelta > 0) {
|
|
12378
|
+
rentalCostLamports = rentalCostLamports.add(lamportChanges);
|
|
12379
|
+
} else {
|
|
12380
|
+
rentalCostLamports = rentalCostLamports.sub(lamportChanges);
|
|
12381
|
+
}
|
|
12382
|
+
}
|
|
12383
|
+
return {
|
|
12384
|
+
amountXDeposited: depositResult.totalAmountXDeposited,
|
|
12385
|
+
amountYDeposited: depositResult.totalAmountYDeposited,
|
|
12386
|
+
actualAmountXDeposited: depositResult.actualTotalAmountXDeposited,
|
|
12387
|
+
actualAmountYDeposited: depositResult.actualTotalAmountYDeposited,
|
|
12388
|
+
actualAmountXWithdrawn: depositResult.actualLiquidityAndFeeXWithdrawn,
|
|
12389
|
+
actualAmountYWithdrawn: depositResult.actualLiquidityAndFeeYWithdrawn,
|
|
12390
|
+
rewardAmountsClaimed: withdrawResult.rewardsAmountClaimed,
|
|
12391
|
+
withdrawParams,
|
|
12392
|
+
depositParams,
|
|
12393
|
+
rentalCostLamports
|
|
12394
|
+
};
|
|
12395
|
+
}
|
|
12396
|
+
totalAmounts() {
|
|
12397
|
+
let totalAmountX = new (0, _bnjs2.default)(0);
|
|
12398
|
+
let totalAmountY = new (0, _bnjs2.default)(0);
|
|
12399
|
+
for (const binData of this.rebalancePositionBinData) {
|
|
12400
|
+
totalAmountX = totalAmountX.add(binData.amountX);
|
|
12401
|
+
totalAmountY = totalAmountY.add(binData.amountY);
|
|
12402
|
+
}
|
|
12403
|
+
return [totalAmountX, totalAmountY];
|
|
12404
|
+
}
|
|
12405
|
+
totalFeeAmounts() {
|
|
12406
|
+
let totalFeeXAmount = new (0, _bnjs2.default)(0);
|
|
12407
|
+
let totalFeeYAmount = new (0, _bnjs2.default)(0);
|
|
12408
|
+
for (const binData of this.rebalancePositionBinData) {
|
|
12409
|
+
totalFeeXAmount = totalFeeXAmount.add(binData.claimableFeeXAmount);
|
|
12410
|
+
totalFeeYAmount = totalFeeYAmount.add(binData.claimableFeeYAmount);
|
|
12411
|
+
}
|
|
12412
|
+
return [totalFeeXAmount, totalFeeYAmount];
|
|
12413
|
+
}
|
|
12414
|
+
totalRewardAmounts() {
|
|
12415
|
+
let totalRewardAmounts = [new (0, _bnjs2.default)(0), new (0, _bnjs2.default)(0)];
|
|
12416
|
+
for (const binData of this.rebalancePositionBinData) {
|
|
12417
|
+
totalRewardAmounts[0] = totalRewardAmounts[0].add(
|
|
12418
|
+
binData.claimableRewardAmount[0]
|
|
12419
|
+
);
|
|
12420
|
+
totalRewardAmounts[1] = totalRewardAmounts[1].add(
|
|
12421
|
+
binData.claimableRewardAmount[1]
|
|
12422
|
+
);
|
|
12423
|
+
}
|
|
12424
|
+
return totalRewardAmounts;
|
|
12425
|
+
}
|
|
12426
|
+
};
|
|
12427
|
+
function getPositionWidthWithMinWidth(lowerBinId, upperBinId) {
|
|
12428
|
+
const width = upperBinId - lowerBinId + 1;
|
|
12429
|
+
return Math.max(width, DEFAULT_BIN_PER_POSITION.toNumber());
|
|
12430
|
+
}
|
|
12431
|
+
function validateAndSortRebalanceDeposit(deposits) {
|
|
12432
|
+
const sortedDeposits = deposits.sort(
|
|
12433
|
+
(a, b) => a.minDeltaId.sub(b.minDeltaId).toNumber()
|
|
12434
|
+
);
|
|
12435
|
+
for (const deposit of deposits) {
|
|
12436
|
+
if (deposit.minDeltaId.gte(deposit.maxDeltaId)) {
|
|
12437
|
+
throw "Invalid minDeltaId or maxDeltaId";
|
|
12438
|
+
}
|
|
12439
|
+
}
|
|
12440
|
+
for (let i = 1; i < sortedDeposits.length; i++) {
|
|
12441
|
+
const prevDeposit = sortedDeposits[i - 1];
|
|
12442
|
+
const currDeposit = sortedDeposits[i];
|
|
12443
|
+
if (prevDeposit.maxDeltaId.gte(currDeposit.minDeltaId)) {
|
|
12444
|
+
throw "Overlap deposit bin range";
|
|
12445
|
+
}
|
|
12446
|
+
}
|
|
12447
|
+
return sortedDeposits;
|
|
12448
|
+
}
|
|
12449
|
+
function validateAndSortRebalanceWithdraw(withdraws, activeId) {
|
|
12450
|
+
const filledWithdraws = [];
|
|
12451
|
+
for (const { minBinId, maxBinId, bps } of withdraws) {
|
|
12452
|
+
if (bps.toNumber() < 0 || bps.toNumber() > BASIS_POINT_MAX) {
|
|
12453
|
+
throw "Invalid bps";
|
|
12454
|
+
}
|
|
12455
|
+
const filledMinBinId = _nullishCoalesce(minBinId, () => ( activeId));
|
|
12456
|
+
const filledMaxBinId = _nullishCoalesce(maxBinId, () => ( activeId));
|
|
12457
|
+
if (filledMinBinId.gt(filledMaxBinId)) {
|
|
12458
|
+
throw "Invalid minBinId or maxBinId";
|
|
12459
|
+
}
|
|
12460
|
+
filledWithdraws.push({
|
|
12461
|
+
minBinId: filledMinBinId,
|
|
12462
|
+
maxBinId: filledMaxBinId,
|
|
12463
|
+
bps
|
|
12464
|
+
});
|
|
12465
|
+
}
|
|
12466
|
+
filledWithdraws.sort((a, b) => {
|
|
12467
|
+
return a.minBinId.sub(b.minBinId).toNumber();
|
|
12468
|
+
});
|
|
12469
|
+
for (let i = 1; i < filledWithdraws.length; i++) {
|
|
12470
|
+
const prev = filledWithdraws[i - 1];
|
|
12471
|
+
const curr = filledWithdraws[i];
|
|
12472
|
+
if (curr.minBinId.lte(prev.maxBinId)) {
|
|
12473
|
+
throw "Overlap withdraw bin range";
|
|
12474
|
+
}
|
|
12475
|
+
}
|
|
12476
|
+
return filledWithdraws;
|
|
12477
|
+
}
|
|
12478
|
+
function binRangeToBinIdArray(minBinId, maxBinId) {
|
|
12479
|
+
const binIdArray = [];
|
|
12480
|
+
const fromBinId = minBinId.toNumber();
|
|
12481
|
+
const toBinId = maxBinId.toNumber();
|
|
12482
|
+
for (let binId = fromBinId; binId <= toBinId; binId++) {
|
|
12483
|
+
binIdArray.push(new (0, _bnjs2.default)(binId));
|
|
12484
|
+
}
|
|
12485
|
+
return binIdArray;
|
|
12486
|
+
}
|
|
12487
|
+
function getRebalanceBinArrayIndexesAndBitmapCoverage(adds, removes, activeId, pairAddress, programId) {
|
|
12488
|
+
let indexMap = /* @__PURE__ */ new Map();
|
|
12489
|
+
removes.forEach((value) => {
|
|
12490
|
+
let minBinId = value.minBinId;
|
|
12491
|
+
if (minBinId == null) {
|
|
12492
|
+
minBinId = activeId;
|
|
12493
|
+
}
|
|
12494
|
+
let maxBinId = value.maxBinId;
|
|
12495
|
+
if (maxBinId == null) {
|
|
12496
|
+
maxBinId = activeId;
|
|
12497
|
+
}
|
|
12498
|
+
let binArrayIndex = binIdToBinArrayIndex(new (0, _bnjs2.default)(minBinId));
|
|
12499
|
+
const upperBinId = new (0, _bnjs2.default)(maxBinId);
|
|
12500
|
+
while (true) {
|
|
12501
|
+
indexMap.set(binArrayIndex.toNumber(), true);
|
|
12502
|
+
const [binArrayLowerBinId, binArrayUpperBinId] = getBinArrayLowerUpperBinId(binArrayIndex);
|
|
12503
|
+
if (upperBinId.gte(binArrayLowerBinId) && upperBinId.lte(binArrayUpperBinId)) {
|
|
12504
|
+
break;
|
|
12505
|
+
} else {
|
|
12506
|
+
binArrayIndex = binArrayIndex.add(new (0, _bnjs2.default)(1));
|
|
12631
12507
|
}
|
|
12632
12508
|
}
|
|
12633
|
-
|
|
12634
|
-
|
|
12635
|
-
|
|
12636
|
-
|
|
12637
|
-
|
|
12638
|
-
|
|
12639
|
-
|
|
12640
|
-
);
|
|
12641
|
-
|
|
12509
|
+
});
|
|
12510
|
+
adds.forEach((value) => {
|
|
12511
|
+
const minBinId = activeId + value.minDeltaId;
|
|
12512
|
+
const maxBinId = activeId + value.maxDeltaId;
|
|
12513
|
+
let binArrayIndex = binIdToBinArrayIndex(new (0, _bnjs2.default)(minBinId));
|
|
12514
|
+
const upperBinId = new (0, _bnjs2.default)(maxBinId);
|
|
12515
|
+
while (true) {
|
|
12516
|
+
indexMap.set(binArrayIndex.toNumber(), true);
|
|
12517
|
+
const [binArrayLowerBinId, binArrayUpperBinId] = getBinArrayLowerUpperBinId(binArrayIndex);
|
|
12518
|
+
if (upperBinId.gte(binArrayLowerBinId) && upperBinId.lte(binArrayUpperBinId)) {
|
|
12519
|
+
break;
|
|
12520
|
+
} else {
|
|
12521
|
+
binArrayIndex = binArrayIndex.add(new (0, _bnjs2.default)(1));
|
|
12522
|
+
}
|
|
12523
|
+
}
|
|
12524
|
+
});
|
|
12525
|
+
const binArrayIndexes = Array.from(indexMap.keys()).map((idx) => new (0, _bnjs2.default)(idx));
|
|
12526
|
+
const requireBitmapExtension = binArrayIndexes.some(
|
|
12527
|
+
(index) => isOverflowDefaultBinArrayBitmap(new (0, _bnjs2.default)(index))
|
|
12528
|
+
);
|
|
12529
|
+
return {
|
|
12530
|
+
binArrayIndexes,
|
|
12531
|
+
binArrayBitmap: requireBitmapExtension ? deriveBinArrayBitmapExtension(pairAddress, programId)[0] : programId
|
|
12532
|
+
};
|
|
12533
|
+
}
|
|
12534
|
+
|
|
12535
|
+
// src/dlmm/helpers/rebalance/liquidity_strategy/index.ts
|
|
12536
|
+
|
|
12537
|
+
|
|
12538
|
+
// src/dlmm/helpers/rebalance/liquidity_strategy/bidAsk.ts
|
|
12539
|
+
|
|
12540
|
+
function findBaseDeltaY(amountY, minDeltaId, maxDeltaId) {
|
|
12541
|
+
if (minDeltaId.gt(maxDeltaId) || amountY.lte(new (0, _bnjs2.default)(0))) {
|
|
12542
|
+
return new (0, _bnjs2.default)(0);
|
|
12543
|
+
}
|
|
12544
|
+
if (minDeltaId.eq(maxDeltaId)) {
|
|
12545
|
+
return amountY;
|
|
12546
|
+
}
|
|
12547
|
+
const m1 = minDeltaId.neg().subn(1);
|
|
12548
|
+
const m2 = maxDeltaId.neg();
|
|
12549
|
+
const b = m2.neg().mul(m1.sub(m2).addn(1));
|
|
12550
|
+
const c = m1.mul(m1.addn(1)).divn(2);
|
|
12551
|
+
const d = m2.mul(m2.subn(1)).divn(2);
|
|
12552
|
+
const a = b.add(c.sub(d));
|
|
12553
|
+
return amountY.div(a);
|
|
12554
|
+
}
|
|
12555
|
+
function findY0AndDeltaY(amountY, minDeltaId, maxDeltaId, activeId) {
|
|
12556
|
+
if (minDeltaId.gt(maxDeltaId) || amountY.isZero()) {
|
|
12557
|
+
return {
|
|
12558
|
+
base: new (0, _bnjs2.default)(0),
|
|
12559
|
+
delta: new (0, _bnjs2.default)(0)
|
|
12560
|
+
};
|
|
12561
|
+
}
|
|
12562
|
+
let baseDeltaY = findBaseDeltaY(amountY, minDeltaId, maxDeltaId);
|
|
12563
|
+
const y0 = baseDeltaY.neg().mul(maxDeltaId).add(baseDeltaY);
|
|
12564
|
+
while (true) {
|
|
12565
|
+
const amountInBins = getAmountInBinsBidSide(
|
|
12566
|
+
activeId,
|
|
12567
|
+
minDeltaId,
|
|
12568
|
+
maxDeltaId,
|
|
12569
|
+
baseDeltaY,
|
|
12570
|
+
y0
|
|
12571
|
+
);
|
|
12572
|
+
const totalAmountY = amountInBins.reduce((acc, { amountY: amountY2 }) => {
|
|
12573
|
+
return acc.add(amountY2);
|
|
12574
|
+
}, new (0, _bnjs2.default)(0));
|
|
12575
|
+
if (totalAmountY.gt(amountY)) {
|
|
12576
|
+
baseDeltaY = baseDeltaY.sub(new (0, _bnjs2.default)(1));
|
|
12642
12577
|
} else {
|
|
12643
|
-
|
|
12644
|
-
|
|
12645
|
-
|
|
12646
|
-
|
|
12578
|
+
return {
|
|
12579
|
+
base: y0,
|
|
12580
|
+
delta: baseDeltaY
|
|
12581
|
+
};
|
|
12647
12582
|
}
|
|
12648
|
-
|
|
12649
|
-
|
|
12650
|
-
|
|
12651
|
-
|
|
12652
|
-
|
|
12583
|
+
}
|
|
12584
|
+
}
|
|
12585
|
+
function findBaseDeltaX(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
|
|
12586
|
+
if (minDeltaId.gt(maxDeltaId) || amountX.lte(new (0, _bnjs2.default)(0))) {
|
|
12587
|
+
return new (0, _bnjs2.default)(0);
|
|
12588
|
+
}
|
|
12589
|
+
let b = new (0, _bnjs2.default)(0);
|
|
12590
|
+
let c = new (0, _bnjs2.default)(0);
|
|
12591
|
+
let m1 = minDeltaId;
|
|
12592
|
+
let m2 = maxDeltaId.addn(1);
|
|
12593
|
+
for (let m = m1.toNumber(); m <= m2.toNumber(); m++) {
|
|
12594
|
+
const binId = activeId.addn(m);
|
|
12595
|
+
const pm = getQPriceFromId(binId.neg(), binStep);
|
|
12596
|
+
const bDelta = m1.mul(pm);
|
|
12597
|
+
b = b.add(bDelta);
|
|
12598
|
+
const cDelta = new (0, _bnjs2.default)(m).mul(pm);
|
|
12599
|
+
c = c.add(cDelta);
|
|
12600
|
+
}
|
|
12601
|
+
return amountX.shln(SCALE_OFFSET).div(c.sub(b));
|
|
12602
|
+
}
|
|
12603
|
+
function findX0AndDeltaX(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
|
|
12604
|
+
if (minDeltaId.gt(maxDeltaId) || amountX.lte(new (0, _bnjs2.default)(0)) || amountX.isZero()) {
|
|
12605
|
+
return {
|
|
12606
|
+
base: new (0, _bnjs2.default)(0),
|
|
12607
|
+
delta: new (0, _bnjs2.default)(0)
|
|
12608
|
+
};
|
|
12609
|
+
}
|
|
12610
|
+
let baseDeltaX = findBaseDeltaX(
|
|
12611
|
+
amountX,
|
|
12612
|
+
minDeltaId,
|
|
12613
|
+
maxDeltaId,
|
|
12614
|
+
binStep,
|
|
12615
|
+
activeId
|
|
12616
|
+
);
|
|
12617
|
+
const x0 = minDeltaId.neg().mul(baseDeltaX).add(baseDeltaX);
|
|
12618
|
+
while (true) {
|
|
12619
|
+
const amountInBins = getAmountInBinsAskSide(
|
|
12620
|
+
activeId,
|
|
12621
|
+
binStep,
|
|
12622
|
+
minDeltaId,
|
|
12623
|
+
maxDeltaId,
|
|
12624
|
+
baseDeltaX,
|
|
12625
|
+
x0
|
|
12626
|
+
);
|
|
12627
|
+
const totalAmountX = amountInBins.reduce((acc, { amountX: amountX2 }) => {
|
|
12628
|
+
return acc.add(amountX2);
|
|
12629
|
+
}, new (0, _bnjs2.default)(0));
|
|
12630
|
+
if (totalAmountX.gt(amountX)) {
|
|
12631
|
+
baseDeltaX = baseDeltaX.sub(new (0, _bnjs2.default)(1));
|
|
12653
12632
|
} else {
|
|
12654
|
-
|
|
12655
|
-
|
|
12656
|
-
|
|
12657
|
-
|
|
12633
|
+
return {
|
|
12634
|
+
base: x0,
|
|
12635
|
+
delta: baseDeltaX
|
|
12636
|
+
};
|
|
12658
12637
|
}
|
|
12638
|
+
}
|
|
12639
|
+
}
|
|
12640
|
+
var BidAskStrategyParameterBuilder = class {
|
|
12641
|
+
findXParameters(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
|
|
12642
|
+
return findX0AndDeltaX(amountX, minDeltaId, maxDeltaId, binStep, activeId);
|
|
12643
|
+
}
|
|
12644
|
+
findYParameters(amountY, minDeltaId, maxDeltaId, activeId) {
|
|
12645
|
+
return findY0AndDeltaY(amountY, minDeltaId, maxDeltaId, activeId);
|
|
12646
|
+
}
|
|
12647
|
+
suggestBalancedXParametersFromY(activeId, binStep, favorXInActiveBin, minDeltaId, maxDeltaId, amountY) {
|
|
12648
|
+
const deltaX = amountY.div(
|
|
12649
|
+
maxDeltaId.addn(1).mul(maxDeltaId.addn(2)).divn(2)
|
|
12650
|
+
);
|
|
12651
|
+
const x0 = minDeltaId.neg().mul(deltaX).add(deltaX);
|
|
12652
|
+
const totalAmountX = toAmountIntoBins(
|
|
12653
|
+
activeId,
|
|
12654
|
+
minDeltaId,
|
|
12655
|
+
maxDeltaId,
|
|
12656
|
+
deltaX,
|
|
12657
|
+
new (0, _bnjs2.default)(0),
|
|
12658
|
+
x0,
|
|
12659
|
+
new (0, _bnjs2.default)(0),
|
|
12660
|
+
binStep,
|
|
12661
|
+
favorXInActiveBin
|
|
12662
|
+
).reduce((acc, bin) => {
|
|
12663
|
+
return acc.add(bin.amountX);
|
|
12664
|
+
}, new (0, _bnjs2.default)(0));
|
|
12665
|
+
return {
|
|
12666
|
+
base: x0,
|
|
12667
|
+
delta: deltaX,
|
|
12668
|
+
amountX: totalAmountX
|
|
12669
|
+
};
|
|
12670
|
+
}
|
|
12671
|
+
suggestBalancedYParametersFromX(activeId, binStep, favorXInActiveBin, minDeltaId, maxDeltaId, amountXInQuoteValue) {
|
|
12672
|
+
const m1 = minDeltaId.neg().subn(1);
|
|
12673
|
+
const m2 = maxDeltaId.neg();
|
|
12674
|
+
const a1 = m2.neg().mul(m1.sub(m2).addn(1));
|
|
12675
|
+
const a2 = m1.mul(m1.addn(1)).divn(2);
|
|
12676
|
+
const a3 = m2.mul(m2.subn(1)).divn(2);
|
|
12677
|
+
const a = a1.add(a2.sub(a3));
|
|
12678
|
+
const deltaY = amountXInQuoteValue.div(a);
|
|
12679
|
+
const y0 = deltaY.neg().mul(m2).add(deltaY);
|
|
12680
|
+
const amountY = toAmountIntoBins(
|
|
12681
|
+
activeId,
|
|
12682
|
+
minDeltaId,
|
|
12683
|
+
maxDeltaId,
|
|
12684
|
+
new (0, _bnjs2.default)(0),
|
|
12685
|
+
deltaY,
|
|
12686
|
+
new (0, _bnjs2.default)(0),
|
|
12687
|
+
y0,
|
|
12688
|
+
binStep,
|
|
12689
|
+
favorXInActiveBin
|
|
12690
|
+
).reduce((acc, bin) => {
|
|
12691
|
+
return acc.add(bin.amountY);
|
|
12692
|
+
}, new (0, _bnjs2.default)(0));
|
|
12659
12693
|
return {
|
|
12660
|
-
|
|
12661
|
-
|
|
12662
|
-
|
|
12663
|
-
actualLiquidityAndFeeXWithdrawn,
|
|
12664
|
-
actualLiquidityAndFeeYWithdrawn,
|
|
12665
|
-
actualTotalAmountXDeposited,
|
|
12666
|
-
actualTotalAmountYDeposited
|
|
12667
|
-
},
|
|
12668
|
-
depositParams: addLiquidityParam
|
|
12694
|
+
base: y0,
|
|
12695
|
+
delta: deltaY,
|
|
12696
|
+
amountY
|
|
12669
12697
|
};
|
|
12670
12698
|
}
|
|
12671
|
-
|
|
12672
|
-
|
|
12673
|
-
|
|
12674
|
-
|
|
12675
|
-
|
|
12676
|
-
|
|
12677
|
-
|
|
12678
|
-
|
|
12679
|
-
|
|
12680
|
-
|
|
12681
|
-
|
|
12699
|
+
};
|
|
12700
|
+
|
|
12701
|
+
// src/dlmm/helpers/rebalance/liquidity_strategy/curve.ts
|
|
12702
|
+
|
|
12703
|
+
function findBaseY0(amountY, minDeltaId, maxDeltaId) {
|
|
12704
|
+
if (minDeltaId.gt(maxDeltaId) || amountY.lte(new (0, _bnjs2.default)(0))) {
|
|
12705
|
+
return new (0, _bnjs2.default)(0);
|
|
12706
|
+
}
|
|
12707
|
+
if (minDeltaId.eq(maxDeltaId)) {
|
|
12708
|
+
return amountY;
|
|
12709
|
+
}
|
|
12710
|
+
const m1 = minDeltaId.neg();
|
|
12711
|
+
const m2 = maxDeltaId.neg();
|
|
12712
|
+
const b = m1.sub(m2).addn(1);
|
|
12713
|
+
const c = m1.mul(m1.addn(1)).divn(2);
|
|
12714
|
+
const d = m2.mul(m2.subn(1)).divn(2);
|
|
12715
|
+
const a = b.sub(c.sub(d).div(m1.addn(1)));
|
|
12716
|
+
return amountY.div(a);
|
|
12717
|
+
}
|
|
12718
|
+
function findY0AndDeltaY2(amountY, minDeltaId, maxDeltaId, activeId) {
|
|
12719
|
+
if (minDeltaId.gt(maxDeltaId) || amountY.isZero()) {
|
|
12720
|
+
return {
|
|
12721
|
+
base: new (0, _bnjs2.default)(0),
|
|
12722
|
+
delta: new (0, _bnjs2.default)(0)
|
|
12723
|
+
};
|
|
12724
|
+
}
|
|
12725
|
+
let baseY0 = findBaseY0(amountY, minDeltaId, maxDeltaId);
|
|
12726
|
+
while (true) {
|
|
12727
|
+
const deltaY = baseY0.neg().div(minDeltaId.neg().addn(1));
|
|
12728
|
+
const amountInBins = getAmountInBinsBidSide(
|
|
12729
|
+
activeId,
|
|
12730
|
+
minDeltaId,
|
|
12731
|
+
maxDeltaId,
|
|
12732
|
+
deltaY,
|
|
12733
|
+
baseY0
|
|
12682
12734
|
);
|
|
12683
|
-
|
|
12684
|
-
|
|
12685
|
-
|
|
12686
|
-
|
|
12687
|
-
|
|
12688
|
-
binId.toNumber(),
|
|
12689
|
-
binStep.toNumber()
|
|
12690
|
-
);
|
|
12691
|
-
const adjustedPrice = price.mul(tokenXMultiplier).div(tokenYMultiplier);
|
|
12692
|
-
this.rebalancePositionBinData.unshift({
|
|
12693
|
-
binId: binId.toNumber(),
|
|
12694
|
-
price: adjustedPrice.toString(),
|
|
12695
|
-
pricePerToken: adjustedPrice.toString(),
|
|
12696
|
-
amountX: new (0, _bnjs2.default)(0),
|
|
12697
|
-
amountY: new (0, _bnjs2.default)(0),
|
|
12698
|
-
claimableRewardAmount: [new (0, _bnjs2.default)(0), new (0, _bnjs2.default)(0)],
|
|
12699
|
-
claimableFeeXAmount: new (0, _bnjs2.default)(0),
|
|
12700
|
-
claimableFeeYAmount: new (0, _bnjs2.default)(0)
|
|
12701
|
-
});
|
|
12702
|
-
}
|
|
12735
|
+
const totalAmountY = amountInBins.reduce((acc, { amountY: amountY2 }) => {
|
|
12736
|
+
return acc.add(amountY2);
|
|
12737
|
+
}, new (0, _bnjs2.default)(0));
|
|
12738
|
+
if (totalAmountY.gt(amountY)) {
|
|
12739
|
+
baseY0 = baseY0.sub(new (0, _bnjs2.default)(1));
|
|
12703
12740
|
} else {
|
|
12704
|
-
|
|
12705
|
-
|
|
12706
|
-
|
|
12707
|
-
}
|
|
12741
|
+
return {
|
|
12742
|
+
base: baseY0,
|
|
12743
|
+
delta: deltaY
|
|
12744
|
+
};
|
|
12708
12745
|
}
|
|
12709
|
-
|
|
12710
|
-
|
|
12711
|
-
|
|
12712
|
-
|
|
12713
|
-
|
|
12714
|
-
|
|
12715
|
-
|
|
12716
|
-
|
|
12717
|
-
|
|
12718
|
-
|
|
12719
|
-
|
|
12720
|
-
|
|
12721
|
-
|
|
12722
|
-
|
|
12723
|
-
|
|
12724
|
-
|
|
12725
|
-
|
|
12726
|
-
|
|
12727
|
-
|
|
12728
|
-
|
|
12746
|
+
}
|
|
12747
|
+
}
|
|
12748
|
+
function findBaseX0(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
|
|
12749
|
+
if (minDeltaId.gt(maxDeltaId) || amountX.lte(new (0, _bnjs2.default)(0))) {
|
|
12750
|
+
return new (0, _bnjs2.default)(0);
|
|
12751
|
+
}
|
|
12752
|
+
let b = new (0, _bnjs2.default)(0);
|
|
12753
|
+
let c = new (0, _bnjs2.default)(0);
|
|
12754
|
+
let m1 = minDeltaId;
|
|
12755
|
+
let m2 = maxDeltaId;
|
|
12756
|
+
for (let m = m1.toNumber(); m <= m2.toNumber(); m++) {
|
|
12757
|
+
const binId = activeId.addn(m);
|
|
12758
|
+
const pm = getQPriceFromId(binId.neg(), binStep);
|
|
12759
|
+
b = b.add(pm);
|
|
12760
|
+
const cDelta = new (0, _bnjs2.default)(m).mul(pm).div(m2);
|
|
12761
|
+
c = c.add(cDelta);
|
|
12762
|
+
}
|
|
12763
|
+
return amountX.shln(SCALE_OFFSET).div(b.sub(c));
|
|
12764
|
+
}
|
|
12765
|
+
function findX0AndDeltaX2(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
|
|
12766
|
+
if (minDeltaId.gt(maxDeltaId) || amountX.lte(new (0, _bnjs2.default)(0)) || amountX.isZero()) {
|
|
12767
|
+
return {
|
|
12768
|
+
base: new (0, _bnjs2.default)(0),
|
|
12769
|
+
delta: new (0, _bnjs2.default)(0)
|
|
12770
|
+
};
|
|
12771
|
+
}
|
|
12772
|
+
let baseX0 = findBaseX0(amountX, minDeltaId, maxDeltaId, binStep, activeId);
|
|
12773
|
+
const deltaX = baseX0.neg().div(maxDeltaId);
|
|
12774
|
+
while (true) {
|
|
12775
|
+
const amountInBins = getAmountInBinsAskSide(
|
|
12776
|
+
activeId,
|
|
12777
|
+
binStep,
|
|
12778
|
+
minDeltaId,
|
|
12779
|
+
maxDeltaId,
|
|
12780
|
+
deltaX,
|
|
12781
|
+
baseX0
|
|
12782
|
+
);
|
|
12783
|
+
const totalAmountX = amountInBins.reduce((acc, { amountX: amountX2 }) => {
|
|
12784
|
+
return acc.add(amountX2);
|
|
12785
|
+
}, new (0, _bnjs2.default)(0));
|
|
12786
|
+
if (totalAmountX.gt(amountX)) {
|
|
12787
|
+
baseX0 = baseX0.sub(new (0, _bnjs2.default)(1));
|
|
12729
12788
|
} else {
|
|
12730
|
-
|
|
12731
|
-
|
|
12732
|
-
|
|
12733
|
-
}
|
|
12789
|
+
return {
|
|
12790
|
+
base: baseX0,
|
|
12791
|
+
delta: deltaX
|
|
12792
|
+
};
|
|
12734
12793
|
}
|
|
12735
|
-
this.lowerBinId = newMinBinId;
|
|
12736
|
-
this.upperBinId = newMaxBinId;
|
|
12737
12794
|
}
|
|
12738
|
-
|
|
12739
|
-
|
|
12740
|
-
|
|
12741
|
-
|
|
12742
|
-
|
|
12743
|
-
|
|
12744
|
-
|
|
12745
|
-
|
|
12746
|
-
|
|
12747
|
-
|
|
12748
|
-
|
|
12749
|
-
|
|
12750
|
-
|
|
12751
|
-
|
|
12752
|
-
|
|
12753
|
-
|
|
12754
|
-
|
|
12755
|
-
|
|
12756
|
-
|
|
12757
|
-
|
|
12758
|
-
|
|
12759
|
-
|
|
12760
|
-
|
|
12761
|
-
|
|
12762
|
-
binData.claimableFeeXAmount
|
|
12763
|
-
);
|
|
12764
|
-
liquidityAndFeeYWithdrawn = liquidityAndFeeYWithdrawn.add(
|
|
12765
|
-
binData.claimableFeeYAmount
|
|
12766
|
-
);
|
|
12767
|
-
binData.claimableFeeXAmount = new (0, _bnjs2.default)(0);
|
|
12768
|
-
binData.claimableFeeYAmount = new (0, _bnjs2.default)(0);
|
|
12769
|
-
}
|
|
12770
|
-
if (this.shouldClaimReward) {
|
|
12771
|
-
for (const [idx2, amount] of binData.claimableRewardAmount.entries()) {
|
|
12772
|
-
rewardsAmountClaimed[idx2] = rewardsAmountClaimed[idx2].add(amount);
|
|
12773
|
-
binData.claimableRewardAmount[idx2] = new (0, _bnjs2.default)(0);
|
|
12774
|
-
}
|
|
12775
|
-
}
|
|
12776
|
-
this.rebalancePositionBinData[idx] = binData;
|
|
12777
|
-
}
|
|
12778
|
-
}
|
|
12779
|
-
const withdrawParams = withdraws.map(
|
|
12780
|
-
({ minBinId, maxBinId, bps }) => {
|
|
12781
|
-
return {
|
|
12782
|
-
minBinId: minBinId ? minBinId.toNumber() : null,
|
|
12783
|
-
maxBinId: maxBinId ? maxBinId.toNumber() : null,
|
|
12784
|
-
bps: bps.toNumber(),
|
|
12785
|
-
padding: Array(16).fill(0)
|
|
12786
|
-
};
|
|
12787
|
-
}
|
|
12788
|
-
);
|
|
12795
|
+
}
|
|
12796
|
+
var CurveStrategyParameterBuilder = class {
|
|
12797
|
+
findXParameters(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
|
|
12798
|
+
return findX0AndDeltaX2(amountX, minDeltaId, maxDeltaId, binStep, activeId);
|
|
12799
|
+
}
|
|
12800
|
+
findYParameters(amountY, minDeltaId, maxDeltaId, activeId) {
|
|
12801
|
+
return findY0AndDeltaY2(amountY, minDeltaId, maxDeltaId, activeId);
|
|
12802
|
+
}
|
|
12803
|
+
suggestBalancedXParametersFromY(activeId, binStep, favorXInActiveBin, minDeltaId, maxDeltaId, amountY) {
|
|
12804
|
+
const x0 = amountY.muln(2).div(maxDeltaId.addn(1));
|
|
12805
|
+
const deltaX = x0.neg().div(maxDeltaId);
|
|
12806
|
+
const totalAmountX = toAmountIntoBins(
|
|
12807
|
+
activeId,
|
|
12808
|
+
minDeltaId,
|
|
12809
|
+
maxDeltaId,
|
|
12810
|
+
deltaX,
|
|
12811
|
+
new (0, _bnjs2.default)(0),
|
|
12812
|
+
x0,
|
|
12813
|
+
new (0, _bnjs2.default)(0),
|
|
12814
|
+
binStep,
|
|
12815
|
+
favorXInActiveBin
|
|
12816
|
+
).reduce((acc, bin) => {
|
|
12817
|
+
return acc.add(bin.amountX);
|
|
12818
|
+
}, new (0, _bnjs2.default)(0));
|
|
12789
12819
|
return {
|
|
12790
|
-
|
|
12791
|
-
|
|
12792
|
-
|
|
12793
|
-
rewardsAmountClaimed
|
|
12794
|
-
},
|
|
12795
|
-
withdrawParams
|
|
12820
|
+
base: x0,
|
|
12821
|
+
delta: deltaX,
|
|
12822
|
+
amountX: totalAmountX
|
|
12796
12823
|
};
|
|
12797
12824
|
}
|
|
12798
|
-
|
|
12799
|
-
|
|
12800
|
-
|
|
12801
|
-
|
|
12802
|
-
const
|
|
12803
|
-
|
|
12804
|
-
|
|
12805
|
-
const
|
|
12806
|
-
|
|
12807
|
-
|
|
12808
|
-
|
|
12809
|
-
|
|
12810
|
-
|
|
12825
|
+
suggestBalancedYParametersFromX(activeId, binStep, favorXInActiveBin, minDeltaId, maxDeltaId, amountXInQuoteValue) {
|
|
12826
|
+
const m1 = minDeltaId.neg();
|
|
12827
|
+
const m2 = maxDeltaId.neg();
|
|
12828
|
+
const a1 = m1.sub(m2).addn(1);
|
|
12829
|
+
const a2 = m1.mul(m1.addn(1)).divn(2);
|
|
12830
|
+
const a3 = m2.mul(m2.subn(1)).divn(2);
|
|
12831
|
+
const a = m1.sub(a3.sub(a2)).div(m1);
|
|
12832
|
+
const y0 = amountXInQuoteValue.div(a);
|
|
12833
|
+
const deltaY = y0.neg().div(m1);
|
|
12834
|
+
const amountY = toAmountIntoBins(
|
|
12835
|
+
activeId,
|
|
12836
|
+
minDeltaId,
|
|
12837
|
+
maxDeltaId,
|
|
12838
|
+
new (0, _bnjs2.default)(0),
|
|
12839
|
+
deltaY,
|
|
12840
|
+
new (0, _bnjs2.default)(0),
|
|
12841
|
+
y0,
|
|
12811
12842
|
binStep,
|
|
12812
|
-
|
|
12813
|
-
|
|
12814
|
-
|
|
12815
|
-
|
|
12816
|
-
);
|
|
12817
|
-
const afterWidth = getPositionWidthWithMinWidth(
|
|
12818
|
-
this.lowerBinId.toNumber(),
|
|
12819
|
-
this.upperBinId.toNumber()
|
|
12820
|
-
);
|
|
12821
|
-
const widthDelta = afterWidth - beforeWidth;
|
|
12822
|
-
let rentalCostLamports = new (0, _bnjs2.default)(0);
|
|
12823
|
-
if (widthDelta != 0) {
|
|
12824
|
-
const sizeChanges = Math.abs(widthDelta) * POSITION_BIN_DATA_SIZE;
|
|
12825
|
-
const [minimumLamports, rentExemptionLamports] = await Promise.all([
|
|
12826
|
-
connection.getMinimumBalanceForRentExemption(0),
|
|
12827
|
-
connection.getMinimumBalanceForRentExemption(sizeChanges)
|
|
12828
|
-
]);
|
|
12829
|
-
const lamportChanges = new (0, _bnjs2.default)(rentExemptionLamports).sub(
|
|
12830
|
-
new (0, _bnjs2.default)(minimumLamports)
|
|
12831
|
-
);
|
|
12832
|
-
if (widthDelta > 0) {
|
|
12833
|
-
rentalCostLamports = rentalCostLamports.add(lamportChanges);
|
|
12834
|
-
} else {
|
|
12835
|
-
rentalCostLamports = rentalCostLamports.sub(lamportChanges);
|
|
12836
|
-
}
|
|
12837
|
-
}
|
|
12843
|
+
favorXInActiveBin
|
|
12844
|
+
).reduce((acc, bin) => {
|
|
12845
|
+
return acc.add(bin.amountY);
|
|
12846
|
+
}, new (0, _bnjs2.default)(0));
|
|
12838
12847
|
return {
|
|
12839
|
-
|
|
12840
|
-
|
|
12841
|
-
|
|
12842
|
-
actualAmountYDeposited: depositResult.actualTotalAmountYDeposited,
|
|
12843
|
-
actualAmountXWithdrawn: depositResult.actualLiquidityAndFeeXWithdrawn,
|
|
12844
|
-
actualAmountYWithdrawn: depositResult.actualLiquidityAndFeeYWithdrawn,
|
|
12845
|
-
rewardAmountsClaimed: withdrawResult.rewardsAmountClaimed,
|
|
12846
|
-
withdrawParams,
|
|
12847
|
-
depositParams,
|
|
12848
|
-
rentalCostLamports
|
|
12848
|
+
base: y0,
|
|
12849
|
+
delta: deltaY,
|
|
12850
|
+
amountY
|
|
12849
12851
|
};
|
|
12850
12852
|
}
|
|
12851
|
-
|
|
12852
|
-
|
|
12853
|
-
|
|
12854
|
-
|
|
12855
|
-
|
|
12856
|
-
|
|
12857
|
-
|
|
12858
|
-
return [totalAmountX, totalAmountY];
|
|
12859
|
-
}
|
|
12860
|
-
totalFeeAmounts() {
|
|
12861
|
-
let totalFeeXAmount = new (0, _bnjs2.default)(0);
|
|
12862
|
-
let totalFeeYAmount = new (0, _bnjs2.default)(0);
|
|
12863
|
-
for (const binData of this.rebalancePositionBinData) {
|
|
12864
|
-
totalFeeXAmount = totalFeeXAmount.add(binData.claimableFeeXAmount);
|
|
12865
|
-
totalFeeYAmount = totalFeeYAmount.add(binData.claimableFeeYAmount);
|
|
12866
|
-
}
|
|
12867
|
-
return [totalFeeXAmount, totalFeeYAmount];
|
|
12853
|
+
};
|
|
12854
|
+
|
|
12855
|
+
// src/dlmm/helpers/rebalance/liquidity_strategy/spot.ts
|
|
12856
|
+
|
|
12857
|
+
function findY0(amountY, minDeltaId, maxDeltaId) {
|
|
12858
|
+
if (minDeltaId.gt(maxDeltaId) || amountY.lte(new (0, _bnjs2.default)(0)) || amountY.isZero()) {
|
|
12859
|
+
return new (0, _bnjs2.default)(0);
|
|
12868
12860
|
}
|
|
12869
|
-
|
|
12870
|
-
|
|
12871
|
-
|
|
12872
|
-
|
|
12873
|
-
|
|
12874
|
-
|
|
12875
|
-
|
|
12876
|
-
|
|
12877
|
-
|
|
12878
|
-
|
|
12879
|
-
|
|
12861
|
+
const m1 = minDeltaId.neg();
|
|
12862
|
+
const m2 = maxDeltaId.neg();
|
|
12863
|
+
const delta = m1.sub(m2).addn(1);
|
|
12864
|
+
return amountY.div(delta);
|
|
12865
|
+
}
|
|
12866
|
+
function findBaseX02(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
|
|
12867
|
+
let totalWeight = new (0, _bnjs2.default)(0);
|
|
12868
|
+
const minBinId = activeId.add(minDeltaId);
|
|
12869
|
+
const maxBinId = activeId.add(maxDeltaId);
|
|
12870
|
+
let baseFactor = getQPriceBaseFactor(binStep);
|
|
12871
|
+
let basePrice = getQPriceFromId(maxBinId.neg(), binStep);
|
|
12872
|
+
for (let binId = minBinId.toNumber(); binId <= maxBinId.toNumber(); binId++) {
|
|
12873
|
+
totalWeight = totalWeight.add(basePrice);
|
|
12874
|
+
basePrice = basePrice.mul(baseFactor).shrn(SCALE_OFFSET);
|
|
12880
12875
|
}
|
|
12881
|
-
|
|
12882
|
-
function getPositionWidthWithMinWidth(lowerBinId, upperBinId) {
|
|
12883
|
-
const width = upperBinId - lowerBinId + 1;
|
|
12884
|
-
return Math.max(width, DEFAULT_BIN_PER_POSITION.toNumber());
|
|
12876
|
+
return amountX.shln(SCALE_OFFSET).div(totalWeight);
|
|
12885
12877
|
}
|
|
12886
|
-
function
|
|
12887
|
-
|
|
12888
|
-
(
|
|
12889
|
-
);
|
|
12890
|
-
for (const deposit of deposits) {
|
|
12891
|
-
if (deposit.minDeltaId.gte(deposit.maxDeltaId)) {
|
|
12892
|
-
throw "Invalid minDeltaId or maxDeltaId";
|
|
12893
|
-
}
|
|
12878
|
+
function findX0(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
|
|
12879
|
+
if (minDeltaId.gt(maxDeltaId) || amountX.lte(new (0, _bnjs2.default)(0)) || amountX.isZero()) {
|
|
12880
|
+
return new (0, _bnjs2.default)(0);
|
|
12894
12881
|
}
|
|
12895
|
-
|
|
12896
|
-
|
|
12897
|
-
const
|
|
12898
|
-
|
|
12899
|
-
|
|
12882
|
+
let x0 = findBaseX02(amountX, minDeltaId, maxDeltaId, binStep, activeId);
|
|
12883
|
+
while (true) {
|
|
12884
|
+
const amountInBins = getAmountInBinsAskSide(
|
|
12885
|
+
activeId,
|
|
12886
|
+
binStep,
|
|
12887
|
+
minDeltaId,
|
|
12888
|
+
maxDeltaId,
|
|
12889
|
+
new (0, _bnjs2.default)(0),
|
|
12890
|
+
x0
|
|
12891
|
+
);
|
|
12892
|
+
const totalAmountX = amountInBins.reduce((acc, bin) => {
|
|
12893
|
+
return acc.add(bin.amountX);
|
|
12894
|
+
}, new (0, _bnjs2.default)(0));
|
|
12895
|
+
if (totalAmountX.lt(amountX)) {
|
|
12896
|
+
x0 = x0.add(new (0, _bnjs2.default)(1));
|
|
12897
|
+
} else {
|
|
12898
|
+
x0 = x0.sub(new (0, _bnjs2.default)(1));
|
|
12899
|
+
return x0;
|
|
12900
12900
|
}
|
|
12901
12901
|
}
|
|
12902
|
-
return sortedDeposits;
|
|
12903
12902
|
}
|
|
12904
|
-
|
|
12905
|
-
|
|
12906
|
-
|
|
12907
|
-
|
|
12908
|
-
|
|
12909
|
-
}
|
|
12910
|
-
const filledMinBinId = _nullishCoalesce(minBinId, () => ( activeId));
|
|
12911
|
-
const filledMaxBinId = _nullishCoalesce(maxBinId, () => ( activeId));
|
|
12912
|
-
if (filledMinBinId.gt(filledMaxBinId)) {
|
|
12913
|
-
throw "Invalid minBinId or maxBinId";
|
|
12914
|
-
}
|
|
12915
|
-
filledWithdraws.push({
|
|
12916
|
-
minBinId: filledMinBinId,
|
|
12917
|
-
maxBinId: filledMaxBinId,
|
|
12918
|
-
bps
|
|
12919
|
-
});
|
|
12903
|
+
var SpotStrategyParameterBuilder = class {
|
|
12904
|
+
findXParameters(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
|
|
12905
|
+
return {
|
|
12906
|
+
base: findX0(amountX, minDeltaId, maxDeltaId, binStep, activeId),
|
|
12907
|
+
delta: new (0, _bnjs2.default)(0)
|
|
12908
|
+
};
|
|
12920
12909
|
}
|
|
12921
|
-
|
|
12922
|
-
return
|
|
12923
|
-
|
|
12924
|
-
|
|
12925
|
-
|
|
12926
|
-
|
|
12927
|
-
|
|
12928
|
-
|
|
12929
|
-
|
|
12910
|
+
findYParameters(amountY, minDeltaId, maxDeltaId, _activeId) {
|
|
12911
|
+
return {
|
|
12912
|
+
base: findY0(amountY, minDeltaId, maxDeltaId),
|
|
12913
|
+
delta: new (0, _bnjs2.default)(0)
|
|
12914
|
+
};
|
|
12915
|
+
}
|
|
12916
|
+
suggestBalancedXParametersFromY(activeId, binStep, favorXInActiveBin, minDeltaId, maxDeltaId, amountY) {
|
|
12917
|
+
const x0 = amountY.div(maxDeltaId.addn(1));
|
|
12918
|
+
const totalAmountX = toAmountIntoBins(
|
|
12919
|
+
activeId,
|
|
12920
|
+
minDeltaId,
|
|
12921
|
+
maxDeltaId,
|
|
12922
|
+
new (0, _bnjs2.default)(0),
|
|
12923
|
+
new (0, _bnjs2.default)(0),
|
|
12924
|
+
x0,
|
|
12925
|
+
new (0, _bnjs2.default)(0),
|
|
12926
|
+
binStep,
|
|
12927
|
+
favorXInActiveBin
|
|
12928
|
+
).reduce((acc, bin) => {
|
|
12929
|
+
return acc.add(bin.amountX);
|
|
12930
|
+
}, new (0, _bnjs2.default)(0));
|
|
12931
|
+
return {
|
|
12932
|
+
base: new (0, _bnjs2.default)(x0.toString()),
|
|
12933
|
+
delta: new (0, _bnjs2.default)(0),
|
|
12934
|
+
amountX: totalAmountX
|
|
12935
|
+
};
|
|
12936
|
+
}
|
|
12937
|
+
suggestBalancedYParametersFromX(activeId, binStep, favorXInActiveBin, minDeltaId, maxDeltaId, amountXInQuoteValue) {
|
|
12938
|
+
const y0 = amountXInQuoteValue.div(maxDeltaId.sub(minDeltaId).addn(1));
|
|
12939
|
+
const amountY = toAmountIntoBins(
|
|
12940
|
+
activeId,
|
|
12941
|
+
minDeltaId,
|
|
12942
|
+
maxDeltaId,
|
|
12943
|
+
new (0, _bnjs2.default)(0),
|
|
12944
|
+
new (0, _bnjs2.default)(0),
|
|
12945
|
+
new (0, _bnjs2.default)(0),
|
|
12946
|
+
y0,
|
|
12947
|
+
binStep,
|
|
12948
|
+
favorXInActiveBin
|
|
12949
|
+
).reduce((acc, bin) => {
|
|
12950
|
+
return acc.add(bin.amountY);
|
|
12951
|
+
}, new (0, _bnjs2.default)(0));
|
|
12952
|
+
return {
|
|
12953
|
+
base: y0,
|
|
12954
|
+
delta: new (0, _bnjs2.default)(0),
|
|
12955
|
+
amountY
|
|
12956
|
+
};
|
|
12957
|
+
}
|
|
12958
|
+
};
|
|
12959
|
+
|
|
12960
|
+
// src/dlmm/helpers/rebalance/liquidity_strategy/index.ts
|
|
12961
|
+
|
|
12962
|
+
function getLiquidityStrategyParameterBuilder(strategyType) {
|
|
12963
|
+
switch (strategyType) {
|
|
12964
|
+
case 0 /* Spot */:
|
|
12965
|
+
return new SpotStrategyParameterBuilder();
|
|
12966
|
+
case 1 /* Curve */:
|
|
12967
|
+
return new CurveStrategyParameterBuilder();
|
|
12968
|
+
case 2 /* BidAsk */:
|
|
12969
|
+
return new BidAskStrategyParameterBuilder();
|
|
12970
|
+
default:
|
|
12971
|
+
throw new Error("Strategy not supported");
|
|
12930
12972
|
}
|
|
12931
|
-
return filledWithdraws;
|
|
12932
12973
|
}
|
|
12933
|
-
function
|
|
12934
|
-
const
|
|
12935
|
-
|
|
12936
|
-
|
|
12937
|
-
|
|
12938
|
-
|
|
12974
|
+
function suggestBalancedXParametersFromY(y0, deltaY, minDeltaId, maxDeltaId, activeId, binStep, favorXInActiveBin, builder) {
|
|
12975
|
+
const endDeltaIdBidSide = favorXInActiveBin ? new (0, _bnjs2.default)(-1) : new (0, _bnjs2.default)(0);
|
|
12976
|
+
if (maxDeltaId.lte(endDeltaIdBidSide)) {
|
|
12977
|
+
return {
|
|
12978
|
+
base: new (0, _bnjs2.default)(0),
|
|
12979
|
+
delta: new (0, _bnjs2.default)(0),
|
|
12980
|
+
amountX: new (0, _bnjs2.default)(0)
|
|
12981
|
+
};
|
|
12939
12982
|
}
|
|
12940
|
-
|
|
12983
|
+
const minYDeltaId = minDeltaId;
|
|
12984
|
+
const maxYDeltaId = endDeltaIdBidSide;
|
|
12985
|
+
const totalAmountY = toAmountIntoBins(
|
|
12986
|
+
activeId,
|
|
12987
|
+
minYDeltaId,
|
|
12988
|
+
maxYDeltaId,
|
|
12989
|
+
new (0, _bnjs2.default)(0),
|
|
12990
|
+
deltaY,
|
|
12991
|
+
new (0, _bnjs2.default)(0),
|
|
12992
|
+
y0,
|
|
12993
|
+
binStep,
|
|
12994
|
+
favorXInActiveBin
|
|
12995
|
+
).reduce((acc, bin) => {
|
|
12996
|
+
return acc.add(bin.amountY);
|
|
12997
|
+
}, new (0, _bnjs2.default)(0));
|
|
12998
|
+
const minXDeltaId = maxYDeltaId.addn(1);
|
|
12999
|
+
const maxXDeltaId = maxDeltaId;
|
|
13000
|
+
return builder.suggestBalancedXParametersFromY(
|
|
13001
|
+
activeId,
|
|
13002
|
+
binStep,
|
|
13003
|
+
favorXInActiveBin,
|
|
13004
|
+
minXDeltaId,
|
|
13005
|
+
maxXDeltaId,
|
|
13006
|
+
totalAmountY
|
|
13007
|
+
);
|
|
12941
13008
|
}
|
|
12942
|
-
function
|
|
12943
|
-
let
|
|
12944
|
-
|
|
12945
|
-
|
|
12946
|
-
|
|
12947
|
-
|
|
12948
|
-
|
|
12949
|
-
|
|
12950
|
-
|
|
12951
|
-
|
|
13009
|
+
function getAutoFillAmountByRebalancedPosition(rebalancePosition, strategyType) {
|
|
13010
|
+
let liquidityInBidSide = new (0, _bnjs2.default)(0);
|
|
13011
|
+
let liquidityInAskSide = new (0, _bnjs2.default)(0);
|
|
13012
|
+
const builder = getLiquidityStrategyParameterBuilder(strategyType);
|
|
13013
|
+
const { lbPair } = rebalancePosition;
|
|
13014
|
+
let favorXInActiveBin = false;
|
|
13015
|
+
let activeIdIndex = -1;
|
|
13016
|
+
for (const [
|
|
13017
|
+
idx,
|
|
13018
|
+
binData
|
|
13019
|
+
] of rebalancePosition.rebalancePositionBinData.entries()) {
|
|
13020
|
+
const liquidityBid = binData.amountY;
|
|
13021
|
+
const liquidityAsk = new (0, _decimaljs2.default)(binData.price).mul(new (0, _decimaljs2.default)(binData.amountX.toString())).floor().toString();
|
|
13022
|
+
liquidityInBidSide = liquidityInBidSide.add(liquidityBid);
|
|
13023
|
+
liquidityInAskSide = liquidityInAskSide.add(new (0, _bnjs2.default)(liquidityAsk));
|
|
13024
|
+
if (binData.binId == lbPair.activeId) {
|
|
13025
|
+
favorXInActiveBin = binData.amountX.gt(binData.amountY);
|
|
13026
|
+
activeIdIndex = idx;
|
|
12952
13027
|
}
|
|
12953
|
-
|
|
12954
|
-
|
|
12955
|
-
|
|
12956
|
-
|
|
12957
|
-
|
|
12958
|
-
|
|
12959
|
-
|
|
12960
|
-
|
|
12961
|
-
binArrayIndex = binArrayIndex.add(new (0, _bnjs2.default)(1));
|
|
12962
|
-
}
|
|
13028
|
+
}
|
|
13029
|
+
if (liquidityInAskSide.gt(liquidityInBidSide)) {
|
|
13030
|
+
const minBinId = rebalancePosition.rebalancePositionBinData[0].binId;
|
|
13031
|
+
let maxBinId;
|
|
13032
|
+
if (activeIdIndex == -1) {
|
|
13033
|
+
maxBinId = rebalancePosition.rebalancePositionBinData[rebalancePosition.rebalancePositionBinData.length - 1].binId;
|
|
13034
|
+
} else {
|
|
13035
|
+
maxBinId = rebalancePosition.rebalancePositionBinData[favorXInActiveBin ? activeIdIndex - 1 : activeIdIndex].binId;
|
|
12963
13036
|
}
|
|
12964
|
-
|
|
12965
|
-
|
|
12966
|
-
const
|
|
12967
|
-
|
|
12968
|
-
|
|
12969
|
-
|
|
12970
|
-
|
|
12971
|
-
|
|
12972
|
-
|
|
12973
|
-
|
|
12974
|
-
|
|
12975
|
-
|
|
12976
|
-
|
|
12977
|
-
|
|
13037
|
+
const minDeltaId = minBinId - lbPair.activeId;
|
|
13038
|
+
const maxDeltaId = maxBinId - lbPair.activeId;
|
|
13039
|
+
const { amountY } = builder.suggestBalancedYParametersFromX(
|
|
13040
|
+
new (0, _bnjs2.default)(lbPair.activeId),
|
|
13041
|
+
new (0, _bnjs2.default)(lbPair.binStep),
|
|
13042
|
+
favorXInActiveBin,
|
|
13043
|
+
new (0, _bnjs2.default)(minDeltaId),
|
|
13044
|
+
new (0, _bnjs2.default)(maxDeltaId),
|
|
13045
|
+
liquidityInAskSide
|
|
13046
|
+
);
|
|
13047
|
+
return {
|
|
13048
|
+
amount: amountY,
|
|
13049
|
+
isBidSide: true
|
|
13050
|
+
};
|
|
13051
|
+
} else if (liquidityInAskSide.lt(liquidityInBidSide)) {
|
|
13052
|
+
const maxBinId = rebalancePosition.rebalancePositionBinData[rebalancePosition.rebalancePositionBinData.length - 1].binId;
|
|
13053
|
+
let minBinId;
|
|
13054
|
+
if (activeIdIndex == -1) {
|
|
13055
|
+
minBinId = rebalancePosition.rebalancePositionBinData[0].binId;
|
|
13056
|
+
} else {
|
|
13057
|
+
minBinId = rebalancePosition.rebalancePositionBinData[favorXInActiveBin ? activeIdIndex - 1 : activeIdIndex].binId;
|
|
12978
13058
|
}
|
|
12979
|
-
|
|
12980
|
-
|
|
12981
|
-
|
|
12982
|
-
|
|
12983
|
-
|
|
12984
|
-
|
|
12985
|
-
|
|
12986
|
-
|
|
12987
|
-
|
|
12988
|
-
|
|
12989
|
-
|
|
12990
|
-
|
|
12991
|
-
|
|
12992
|
-
|
|
12993
|
-
|
|
12994
|
-
|
|
12995
|
-
|
|
12996
|
-
|
|
12997
|
-
|
|
12998
|
-
}
|
|
12999
|
-
if (minDeltaId.eq(maxDeltaId)) {
|
|
13000
|
-
return amountY;
|
|
13059
|
+
const minDeltaId = lbPair.activeId - minBinId;
|
|
13060
|
+
const maxDeltaId = lbPair.activeId - maxBinId;
|
|
13061
|
+
const { amountX } = builder.suggestBalancedXParametersFromY(
|
|
13062
|
+
new (0, _bnjs2.default)(lbPair.activeId),
|
|
13063
|
+
new (0, _bnjs2.default)(lbPair.binStep),
|
|
13064
|
+
favorXInActiveBin,
|
|
13065
|
+
new (0, _bnjs2.default)(minDeltaId),
|
|
13066
|
+
new (0, _bnjs2.default)(maxDeltaId),
|
|
13067
|
+
liquidityInBidSide
|
|
13068
|
+
);
|
|
13069
|
+
return {
|
|
13070
|
+
amount: amountX,
|
|
13071
|
+
isBidSide: false
|
|
13072
|
+
};
|
|
13073
|
+
} else {
|
|
13074
|
+
return {
|
|
13075
|
+
amount: new (0, _bnjs2.default)(0),
|
|
13076
|
+
isBidSide: false
|
|
13077
|
+
};
|
|
13001
13078
|
}
|
|
13002
|
-
const m1 = minDeltaId.neg().subn(1);
|
|
13003
|
-
const m2 = maxDeltaId.neg();
|
|
13004
|
-
const b = m2.neg().mul(m1.sub(m2).addn(1));
|
|
13005
|
-
const c = m1.mul(m1.addn(1)).divn(2);
|
|
13006
|
-
const d = m2.mul(m2.subn(1)).divn(2);
|
|
13007
|
-
const a = b.add(c.sub(d));
|
|
13008
|
-
return amountY.div(a);
|
|
13009
13079
|
}
|
|
13010
|
-
function
|
|
13011
|
-
|
|
13080
|
+
function suggestBalancedYParametersFromX(x0, deltaX, minDeltaId, maxDeltaId, activeId, binStep, favorXInActiveBin, builder) {
|
|
13081
|
+
const startDeltaIdAskSide = favorXInActiveBin ? new (0, _bnjs2.default)(0) : new (0, _bnjs2.default)(1);
|
|
13082
|
+
if (minDeltaId.gte(startDeltaIdAskSide)) {
|
|
13012
13083
|
return {
|
|
13013
13084
|
base: new (0, _bnjs2.default)(0),
|
|
13014
|
-
delta: new (0, _bnjs2.default)(0)
|
|
13085
|
+
delta: new (0, _bnjs2.default)(0),
|
|
13086
|
+
amountY: new (0, _bnjs2.default)(0)
|
|
13015
13087
|
};
|
|
13016
13088
|
}
|
|
13017
|
-
|
|
13018
|
-
const
|
|
13019
|
-
|
|
13020
|
-
|
|
13021
|
-
|
|
13089
|
+
const minXDeltaId = startDeltaIdAskSide;
|
|
13090
|
+
const maxXDeltaId = maxDeltaId;
|
|
13091
|
+
const amountXInBins = toAmountIntoBins(
|
|
13092
|
+
activeId,
|
|
13093
|
+
minXDeltaId,
|
|
13094
|
+
maxXDeltaId,
|
|
13095
|
+
deltaX,
|
|
13096
|
+
new (0, _bnjs2.default)(0),
|
|
13097
|
+
x0,
|
|
13098
|
+
new (0, _bnjs2.default)(0),
|
|
13099
|
+
binStep,
|
|
13100
|
+
favorXInActiveBin
|
|
13101
|
+
);
|
|
13102
|
+
const totalAmountXInQuote = amountXInBins.reduce((acc, bin) => {
|
|
13103
|
+
const price = getPriceOfBinByBinId(
|
|
13104
|
+
bin.binId.toNumber(),
|
|
13105
|
+
binStep.toNumber()
|
|
13106
|
+
);
|
|
13107
|
+
return acc.add(price.mul(new (0, _decimaljs2.default)(bin.amountX.toString())));
|
|
13108
|
+
}, new (0, _decimaljs2.default)(0));
|
|
13109
|
+
const totalAmountXInQuoteBN = new (0, _bnjs2.default)(totalAmountXInQuote.floor().toString());
|
|
13110
|
+
const minYDeltaId = minDeltaId;
|
|
13111
|
+
const maxYDeltaId = startDeltaIdAskSide.subn(1);
|
|
13112
|
+
return builder.suggestBalancedYParametersFromX(
|
|
13113
|
+
activeId,
|
|
13114
|
+
binStep,
|
|
13115
|
+
favorXInActiveBin,
|
|
13116
|
+
minYDeltaId,
|
|
13117
|
+
maxYDeltaId,
|
|
13118
|
+
totalAmountXInQuoteBN
|
|
13119
|
+
);
|
|
13120
|
+
}
|
|
13121
|
+
function buildLiquidityStrategyParameters(amountX, amountY, minDeltaId, maxDeltaId, binStep, favorXInActiveId, activeId, strategyParameterBuilder) {
|
|
13122
|
+
if (minDeltaId.gt(maxDeltaId)) {
|
|
13123
|
+
return {
|
|
13124
|
+
x0: new (0, _bnjs2.default)(0),
|
|
13125
|
+
y0: new (0, _bnjs2.default)(0),
|
|
13126
|
+
deltaX: new (0, _bnjs2.default)(0),
|
|
13127
|
+
deltaY: new (0, _bnjs2.default)(0)
|
|
13128
|
+
};
|
|
13129
|
+
}
|
|
13130
|
+
const depositOnlyY = maxDeltaId.lt(new (0, _bnjs2.default)(0)) || maxDeltaId.isZero() && !favorXInActiveId;
|
|
13131
|
+
const depositOnlyX = minDeltaId.gt(new (0, _bnjs2.default)(0)) || minDeltaId.isZero() && favorXInActiveId;
|
|
13132
|
+
if (depositOnlyY) {
|
|
13133
|
+
const { base, delta } = strategyParameterBuilder.findYParameters(
|
|
13134
|
+
amountY,
|
|
13022
13135
|
minDeltaId,
|
|
13023
13136
|
maxDeltaId,
|
|
13024
|
-
|
|
13025
|
-
y0
|
|
13137
|
+
activeId
|
|
13026
13138
|
);
|
|
13027
|
-
|
|
13028
|
-
|
|
13029
|
-
|
|
13030
|
-
|
|
13031
|
-
|
|
13032
|
-
}
|
|
13033
|
-
|
|
13034
|
-
|
|
13035
|
-
|
|
13036
|
-
|
|
13139
|
+
return {
|
|
13140
|
+
x0: new (0, _bnjs2.default)(0),
|
|
13141
|
+
deltaX: new (0, _bnjs2.default)(0),
|
|
13142
|
+
y0: base,
|
|
13143
|
+
deltaY: delta
|
|
13144
|
+
};
|
|
13145
|
+
}
|
|
13146
|
+
if (depositOnlyX) {
|
|
13147
|
+
const { base, delta } = strategyParameterBuilder.findXParameters(
|
|
13148
|
+
amountX,
|
|
13149
|
+
minDeltaId,
|
|
13150
|
+
maxDeltaId,
|
|
13151
|
+
binStep,
|
|
13152
|
+
activeId
|
|
13153
|
+
);
|
|
13154
|
+
return {
|
|
13155
|
+
x0: base,
|
|
13156
|
+
deltaX: delta,
|
|
13157
|
+
y0: new (0, _bnjs2.default)(0),
|
|
13158
|
+
deltaY: new (0, _bnjs2.default)(0)
|
|
13159
|
+
};
|
|
13160
|
+
}
|
|
13161
|
+
const maxDeltaIdBidSide = favorXInActiveId ? new (0, _bnjs2.default)(-1) : new (0, _bnjs2.default)(0);
|
|
13162
|
+
const minDeltaIdAskSide = favorXInActiveId ? new (0, _bnjs2.default)(0) : new (0, _bnjs2.default)(1);
|
|
13163
|
+
const { base: y0, delta: deltaY } = strategyParameterBuilder.findYParameters(
|
|
13164
|
+
amountY,
|
|
13165
|
+
minDeltaId,
|
|
13166
|
+
maxDeltaIdBidSide,
|
|
13167
|
+
activeId
|
|
13168
|
+
);
|
|
13169
|
+
const { base: x0, delta: deltaX } = strategyParameterBuilder.findXParameters(
|
|
13170
|
+
amountX,
|
|
13171
|
+
minDeltaIdAskSide,
|
|
13172
|
+
maxDeltaId,
|
|
13173
|
+
binStep,
|
|
13174
|
+
activeId
|
|
13175
|
+
);
|
|
13176
|
+
return {
|
|
13177
|
+
x0,
|
|
13178
|
+
deltaX,
|
|
13179
|
+
y0,
|
|
13180
|
+
deltaY
|
|
13181
|
+
};
|
|
13182
|
+
}
|
|
13183
|
+
|
|
13184
|
+
// src/dlmm/helpers/index.ts
|
|
13185
|
+
function chunks(array, size) {
|
|
13186
|
+
return Array.apply(0, new Array(Math.ceil(array.length / size))).map(
|
|
13187
|
+
(_, index) => array.slice(index * size, (index + 1) * size)
|
|
13188
|
+
);
|
|
13189
|
+
}
|
|
13190
|
+
function range(min, max, mapfn) {
|
|
13191
|
+
const length = max - min + 1;
|
|
13192
|
+
return Array.from({ length }, (_, i) => mapfn(min + i));
|
|
13193
|
+
}
|
|
13194
|
+
async function chunkedFetchMultiplePoolAccount(program, pks, chunkSize = 100) {
|
|
13195
|
+
const accounts = (await Promise.all(
|
|
13196
|
+
chunks(pks, chunkSize).map(
|
|
13197
|
+
(chunk) => program.account.lbPair.fetchMultiple(chunk)
|
|
13198
|
+
)
|
|
13199
|
+
)).flat();
|
|
13200
|
+
return accounts.filter(Boolean);
|
|
13201
|
+
}
|
|
13202
|
+
async function chunkedFetchMultipleBinArrayBitmapExtensionAccount(program, pks, chunkSize = 100) {
|
|
13203
|
+
const accounts = (await Promise.all(
|
|
13204
|
+
chunks(pks, chunkSize).map(
|
|
13205
|
+
(chunk) => program.account.binArrayBitmapExtension.fetchMultiple(chunk)
|
|
13206
|
+
)
|
|
13207
|
+
)).flat();
|
|
13208
|
+
return accounts;
|
|
13209
|
+
}
|
|
13210
|
+
function getOutAmount(bin, inAmount, swapForY) {
|
|
13211
|
+
return swapForY ? mulShr(inAmount, bin.price, SCALE_OFFSET, 1 /* Down */) : shlDiv(inAmount, bin.price, SCALE_OFFSET, 1 /* Down */);
|
|
13212
|
+
}
|
|
13213
|
+
async function getTokenDecimals(conn, mint) {
|
|
13214
|
+
const token = await _spltoken.getMint.call(void 0, conn, mint);
|
|
13215
|
+
return await token.decimals;
|
|
13216
|
+
}
|
|
13217
|
+
var getOrCreateATAInstruction = async (connection, tokenMint, owner, programId, payer = owner, allowOwnerOffCurve = true) => {
|
|
13218
|
+
programId = _nullishCoalesce(programId, () => ( _spltoken.TOKEN_PROGRAM_ID));
|
|
13219
|
+
const toAccount = _spltoken.getAssociatedTokenAddressSync.call(void 0,
|
|
13220
|
+
tokenMint,
|
|
13221
|
+
owner,
|
|
13222
|
+
allowOwnerOffCurve,
|
|
13223
|
+
programId,
|
|
13224
|
+
_spltoken.ASSOCIATED_TOKEN_PROGRAM_ID
|
|
13225
|
+
);
|
|
13226
|
+
try {
|
|
13227
|
+
await _spltoken.getAccount.call(void 0, connection, toAccount, connection.commitment, programId);
|
|
13228
|
+
return { ataPubKey: toAccount, ix: void 0 };
|
|
13229
|
+
} catch (e) {
|
|
13230
|
+
if (e instanceof _spltoken.TokenAccountNotFoundError || e instanceof _spltoken.TokenInvalidAccountOwnerError) {
|
|
13231
|
+
const ix = _spltoken.createAssociatedTokenAccountIdempotentInstruction.call(void 0,
|
|
13232
|
+
payer,
|
|
13233
|
+
toAccount,
|
|
13234
|
+
owner,
|
|
13235
|
+
tokenMint,
|
|
13236
|
+
programId,
|
|
13237
|
+
_spltoken.ASSOCIATED_TOKEN_PROGRAM_ID
|
|
13238
|
+
);
|
|
13239
|
+
return { ataPubKey: toAccount, ix };
|
|
13240
|
+
} else {
|
|
13241
|
+
console.error("Error::getOrCreateATAInstruction", e);
|
|
13242
|
+
throw e;
|
|
13037
13243
|
}
|
|
13038
13244
|
}
|
|
13245
|
+
};
|
|
13246
|
+
async function getTokenBalance(conn, tokenAccount) {
|
|
13247
|
+
const acc = await _spltoken.getAccount.call(void 0, conn, tokenAccount);
|
|
13248
|
+
return acc.amount;
|
|
13039
13249
|
}
|
|
13040
|
-
|
|
13041
|
-
if (
|
|
13042
|
-
|
|
13043
|
-
|
|
13044
|
-
|
|
13045
|
-
let c = new (0, _bnjs2.default)(0);
|
|
13046
|
-
let m1 = minDeltaId;
|
|
13047
|
-
let m2 = maxDeltaId.addn(1);
|
|
13048
|
-
for (let m = m1.toNumber(); m <= m2.toNumber(); m++) {
|
|
13049
|
-
const binId = activeId.addn(m);
|
|
13050
|
-
const pm = getQPriceFromId(binId.neg(), binStep);
|
|
13051
|
-
const bDelta = m1.mul(pm);
|
|
13052
|
-
b = b.add(bDelta);
|
|
13053
|
-
const cDelta = new (0, _bnjs2.default)(m).mul(pm);
|
|
13054
|
-
c = c.add(cDelta);
|
|
13055
|
-
}
|
|
13056
|
-
return amountX.shln(SCALE_OFFSET).div(c.sub(b));
|
|
13057
|
-
}
|
|
13058
|
-
function findX0AndDeltaX(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
|
|
13059
|
-
if (minDeltaId.gt(maxDeltaId) || amountX.lte(new (0, _bnjs2.default)(0)) || amountX.isZero()) {
|
|
13060
|
-
return {
|
|
13061
|
-
base: new (0, _bnjs2.default)(0),
|
|
13062
|
-
delta: new (0, _bnjs2.default)(0)
|
|
13063
|
-
};
|
|
13250
|
+
var parseLogs = (eventParser, logs) => {
|
|
13251
|
+
if (!logs.length)
|
|
13252
|
+
throw new Error("No logs found");
|
|
13253
|
+
for (const event of _optionalChain([eventParser, 'optionalAccess', _41 => _41.parseLogs, 'call', _42 => _42(logs)])) {
|
|
13254
|
+
return event.data;
|
|
13064
13255
|
}
|
|
13065
|
-
|
|
13066
|
-
|
|
13067
|
-
|
|
13068
|
-
|
|
13069
|
-
|
|
13070
|
-
|
|
13256
|
+
throw new Error("No events found");
|
|
13257
|
+
};
|
|
13258
|
+
var wrapSOLInstruction = (from, to, amount) => {
|
|
13259
|
+
return [
|
|
13260
|
+
_web3js.SystemProgram.transfer({
|
|
13261
|
+
fromPubkey: from,
|
|
13262
|
+
toPubkey: to,
|
|
13263
|
+
lamports: amount
|
|
13264
|
+
}),
|
|
13265
|
+
new (0, _web3js.TransactionInstruction)({
|
|
13266
|
+
keys: [
|
|
13267
|
+
{
|
|
13268
|
+
pubkey: to,
|
|
13269
|
+
isSigner: false,
|
|
13270
|
+
isWritable: true
|
|
13271
|
+
}
|
|
13272
|
+
],
|
|
13273
|
+
data: Buffer.from(new Uint8Array([17])),
|
|
13274
|
+
programId: _spltoken.TOKEN_PROGRAM_ID
|
|
13275
|
+
})
|
|
13276
|
+
];
|
|
13277
|
+
};
|
|
13278
|
+
var unwrapSOLInstruction = async (owner, allowOwnerOffCurve = true) => {
|
|
13279
|
+
const wSolATAAccount = _spltoken.getAssociatedTokenAddressSync.call(void 0,
|
|
13280
|
+
_spltoken.NATIVE_MINT,
|
|
13281
|
+
owner,
|
|
13282
|
+
allowOwnerOffCurve
|
|
13071
13283
|
);
|
|
13072
|
-
|
|
13073
|
-
|
|
13074
|
-
|
|
13075
|
-
|
|
13076
|
-
|
|
13077
|
-
|
|
13078
|
-
|
|
13079
|
-
baseDeltaX,
|
|
13080
|
-
x0
|
|
13284
|
+
if (wSolATAAccount) {
|
|
13285
|
+
const closedWrappedSolInstruction = _spltoken.createCloseAccountInstruction.call(void 0,
|
|
13286
|
+
wSolATAAccount,
|
|
13287
|
+
owner,
|
|
13288
|
+
owner,
|
|
13289
|
+
[],
|
|
13290
|
+
_spltoken.TOKEN_PROGRAM_ID
|
|
13081
13291
|
);
|
|
13082
|
-
|
|
13083
|
-
return acc.add(amountX2);
|
|
13084
|
-
}, new (0, _bnjs2.default)(0));
|
|
13085
|
-
if (totalAmountX.gt(amountX)) {
|
|
13086
|
-
baseDeltaX = baseDeltaX.sub(new (0, _bnjs2.default)(1));
|
|
13087
|
-
} else {
|
|
13088
|
-
return {
|
|
13089
|
-
base: x0,
|
|
13090
|
-
delta: baseDeltaX
|
|
13091
|
-
};
|
|
13092
|
-
}
|
|
13292
|
+
return closedWrappedSolInstruction;
|
|
13093
13293
|
}
|
|
13294
|
+
return null;
|
|
13295
|
+
};
|
|
13296
|
+
async function chunkedGetMultipleAccountInfos(connection, pks, chunkSize = 100) {
|
|
13297
|
+
const accountInfos = (await Promise.all(
|
|
13298
|
+
chunks(pks, chunkSize).map(
|
|
13299
|
+
(chunk) => connection.getMultipleAccountsInfo(chunk)
|
|
13300
|
+
)
|
|
13301
|
+
)).flat();
|
|
13302
|
+
return accountInfos;
|
|
13094
13303
|
}
|
|
13095
|
-
var
|
|
13096
|
-
|
|
13097
|
-
|
|
13304
|
+
var getEstimatedComputeUnitUsageWithBuffer = async (connection, instructions, feePayer, buffer) => {
|
|
13305
|
+
if (!buffer) {
|
|
13306
|
+
buffer = 0.1;
|
|
13098
13307
|
}
|
|
13099
|
-
|
|
13100
|
-
|
|
13308
|
+
buffer = Math.max(0, buffer);
|
|
13309
|
+
buffer = Math.min(1, buffer);
|
|
13310
|
+
const estimatedComputeUnitUsage = await getSimulationComputeUnits(
|
|
13311
|
+
connection,
|
|
13312
|
+
instructions,
|
|
13313
|
+
feePayer,
|
|
13314
|
+
[]
|
|
13315
|
+
);
|
|
13316
|
+
let extraComputeUnitBuffer = estimatedComputeUnitUsage * buffer;
|
|
13317
|
+
if (extraComputeUnitBuffer > MAX_CU_BUFFER) {
|
|
13318
|
+
extraComputeUnitBuffer = MAX_CU_BUFFER;
|
|
13319
|
+
} else if (extraComputeUnitBuffer < MIN_CU_BUFFER) {
|
|
13320
|
+
extraComputeUnitBuffer = MIN_CU_BUFFER;
|
|
13101
13321
|
}
|
|
13102
|
-
|
|
13103
|
-
|
|
13104
|
-
|
|
13105
|
-
|
|
13106
|
-
|
|
13107
|
-
|
|
13108
|
-
|
|
13109
|
-
|
|
13110
|
-
|
|
13111
|
-
|
|
13112
|
-
|
|
13113
|
-
|
|
13114
|
-
|
|
13115
|
-
|
|
13116
|
-
|
|
13117
|
-
|
|
13118
|
-
|
|
13119
|
-
|
|
13120
|
-
|
|
13121
|
-
|
|
13122
|
-
|
|
13123
|
-
|
|
13124
|
-
}
|
|
13322
|
+
return estimatedComputeUnitUsage + extraComputeUnitBuffer;
|
|
13323
|
+
};
|
|
13324
|
+
var getEstimatedComputeUnitIxWithBuffer = async (connection, instructions, feePayer, buffer) => {
|
|
13325
|
+
const units = await getEstimatedComputeUnitUsageWithBuffer(
|
|
13326
|
+
connection,
|
|
13327
|
+
instructions,
|
|
13328
|
+
feePayer,
|
|
13329
|
+
buffer
|
|
13330
|
+
).catch((error) => {
|
|
13331
|
+
console.error("Error::getEstimatedComputeUnitUsageWithBuffer", error);
|
|
13332
|
+
return 14e5;
|
|
13333
|
+
});
|
|
13334
|
+
return _web3js.ComputeBudgetProgram.setComputeUnitLimit({ units });
|
|
13335
|
+
};
|
|
13336
|
+
function createProgram(connection, opt) {
|
|
13337
|
+
const cluster = _optionalChain([opt, 'optionalAccess', _43 => _43.cluster]) || "mainnet-beta";
|
|
13338
|
+
const provider = new (0, _anchor.AnchorProvider)(
|
|
13339
|
+
connection,
|
|
13340
|
+
{},
|
|
13341
|
+
_anchor.AnchorProvider.defaultOptions()
|
|
13342
|
+
);
|
|
13343
|
+
return new (0, _anchor.Program)(
|
|
13344
|
+
{ ...dlmm_default, address: LBCLMM_PROGRAM_IDS[cluster] },
|
|
13345
|
+
provider
|
|
13346
|
+
);
|
|
13347
|
+
}
|
|
13348
|
+
function decodeAccount(program, accountName, buffer) {
|
|
13349
|
+
return program.coder.accounts.decode(accountName, buffer);
|
|
13350
|
+
}
|
|
13351
|
+
function getAccountDiscriminator(accountName) {
|
|
13352
|
+
return _optionalChain([dlmm_default, 'access', _44 => _44.accounts, 'access', _45 => _45.find, 'call', _46 => _46(
|
|
13353
|
+
(acc) => acc.name.toLowerCase() === accountName.toLowerCase()
|
|
13354
|
+
), 'optionalAccess', _47 => _47.discriminator]);
|
|
13355
|
+
}
|
|
13356
|
+
function capSlippagePercentage(slippage) {
|
|
13357
|
+
if (slippage > 100) {
|
|
13358
|
+
slippage = 100;
|
|
13125
13359
|
}
|
|
13126
|
-
|
|
13127
|
-
|
|
13128
|
-
const m2 = maxDeltaId.neg();
|
|
13129
|
-
const a1 = m2.neg().mul(m1.sub(m2).addn(1));
|
|
13130
|
-
const a2 = m1.mul(m1.addn(1)).divn(2);
|
|
13131
|
-
const a3 = m2.mul(m2.subn(1)).divn(2);
|
|
13132
|
-
const a = a1.add(a2.sub(a3));
|
|
13133
|
-
const deltaY = amountXInQuoteValue.div(a);
|
|
13134
|
-
const y0 = deltaY.neg().mul(m2).add(deltaY);
|
|
13135
|
-
const amountY = toAmountIntoBins(
|
|
13136
|
-
activeId,
|
|
13137
|
-
minDeltaId,
|
|
13138
|
-
maxDeltaId,
|
|
13139
|
-
new (0, _bnjs2.default)(0),
|
|
13140
|
-
deltaY,
|
|
13141
|
-
new (0, _bnjs2.default)(0),
|
|
13142
|
-
y0,
|
|
13143
|
-
binStep,
|
|
13144
|
-
favorXInActiveBin
|
|
13145
|
-
).reduce((acc, bin) => {
|
|
13146
|
-
return acc.add(bin.amountY);
|
|
13147
|
-
}, new (0, _bnjs2.default)(0));
|
|
13148
|
-
return {
|
|
13149
|
-
base: y0,
|
|
13150
|
-
delta: deltaY,
|
|
13151
|
-
amountY
|
|
13152
|
-
};
|
|
13360
|
+
if (slippage < 0) {
|
|
13361
|
+
slippage = 0;
|
|
13153
13362
|
}
|
|
13363
|
+
return slippage;
|
|
13364
|
+
}
|
|
13365
|
+
|
|
13366
|
+
// src/dlmm/helpers/accountFilters.ts
|
|
13367
|
+
var _bytes = require('@coral-xyz/anchor/dist/cjs/utils/bytes');
|
|
13368
|
+
var presetParameter2BinStepFilter = (binStep) => {
|
|
13369
|
+
return {
|
|
13370
|
+
memcmp: {
|
|
13371
|
+
bytes: _bytes.bs58.encode(binStep.toArrayLike(Buffer, "le", 2)),
|
|
13372
|
+
offset: 8
|
|
13373
|
+
}
|
|
13374
|
+
};
|
|
13375
|
+
};
|
|
13376
|
+
var presetParameter2BaseFactorFilter = (baseFactor) => {
|
|
13377
|
+
return {
|
|
13378
|
+
memcmp: {
|
|
13379
|
+
bytes: _bytes.bs58.encode(baseFactor.toArrayLike(Buffer, "le", 2)),
|
|
13380
|
+
offset: 8 + 2
|
|
13381
|
+
}
|
|
13382
|
+
};
|
|
13383
|
+
};
|
|
13384
|
+
var presetParameter2BaseFeePowerFactor = (baseFeePowerFactor) => {
|
|
13385
|
+
return {
|
|
13386
|
+
memcmp: {
|
|
13387
|
+
bytes: _bytes.bs58.encode(baseFeePowerFactor.toArrayLike(Buffer, "le", 1)),
|
|
13388
|
+
offset: 8 + 22
|
|
13389
|
+
}
|
|
13390
|
+
};
|
|
13391
|
+
};
|
|
13392
|
+
var binArrayLbPairFilter = (lbPair) => {
|
|
13393
|
+
return {
|
|
13394
|
+
memcmp: {
|
|
13395
|
+
bytes: lbPair.toBase58(),
|
|
13396
|
+
offset: 8 + 16
|
|
13397
|
+
}
|
|
13398
|
+
};
|
|
13399
|
+
};
|
|
13400
|
+
var positionOwnerFilter = (owner) => {
|
|
13401
|
+
return {
|
|
13402
|
+
memcmp: {
|
|
13403
|
+
bytes: owner.toBase58(),
|
|
13404
|
+
offset: 8 + 32
|
|
13405
|
+
}
|
|
13406
|
+
};
|
|
13407
|
+
};
|
|
13408
|
+
var positionLbPairFilter = (lbPair) => {
|
|
13409
|
+
return {
|
|
13410
|
+
memcmp: {
|
|
13411
|
+
bytes: _bytes.bs58.encode(lbPair.toBuffer()),
|
|
13412
|
+
offset: 8
|
|
13413
|
+
}
|
|
13414
|
+
};
|
|
13415
|
+
};
|
|
13416
|
+
var positionV2Filter = () => {
|
|
13417
|
+
return {
|
|
13418
|
+
memcmp: {
|
|
13419
|
+
bytes: _bytes.bs58.encode(Buffer.from(getAccountDiscriminator("positionV2"))),
|
|
13420
|
+
offset: 0
|
|
13421
|
+
}
|
|
13422
|
+
};
|
|
13154
13423
|
};
|
|
13155
13424
|
|
|
13156
|
-
// src/dlmm/helpers/
|
|
13425
|
+
// src/dlmm/helpers/positions/index.ts
|
|
13157
13426
|
|
|
13158
|
-
|
|
13159
|
-
|
|
13160
|
-
|
|
13161
|
-
|
|
13162
|
-
|
|
13163
|
-
|
|
13427
|
+
|
|
13428
|
+
// src/dlmm/helpers/positions/wrapper.ts
|
|
13429
|
+
|
|
13430
|
+
function combineBaseAndExtendedPositionBinData(base, extended) {
|
|
13431
|
+
const combinedLiquidityShares = base.liquidityShares;
|
|
13432
|
+
const combinedRewardInfos = base.rewardInfos;
|
|
13433
|
+
const combinedFeeInfos = base.feeInfos;
|
|
13434
|
+
for (const binData of extended) {
|
|
13435
|
+
combinedLiquidityShares.push(binData.liquidityShare);
|
|
13436
|
+
combinedRewardInfos.push(binData.rewardInfo);
|
|
13437
|
+
combinedFeeInfos.push(binData.feeInfo);
|
|
13164
13438
|
}
|
|
13165
|
-
|
|
13166
|
-
|
|
13167
|
-
|
|
13168
|
-
|
|
13169
|
-
|
|
13170
|
-
const a = b.sub(c.sub(d).div(m1.addn(1)));
|
|
13171
|
-
return amountY.div(a);
|
|
13439
|
+
return {
|
|
13440
|
+
liquidityShares: combinedLiquidityShares,
|
|
13441
|
+
rewardInfos: combinedRewardInfos,
|
|
13442
|
+
feeInfos: combinedFeeInfos
|
|
13443
|
+
};
|
|
13172
13444
|
}
|
|
13173
|
-
function
|
|
13174
|
-
|
|
13175
|
-
|
|
13176
|
-
|
|
13177
|
-
|
|
13178
|
-
|
|
13179
|
-
|
|
13180
|
-
let baseY0 = findBaseY0(amountY, minDeltaId, maxDeltaId);
|
|
13181
|
-
while (true) {
|
|
13182
|
-
const deltaY = baseY0.neg().div(minDeltaId.neg().addn(1));
|
|
13183
|
-
const amountInBins = getAmountInBinsBidSide(
|
|
13184
|
-
activeId,
|
|
13185
|
-
minDeltaId,
|
|
13186
|
-
maxDeltaId,
|
|
13187
|
-
deltaY,
|
|
13188
|
-
baseY0
|
|
13445
|
+
function wrapPosition(program, key, account) {
|
|
13446
|
+
const disc = account.data.subarray(0, 8);
|
|
13447
|
+
if (disc.equals(Buffer.from(getAccountDiscriminator("positionV2")))) {
|
|
13448
|
+
const state = decodeAccount(
|
|
13449
|
+
program,
|
|
13450
|
+
"positionV2",
|
|
13451
|
+
account.data
|
|
13189
13452
|
);
|
|
13190
|
-
const
|
|
13191
|
-
|
|
13192
|
-
|
|
13193
|
-
|
|
13194
|
-
|
|
13195
|
-
|
|
13196
|
-
|
|
13197
|
-
|
|
13198
|
-
|
|
13199
|
-
|
|
13200
|
-
|
|
13453
|
+
const extended = decodeExtendedPosition(
|
|
13454
|
+
state,
|
|
13455
|
+
program,
|
|
13456
|
+
account.data.subarray(8 + POSITION_MIN_SIZE)
|
|
13457
|
+
);
|
|
13458
|
+
const combinedPositionBinData = combineBaseAndExtendedPositionBinData(
|
|
13459
|
+
state,
|
|
13460
|
+
extended
|
|
13461
|
+
);
|
|
13462
|
+
return new PositionV2Wrapper(key, state, extended, combinedPositionBinData);
|
|
13463
|
+
} else {
|
|
13464
|
+
throw new Error("Unknown position account");
|
|
13201
13465
|
}
|
|
13202
13466
|
}
|
|
13203
|
-
|
|
13204
|
-
|
|
13205
|
-
|
|
13467
|
+
var PositionV2Wrapper = class {
|
|
13468
|
+
constructor(positionAddress, inner, extended, combinedPositionBinData) {
|
|
13469
|
+
this.positionAddress = positionAddress;
|
|
13470
|
+
this.inner = inner;
|
|
13471
|
+
this.extended = extended;
|
|
13472
|
+
this.combinedPositionBinData = combinedPositionBinData;
|
|
13206
13473
|
}
|
|
13207
|
-
|
|
13208
|
-
|
|
13209
|
-
let m1 = minDeltaId;
|
|
13210
|
-
let m2 = maxDeltaId;
|
|
13211
|
-
for (let m = m1.toNumber(); m <= m2.toNumber(); m++) {
|
|
13212
|
-
const binId = activeId.addn(m);
|
|
13213
|
-
const pm = getQPriceFromId(binId.neg(), binStep);
|
|
13214
|
-
b = b.add(pm);
|
|
13215
|
-
const cDelta = new (0, _bnjs2.default)(m).mul(pm).div(m2);
|
|
13216
|
-
c = c.add(cDelta);
|
|
13474
|
+
address() {
|
|
13475
|
+
return this.positionAddress;
|
|
13217
13476
|
}
|
|
13218
|
-
|
|
13219
|
-
|
|
13220
|
-
function findX0AndDeltaX2(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
|
|
13221
|
-
if (minDeltaId.gt(maxDeltaId) || amountX.lte(new (0, _bnjs2.default)(0)) || amountX.isZero()) {
|
|
13222
|
-
return {
|
|
13223
|
-
base: new (0, _bnjs2.default)(0),
|
|
13224
|
-
delta: new (0, _bnjs2.default)(0)
|
|
13225
|
-
};
|
|
13477
|
+
totalClaimedRewards() {
|
|
13478
|
+
return this.inner.totalClaimedRewards;
|
|
13226
13479
|
}
|
|
13227
|
-
|
|
13228
|
-
|
|
13229
|
-
while (true) {
|
|
13230
|
-
const amountInBins = getAmountInBinsAskSide(
|
|
13231
|
-
activeId,
|
|
13232
|
-
binStep,
|
|
13233
|
-
minDeltaId,
|
|
13234
|
-
maxDeltaId,
|
|
13235
|
-
deltaX,
|
|
13236
|
-
baseX0
|
|
13237
|
-
);
|
|
13238
|
-
const totalAmountX = amountInBins.reduce((acc, { amountX: amountX2 }) => {
|
|
13239
|
-
return acc.add(amountX2);
|
|
13240
|
-
}, new (0, _bnjs2.default)(0));
|
|
13241
|
-
if (totalAmountX.gt(amountX)) {
|
|
13242
|
-
baseX0 = baseX0.sub(new (0, _bnjs2.default)(1));
|
|
13243
|
-
} else {
|
|
13244
|
-
return {
|
|
13245
|
-
base: baseX0,
|
|
13246
|
-
delta: deltaX
|
|
13247
|
-
};
|
|
13248
|
-
}
|
|
13480
|
+
feeOwner() {
|
|
13481
|
+
return this.inner.feeOwner;
|
|
13249
13482
|
}
|
|
13250
|
-
|
|
13251
|
-
|
|
13252
|
-
findXParameters(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
|
|
13253
|
-
return findX0AndDeltaX2(amountX, minDeltaId, maxDeltaId, binStep, activeId);
|
|
13483
|
+
lockReleasePoint() {
|
|
13484
|
+
return this.inner.lockReleasePoint;
|
|
13254
13485
|
}
|
|
13255
|
-
|
|
13256
|
-
return
|
|
13486
|
+
operator() {
|
|
13487
|
+
return this.inner.operator;
|
|
13257
13488
|
}
|
|
13258
|
-
|
|
13259
|
-
|
|
13260
|
-
const deltaX = x0.neg().div(maxDeltaId);
|
|
13261
|
-
const totalAmountX = toAmountIntoBins(
|
|
13262
|
-
activeId,
|
|
13263
|
-
minDeltaId,
|
|
13264
|
-
maxDeltaId,
|
|
13265
|
-
deltaX,
|
|
13266
|
-
new (0, _bnjs2.default)(0),
|
|
13267
|
-
x0,
|
|
13268
|
-
new (0, _bnjs2.default)(0),
|
|
13269
|
-
binStep,
|
|
13270
|
-
favorXInActiveBin
|
|
13271
|
-
).reduce((acc, bin) => {
|
|
13272
|
-
return acc.add(bin.amountX);
|
|
13273
|
-
}, new (0, _bnjs2.default)(0));
|
|
13274
|
-
return {
|
|
13275
|
-
base: x0,
|
|
13276
|
-
delta: deltaX,
|
|
13277
|
-
amountX: totalAmountX
|
|
13278
|
-
};
|
|
13489
|
+
totalClaimedFeeYAmount() {
|
|
13490
|
+
return this.inner.totalClaimedFeeYAmount;
|
|
13279
13491
|
}
|
|
13280
|
-
|
|
13281
|
-
|
|
13282
|
-
const m2 = maxDeltaId.neg();
|
|
13283
|
-
const a1 = m1.sub(m2).addn(1);
|
|
13284
|
-
const a2 = m1.mul(m1.addn(1)).divn(2);
|
|
13285
|
-
const a3 = m2.mul(m2.subn(1)).divn(2);
|
|
13286
|
-
const a = m1.sub(a3.sub(a2)).div(m1);
|
|
13287
|
-
const y0 = amountXInQuoteValue.div(a);
|
|
13288
|
-
const deltaY = y0.neg().div(m1);
|
|
13289
|
-
const amountY = toAmountIntoBins(
|
|
13290
|
-
activeId,
|
|
13291
|
-
minDeltaId,
|
|
13292
|
-
maxDeltaId,
|
|
13293
|
-
new (0, _bnjs2.default)(0),
|
|
13294
|
-
deltaY,
|
|
13295
|
-
new (0, _bnjs2.default)(0),
|
|
13296
|
-
y0,
|
|
13297
|
-
binStep,
|
|
13298
|
-
favorXInActiveBin
|
|
13299
|
-
).reduce((acc, bin) => {
|
|
13300
|
-
return acc.add(bin.amountY);
|
|
13301
|
-
}, new (0, _bnjs2.default)(0));
|
|
13302
|
-
return {
|
|
13303
|
-
base: y0,
|
|
13304
|
-
delta: deltaY,
|
|
13305
|
-
amountY
|
|
13306
|
-
};
|
|
13492
|
+
totalClaimedFeeXAmount() {
|
|
13493
|
+
return this.inner.totalClaimedFeeXAmount;
|
|
13307
13494
|
}
|
|
13308
|
-
|
|
13309
|
-
|
|
13310
|
-
// src/dlmm/helpers/rebalance/liquidity_strategy/spot.ts
|
|
13311
|
-
|
|
13312
|
-
function findY0(amountY, minDeltaId, maxDeltaId) {
|
|
13313
|
-
if (minDeltaId.gt(maxDeltaId) || amountY.lte(new (0, _bnjs2.default)(0)) || amountY.isZero()) {
|
|
13314
|
-
return new (0, _bnjs2.default)(0);
|
|
13495
|
+
lbPair() {
|
|
13496
|
+
return this.inner.lbPair;
|
|
13315
13497
|
}
|
|
13316
|
-
|
|
13317
|
-
|
|
13318
|
-
const delta = m1.sub(m2).addn(1);
|
|
13319
|
-
return amountY.div(delta);
|
|
13320
|
-
}
|
|
13321
|
-
function findBaseX02(amountX, minDeltaId, maxDeltaId, binStep, activeId) {
|
|
13322
|
-
let totalWeight = new (0, _bnjs2.default)(0);
|
|
13323
|
-
const minBinId = activeId.add(minDeltaId);
|
|
13324
|
-
const maxBinId = activeId.add(maxDeltaId);
|
|
13325
|
-
let baseFactor = getQPriceBaseFactor(binStep);
|
|
13326
|
-
let basePrice = getQPriceFromId(maxBinId.neg(), binStep);
|
|
13327
|
-
for (let binId = minBinId.toNumber(); binId <= maxBinId.toNumber(); binId++) {
|
|
13328
|
-
totalWeight = totalWeight.add(basePrice);
|
|
13329
|
-
basePrice = basePrice.mul(baseFactor).shrn(SCALE_OFFSET);
|
|
13498
|
+
lowerBinId() {
|
|
13499
|
+
return new (0, _bnjs2.default)(this.inner.lowerBinId);
|
|
13330
13500
|
}
|
|
13331
|
-
|
|
13332
|
-
|
|
13333
|
-
|
|
13334
|
-
|
|
13335
|
-
return
|
|
13501
|
+
upperBinId() {
|
|
13502
|
+
return new (0, _bnjs2.default)(this.inner.upperBinId);
|
|
13503
|
+
}
|
|
13504
|
+
liquidityShares() {
|
|
13505
|
+
return this.combinedPositionBinData.liquidityShares;
|
|
13506
|
+
}
|
|
13507
|
+
rewardInfos() {
|
|
13508
|
+
return this.combinedPositionBinData.rewardInfos;
|
|
13509
|
+
}
|
|
13510
|
+
feeInfos() {
|
|
13511
|
+
return this.combinedPositionBinData.feeInfos;
|
|
13336
13512
|
}
|
|
13337
|
-
|
|
13338
|
-
|
|
13339
|
-
|
|
13340
|
-
|
|
13341
|
-
|
|
13342
|
-
|
|
13343
|
-
|
|
13344
|
-
new (0, _bnjs2.default)(0),
|
|
13345
|
-
x0
|
|
13346
|
-
);
|
|
13347
|
-
const totalAmountX = amountInBins.reduce((acc, bin) => {
|
|
13348
|
-
return acc.add(bin.amountX);
|
|
13349
|
-
}, new (0, _bnjs2.default)(0));
|
|
13350
|
-
if (totalAmountX.lt(amountX)) {
|
|
13351
|
-
x0 = x0.add(new (0, _bnjs2.default)(1));
|
|
13513
|
+
lastUpdatedAt() {
|
|
13514
|
+
return this.inner.lastUpdatedAt;
|
|
13515
|
+
}
|
|
13516
|
+
getBinArrayIndexesCoverage() {
|
|
13517
|
+
const isExtended = this.extended.length > 0;
|
|
13518
|
+
if (isExtended) {
|
|
13519
|
+
return getBinArrayIndexesCoverage(this.lowerBinId(), this.upperBinId());
|
|
13352
13520
|
} else {
|
|
13353
|
-
|
|
13354
|
-
|
|
13521
|
+
const lowerBinArrayIndex = binIdToBinArrayIndex(this.lowerBinId());
|
|
13522
|
+
const upperBinArrayIndex = lowerBinArrayIndex.add(new (0, _bnjs2.default)(1));
|
|
13523
|
+
return [lowerBinArrayIndex, upperBinArrayIndex];
|
|
13355
13524
|
}
|
|
13356
13525
|
}
|
|
13357
|
-
|
|
13358
|
-
|
|
13359
|
-
|
|
13360
|
-
|
|
13361
|
-
base: findX0(amountX, minDeltaId, maxDeltaId, binStep, activeId),
|
|
13362
|
-
delta: new (0, _bnjs2.default)(0)
|
|
13363
|
-
};
|
|
13526
|
+
getBinArrayKeysCoverage(programId) {
|
|
13527
|
+
return this.getBinArrayIndexesCoverage().map(
|
|
13528
|
+
(index) => deriveBinArray(this.lbPair(), index, programId)[0]
|
|
13529
|
+
);
|
|
13364
13530
|
}
|
|
13365
|
-
|
|
13366
|
-
return
|
|
13367
|
-
base: findY0(amountY, minDeltaId, maxDeltaId),
|
|
13368
|
-
delta: new (0, _bnjs2.default)(0)
|
|
13369
|
-
};
|
|
13531
|
+
version() {
|
|
13532
|
+
return 1 /* V2 */;
|
|
13370
13533
|
}
|
|
13371
|
-
|
|
13372
|
-
|
|
13373
|
-
const totalAmountX = toAmountIntoBins(
|
|
13374
|
-
activeId,
|
|
13375
|
-
minDeltaId,
|
|
13376
|
-
maxDeltaId,
|
|
13377
|
-
new (0, _bnjs2.default)(0),
|
|
13378
|
-
new (0, _bnjs2.default)(0),
|
|
13379
|
-
x0,
|
|
13380
|
-
new (0, _bnjs2.default)(0),
|
|
13381
|
-
binStep,
|
|
13382
|
-
favorXInActiveBin
|
|
13383
|
-
).reduce((acc, bin) => {
|
|
13384
|
-
return acc.add(bin.amountX);
|
|
13385
|
-
}, new (0, _bnjs2.default)(0));
|
|
13386
|
-
return {
|
|
13387
|
-
base: new (0, _bnjs2.default)(x0.toString()),
|
|
13388
|
-
delta: new (0, _bnjs2.default)(0),
|
|
13389
|
-
amountX: totalAmountX
|
|
13390
|
-
};
|
|
13534
|
+
owner() {
|
|
13535
|
+
return this.inner.owner;
|
|
13391
13536
|
}
|
|
13392
|
-
|
|
13393
|
-
|
|
13394
|
-
const amountY = toAmountIntoBins(
|
|
13395
|
-
activeId,
|
|
13396
|
-
minDeltaId,
|
|
13397
|
-
maxDeltaId,
|
|
13398
|
-
new (0, _bnjs2.default)(0),
|
|
13399
|
-
new (0, _bnjs2.default)(0),
|
|
13400
|
-
new (0, _bnjs2.default)(0),
|
|
13401
|
-
y0,
|
|
13402
|
-
binStep,
|
|
13403
|
-
favorXInActiveBin
|
|
13404
|
-
).reduce((acc, bin) => {
|
|
13405
|
-
return acc.add(bin.amountY);
|
|
13406
|
-
}, new (0, _bnjs2.default)(0));
|
|
13407
|
-
return {
|
|
13408
|
-
base: y0,
|
|
13409
|
-
delta: new (0, _bnjs2.default)(0),
|
|
13410
|
-
amountY
|
|
13411
|
-
};
|
|
13537
|
+
width() {
|
|
13538
|
+
return this.upperBinId().sub(this.lowerBinId()).add(new (0, _bnjs2.default)(1));
|
|
13412
13539
|
}
|
|
13413
13540
|
};
|
|
13414
13541
|
|
|
13415
|
-
// src/dlmm/helpers/
|
|
13416
|
-
|
|
13417
|
-
|
|
13418
|
-
|
|
13419
|
-
|
|
13420
|
-
|
|
13421
|
-
|
|
13422
|
-
return new CurveStrategyParameterBuilder();
|
|
13423
|
-
case 2 /* BidAsk */:
|
|
13424
|
-
return new BidAskStrategyParameterBuilder();
|
|
13425
|
-
default:
|
|
13426
|
-
throw new Error("Strategy not supported");
|
|
13542
|
+
// src/dlmm/helpers/positions/index.ts
|
|
13543
|
+
function getBinArrayIndexesCoverage(lowerBinId, upperBinId) {
|
|
13544
|
+
const lowerBinArrayIndex = binIdToBinArrayIndex(lowerBinId);
|
|
13545
|
+
const upperBinArrayIndex = binIdToBinArrayIndex(upperBinId);
|
|
13546
|
+
const binArrayIndexes = [];
|
|
13547
|
+
for (let i = lowerBinArrayIndex.toNumber(); i <= upperBinArrayIndex.toNumber(); i++) {
|
|
13548
|
+
binArrayIndexes.push(new (0, _bnjs2.default)(i));
|
|
13427
13549
|
}
|
|
13550
|
+
return binArrayIndexes;
|
|
13428
13551
|
}
|
|
13429
|
-
function
|
|
13430
|
-
|
|
13431
|
-
|
|
13432
|
-
|
|
13433
|
-
|
|
13434
|
-
|
|
13435
|
-
|
|
13436
|
-
|
|
13552
|
+
function getBinArrayKeysCoverage2(lowerBinId, upperBinId, lbPair, programId) {
|
|
13553
|
+
const binArrayIndexes = getBinArrayIndexesCoverage(lowerBinId, upperBinId);
|
|
13554
|
+
return binArrayIndexes.map((index) => {
|
|
13555
|
+
return deriveBinArray(lbPair, index, programId)[0];
|
|
13556
|
+
});
|
|
13557
|
+
}
|
|
13558
|
+
function getBinArrayAccountMetasCoverage(lowerBinId, upperBinId, lbPair, programId) {
|
|
13559
|
+
return getBinArrayKeysCoverage2(lowerBinId, upperBinId, lbPair, programId).map(
|
|
13560
|
+
(key) => {
|
|
13561
|
+
return {
|
|
13562
|
+
pubkey: key,
|
|
13563
|
+
isSigner: false,
|
|
13564
|
+
isWritable: true
|
|
13565
|
+
};
|
|
13566
|
+
}
|
|
13567
|
+
);
|
|
13568
|
+
}
|
|
13569
|
+
function getPositionLowerUpperBinIdWithLiquidity(position) {
|
|
13570
|
+
const binWithLiquidity = position.positionBinData.filter(
|
|
13571
|
+
(b) => !new (0, _bnjs2.default)(b.binLiquidity).isZero() || !new (0, _bnjs2.default)(b.positionFeeXAmount.toString()).isZero() || !new (0, _bnjs2.default)(b.positionFeeYAmount.toString()).isZero() || !new (0, _bnjs2.default)(b.positionRewardAmount[0].toString()).isZero() || !new (0, _bnjs2.default)(b.positionRewardAmount[1].toString()).isZero()
|
|
13572
|
+
);
|
|
13573
|
+
return binWithLiquidity.length > 0 ? {
|
|
13574
|
+
lowerBinId: new (0, _bnjs2.default)(binWithLiquidity[0].binId),
|
|
13575
|
+
upperBinId: new (0, _bnjs2.default)(binWithLiquidity[binWithLiquidity.length - 1].binId)
|
|
13576
|
+
} : null;
|
|
13577
|
+
}
|
|
13578
|
+
function isPositionNoFee(position) {
|
|
13579
|
+
return position.feeX.isZero() && position.feeY.isZero();
|
|
13580
|
+
}
|
|
13581
|
+
function isPositionNoReward(position) {
|
|
13582
|
+
return position.rewardOne.isZero() && position.rewardTwo.isZero();
|
|
13583
|
+
}
|
|
13584
|
+
function chunkBinRange(minBinId, maxBinId) {
|
|
13585
|
+
const chunkedBinRange = [];
|
|
13586
|
+
let startBinId = minBinId;
|
|
13587
|
+
while (startBinId <= maxBinId) {
|
|
13588
|
+
const endBinId = Math.min(
|
|
13589
|
+
startBinId + DEFAULT_BIN_PER_POSITION.toNumber() - 1,
|
|
13590
|
+
maxBinId
|
|
13591
|
+
);
|
|
13592
|
+
chunkedBinRange.push({
|
|
13593
|
+
lowerBinId: startBinId,
|
|
13594
|
+
upperBinId: endBinId
|
|
13595
|
+
});
|
|
13596
|
+
startBinId += DEFAULT_BIN_PER_POSITION.toNumber();
|
|
13437
13597
|
}
|
|
13438
|
-
|
|
13439
|
-
|
|
13440
|
-
|
|
13441
|
-
|
|
13442
|
-
|
|
13443
|
-
|
|
13444
|
-
|
|
13445
|
-
|
|
13598
|
+
return chunkedBinRange;
|
|
13599
|
+
}
|
|
13600
|
+
async function getPositionExpandRentExemption(currentMinBinId, currentMaxBinId, connection, binCountToExpand) {
|
|
13601
|
+
const currentPositionWidth = currentMaxBinId.sub(currentMinBinId).addn(1);
|
|
13602
|
+
const positionWidthAfterExpand = currentPositionWidth.add(binCountToExpand);
|
|
13603
|
+
if (positionWidthAfterExpand.lte(DEFAULT_BIN_PER_POSITION)) {
|
|
13604
|
+
return 0;
|
|
13605
|
+
} else {
|
|
13606
|
+
const binCountInExpandedBytes = positionWidthAfterExpand.sub(
|
|
13607
|
+
DEFAULT_BIN_PER_POSITION
|
|
13446
13608
|
);
|
|
13447
|
-
|
|
13448
|
-
|
|
13449
|
-
|
|
13450
|
-
|
|
13451
|
-
|
|
13452
|
-
|
|
13609
|
+
const expandSize = binCountInExpandedBytes.toNumber() * POSITION_BIN_DATA_SIZE;
|
|
13610
|
+
const [minimumLamports, rentExemptionLamports] = await Promise.all([
|
|
13611
|
+
connection.getMinimumBalanceForRentExemption(0),
|
|
13612
|
+
connection.getMinimumBalanceForRentExemption(expandSize)
|
|
13613
|
+
]);
|
|
13614
|
+
return rentExemptionLamports - minimumLamports;
|
|
13453
13615
|
}
|
|
13454
|
-
|
|
13455
|
-
|
|
13456
|
-
|
|
13457
|
-
|
|
13458
|
-
|
|
13459
|
-
|
|
13460
|
-
|
|
13616
|
+
}
|
|
13617
|
+
function getExtendedPositionBinCount(minBinId, maxBinId) {
|
|
13618
|
+
const width = maxBinId.sub(minBinId).addn(1);
|
|
13619
|
+
const extended = width.sub(DEFAULT_BIN_PER_POSITION);
|
|
13620
|
+
return extended.lte(new (0, _bnjs2.default)(0)) ? new (0, _bnjs2.default)(0) : extended;
|
|
13621
|
+
}
|
|
13622
|
+
function decodeExtendedPosition(base, program, bytes) {
|
|
13623
|
+
const width = base.upperBinId - base.lowerBinId + 1;
|
|
13624
|
+
const extendedWidth = width - DEFAULT_BIN_PER_POSITION.toNumber();
|
|
13625
|
+
const extendedPosition = [];
|
|
13626
|
+
for (let i = 0; i < extendedWidth; i++) {
|
|
13627
|
+
const offset = i * POSITION_BIN_DATA_SIZE;
|
|
13628
|
+
const data = bytes.subarray(offset, offset + POSITION_BIN_DATA_SIZE);
|
|
13629
|
+
const decodedPositionBinData = program.coder.types.decode(
|
|
13630
|
+
// TODO: Find a type safe way
|
|
13631
|
+
"positionBinData",
|
|
13632
|
+
data
|
|
13461
13633
|
);
|
|
13462
|
-
|
|
13463
|
-
x0: base,
|
|
13464
|
-
deltaX: delta,
|
|
13465
|
-
y0: new (0, _bnjs2.default)(0),
|
|
13466
|
-
deltaY: new (0, _bnjs2.default)(0)
|
|
13467
|
-
};
|
|
13634
|
+
extendedPosition.push(decodedPositionBinData);
|
|
13468
13635
|
}
|
|
13469
|
-
|
|
13470
|
-
const minDeltaIdAskSide = favorXInActiveId ? new (0, _bnjs2.default)(0) : new (0, _bnjs2.default)(1);
|
|
13471
|
-
const { base: y0, delta: deltaY } = strategyParameterBuilder.findYParameters(
|
|
13472
|
-
amountY,
|
|
13473
|
-
minDeltaId,
|
|
13474
|
-
maxDeltaIdBidSide,
|
|
13475
|
-
activeId
|
|
13476
|
-
);
|
|
13477
|
-
const { base: x0, delta: deltaX } = strategyParameterBuilder.findXParameters(
|
|
13478
|
-
amountX,
|
|
13479
|
-
minDeltaIdAskSide,
|
|
13480
|
-
maxDeltaId,
|
|
13481
|
-
binStep,
|
|
13482
|
-
activeId
|
|
13483
|
-
);
|
|
13484
|
-
return {
|
|
13485
|
-
x0,
|
|
13486
|
-
deltaX,
|
|
13487
|
-
y0,
|
|
13488
|
-
deltaY
|
|
13489
|
-
};
|
|
13636
|
+
return extendedPosition;
|
|
13490
13637
|
}
|
|
13491
13638
|
|
|
13492
13639
|
// src/dlmm/helpers/rebalance/strategy/balanced.ts
|
|
@@ -19477,5 +19624,15 @@ var src_default = DLMM;
|
|
|
19477
19624
|
|
|
19478
19625
|
|
|
19479
19626
|
|
|
19480
|
-
|
|
19627
|
+
|
|
19628
|
+
|
|
19629
|
+
|
|
19630
|
+
|
|
19631
|
+
|
|
19632
|
+
|
|
19633
|
+
|
|
19634
|
+
|
|
19635
|
+
|
|
19636
|
+
|
|
19637
|
+
exports.ADMIN = ADMIN; exports.ActionType = ActionType; exports.ActivationType = ActivationType; exports.BASIS_POINT_MAX = BASIS_POINT_MAX; exports.BIN_ARRAY_BITMAP_FEE = BIN_ARRAY_BITMAP_FEE; exports.BIN_ARRAY_BITMAP_FEE_BN = BIN_ARRAY_BITMAP_FEE_BN; exports.BIN_ARRAY_BITMAP_SIZE = BIN_ARRAY_BITMAP_SIZE; exports.BIN_ARRAY_FEE = BIN_ARRAY_FEE; exports.BIN_ARRAY_FEE_BN = BIN_ARRAY_FEE_BN; exports.BinLiquidity = BinLiquidity; exports.BitmapType = BitmapType; exports.ClockLayout = ClockLayout; exports.DEFAULT_BIN_PER_POSITION = DEFAULT_BIN_PER_POSITION; exports.DLMMError = DLMMError; exports.DlmmSdkError = DlmmSdkError; exports.EXTENSION_BINARRAY_BITMAP_SIZE = EXTENSION_BINARRAY_BITMAP_SIZE; exports.FEE_PRECISION = FEE_PRECISION; exports.IDL = dlmm_default; exports.ILM_BASE = ILM_BASE; exports.LBCLMM_PROGRAM_IDS = LBCLMM_PROGRAM_IDS; exports.MAX_ACTIVE_BIN_SLIPPAGE = MAX_ACTIVE_BIN_SLIPPAGE; exports.MAX_BINS_PER_POSITION = MAX_BINS_PER_POSITION; 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_CLAIM_ALL_ALLOWED = MAX_CLAIM_ALL_ALLOWED; exports.MAX_EXTRA_BIN_ARRAYS = MAX_EXTRA_BIN_ARRAYS; exports.MAX_FEE_RATE = MAX_FEE_RATE; exports.MAX_RESIZE_LENGTH = MAX_RESIZE_LENGTH; exports.MEMO_PROGRAM_ID = MEMO_PROGRAM_ID; exports.Network = Network; exports.POOL_FEE = POOL_FEE; exports.POOL_FEE_BN = POOL_FEE_BN; exports.POSITION_BIN_DATA_SIZE = POSITION_BIN_DATA_SIZE; exports.POSITION_FEE = POSITION_FEE; exports.POSITION_FEE_BN = POSITION_FEE_BN; exports.POSITION_MAX_LENGTH = POSITION_MAX_LENGTH; exports.POSITION_MIN_SIZE = POSITION_MIN_SIZE; exports.PRECISION = PRECISION; exports.PairStatus = PairStatus; exports.PairType = PairType; exports.PositionVersion = PositionVersion; exports.RebalancePosition = RebalancePosition; exports.ResizeSide = ResizeSide; exports.SCALE = SCALE; exports.SCALE_OFFSET = SCALE_OFFSET; exports.SIMULATION_USER = SIMULATION_USER; exports.Strategy = Strategy; exports.StrategyType = StrategyType; exports.TOKEN_ACCOUNT_FEE = TOKEN_ACCOUNT_FEE; exports.TOKEN_ACCOUNT_FEE_BN = TOKEN_ACCOUNT_FEE_BN; exports.U64_MAX = U64_MAX; exports.autoFillXByStrategy = autoFillXByStrategy; exports.autoFillXByWeight = autoFillXByWeight; exports.autoFillYByStrategy = autoFillYByStrategy; exports.autoFillYByWeight = autoFillYByWeight; exports.binIdToBinArrayIndex = binIdToBinArrayIndex; exports.buildLiquidityStrategyParameters = buildLiquidityStrategyParameters; exports.calculateBidAskDistribution = calculateBidAskDistribution; exports.calculateNormalDistribution = calculateNormalDistribution; exports.calculateSpotDistribution = calculateSpotDistribution; exports.capSlippagePercentage = capSlippagePercentage; exports.chunkedFetchMultipleBinArrayBitmapExtensionAccount = chunkedFetchMultipleBinArrayBitmapExtensionAccount; exports.chunkedFetchMultiplePoolAccount = chunkedFetchMultiplePoolAccount; exports.chunkedGetMultipleAccountInfos = chunkedGetMultipleAccountInfos; exports.chunks = chunks; exports.computeFee = computeFee; exports.computeFeeFromAmount = computeFeeFromAmount; exports.computeProtocolFee = computeProtocolFee; exports.createProgram = createProgram; exports.decodeAccount = decodeAccount; exports.default = src_default; exports.deriveBinArray = deriveBinArray; exports.deriveBinArrayBitmapExtension = deriveBinArrayBitmapExtension; exports.deriveCustomizablePermissionlessLbPair = deriveCustomizablePermissionlessLbPair; exports.deriveEventAuthority = deriveEventAuthority; exports.deriveLbPair = deriveLbPair; exports.deriveLbPair2 = deriveLbPair2; exports.deriveLbPairWithPresetParamWithIndexKey = deriveLbPairWithPresetParamWithIndexKey; exports.deriveOracle = deriveOracle; exports.derivePermissionLbPair = derivePermissionLbPair; exports.derivePosition = derivePosition; exports.derivePresetParameter = derivePresetParameter; exports.derivePresetParameter2 = derivePresetParameter2; exports.derivePresetParameterWithIndex = derivePresetParameterWithIndex; exports.deriveReserve = deriveReserve; exports.deriveRewardVault = deriveRewardVault; exports.deriveTokenBadge = deriveTokenBadge; exports.enumerateBins = enumerateBins; exports.findNextBinArrayIndexWithLiquidity = findNextBinArrayIndexWithLiquidity; exports.findNextBinArrayWithLiquidity = findNextBinArrayWithLiquidity; exports.fromWeightDistributionToAmount = fromWeightDistributionToAmount; exports.fromWeightDistributionToAmountOneSide = fromWeightDistributionToAmountOneSide; exports.getAccountDiscriminator = getAccountDiscriminator; exports.getAmountInBinsAskSide = getAmountInBinsAskSide; exports.getAmountInBinsBidSide = getAmountInBinsBidSide; exports.getAutoFillAmountByRebalancedPosition = getAutoFillAmountByRebalancedPosition; exports.getBaseFee = getBaseFee; exports.getBinArrayLowerUpperBinId = getBinArrayLowerUpperBinId; exports.getBinArraysRequiredByPositionRange = getBinArraysRequiredByPositionRange; exports.getBinFromBinArray = getBinFromBinArray; exports.getBinIdIndexInBinArray = getBinIdIndexInBinArray; exports.getEstimatedComputeUnitIxWithBuffer = getEstimatedComputeUnitIxWithBuffer; exports.getEstimatedComputeUnitUsageWithBuffer = getEstimatedComputeUnitUsageWithBuffer; exports.getLiquidityStrategyParameterBuilder = getLiquidityStrategyParameterBuilder; exports.getOrCreateATAInstruction = getOrCreateATAInstruction; exports.getOutAmount = getOutAmount; exports.getPriceOfBinByBinId = getPriceOfBinByBinId; exports.getRebalanceBinArrayIndexesAndBitmapCoverage = getRebalanceBinArrayIndexesAndBitmapCoverage; exports.getTokenBalance = getTokenBalance; exports.getTokenDecimals = getTokenDecimals; exports.getTokenProgramId = getTokenProgramId; exports.getTokensMintFromPoolAddress = getTokensMintFromPoolAddress; exports.getTotalFee = getTotalFee; exports.getVariableFee = getVariableFee; exports.isBinIdWithinBinArray = isBinIdWithinBinArray; exports.isOverflowDefaultBinArrayBitmap = isOverflowDefaultBinArrayBitmap; exports.parseLogs = parseLogs; exports.range = range; exports.suggestBalancedXParametersFromY = suggestBalancedXParametersFromY; exports.suggestBalancedYParametersFromX = suggestBalancedYParametersFromX; exports.swapExactInQuoteAtBin = swapExactInQuoteAtBin; exports.swapExactOutQuoteAtBin = swapExactOutQuoteAtBin; exports.toAmountAskSide = toAmountAskSide; exports.toAmountBidSide = toAmountBidSide; exports.toAmountBothSide = toAmountBothSide; exports.toAmountIntoBins = toAmountIntoBins; exports.toAmountsBothSideByStrategy = toAmountsBothSideByStrategy; exports.toStrategyParameters = toStrategyParameters; exports.toWeightDistribution = toWeightDistribution; exports.unwrapSOLInstruction = unwrapSOLInstruction; exports.updateBinArray = updateBinArray; exports.wrapSOLInstruction = wrapSOLInstruction;
|
|
19481
19638
|
//# sourceMappingURL=index.js.map
|