@meteora-ag/cp-amm-sdk 1.0.1-rc.23 → 1.0.1-rc.24
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 +2594 -3367
- package/dist/index.d.ts +2594 -3367
- package/dist/index.js +503 -62
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +504 -63
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -21,7 +21,7 @@ var __async = (__this, __arguments, generator) => {
|
|
|
21
21
|
};
|
|
22
22
|
|
|
23
23
|
// src/CpAmm.ts
|
|
24
|
-
import { Program, BN as
|
|
24
|
+
import { Program, BN as BN9 } from "@coral-xyz/anchor";
|
|
25
25
|
import { NATIVE_MINT as NATIVE_MINT2, TOKEN_2022_PROGRAM_ID as TOKEN_2022_PROGRAM_ID2 } from "@solana/spl-token";
|
|
26
26
|
import invariant from "invariant";
|
|
27
27
|
|
|
@@ -1070,6 +1070,123 @@ var cp_amm_default = {
|
|
|
1070
1070
|
],
|
|
1071
1071
|
args: []
|
|
1072
1072
|
},
|
|
1073
|
+
{
|
|
1074
|
+
name: "close_position",
|
|
1075
|
+
discriminator: [
|
|
1076
|
+
123,
|
|
1077
|
+
134,
|
|
1078
|
+
81,
|
|
1079
|
+
0,
|
|
1080
|
+
49,
|
|
1081
|
+
68,
|
|
1082
|
+
98,
|
|
1083
|
+
98
|
|
1084
|
+
],
|
|
1085
|
+
accounts: [
|
|
1086
|
+
{
|
|
1087
|
+
name: "position_nft_mint",
|
|
1088
|
+
docs: [
|
|
1089
|
+
"position_nft_mint"
|
|
1090
|
+
],
|
|
1091
|
+
writable: true
|
|
1092
|
+
},
|
|
1093
|
+
{
|
|
1094
|
+
name: "position_nft_account",
|
|
1095
|
+
docs: [
|
|
1096
|
+
"The token account for nft"
|
|
1097
|
+
],
|
|
1098
|
+
writable: true
|
|
1099
|
+
},
|
|
1100
|
+
{
|
|
1101
|
+
name: "pool",
|
|
1102
|
+
writable: true,
|
|
1103
|
+
relations: [
|
|
1104
|
+
"position"
|
|
1105
|
+
]
|
|
1106
|
+
},
|
|
1107
|
+
{
|
|
1108
|
+
name: "position",
|
|
1109
|
+
writable: true
|
|
1110
|
+
},
|
|
1111
|
+
{
|
|
1112
|
+
name: "pool_authority",
|
|
1113
|
+
pda: {
|
|
1114
|
+
seeds: [
|
|
1115
|
+
{
|
|
1116
|
+
kind: "const",
|
|
1117
|
+
value: [
|
|
1118
|
+
112,
|
|
1119
|
+
111,
|
|
1120
|
+
111,
|
|
1121
|
+
108,
|
|
1122
|
+
95,
|
|
1123
|
+
97,
|
|
1124
|
+
117,
|
|
1125
|
+
116,
|
|
1126
|
+
104,
|
|
1127
|
+
111,
|
|
1128
|
+
114,
|
|
1129
|
+
105,
|
|
1130
|
+
116,
|
|
1131
|
+
121
|
|
1132
|
+
]
|
|
1133
|
+
}
|
|
1134
|
+
]
|
|
1135
|
+
}
|
|
1136
|
+
},
|
|
1137
|
+
{
|
|
1138
|
+
name: "rent_receiver",
|
|
1139
|
+
writable: true
|
|
1140
|
+
},
|
|
1141
|
+
{
|
|
1142
|
+
name: "owner",
|
|
1143
|
+
docs: [
|
|
1144
|
+
"Owner of position"
|
|
1145
|
+
],
|
|
1146
|
+
signer: true
|
|
1147
|
+
},
|
|
1148
|
+
{
|
|
1149
|
+
name: "token_program",
|
|
1150
|
+
docs: [
|
|
1151
|
+
"Program to create NFT mint/token account and transfer for token22 account"
|
|
1152
|
+
],
|
|
1153
|
+
address: "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"
|
|
1154
|
+
},
|
|
1155
|
+
{
|
|
1156
|
+
name: "event_authority",
|
|
1157
|
+
pda: {
|
|
1158
|
+
seeds: [
|
|
1159
|
+
{
|
|
1160
|
+
kind: "const",
|
|
1161
|
+
value: [
|
|
1162
|
+
95,
|
|
1163
|
+
95,
|
|
1164
|
+
101,
|
|
1165
|
+
118,
|
|
1166
|
+
101,
|
|
1167
|
+
110,
|
|
1168
|
+
116,
|
|
1169
|
+
95,
|
|
1170
|
+
97,
|
|
1171
|
+
117,
|
|
1172
|
+
116,
|
|
1173
|
+
104,
|
|
1174
|
+
111,
|
|
1175
|
+
114,
|
|
1176
|
+
105,
|
|
1177
|
+
116,
|
|
1178
|
+
121
|
|
1179
|
+
]
|
|
1180
|
+
}
|
|
1181
|
+
]
|
|
1182
|
+
}
|
|
1183
|
+
},
|
|
1184
|
+
{
|
|
1185
|
+
name: "program"
|
|
1186
|
+
}
|
|
1187
|
+
],
|
|
1188
|
+
args: []
|
|
1189
|
+
},
|
|
1073
1190
|
{
|
|
1074
1191
|
name: "create_claim_fee_operator",
|
|
1075
1192
|
discriminator: [
|
|
@@ -1161,7 +1278,7 @@ var cp_amm_default = {
|
|
|
1161
1278
|
{
|
|
1162
1279
|
name: "create_config",
|
|
1163
1280
|
docs: [
|
|
1164
|
-
"ADMIN FUNCTIONS
|
|
1281
|
+
"ADMIN FUNCTIONS /////"
|
|
1165
1282
|
],
|
|
1166
1283
|
discriminator: [
|
|
1167
1284
|
201,
|
|
@@ -3504,6 +3621,19 @@ var cp_amm_default = {
|
|
|
3504
3621
|
5
|
|
3505
3622
|
]
|
|
3506
3623
|
},
|
|
3624
|
+
{
|
|
3625
|
+
name: "EvtClosePosition",
|
|
3626
|
+
discriminator: [
|
|
3627
|
+
20,
|
|
3628
|
+
145,
|
|
3629
|
+
144,
|
|
3630
|
+
68,
|
|
3631
|
+
143,
|
|
3632
|
+
142,
|
|
3633
|
+
214,
|
|
3634
|
+
178
|
|
3635
|
+
]
|
|
3636
|
+
},
|
|
3507
3637
|
{
|
|
3508
3638
|
name: "EvtCreateClaimFeeOperator",
|
|
3509
3639
|
discriminator: [
|
|
@@ -3895,6 +4025,11 @@ var cp_amm_default = {
|
|
|
3895
4025
|
code: 6038,
|
|
3896
4026
|
name: "FeeInverseIsIncorrect",
|
|
3897
4027
|
msg: "Fee inverse is incorrect"
|
|
4028
|
+
},
|
|
4029
|
+
{
|
|
4030
|
+
code: 6039,
|
|
4031
|
+
name: "PositionIsNotEmpty",
|
|
4032
|
+
msg: "Position is not empty"
|
|
3898
4033
|
}
|
|
3899
4034
|
],
|
|
3900
4035
|
types: [
|
|
@@ -4408,6 +4543,14 @@ var cp_amm_default = {
|
|
|
4408
4543
|
}
|
|
4409
4544
|
}
|
|
4410
4545
|
},
|
|
4546
|
+
{
|
|
4547
|
+
name: "token_a_amount",
|
|
4548
|
+
type: "u64"
|
|
4549
|
+
},
|
|
4550
|
+
{
|
|
4551
|
+
name: "token_b_amount",
|
|
4552
|
+
type: "u64"
|
|
4553
|
+
},
|
|
4411
4554
|
{
|
|
4412
4555
|
name: "total_amount_a",
|
|
4413
4556
|
type: "u64"
|
|
@@ -4563,6 +4706,30 @@ var cp_amm_default = {
|
|
|
4563
4706
|
]
|
|
4564
4707
|
}
|
|
4565
4708
|
},
|
|
4709
|
+
{
|
|
4710
|
+
name: "EvtClosePosition",
|
|
4711
|
+
type: {
|
|
4712
|
+
kind: "struct",
|
|
4713
|
+
fields: [
|
|
4714
|
+
{
|
|
4715
|
+
name: "pool",
|
|
4716
|
+
type: "pubkey"
|
|
4717
|
+
},
|
|
4718
|
+
{
|
|
4719
|
+
name: "owner",
|
|
4720
|
+
type: "pubkey"
|
|
4721
|
+
},
|
|
4722
|
+
{
|
|
4723
|
+
name: "position",
|
|
4724
|
+
type: "pubkey"
|
|
4725
|
+
},
|
|
4726
|
+
{
|
|
4727
|
+
name: "position_nft_mint",
|
|
4728
|
+
type: "pubkey"
|
|
4729
|
+
}
|
|
4730
|
+
]
|
|
4731
|
+
}
|
|
4732
|
+
},
|
|
4566
4733
|
{
|
|
4567
4734
|
name: "EvtCreateClaimFeeOperator",
|
|
4568
4735
|
docs: [
|
|
@@ -4705,6 +4872,10 @@ var cp_amm_default = {
|
|
|
4705
4872
|
type: {
|
|
4706
4873
|
kind: "struct",
|
|
4707
4874
|
fields: [
|
|
4875
|
+
{
|
|
4876
|
+
name: "pool",
|
|
4877
|
+
type: "pubkey"
|
|
4878
|
+
},
|
|
4708
4879
|
{
|
|
4709
4880
|
name: "token_a_mint",
|
|
4710
4881
|
type: "pubkey"
|
|
@@ -4769,6 +4940,14 @@ var cp_amm_default = {
|
|
|
4769
4940
|
name: "token_b_flag",
|
|
4770
4941
|
type: "u8"
|
|
4771
4942
|
},
|
|
4943
|
+
{
|
|
4944
|
+
name: "token_a_amount",
|
|
4945
|
+
type: "u64"
|
|
4946
|
+
},
|
|
4947
|
+
{
|
|
4948
|
+
name: "token_b_amount",
|
|
4949
|
+
type: "u64"
|
|
4950
|
+
},
|
|
4772
4951
|
{
|
|
4773
4952
|
name: "total_amount_a",
|
|
4774
4953
|
type: "u64"
|
|
@@ -4870,11 +5049,11 @@ var cp_amm_default = {
|
|
|
4870
5049
|
type: "pubkey"
|
|
4871
5050
|
},
|
|
4872
5051
|
{
|
|
4873
|
-
name: "
|
|
5052
|
+
name: "lock_liquidity_amount",
|
|
4874
5053
|
type: "u128"
|
|
4875
5054
|
},
|
|
4876
5055
|
{
|
|
4877
|
-
name: "
|
|
5056
|
+
name: "total_permanent_locked_liquidity",
|
|
4878
5057
|
type: "u128"
|
|
4879
5058
|
}
|
|
4880
5059
|
]
|
|
@@ -4906,11 +5085,11 @@ var cp_amm_default = {
|
|
|
4906
5085
|
}
|
|
4907
5086
|
},
|
|
4908
5087
|
{
|
|
4909
|
-
name: "
|
|
5088
|
+
name: "token_a_amount",
|
|
4910
5089
|
type: "u64"
|
|
4911
5090
|
},
|
|
4912
5091
|
{
|
|
4913
|
-
name: "
|
|
5092
|
+
name: "token_b_amount",
|
|
4914
5093
|
type: "u64"
|
|
4915
5094
|
}
|
|
4916
5095
|
]
|
|
@@ -4946,7 +5125,7 @@ var cp_amm_default = {
|
|
|
4946
5125
|
type: "u8"
|
|
4947
5126
|
},
|
|
4948
5127
|
{
|
|
4949
|
-
name: "
|
|
5128
|
+
name: "has_referral",
|
|
4950
5129
|
type: "bool"
|
|
4951
5130
|
},
|
|
4952
5131
|
{
|
|
@@ -4966,7 +5145,7 @@ var cp_amm_default = {
|
|
|
4966
5145
|
}
|
|
4967
5146
|
},
|
|
4968
5147
|
{
|
|
4969
|
-
name: "
|
|
5148
|
+
name: "actual_amount_in",
|
|
4970
5149
|
type: "u64"
|
|
4971
5150
|
},
|
|
4972
5151
|
{
|
|
@@ -6285,9 +6464,16 @@ function deriveEventAuthority(programId) {
|
|
|
6285
6464
|
programId
|
|
6286
6465
|
);
|
|
6287
6466
|
}
|
|
6467
|
+
function deriveTokenBadge(tokenMint, programId) {
|
|
6468
|
+
return PublicKey2.findProgramAddressSync(
|
|
6469
|
+
[Buffer.from("token_badge"), tokenMint.toBuffer()],
|
|
6470
|
+
programId
|
|
6471
|
+
)[0];
|
|
6472
|
+
}
|
|
6288
6473
|
|
|
6289
6474
|
// src/helpers/token.ts
|
|
6290
6475
|
import {
|
|
6476
|
+
AccountLayout,
|
|
6291
6477
|
createAssociatedTokenAccountIdempotentInstruction,
|
|
6292
6478
|
createCloseAccountInstruction,
|
|
6293
6479
|
getAccount,
|
|
@@ -6384,6 +6570,21 @@ function getNftOwner(connection, nftMint) {
|
|
|
6384
6570
|
return new PublicKey3(owner);
|
|
6385
6571
|
});
|
|
6386
6572
|
}
|
|
6573
|
+
function getAllNftByUser(_0, _1) {
|
|
6574
|
+
return __async(this, arguments, function* (connection, user, tokenProgram = TOKEN_2022_PROGRAM_ID) {
|
|
6575
|
+
const allUserTokenAccounts = yield connection.getTokenAccountsByOwner(user, {
|
|
6576
|
+
programId: tokenProgram
|
|
6577
|
+
});
|
|
6578
|
+
const userNfts = [];
|
|
6579
|
+
for (const { account } of allUserTokenAccounts.value) {
|
|
6580
|
+
const tokenAccountData = AccountLayout.decode(account.data);
|
|
6581
|
+
if (tokenAccountData.amount.toString() === "1") {
|
|
6582
|
+
userNfts.push(tokenAccountData.mint.toString());
|
|
6583
|
+
}
|
|
6584
|
+
}
|
|
6585
|
+
return userNfts;
|
|
6586
|
+
});
|
|
6587
|
+
}
|
|
6387
6588
|
|
|
6388
6589
|
// src/helpers/fee.ts
|
|
6389
6590
|
import { BN as BN5 } from "@coral-xyz/anchor";
|
|
@@ -6605,19 +6806,25 @@ function getLiquidityDeltaFromAmountB(maxAmountB, lowerSqrtPrice, upperSqrtPrice
|
|
|
6605
6806
|
);
|
|
6606
6807
|
return new BN4(prod.div(denominator).floor().toFixed());
|
|
6607
6808
|
}
|
|
6608
|
-
function getAmountAFromLiquidityDelta(liquidity, currentSqrtPrice, maxSqrtPrice) {
|
|
6809
|
+
function getAmountAFromLiquidityDelta(liquidity, currentSqrtPrice, maxSqrtPrice, rounding) {
|
|
6609
6810
|
const prod = new Decimal2(liquidity.toString()).mul(
|
|
6610
6811
|
new Decimal2(maxSqrtPrice.sub(currentSqrtPrice).toString())
|
|
6611
6812
|
);
|
|
6612
6813
|
const denominator = currentSqrtPrice.mul(maxSqrtPrice);
|
|
6613
6814
|
const result = prod.mul(Decimal2.pow(2, 64)).div(new Decimal2(denominator.toString()));
|
|
6815
|
+
if (rounding == 0 /* Up */) {
|
|
6816
|
+
return result.div(Decimal2.pow(2, 64)).ceil().toFixed();
|
|
6817
|
+
}
|
|
6614
6818
|
return result.div(Decimal2.pow(2, 64)).floor().toFixed();
|
|
6615
6819
|
}
|
|
6616
|
-
function getAmountBFromLiquidityDelta(liquidity, currentSqrtPrice, minSqrtPrice) {
|
|
6820
|
+
function getAmountBFromLiquidityDelta(liquidity, currentSqrtPrice, minSqrtPrice, rounding) {
|
|
6617
6821
|
const delta = currentSqrtPrice.sub(minSqrtPrice);
|
|
6618
6822
|
const prod = new Decimal2(liquidity.toString()).mul(
|
|
6619
6823
|
new Decimal2(delta.toString())
|
|
6620
6824
|
);
|
|
6825
|
+
if (rounding == 0 /* Up */) {
|
|
6826
|
+
return prod.div(Decimal2.pow(2, 128)).ceil().toFixed();
|
|
6827
|
+
}
|
|
6621
6828
|
return prod.div(Decimal2.pow(2, 128)).floor().toFixed();
|
|
6622
6829
|
}
|
|
6623
6830
|
|
|
@@ -6705,8 +6912,8 @@ function getSwapAmount(inAmount, sqrtPrice, liquidity, tradeFeeNumerator, aToB,
|
|
|
6705
6912
|
liquidity,
|
|
6706
6913
|
1 /* Down */
|
|
6707
6914
|
);
|
|
6708
|
-
const
|
|
6709
|
-
return {
|
|
6915
|
+
const amountOut = feeMode.feeOnInput ? outAmount : (totalFee = getTotalFeeOnAmount(outAmount, tradeFeeNumerator), outAmount.sub(totalFee));
|
|
6916
|
+
return { amountOut, totalFee };
|
|
6710
6917
|
}
|
|
6711
6918
|
|
|
6712
6919
|
// src/helpers/computeUnits.ts
|
|
@@ -6866,7 +7073,90 @@ function calculateInitSqrtPrice(tokenAAmount, tokenBAmount, minSqrtPrice, maxSqr
|
|
|
6866
7073
|
return new BN7(result.floor().toFixed());
|
|
6867
7074
|
}
|
|
6868
7075
|
|
|
7076
|
+
// src/helpers/token2022.ts
|
|
7077
|
+
import { BN as BN8 } from "@coral-xyz/anchor";
|
|
7078
|
+
import {
|
|
7079
|
+
calculateFee,
|
|
7080
|
+
getEpochFee,
|
|
7081
|
+
getTransferFeeConfig,
|
|
7082
|
+
MAX_FEE_BASIS_POINTS
|
|
7083
|
+
} from "@solana/spl-token";
|
|
7084
|
+
function calculatePreFeeAmount(transferFee, postFeeAmount) {
|
|
7085
|
+
if (postFeeAmount.isZero()) {
|
|
7086
|
+
return new BN8(0);
|
|
7087
|
+
}
|
|
7088
|
+
if (transferFee.transferFeeBasisPoints === 0) {
|
|
7089
|
+
return postFeeAmount;
|
|
7090
|
+
}
|
|
7091
|
+
const maximumFee = new BN8(transferFee.maximumFee.toString());
|
|
7092
|
+
if (transferFee.transferFeeBasisPoints === MAX_FEE_BASIS_POINTS) {
|
|
7093
|
+
return postFeeAmount.add(maximumFee);
|
|
7094
|
+
}
|
|
7095
|
+
const ONE_IN_BASIS_POINTS = new BN8(MAX_FEE_BASIS_POINTS);
|
|
7096
|
+
const numerator = postFeeAmount.mul(ONE_IN_BASIS_POINTS);
|
|
7097
|
+
const denominator = ONE_IN_BASIS_POINTS.sub(
|
|
7098
|
+
new BN8(transferFee.transferFeeBasisPoints)
|
|
7099
|
+
);
|
|
7100
|
+
const rawPreFeeAmount = numerator.add(denominator).sub(new BN8(1)).div(denominator);
|
|
7101
|
+
if (rawPreFeeAmount.sub(postFeeAmount).gte(maximumFee)) {
|
|
7102
|
+
return postFeeAmount.add(maximumFee);
|
|
7103
|
+
}
|
|
7104
|
+
return rawPreFeeAmount;
|
|
7105
|
+
}
|
|
7106
|
+
function calculateInverseFee(transferFee, postFeeAmount) {
|
|
7107
|
+
const preFeeAmount = calculatePreFeeAmount(transferFee, postFeeAmount);
|
|
7108
|
+
return new BN8(
|
|
7109
|
+
calculateFee(transferFee, BigInt(preFeeAmount.toString())).toString()
|
|
7110
|
+
);
|
|
7111
|
+
}
|
|
7112
|
+
function calculateTransferFeeIncludedAmount(transferFeeExcludedAmount, mint, currentEpoch) {
|
|
7113
|
+
if (transferFeeExcludedAmount.isZero()) {
|
|
7114
|
+
return {
|
|
7115
|
+
amount: new BN8(0),
|
|
7116
|
+
transferFee: new BN8(0)
|
|
7117
|
+
};
|
|
7118
|
+
}
|
|
7119
|
+
const transferFeeConfig = getTransferFeeConfig(mint);
|
|
7120
|
+
if (transferFeeConfig === null) {
|
|
7121
|
+
return {
|
|
7122
|
+
amount: transferFeeExcludedAmount,
|
|
7123
|
+
transferFee: new BN8(0)
|
|
7124
|
+
};
|
|
7125
|
+
}
|
|
7126
|
+
const epochFee = getEpochFee(transferFeeConfig, BigInt(currentEpoch));
|
|
7127
|
+
const transferFee = epochFee.transferFeeBasisPoints == MAX_FEE_BASIS_POINTS ? new BN8(epochFee.maximumFee.toString()) : calculateInverseFee(epochFee, transferFeeExcludedAmount);
|
|
7128
|
+
const transferFeeIncludedAmount = transferFeeExcludedAmount.add(transferFee);
|
|
7129
|
+
return {
|
|
7130
|
+
amount: transferFeeIncludedAmount,
|
|
7131
|
+
transferFee
|
|
7132
|
+
};
|
|
7133
|
+
}
|
|
7134
|
+
function calculateTransferFeeExcludedAmount(transferFeeIncludedAmount, mint, currentEpoch) {
|
|
7135
|
+
const transferFeeConfig = getTransferFeeConfig(mint);
|
|
7136
|
+
if (transferFeeConfig === null) {
|
|
7137
|
+
return {
|
|
7138
|
+
amount: transferFeeIncludedAmount,
|
|
7139
|
+
transferFee: new BN8(0)
|
|
7140
|
+
};
|
|
7141
|
+
}
|
|
7142
|
+
const transferFeeIncludedAmountN = BigInt(
|
|
7143
|
+
transferFeeIncludedAmount.toString()
|
|
7144
|
+
);
|
|
7145
|
+
const transferFee = calculateFee(
|
|
7146
|
+
getEpochFee(transferFeeConfig, BigInt(currentEpoch)),
|
|
7147
|
+
transferFeeIncludedAmountN
|
|
7148
|
+
);
|
|
7149
|
+
const transferFeeExcludedAmount = new BN8(
|
|
7150
|
+
(transferFeeIncludedAmountN - transferFee).toString()
|
|
7151
|
+
);
|
|
7152
|
+
return {
|
|
7153
|
+
amount: transferFeeExcludedAmount,
|
|
7154
|
+
transferFee: new BN8(transferFee.toString())
|
|
7155
|
+
};
|
|
7156
|
+
}
|
|
7157
|
+
|
|
6869
7158
|
// src/CpAmm.ts
|
|
7159
|
+
import { min } from "bn.js";
|
|
6870
7160
|
var CpAmm = class {
|
|
6871
7161
|
constructor(connection) {
|
|
6872
7162
|
this._program = new Program(cp_amm_default, {
|
|
@@ -6889,7 +7179,7 @@ var CpAmm = class {
|
|
|
6889
7179
|
preparePoolCreationParams(params) {
|
|
6890
7180
|
return __async(this, null, function* () {
|
|
6891
7181
|
const { tokenAAmount, tokenBAmount, minSqrtPrice, maxSqrtPrice } = params;
|
|
6892
|
-
if (tokenAAmount.eq(new
|
|
7182
|
+
if (tokenAAmount.eq(new BN9(0)) && tokenBAmount.eq(new BN9(0))) {
|
|
6893
7183
|
throw new Error("Invalid input amount");
|
|
6894
7184
|
}
|
|
6895
7185
|
if (minSqrtPrice.gt(maxSqrtPrice)) {
|
|
@@ -6920,6 +7210,26 @@ var CpAmm = class {
|
|
|
6920
7210
|
};
|
|
6921
7211
|
});
|
|
6922
7212
|
}
|
|
7213
|
+
/**
|
|
7214
|
+
* Derives token badge account metadata
|
|
7215
|
+
* @param tokenAMint - Public key of token A mint
|
|
7216
|
+
* @param tokenBMint - Public key of token B mint
|
|
7217
|
+
* @returns Array of account metadata for token badges
|
|
7218
|
+
*/
|
|
7219
|
+
getTokenBadgeAccounts(tokenAMint, tokenBMint) {
|
|
7220
|
+
return [
|
|
7221
|
+
{
|
|
7222
|
+
pubkey: deriveTokenBadge(tokenAMint, this._program.programId),
|
|
7223
|
+
isWritable: false,
|
|
7224
|
+
isSigner: false
|
|
7225
|
+
},
|
|
7226
|
+
{
|
|
7227
|
+
pubkey: deriveTokenBadge(tokenBMint, this._program.programId),
|
|
7228
|
+
isWritable: false,
|
|
7229
|
+
isSigner: false
|
|
7230
|
+
}
|
|
7231
|
+
];
|
|
7232
|
+
}
|
|
6923
7233
|
/**
|
|
6924
7234
|
* Fetches the Config state of the program.
|
|
6925
7235
|
* @param config - Public key of the config account.
|
|
@@ -7013,16 +7323,13 @@ var CpAmm = class {
|
|
|
7013
7323
|
const positions = yield this._program.account.position.all([
|
|
7014
7324
|
positionByPoolFilter(pool)
|
|
7015
7325
|
]);
|
|
7016
|
-
const
|
|
7017
|
-
|
|
7018
|
-
|
|
7019
|
-
|
|
7020
|
-
|
|
7021
|
-
)
|
|
7022
|
-
|
|
7023
|
-
result.push(position);
|
|
7024
|
-
}
|
|
7025
|
-
}
|
|
7326
|
+
const userNfts = yield getAllNftByUser(
|
|
7327
|
+
this._program.provider.connection,
|
|
7328
|
+
user
|
|
7329
|
+
);
|
|
7330
|
+
const result = positions.filter(
|
|
7331
|
+
(item) => userNfts.includes(item.account.nftMint.toString())
|
|
7332
|
+
);
|
|
7026
7333
|
return result;
|
|
7027
7334
|
});
|
|
7028
7335
|
}
|
|
@@ -7033,17 +7340,14 @@ var CpAmm = class {
|
|
|
7033
7340
|
*/
|
|
7034
7341
|
getPositionsByUser(user) {
|
|
7035
7342
|
return __async(this, null, function* () {
|
|
7343
|
+
const userNfts = yield getAllNftByUser(
|
|
7344
|
+
this._program.provider.connection,
|
|
7345
|
+
user
|
|
7346
|
+
);
|
|
7036
7347
|
const positions = yield this._program.account.position.all();
|
|
7037
|
-
const result =
|
|
7038
|
-
|
|
7039
|
-
|
|
7040
|
-
this._program.provider.connection,
|
|
7041
|
-
position.account.nftMint
|
|
7042
|
-
);
|
|
7043
|
-
if (owner.equals(user)) {
|
|
7044
|
-
result.push(position);
|
|
7045
|
-
}
|
|
7046
|
-
}
|
|
7348
|
+
const result = positions.filter(
|
|
7349
|
+
(item) => userNfts.includes(item.account.nftMint.toString())
|
|
7350
|
+
);
|
|
7047
7351
|
return result;
|
|
7048
7352
|
});
|
|
7049
7353
|
}
|
|
@@ -7062,8 +7366,16 @@ var CpAmm = class {
|
|
|
7062
7366
|
*/
|
|
7063
7367
|
getQuote(params) {
|
|
7064
7368
|
return __async(this, null, function* () {
|
|
7065
|
-
|
|
7066
|
-
|
|
7369
|
+
const {
|
|
7370
|
+
inAmount,
|
|
7371
|
+
inputTokenMint,
|
|
7372
|
+
slippage,
|
|
7373
|
+
poolState,
|
|
7374
|
+
currentTime,
|
|
7375
|
+
currentSlot,
|
|
7376
|
+
inputTokenInfo,
|
|
7377
|
+
outputTokenInfo
|
|
7378
|
+
} = params;
|
|
7067
7379
|
const {
|
|
7068
7380
|
sqrtPrice: sqrtPriceQ64,
|
|
7069
7381
|
liquidity: liquidityQ64,
|
|
@@ -7080,13 +7392,16 @@ var CpAmm = class {
|
|
|
7080
7392
|
periodFrequency
|
|
7081
7393
|
} = poolFees.baseFee;
|
|
7082
7394
|
const dynamicFee = poolFees.dynamicFee;
|
|
7395
|
+
let actualAmountIn = inAmount;
|
|
7396
|
+
if (inputTokenInfo) {
|
|
7397
|
+
actualAmountIn = calculateTransferFeeExcludedAmount(
|
|
7398
|
+
inAmount,
|
|
7399
|
+
inputTokenInfo.mint,
|
|
7400
|
+
inputTokenInfo.currentEpoch
|
|
7401
|
+
).amount;
|
|
7402
|
+
}
|
|
7083
7403
|
const aToB = poolState.tokenAMint.equals(inputTokenMint);
|
|
7084
|
-
const
|
|
7085
|
-
const blockInfo = yield this._program.provider.connection.getBlock(slot, {
|
|
7086
|
-
maxSupportedTransactionVersion: 0
|
|
7087
|
-
});
|
|
7088
|
-
const currentTime = (_a = blockInfo == null ? void 0 : blockInfo.blockTime) != null ? _a : Math.floor(Date.now() / 1e3);
|
|
7089
|
-
const currentPoint = activationType ? currentTime : slot;
|
|
7404
|
+
const currentPoint = activationType ? currentTime : currentSlot;
|
|
7090
7405
|
let dynamicFeeParams;
|
|
7091
7406
|
if (dynamicFee.initialized) {
|
|
7092
7407
|
const { volatilityAccumulator, binStep, variableFeeControl } = dynamicFee;
|
|
@@ -7102,24 +7417,30 @@ var CpAmm = class {
|
|
|
7102
7417
|
reductionFactor,
|
|
7103
7418
|
dynamicFeeParams
|
|
7104
7419
|
);
|
|
7105
|
-
const {
|
|
7106
|
-
|
|
7420
|
+
const { amountOut, totalFee } = getSwapAmount(
|
|
7421
|
+
actualAmountIn,
|
|
7107
7422
|
sqrtPriceQ64,
|
|
7108
7423
|
liquidityQ64,
|
|
7109
7424
|
tradeFeeNumerator,
|
|
7110
7425
|
aToB,
|
|
7111
7426
|
collectFeeMode
|
|
7112
7427
|
);
|
|
7113
|
-
|
|
7114
|
-
|
|
7115
|
-
|
|
7116
|
-
|
|
7428
|
+
let actualAmoutOut = amountOut;
|
|
7429
|
+
if (outputTokenInfo) {
|
|
7430
|
+
actualAmoutOut = calculateTransferFeeExcludedAmount(
|
|
7431
|
+
amountOut,
|
|
7432
|
+
outputTokenInfo.mint,
|
|
7433
|
+
outputTokenInfo.currentEpoch
|
|
7434
|
+
).amount;
|
|
7435
|
+
}
|
|
7436
|
+
const minSwapOutAmount = getMinAmountWithSlippage(actualAmoutOut, slippage);
|
|
7117
7437
|
return {
|
|
7118
7438
|
swapInAmount: inAmount,
|
|
7119
|
-
|
|
7439
|
+
consumedInAmount: actualAmountIn,
|
|
7440
|
+
swapOutAmount: actualAmoutOut,
|
|
7120
7441
|
minSwapOutAmount,
|
|
7121
7442
|
totalFee,
|
|
7122
|
-
priceImpact: getPriceImpact(minSwapOutAmount,
|
|
7443
|
+
priceImpact: getPriceImpact(minSwapOutAmount, actualAmoutOut)
|
|
7123
7444
|
};
|
|
7124
7445
|
});
|
|
7125
7446
|
}
|
|
@@ -7136,19 +7457,37 @@ var CpAmm = class {
|
|
|
7136
7457
|
maxAmountTokenB,
|
|
7137
7458
|
sqrtMaxPrice,
|
|
7138
7459
|
sqrtMinPrice,
|
|
7139
|
-
sqrtPrice
|
|
7460
|
+
sqrtPrice,
|
|
7461
|
+
tokenAInfo,
|
|
7462
|
+
tokenBInfo
|
|
7140
7463
|
} = params;
|
|
7464
|
+
let actualMaxAmountTokenA = maxAmountTokenA;
|
|
7465
|
+
let actualMaxAmountTokenB = maxAmountTokenB;
|
|
7466
|
+
if (tokenAInfo) {
|
|
7467
|
+
actualMaxAmountTokenA = calculateTransferFeeExcludedAmount(
|
|
7468
|
+
maxAmountTokenA,
|
|
7469
|
+
tokenAInfo.mint,
|
|
7470
|
+
tokenAInfo.currentEpoch
|
|
7471
|
+
).amount;
|
|
7472
|
+
}
|
|
7473
|
+
if (tokenBInfo) {
|
|
7474
|
+
actualMaxAmountTokenB = calculateTransferFeeExcludedAmount(
|
|
7475
|
+
maxAmountTokenB,
|
|
7476
|
+
tokenBInfo.mint,
|
|
7477
|
+
tokenBInfo.currentEpoch
|
|
7478
|
+
).amount;
|
|
7479
|
+
}
|
|
7141
7480
|
const liquidityDeltaFromAmountA = getLiquidityDeltaFromAmountA(
|
|
7142
|
-
|
|
7481
|
+
actualMaxAmountTokenA,
|
|
7143
7482
|
sqrtPrice,
|
|
7144
7483
|
sqrtMaxPrice
|
|
7145
7484
|
);
|
|
7146
7485
|
const liquidityDeltaFromAmountB = getLiquidityDeltaFromAmountB(
|
|
7147
|
-
|
|
7486
|
+
actualMaxAmountTokenB,
|
|
7148
7487
|
sqrtMinPrice,
|
|
7149
7488
|
sqrtPrice
|
|
7150
7489
|
);
|
|
7151
|
-
return liquidityDeltaFromAmountA
|
|
7490
|
+
return min(liquidityDeltaFromAmountA, liquidityDeltaFromAmountB);
|
|
7152
7491
|
});
|
|
7153
7492
|
}
|
|
7154
7493
|
/**
|
|
@@ -7250,11 +7589,15 @@ var CpAmm = class {
|
|
|
7250
7589
|
);
|
|
7251
7590
|
preInstructions.push(...wrapSOLIx);
|
|
7252
7591
|
}
|
|
7592
|
+
const tokenBadgeAccounts = this.getTokenBadgeAccounts(
|
|
7593
|
+
tokenAMint,
|
|
7594
|
+
tokenBMint
|
|
7595
|
+
);
|
|
7253
7596
|
const tx = yield this._program.methods.initializePool({
|
|
7254
7597
|
liquidity: liquidityQ64,
|
|
7255
7598
|
sqrtPrice: sqrtPriceQ64,
|
|
7256
7599
|
activationPoint
|
|
7257
|
-
}).
|
|
7600
|
+
}).accountsPartial({
|
|
7258
7601
|
creator,
|
|
7259
7602
|
positionNftAccount,
|
|
7260
7603
|
positionNftMint: positionNft,
|
|
@@ -7272,10 +7615,8 @@ var CpAmm = class {
|
|
|
7272
7615
|
token2022Program: TOKEN_2022_PROGRAM_ID2,
|
|
7273
7616
|
tokenAProgram,
|
|
7274
7617
|
tokenBProgram,
|
|
7275
|
-
systemProgram: SystemProgram2.programId
|
|
7276
|
-
|
|
7277
|
-
program: this._program.programId
|
|
7278
|
-
}).preInstructions(preInstructions).transaction();
|
|
7618
|
+
systemProgram: SystemProgram2.programId
|
|
7619
|
+
}).preInstructions(preInstructions).remainingAccounts(tokenBadgeAccounts).transaction();
|
|
7279
7620
|
return tx;
|
|
7280
7621
|
});
|
|
7281
7622
|
}
|
|
@@ -7380,6 +7721,10 @@ var CpAmm = class {
|
|
|
7380
7721
|
);
|
|
7381
7722
|
preInstructions.push(...wrapSOLIx);
|
|
7382
7723
|
}
|
|
7724
|
+
const tokenBadgeAccounts = this.getTokenBadgeAccounts(
|
|
7725
|
+
tokenAMint,
|
|
7726
|
+
tokenBMint
|
|
7727
|
+
);
|
|
7383
7728
|
const transaction = yield this._program.methods.initializeCustomizablePool({
|
|
7384
7729
|
poolFees,
|
|
7385
7730
|
sqrtMinPrice: MIN_SQRT_PRICE,
|
|
@@ -7390,7 +7735,7 @@ var CpAmm = class {
|
|
|
7390
7735
|
activationType,
|
|
7391
7736
|
collectFeeMode,
|
|
7392
7737
|
activationPoint
|
|
7393
|
-
}).
|
|
7738
|
+
}).accountsPartial({
|
|
7394
7739
|
creator,
|
|
7395
7740
|
positionNftAccount,
|
|
7396
7741
|
positionNftMint: positionNft,
|
|
@@ -7407,10 +7752,8 @@ var CpAmm = class {
|
|
|
7407
7752
|
token2022Program: TOKEN_2022_PROGRAM_ID2,
|
|
7408
7753
|
tokenAProgram,
|
|
7409
7754
|
tokenBProgram,
|
|
7410
|
-
systemProgram: SystemProgram2.programId
|
|
7411
|
-
|
|
7412
|
-
program: this._program.programId
|
|
7413
|
-
}).preInstructions(preInstructions).transaction();
|
|
7755
|
+
systemProgram: SystemProgram2.programId
|
|
7756
|
+
}).preInstructions(preInstructions).remainingAccounts(tokenBadgeAccounts).transaction();
|
|
7414
7757
|
return { tx: transaction, pool, position };
|
|
7415
7758
|
});
|
|
7416
7759
|
}
|
|
@@ -7624,6 +7967,80 @@ var CpAmm = class {
|
|
|
7624
7967
|
}).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
|
|
7625
7968
|
});
|
|
7626
7969
|
}
|
|
7970
|
+
/**
|
|
7971
|
+
* Builds a transaction to remove liquidity from a position.
|
|
7972
|
+
* @param {RemoveLiquidityParams} params - Parameters for removing liquidity.
|
|
7973
|
+
* @returns Transaction builder.
|
|
7974
|
+
*/
|
|
7975
|
+
removeAllLiquidity(params) {
|
|
7976
|
+
return __async(this, null, function* () {
|
|
7977
|
+
const {
|
|
7978
|
+
owner,
|
|
7979
|
+
pool,
|
|
7980
|
+
position,
|
|
7981
|
+
positionNftMint,
|
|
7982
|
+
tokenAAmountThreshold,
|
|
7983
|
+
tokenBAmountThreshold,
|
|
7984
|
+
tokenAMint,
|
|
7985
|
+
tokenBMint,
|
|
7986
|
+
tokenAVault,
|
|
7987
|
+
tokenBVault,
|
|
7988
|
+
tokenAProgram,
|
|
7989
|
+
tokenBProgram
|
|
7990
|
+
} = params;
|
|
7991
|
+
const positionNftAccount = derivePositionNftAccount(
|
|
7992
|
+
positionNftMint,
|
|
7993
|
+
this._program.programId
|
|
7994
|
+
);
|
|
7995
|
+
const poolAuthority = derivePoolAuthority(this._program.programId);
|
|
7996
|
+
const preInstructions = [];
|
|
7997
|
+
const [
|
|
7998
|
+
{ ataPubkey: tokenAAccount, ix: createTokenAAccountIx },
|
|
7999
|
+
{ ataPubkey: tokenBAccount, ix: createTokenBAccountIx }
|
|
8000
|
+
] = yield Promise.all([
|
|
8001
|
+
getOrCreateATAInstruction(
|
|
8002
|
+
this._program.provider.connection,
|
|
8003
|
+
tokenAMint,
|
|
8004
|
+
owner,
|
|
8005
|
+
owner,
|
|
8006
|
+
true,
|
|
8007
|
+
tokenAProgram
|
|
8008
|
+
),
|
|
8009
|
+
getOrCreateATAInstruction(
|
|
8010
|
+
this._program.provider.connection,
|
|
8011
|
+
tokenBMint,
|
|
8012
|
+
owner,
|
|
8013
|
+
owner,
|
|
8014
|
+
true,
|
|
8015
|
+
tokenBProgram
|
|
8016
|
+
)
|
|
8017
|
+
]);
|
|
8018
|
+
createTokenAAccountIx && preInstructions.push(createTokenAAccountIx);
|
|
8019
|
+
createTokenBAccountIx && preInstructions.push(createTokenBAccountIx);
|
|
8020
|
+
const postInstructions = [];
|
|
8021
|
+
if ([tokenAMint.toBase58(), tokenBMint.toBase58()].includes(
|
|
8022
|
+
NATIVE_MINT2.toBase58()
|
|
8023
|
+
)) {
|
|
8024
|
+
const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
|
|
8025
|
+
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
8026
|
+
}
|
|
8027
|
+
return yield this._program.methods.removeAllLiquidity(tokenAAmountThreshold, tokenBAmountThreshold).accountsPartial({
|
|
8028
|
+
poolAuthority,
|
|
8029
|
+
pool,
|
|
8030
|
+
position,
|
|
8031
|
+
positionNftAccount,
|
|
8032
|
+
owner,
|
|
8033
|
+
tokenAAccount,
|
|
8034
|
+
tokenBAccount,
|
|
8035
|
+
tokenAMint,
|
|
8036
|
+
tokenBMint,
|
|
8037
|
+
tokenAVault,
|
|
8038
|
+
tokenBVault,
|
|
8039
|
+
tokenAProgram,
|
|
8040
|
+
tokenBProgram
|
|
8041
|
+
}).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
|
|
8042
|
+
});
|
|
8043
|
+
}
|
|
7627
8044
|
/**
|
|
7628
8045
|
* Builds a transaction to perform a swap in the pool.
|
|
7629
8046
|
* @param {SwapParams} params - Parameters for swapping tokens.
|
|
@@ -7875,6 +8292,26 @@ var CpAmm = class {
|
|
|
7875
8292
|
}).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
|
|
7876
8293
|
});
|
|
7877
8294
|
}
|
|
8295
|
+
closePosition(params) {
|
|
8296
|
+
return __async(this, null, function* () {
|
|
8297
|
+
const { owner, pool, position, positionNftMint } = params;
|
|
8298
|
+
const poolAuthority = derivePoolAuthority(this._program.programId);
|
|
8299
|
+
const positionNftAccount = derivePositionNftAccount(
|
|
8300
|
+
positionNftMint,
|
|
8301
|
+
this._program.programId
|
|
8302
|
+
);
|
|
8303
|
+
return yield this._program.methods.closePosition().accountsPartial({
|
|
8304
|
+
positionNftMint,
|
|
8305
|
+
positionNftAccount,
|
|
8306
|
+
pool,
|
|
8307
|
+
position,
|
|
8308
|
+
poolAuthority,
|
|
8309
|
+
rentReceiver: owner,
|
|
8310
|
+
owner,
|
|
8311
|
+
tokenProgram: TOKEN_2022_PROGRAM_ID2
|
|
8312
|
+
}).transaction();
|
|
8313
|
+
});
|
|
8314
|
+
}
|
|
7878
8315
|
/**
|
|
7879
8316
|
* Builds a transaction to update reward duration.
|
|
7880
8317
|
* @param {UpdateRewardDurationParams} params - Parameters including pool and new duration.
|
|
@@ -8131,6 +8568,8 @@ export {
|
|
|
8131
8568
|
SCALE_OFFSET,
|
|
8132
8569
|
TradeDirection,
|
|
8133
8570
|
calculateInitSqrtPrice,
|
|
8571
|
+
calculateTransferFeeExcludedAmount,
|
|
8572
|
+
calculateTransferFeeIncludedAmount,
|
|
8134
8573
|
decimalToQ64,
|
|
8135
8574
|
index_default as default,
|
|
8136
8575
|
deriveClaimFeeOperatorAddress,
|
|
@@ -8142,9 +8581,11 @@ export {
|
|
|
8142
8581
|
derivePositionAddress,
|
|
8143
8582
|
derivePositionNftAccount,
|
|
8144
8583
|
deriveRewardVaultAddress,
|
|
8584
|
+
deriveTokenBadge,
|
|
8145
8585
|
deriveTokenBadgeAddress,
|
|
8146
8586
|
deriveTokenVaultAddress,
|
|
8147
8587
|
divCeil,
|
|
8588
|
+
getAllNftByUser,
|
|
8148
8589
|
getAmountAFromLiquidityDelta,
|
|
8149
8590
|
getAmountBFromLiquidityDelta,
|
|
8150
8591
|
getBaseFeeNumerator,
|