@meteora-ag/cp-amm-sdk 1.2.2 → 1.2.3
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.mts +94 -5
- package/dist/index.d.ts +94 -5
- package/dist/index.js +226 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +226 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -7952,7 +7952,7 @@ var getSqrtPriceFromPrice = (price, tokenADecimal, tokenBDecimal) => {
|
|
|
7952
7952
|
const sqrtValueQ64 = sqrtValue.mul(Decimal.pow(2, 64));
|
|
7953
7953
|
return new BN2(sqrtValueQ64.floor().toFixed());
|
|
7954
7954
|
};
|
|
7955
|
-
var
|
|
7955
|
+
var getUnClaimLpFee = (poolState, positionState) => {
|
|
7956
7956
|
const totalPositionLiquidity = positionState.unlockedLiquidity.add(positionState.vestedLiquidity).add(positionState.permanentLockedLiquidity);
|
|
7957
7957
|
const feeAPerTokenStored = new BN2(
|
|
7958
7958
|
Buffer.from(poolState.feeAPerLiquidity).reverse()
|
|
@@ -7968,6 +7968,100 @@ var getUnClaimReward = (poolState, positionState) => {
|
|
|
7968
7968
|
rewards: positionState.rewardInfos.length > 0 ? positionState.rewardInfos.map((item) => item.rewardPendings) : []
|
|
7969
7969
|
};
|
|
7970
7970
|
};
|
|
7971
|
+
function getRewardPerTokenStore(poolReward, poolLiquidity, currentTime) {
|
|
7972
|
+
if (poolLiquidity.eq(new BN2(0))) {
|
|
7973
|
+
return new BN2(0);
|
|
7974
|
+
}
|
|
7975
|
+
const lastTimeRewardApplicable = BN2.min(
|
|
7976
|
+
currentTime,
|
|
7977
|
+
poolReward.rewardDurationEnd
|
|
7978
|
+
);
|
|
7979
|
+
const timePeriod = lastTimeRewardApplicable.sub(poolReward.lastUpdateTime);
|
|
7980
|
+
const currentTotalReward = timePeriod.mul(poolReward.rewardRate);
|
|
7981
|
+
const rewardPerTokenStore = currentTotalReward.shln(128).div(poolLiquidity);
|
|
7982
|
+
const totalRewardPerTokenStore = new BN2(
|
|
7983
|
+
Buffer.from(poolReward.rewardPerTokenStored).reverse()
|
|
7984
|
+
).add(rewardPerTokenStore);
|
|
7985
|
+
return totalRewardPerTokenStore;
|
|
7986
|
+
}
|
|
7987
|
+
function getRewardPerPeriod(poolReward, currentTime, periodTime) {
|
|
7988
|
+
const timeRewardAppicable = currentTime.add(periodTime);
|
|
7989
|
+
const period = timeRewardAppicable <= poolReward.rewardDurationEnd ? periodTime : poolReward.rewardDurationEnd.sub(currentTime);
|
|
7990
|
+
const rewardPerPeriod = poolReward.rewardRate.mul(period);
|
|
7991
|
+
return rewardPerPeriod;
|
|
7992
|
+
}
|
|
7993
|
+
function getRewardInfo(poolState, rewardIndex, periodTime, currentTime) {
|
|
7994
|
+
const poolReward = poolState.rewardInfos[rewardIndex];
|
|
7995
|
+
const rewardPerTokenStore = getRewardPerTokenStore(
|
|
7996
|
+
poolReward,
|
|
7997
|
+
poolState.liquidity,
|
|
7998
|
+
currentTime
|
|
7999
|
+
);
|
|
8000
|
+
const totalRewardDistributed = rewardPerTokenStore.mul(poolState.liquidity).shrn(192);
|
|
8001
|
+
if (poolReward.rewardDurationEnd <= currentTime) {
|
|
8002
|
+
return {
|
|
8003
|
+
rewardPerPeriod: new BN2(0),
|
|
8004
|
+
rewardBalance: new BN2(0),
|
|
8005
|
+
totalRewardDistributed
|
|
8006
|
+
};
|
|
8007
|
+
}
|
|
8008
|
+
const rewardPerPeriod = getRewardPerPeriod(
|
|
8009
|
+
poolReward,
|
|
8010
|
+
currentTime,
|
|
8011
|
+
periodTime
|
|
8012
|
+
);
|
|
8013
|
+
const remainTime = poolReward.rewardDurationEnd.sub(currentTime);
|
|
8014
|
+
const rewardBalance = poolReward.rewardRate.mul(remainTime).shrn(64);
|
|
8015
|
+
if (poolState.liquidity.eq(new BN2(0))) {
|
|
8016
|
+
return {
|
|
8017
|
+
rewardPerPeriod,
|
|
8018
|
+
rewardBalance,
|
|
8019
|
+
totalRewardDistributed: new BN2(0)
|
|
8020
|
+
};
|
|
8021
|
+
}
|
|
8022
|
+
return {
|
|
8023
|
+
rewardPerPeriod: rewardPerPeriod.shrn(64),
|
|
8024
|
+
rewardBalance,
|
|
8025
|
+
totalRewardDistributed
|
|
8026
|
+
};
|
|
8027
|
+
}
|
|
8028
|
+
function getUserRewardPending(poolState, positionState, rewardIndex, currentTime, periodTime) {
|
|
8029
|
+
if (poolState.liquidity.eq(new BN2(0))) {
|
|
8030
|
+
return {
|
|
8031
|
+
userRewardPerPeriod: new BN2(0),
|
|
8032
|
+
userPendingReward: new BN2(0)
|
|
8033
|
+
};
|
|
8034
|
+
}
|
|
8035
|
+
const poolReward = poolState.rewardInfos[rewardIndex];
|
|
8036
|
+
const userRewardInfo = positionState.rewardInfos[rewardIndex];
|
|
8037
|
+
const rewardPerTokenStore = getRewardPerTokenStore(
|
|
8038
|
+
poolReward,
|
|
8039
|
+
poolState.liquidity,
|
|
8040
|
+
currentTime
|
|
8041
|
+
);
|
|
8042
|
+
const totalPositionLiquidity = positionState.unlockedLiquidity.add(positionState.vestedLiquidity).add(positionState.permanentLockedLiquidity);
|
|
8043
|
+
const userRewardPerTokenCheckPoint = new BN2(
|
|
8044
|
+
Buffer.from(userRewardInfo.rewardPerTokenCheckpoint).reverse()
|
|
8045
|
+
);
|
|
8046
|
+
const newReward = totalPositionLiquidity.mul(rewardPerTokenStore.sub(userRewardPerTokenCheckPoint)).shrn(192);
|
|
8047
|
+
if (poolReward.rewardDurationEnd <= currentTime) {
|
|
8048
|
+
return {
|
|
8049
|
+
userPendingReward: userRewardInfo.rewardPendings.add(newReward),
|
|
8050
|
+
userRewardPerPeriod: new BN2(0)
|
|
8051
|
+
};
|
|
8052
|
+
}
|
|
8053
|
+
const rewardPerPeriod = getRewardPerPeriod(
|
|
8054
|
+
poolReward,
|
|
8055
|
+
currentTime,
|
|
8056
|
+
periodTime
|
|
8057
|
+
);
|
|
8058
|
+
const rewardPerTokenStorePerPeriod = rewardPerPeriod.shln(128).div(poolState.liquidity);
|
|
8059
|
+
const userRewardPerPeriod = totalPositionLiquidity.mul(rewardPerTokenStorePerPeriod).shrn(192);
|
|
8060
|
+
return {
|
|
8061
|
+
userPendingReward: userRewardInfo.rewardPendings.add(newReward),
|
|
8062
|
+
userRewardPerPeriod
|
|
8063
|
+
};
|
|
8064
|
+
}
|
|
7971
8065
|
|
|
7972
8066
|
// src/helpers/accountFilters.ts
|
|
7973
8067
|
var positionByPoolFilter = (pool) => {
|
|
@@ -10564,6 +10658,11 @@ var CpAmm = class {
|
|
|
10564
10658
|
return positionResult;
|
|
10565
10659
|
});
|
|
10566
10660
|
}
|
|
10661
|
+
/**
|
|
10662
|
+
* Retrieves all vesting accounts associated with a position.
|
|
10663
|
+
* @param position - Public key of the position.
|
|
10664
|
+
* @returns Array of vesting account public keys and their states.
|
|
10665
|
+
*/
|
|
10567
10666
|
getAllVestingsByPosition(position) {
|
|
10568
10667
|
return __async(this, null, function* () {
|
|
10569
10668
|
const vestings = yield this._program.account.vesting.all([
|
|
@@ -10572,12 +10671,22 @@ var CpAmm = class {
|
|
|
10572
10671
|
return vestings;
|
|
10573
10672
|
});
|
|
10574
10673
|
}
|
|
10674
|
+
/**
|
|
10675
|
+
* Checks if a position has any locked liquidity.
|
|
10676
|
+
* @param position - Position state.
|
|
10677
|
+
* @returns True if the position has locked liquidity, false otherwise.
|
|
10678
|
+
*/
|
|
10575
10679
|
isLockedPosition(position) {
|
|
10576
10680
|
const totalLockedLiquidity = position.vestedLiquidity.add(
|
|
10577
10681
|
position.permanentLockedLiquidity
|
|
10578
10682
|
);
|
|
10579
10683
|
return totalLockedLiquidity.gtn(0);
|
|
10580
10684
|
}
|
|
10685
|
+
/**
|
|
10686
|
+
* Checks if a position is permanently locked.
|
|
10687
|
+
* @param positionState - Position state.
|
|
10688
|
+
* @returns True if the position is permanently locked, false otherwise.
|
|
10689
|
+
*/
|
|
10581
10690
|
isPermanentLockedPosition(positionState) {
|
|
10582
10691
|
return positionState.permanentLockedLiquidity.gtn(0);
|
|
10583
10692
|
}
|
|
@@ -10616,6 +10725,11 @@ var CpAmm = class {
|
|
|
10616
10725
|
}
|
|
10617
10726
|
return { canUnlock: true };
|
|
10618
10727
|
}
|
|
10728
|
+
/**
|
|
10729
|
+
* Checks if a pool exists.
|
|
10730
|
+
* @param pool - Public key of the pool.
|
|
10731
|
+
* @returns True if the pool exists, false otherwise.
|
|
10732
|
+
*/
|
|
10619
10733
|
isPoolExist(pool) {
|
|
10620
10734
|
return __async(this, null, function* () {
|
|
10621
10735
|
const poolState = yield this._program.account.pool.fetchNullable(pool);
|
|
@@ -10691,6 +10805,11 @@ var CpAmm = class {
|
|
|
10691
10805
|
priceImpact: swapResult.priceImpact
|
|
10692
10806
|
};
|
|
10693
10807
|
}
|
|
10808
|
+
/**
|
|
10809
|
+
* Calculates the expected output amount or input amount for a swap depending on the swap mode.
|
|
10810
|
+
* @param params GetQuote2Params
|
|
10811
|
+
* @returns Quote2Result
|
|
10812
|
+
*/
|
|
10694
10813
|
getQuote2(params) {
|
|
10695
10814
|
const {
|
|
10696
10815
|
inputTokenMint,
|
|
@@ -11130,6 +11249,11 @@ var CpAmm = class {
|
|
|
11130
11249
|
return { tx: transaction, pool, position };
|
|
11131
11250
|
});
|
|
11132
11251
|
}
|
|
11252
|
+
/**
|
|
11253
|
+
* Builds a transaction to create a customizable pool with dynamic config.
|
|
11254
|
+
* @param params InitializeCustomizeablePoolWithDynamicConfigParams
|
|
11255
|
+
* @returns Transaction and related addresses.
|
|
11256
|
+
*/
|
|
11133
11257
|
createCustomPoolWithDynamicConfig(params) {
|
|
11134
11258
|
return __async(this, null, function* () {
|
|
11135
11259
|
const {
|
|
@@ -11667,6 +11791,11 @@ var CpAmm = class {
|
|
|
11667
11791
|
}).remainingAccounts(remainingAccounts).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
|
|
11668
11792
|
});
|
|
11669
11793
|
}
|
|
11794
|
+
/**
|
|
11795
|
+
* Builds a transaction to perform a swap in the pool.
|
|
11796
|
+
* @param params Swap2Params
|
|
11797
|
+
* @returns Transaction builder.
|
|
11798
|
+
*/
|
|
11670
11799
|
swap2(params) {
|
|
11671
11800
|
return __async(this, null, function* () {
|
|
11672
11801
|
const {
|
|
@@ -11835,6 +11964,11 @@ var CpAmm = class {
|
|
|
11835
11964
|
return new Transaction().add(instruction);
|
|
11836
11965
|
});
|
|
11837
11966
|
}
|
|
11967
|
+
/**
|
|
11968
|
+
* Builds a transaction to close a position.
|
|
11969
|
+
* @param params ClosePositionParams
|
|
11970
|
+
* @returns Transaction builder.
|
|
11971
|
+
*/
|
|
11838
11972
|
closePosition(params) {
|
|
11839
11973
|
return __async(this, null, function* () {
|
|
11840
11974
|
const { owner, pool, position, positionNftMint, positionNftAccount } = params;
|
|
@@ -12077,6 +12211,77 @@ var CpAmm = class {
|
|
|
12077
12211
|
return transaction;
|
|
12078
12212
|
});
|
|
12079
12213
|
}
|
|
12214
|
+
/**
|
|
12215
|
+
* Builds a transaction to initialize a reward for a pool.
|
|
12216
|
+
* @param params InitializeRewardParams
|
|
12217
|
+
* @returns Transaction builder.
|
|
12218
|
+
*/
|
|
12219
|
+
initializeReward(params) {
|
|
12220
|
+
return __async(this, null, function* () {
|
|
12221
|
+
const {
|
|
12222
|
+
rewardIndex,
|
|
12223
|
+
rewardDuration,
|
|
12224
|
+
funder,
|
|
12225
|
+
pool,
|
|
12226
|
+
creator,
|
|
12227
|
+
payer,
|
|
12228
|
+
rewardMint,
|
|
12229
|
+
rewardMintProgram
|
|
12230
|
+
} = params;
|
|
12231
|
+
const rewardVault = deriveRewardVaultAddress(pool, rewardIndex);
|
|
12232
|
+
return yield this._program.methods.initializeReward(rewardIndex, rewardDuration, funder).accountsPartial({
|
|
12233
|
+
poolAuthority: this.poolAuthority,
|
|
12234
|
+
pool,
|
|
12235
|
+
rewardVault,
|
|
12236
|
+
rewardMint,
|
|
12237
|
+
signer: creator,
|
|
12238
|
+
payer,
|
|
12239
|
+
tokenProgram: rewardMintProgram
|
|
12240
|
+
}).transaction();
|
|
12241
|
+
});
|
|
12242
|
+
}
|
|
12243
|
+
/**
|
|
12244
|
+
* Builds a transaction to initialize and fund a reward for a pool.
|
|
12245
|
+
* @param params InitializeAndFundReward
|
|
12246
|
+
* @returns Transaction builder.
|
|
12247
|
+
*/
|
|
12248
|
+
initializeAndFundReward(params) {
|
|
12249
|
+
return __async(this, null, function* () {
|
|
12250
|
+
const {
|
|
12251
|
+
rewardIndex,
|
|
12252
|
+
rewardDuration,
|
|
12253
|
+
pool,
|
|
12254
|
+
creator,
|
|
12255
|
+
payer,
|
|
12256
|
+
rewardMint,
|
|
12257
|
+
carryForward,
|
|
12258
|
+
amount,
|
|
12259
|
+
rewardMintProgram
|
|
12260
|
+
} = params;
|
|
12261
|
+
const rewardVault = deriveRewardVaultAddress(pool, rewardIndex);
|
|
12262
|
+
const initializeRewardTx = yield this.initializeReward({
|
|
12263
|
+
rewardIndex,
|
|
12264
|
+
rewardDuration,
|
|
12265
|
+
funder: payer,
|
|
12266
|
+
pool,
|
|
12267
|
+
creator,
|
|
12268
|
+
payer,
|
|
12269
|
+
rewardMint,
|
|
12270
|
+
rewardMintProgram
|
|
12271
|
+
});
|
|
12272
|
+
const fundRewardTx = yield this.fundReward({
|
|
12273
|
+
rewardIndex,
|
|
12274
|
+
carryForward,
|
|
12275
|
+
pool,
|
|
12276
|
+
funder: payer,
|
|
12277
|
+
amount,
|
|
12278
|
+
rewardMint,
|
|
12279
|
+
rewardVault,
|
|
12280
|
+
rewardMintProgram
|
|
12281
|
+
});
|
|
12282
|
+
return new Transaction().add(initializeRewardTx).add(fundRewardTx);
|
|
12283
|
+
});
|
|
12284
|
+
}
|
|
12080
12285
|
/**
|
|
12081
12286
|
* Builds a transaction to update reward duration.
|
|
12082
12287
|
* @param {UpdateRewardDurationParams} params - Parameters including pool and new duration.
|
|
@@ -12084,10 +12289,10 @@ var CpAmm = class {
|
|
|
12084
12289
|
*/
|
|
12085
12290
|
updateRewardDuration(params) {
|
|
12086
12291
|
return __async(this, null, function* () {
|
|
12087
|
-
const { pool,
|
|
12292
|
+
const { pool, signer, rewardIndex, newDuration } = params;
|
|
12088
12293
|
return yield this._program.methods.updateRewardDuration(rewardIndex, newDuration).accountsPartial({
|
|
12089
12294
|
pool,
|
|
12090
|
-
signer
|
|
12295
|
+
signer
|
|
12091
12296
|
}).transaction();
|
|
12092
12297
|
});
|
|
12093
12298
|
}
|
|
@@ -12098,10 +12303,10 @@ var CpAmm = class {
|
|
|
12098
12303
|
*/
|
|
12099
12304
|
updateRewardFunder(params) {
|
|
12100
12305
|
return __async(this, null, function* () {
|
|
12101
|
-
const { pool,
|
|
12306
|
+
const { pool, signer, rewardIndex, newFunder } = params;
|
|
12102
12307
|
return yield this._program.methods.updateRewardFunder(rewardIndex, newFunder).accountsPartial({
|
|
12103
12308
|
pool,
|
|
12104
|
-
signer
|
|
12309
|
+
signer
|
|
12105
12310
|
}).transaction();
|
|
12106
12311
|
});
|
|
12107
12312
|
}
|
|
@@ -12378,9 +12583,9 @@ var CpAmm = class {
|
|
|
12378
12583
|
position,
|
|
12379
12584
|
positionNftAccount,
|
|
12380
12585
|
rewardIndex,
|
|
12381
|
-
skipReward,
|
|
12382
12586
|
poolState,
|
|
12383
|
-
positionState
|
|
12587
|
+
positionState,
|
|
12588
|
+
isSkipReward
|
|
12384
12589
|
} = params;
|
|
12385
12590
|
const rewardInfo = poolState.rewardInfos[rewardIndex];
|
|
12386
12591
|
const tokenProgram = getTokenProgram(rewardInfo.rewardTokenFlag);
|
|
@@ -12399,6 +12604,7 @@ var CpAmm = class {
|
|
|
12399
12604
|
const closeWrappedSOLIx = yield unwrapSOLInstruction(user);
|
|
12400
12605
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
12401
12606
|
}
|
|
12607
|
+
const skipReward = isSkipReward ? 1 : 0;
|
|
12402
12608
|
return yield this._program.methods.claimReward(rewardIndex, skipReward).accountsPartial({
|
|
12403
12609
|
pool: positionState.pool,
|
|
12404
12610
|
positionNftAccount,
|
|
@@ -12412,6 +12618,11 @@ var CpAmm = class {
|
|
|
12412
12618
|
}).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
|
|
12413
12619
|
});
|
|
12414
12620
|
}
|
|
12621
|
+
/**
|
|
12622
|
+
* Builds a transaction to split a position into two positions.
|
|
12623
|
+
* @param params SplitPositionParams
|
|
12624
|
+
* @returns Transaction builder.
|
|
12625
|
+
*/
|
|
12415
12626
|
splitPosition(params) {
|
|
12416
12627
|
return __async(this, null, function* () {
|
|
12417
12628
|
const {
|
|
@@ -12448,6 +12659,11 @@ var CpAmm = class {
|
|
|
12448
12659
|
}).transaction();
|
|
12449
12660
|
});
|
|
12450
12661
|
}
|
|
12662
|
+
/**
|
|
12663
|
+
* Builds a transaction to split a position into two positions.
|
|
12664
|
+
* @param params SplitPosition2Params
|
|
12665
|
+
* @returns Transaction builder.
|
|
12666
|
+
*/
|
|
12451
12667
|
splitPosition2(params) {
|
|
12452
12668
|
return __async(this, null, function* () {
|
|
12453
12669
|
const {
|
|
@@ -12592,6 +12808,7 @@ export {
|
|
|
12592
12808
|
getPriceFromSqrtPrice,
|
|
12593
12809
|
getPriceImpact,
|
|
12594
12810
|
getRateLimiterParams,
|
|
12811
|
+
getRewardInfo,
|
|
12595
12812
|
getSecondKey,
|
|
12596
12813
|
getSimulationComputeUnits,
|
|
12597
12814
|
getSqrtPriceFromPrice,
|
|
@@ -12604,7 +12821,8 @@ export {
|
|
|
12604
12821
|
getTotalLockedLiquidity,
|
|
12605
12822
|
getTotalTradingFeeFromExcludedFeeAmount,
|
|
12606
12823
|
getTotalTradingFeeFromIncludedFeeAmount,
|
|
12607
|
-
|
|
12824
|
+
getUnClaimLpFee,
|
|
12825
|
+
getUserRewardPending,
|
|
12608
12826
|
hasPartner,
|
|
12609
12827
|
isDynamicFeeEnabled,
|
|
12610
12828
|
isNonZeroRateLimiter,
|