@meteora-ag/cp-amm-sdk 1.0.1-rc.8 → 1.0.1
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/README.md +45 -200
- package/dist/index.d.mts +3303 -3426
- package/dist/index.d.ts +3303 -3426
- package/dist/index.js +2251 -795
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2288 -832
- package/dist/index.mjs.map +1 -1
- package/node_modules/@types/node/README.md +1 -1
- package/node_modules/@types/node/fs.d.ts +15 -16
- package/node_modules/@types/node/https.d.ts +3 -1
- package/node_modules/@types/node/module.d.ts +50 -5
- package/node_modules/@types/node/package.json +4 -4
- package/node_modules/@types/node/process.d.ts +28 -0
- package/node_modules/@types/node/sqlite.d.ts +63 -17
- package/node_modules/@types/node/test.d.ts +82 -0
- package/node_modules/@types/node/timers/promises.d.ts +24 -13
- package/node_modules/@types/node/timers.d.ts +159 -112
- package/node_modules/@types/node/util.d.ts +11 -4
- package/node_modules/@types/node/worker_threads.d.ts +1 -0
- package/node_modules/bigint-buffer/build/Makefile +2 -2
- package/node_modules/bigint-buffer/build/Release/bigint_buffer.node +0 -0
- package/node_modules/bigint-buffer/build/Release/obj.target/bigint_buffer/src/bigint-buffer.o +0 -0
- package/node_modules/bigint-buffer/build/config.gypi +1 -1
- package/node_modules/rpc-websockets/node_modules/@types/ws/README.md +1 -1
- package/node_modules/rpc-websockets/node_modules/@types/ws/index.d.mts +1 -1
- package/node_modules/rpc-websockets/node_modules/@types/ws/index.d.ts +1 -1
- package/node_modules/rpc-websockets/node_modules/@types/ws/package.json +3 -3
- package/node_modules/undici-types/dispatcher.d.ts +1 -0
- package/node_modules/undici-types/package.json +1 -1
- package/node_modules/undici-types/readable.d.ts +5 -0
- package/node_modules/undici-types/webidl.d.ts +6 -0
- package/package.json +42 -41
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
var __pow = Math.pow;
|
|
1
2
|
var __async = (__this, __arguments, generator) => {
|
|
2
3
|
return new Promise((resolve, reject) => {
|
|
3
4
|
var fulfilled = (value) => {
|
|
@@ -20,14 +21,9 @@ var __async = (__this, __arguments, generator) => {
|
|
|
20
21
|
};
|
|
21
22
|
|
|
22
23
|
// src/CpAmm.ts
|
|
23
|
-
import { Program } from "@coral-xyz/anchor";
|
|
24
|
-
import {
|
|
25
|
-
getAssociatedTokenAddressSync as getAssociatedTokenAddressSync2,
|
|
26
|
-
NATIVE_MINT as NATIVE_MINT2,
|
|
27
|
-
TOKEN_2022_PROGRAM_ID as TOKEN_2022_PROGRAM_ID2
|
|
28
|
-
} from "@solana/spl-token";
|
|
24
|
+
import { Program, BN as BN10 } from "@coral-xyz/anchor";
|
|
25
|
+
import { NATIVE_MINT as NATIVE_MINT2, TOKEN_2022_PROGRAM_ID as TOKEN_2022_PROGRAM_ID2 } from "@solana/spl-token";
|
|
29
26
|
import invariant from "invariant";
|
|
30
|
-
import Decimal3 from "decimal.js";
|
|
31
27
|
|
|
32
28
|
// src/idl/cp_amm.json
|
|
33
29
|
var cp_amm_default = {
|
|
@@ -1074,6 +1070,123 @@ var cp_amm_default = {
|
|
|
1074
1070
|
],
|
|
1075
1071
|
args: []
|
|
1076
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
|
+
},
|
|
1077
1190
|
{
|
|
1078
1191
|
name: "create_claim_fee_operator",
|
|
1079
1192
|
discriminator: [
|
|
@@ -1165,7 +1278,7 @@ var cp_amm_default = {
|
|
|
1165
1278
|
{
|
|
1166
1279
|
name: "create_config",
|
|
1167
1280
|
docs: [
|
|
1168
|
-
"ADMIN FUNCTIONS
|
|
1281
|
+
"ADMIN FUNCTIONS /////"
|
|
1169
1282
|
],
|
|
1170
1283
|
discriminator: [
|
|
1171
1284
|
201,
|
|
@@ -3508,6 +3621,19 @@ var cp_amm_default = {
|
|
|
3508
3621
|
5
|
|
3509
3622
|
]
|
|
3510
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
|
+
},
|
|
3511
3637
|
{
|
|
3512
3638
|
name: "EvtCreateClaimFeeOperator",
|
|
3513
3639
|
discriminator: [
|
|
@@ -3899,6 +4025,11 @@ var cp_amm_default = {
|
|
|
3899
4025
|
code: 6038,
|
|
3900
4026
|
name: "FeeInverseIsIncorrect",
|
|
3901
4027
|
msg: "Fee inverse is incorrect"
|
|
4028
|
+
},
|
|
4029
|
+
{
|
|
4030
|
+
code: 6039,
|
|
4031
|
+
name: "PositionIsNotEmpty",
|
|
4032
|
+
msg: "Position is not empty"
|
|
3902
4033
|
}
|
|
3903
4034
|
],
|
|
3904
4035
|
types: [
|
|
@@ -4412,6 +4543,14 @@ var cp_amm_default = {
|
|
|
4412
4543
|
}
|
|
4413
4544
|
}
|
|
4414
4545
|
},
|
|
4546
|
+
{
|
|
4547
|
+
name: "token_a_amount",
|
|
4548
|
+
type: "u64"
|
|
4549
|
+
},
|
|
4550
|
+
{
|
|
4551
|
+
name: "token_b_amount",
|
|
4552
|
+
type: "u64"
|
|
4553
|
+
},
|
|
4415
4554
|
{
|
|
4416
4555
|
name: "total_amount_a",
|
|
4417
4556
|
type: "u64"
|
|
@@ -4567,6 +4706,30 @@ var cp_amm_default = {
|
|
|
4567
4706
|
]
|
|
4568
4707
|
}
|
|
4569
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
|
+
},
|
|
4570
4733
|
{
|
|
4571
4734
|
name: "EvtCreateClaimFeeOperator",
|
|
4572
4735
|
docs: [
|
|
@@ -4709,6 +4872,10 @@ var cp_amm_default = {
|
|
|
4709
4872
|
type: {
|
|
4710
4873
|
kind: "struct",
|
|
4711
4874
|
fields: [
|
|
4875
|
+
{
|
|
4876
|
+
name: "pool",
|
|
4877
|
+
type: "pubkey"
|
|
4878
|
+
},
|
|
4712
4879
|
{
|
|
4713
4880
|
name: "token_a_mint",
|
|
4714
4881
|
type: "pubkey"
|
|
@@ -4773,6 +4940,14 @@ var cp_amm_default = {
|
|
|
4773
4940
|
name: "token_b_flag",
|
|
4774
4941
|
type: "u8"
|
|
4775
4942
|
},
|
|
4943
|
+
{
|
|
4944
|
+
name: "token_a_amount",
|
|
4945
|
+
type: "u64"
|
|
4946
|
+
},
|
|
4947
|
+
{
|
|
4948
|
+
name: "token_b_amount",
|
|
4949
|
+
type: "u64"
|
|
4950
|
+
},
|
|
4776
4951
|
{
|
|
4777
4952
|
name: "total_amount_a",
|
|
4778
4953
|
type: "u64"
|
|
@@ -4874,11 +5049,11 @@ var cp_amm_default = {
|
|
|
4874
5049
|
type: "pubkey"
|
|
4875
5050
|
},
|
|
4876
5051
|
{
|
|
4877
|
-
name: "
|
|
5052
|
+
name: "lock_liquidity_amount",
|
|
4878
5053
|
type: "u128"
|
|
4879
5054
|
},
|
|
4880
5055
|
{
|
|
4881
|
-
name: "
|
|
5056
|
+
name: "total_permanent_locked_liquidity",
|
|
4882
5057
|
type: "u128"
|
|
4883
5058
|
}
|
|
4884
5059
|
]
|
|
@@ -4910,11 +5085,11 @@ var cp_amm_default = {
|
|
|
4910
5085
|
}
|
|
4911
5086
|
},
|
|
4912
5087
|
{
|
|
4913
|
-
name: "
|
|
5088
|
+
name: "token_a_amount",
|
|
4914
5089
|
type: "u64"
|
|
4915
5090
|
},
|
|
4916
5091
|
{
|
|
4917
|
-
name: "
|
|
5092
|
+
name: "token_b_amount",
|
|
4918
5093
|
type: "u64"
|
|
4919
5094
|
}
|
|
4920
5095
|
]
|
|
@@ -4950,7 +5125,7 @@ var cp_amm_default = {
|
|
|
4950
5125
|
type: "u8"
|
|
4951
5126
|
},
|
|
4952
5127
|
{
|
|
4953
|
-
name: "
|
|
5128
|
+
name: "has_referral",
|
|
4954
5129
|
type: "bool"
|
|
4955
5130
|
},
|
|
4956
5131
|
{
|
|
@@ -4970,7 +5145,7 @@ var cp_amm_default = {
|
|
|
4970
5145
|
}
|
|
4971
5146
|
},
|
|
4972
5147
|
{
|
|
4973
|
-
name: "
|
|
5148
|
+
name: "actual_amount_in",
|
|
4974
5149
|
type: "u64"
|
|
4975
5150
|
},
|
|
4976
5151
|
{
|
|
@@ -6176,16 +6351,52 @@ var cp_amm_default = {
|
|
|
6176
6351
|
|
|
6177
6352
|
// src/CpAmm.ts
|
|
6178
6353
|
import {
|
|
6179
|
-
|
|
6354
|
+
Transaction,
|
|
6180
6355
|
SystemProgram as SystemProgram2
|
|
6181
6356
|
} from "@solana/web3.js";
|
|
6182
6357
|
|
|
6358
|
+
// src/types.ts
|
|
6359
|
+
var Rounding = /* @__PURE__ */ ((Rounding2) => {
|
|
6360
|
+
Rounding2[Rounding2["Up"] = 0] = "Up";
|
|
6361
|
+
Rounding2[Rounding2["Down"] = 1] = "Down";
|
|
6362
|
+
return Rounding2;
|
|
6363
|
+
})(Rounding || {});
|
|
6364
|
+
var ActivationPoint = /* @__PURE__ */ ((ActivationPoint2) => {
|
|
6365
|
+
ActivationPoint2[ActivationPoint2["Timestamp"] = 0] = "Timestamp";
|
|
6366
|
+
ActivationPoint2[ActivationPoint2["Slot"] = 1] = "Slot";
|
|
6367
|
+
return ActivationPoint2;
|
|
6368
|
+
})(ActivationPoint || {});
|
|
6369
|
+
var FeeSchedulerMode = /* @__PURE__ */ ((FeeSchedulerMode2) => {
|
|
6370
|
+
FeeSchedulerMode2[FeeSchedulerMode2["Linear"] = 0] = "Linear";
|
|
6371
|
+
FeeSchedulerMode2[FeeSchedulerMode2["Exponential"] = 1] = "Exponential";
|
|
6372
|
+
return FeeSchedulerMode2;
|
|
6373
|
+
})(FeeSchedulerMode || {});
|
|
6374
|
+
var CollectFeeMode = /* @__PURE__ */ ((CollectFeeMode2) => {
|
|
6375
|
+
CollectFeeMode2[CollectFeeMode2["BothToken"] = 0] = "BothToken";
|
|
6376
|
+
CollectFeeMode2[CollectFeeMode2["OnlyB"] = 1] = "OnlyB";
|
|
6377
|
+
return CollectFeeMode2;
|
|
6378
|
+
})(CollectFeeMode || {});
|
|
6379
|
+
var TradeDirection = /* @__PURE__ */ ((TradeDirection2) => {
|
|
6380
|
+
TradeDirection2[TradeDirection2["AtoB"] = 0] = "AtoB";
|
|
6381
|
+
TradeDirection2[TradeDirection2["BtoA"] = 1] = "BtoA";
|
|
6382
|
+
return TradeDirection2;
|
|
6383
|
+
})(TradeDirection || {});
|
|
6384
|
+
var ActivationType = /* @__PURE__ */ ((ActivationType2) => {
|
|
6385
|
+
ActivationType2[ActivationType2["Slot"] = 0] = "Slot";
|
|
6386
|
+
ActivationType2[ActivationType2["Timestamp"] = 1] = "Timestamp";
|
|
6387
|
+
return ActivationType2;
|
|
6388
|
+
})(ActivationType || {});
|
|
6389
|
+
|
|
6390
|
+
// src/pda.ts
|
|
6391
|
+
import { PublicKey as PublicKey2 } from "@solana/web3.js";
|
|
6392
|
+
|
|
6183
6393
|
// src/constants.ts
|
|
6184
6394
|
import { BN } from "@coral-xyz/anchor";
|
|
6185
6395
|
import { PublicKey } from "@solana/web3.js";
|
|
6186
6396
|
var CP_AMM_PROGRAM_ID = new PublicKey(
|
|
6187
6397
|
"cpamdpZCGKUy5JxQXB4dcpGPiikHawvSWAd6mEn1sGG"
|
|
6188
6398
|
);
|
|
6399
|
+
var LIQUIDITY_SCALE = 128;
|
|
6189
6400
|
var SCALE_OFFSET = 64;
|
|
6190
6401
|
var BASIS_POINT_MAX = 1e4;
|
|
6191
6402
|
var MAX_FEE_NUMERATOR = 5e8;
|
|
@@ -6194,9 +6405,14 @@ var MIN_SQRT_PRICE = new BN("4295048016");
|
|
|
6194
6405
|
var MAX_SQRT_PRICE = new BN("79226673521066979257578248091");
|
|
6195
6406
|
var MIN_CU_BUFFER = 5e4;
|
|
6196
6407
|
var MAX_CU_BUFFER = 2e5;
|
|
6408
|
+
var DYNAMIC_FEE_FILTER_PERIOD_DEFAULT = 10;
|
|
6409
|
+
var DYNAMIC_FEE_DECAY_PERIOD_DEFAULT = 120;
|
|
6410
|
+
var DYNAMIC_FEE_REDUCTION_FACTOR_DEFAULT = 5e3;
|
|
6411
|
+
var BIN_STEP_BPS_DEFAULT = 1;
|
|
6412
|
+
var BIN_STEP_BPS_U128_DEFAULT = new BN("1844674407370955");
|
|
6413
|
+
var MAX_PRICE_CHANGE_BPS_DEFAULT = 1500;
|
|
6197
6414
|
|
|
6198
6415
|
// src/pda.ts
|
|
6199
|
-
import { PublicKey as PublicKey2 } from "@solana/web3.js";
|
|
6200
6416
|
function getFirstKey(key1, key2) {
|
|
6201
6417
|
const buf1 = key1.toBuffer();
|
|
6202
6418
|
const buf2 = key2.toBuffer();
|
|
@@ -6213,19 +6429,19 @@ function getSecondKey(key1, key2) {
|
|
|
6213
6429
|
}
|
|
6214
6430
|
return buf1;
|
|
6215
6431
|
}
|
|
6216
|
-
function derivePoolAuthority(
|
|
6432
|
+
function derivePoolAuthority() {
|
|
6217
6433
|
return PublicKey2.findProgramAddressSync(
|
|
6218
6434
|
[Buffer.from("pool_authority")],
|
|
6219
|
-
|
|
6435
|
+
CP_AMM_PROGRAM_ID
|
|
6220
6436
|
)[0];
|
|
6221
6437
|
}
|
|
6222
|
-
function deriveConfigAddress(index
|
|
6438
|
+
function deriveConfigAddress(index) {
|
|
6223
6439
|
return PublicKey2.findProgramAddressSync(
|
|
6224
6440
|
[Buffer.from("config"), index.toArrayLike(Buffer, "le", 8)],
|
|
6225
|
-
|
|
6441
|
+
CP_AMM_PROGRAM_ID
|
|
6226
6442
|
)[0];
|
|
6227
6443
|
}
|
|
6228
|
-
function derivePoolAddress(config, tokenAMint, tokenBMint
|
|
6444
|
+
function derivePoolAddress(config, tokenAMint, tokenBMint) {
|
|
6229
6445
|
return PublicKey2.findProgramAddressSync(
|
|
6230
6446
|
[
|
|
6231
6447
|
Buffer.from("pool"),
|
|
@@ -6233,124 +6449,60 @@ function derivePoolAddress(config, tokenAMint, tokenBMint, programId) {
|
|
|
6233
6449
|
getFirstKey(tokenAMint, tokenBMint),
|
|
6234
6450
|
getSecondKey(tokenAMint, tokenBMint)
|
|
6235
6451
|
],
|
|
6236
|
-
|
|
6452
|
+
CP_AMM_PROGRAM_ID
|
|
6237
6453
|
)[0];
|
|
6238
6454
|
}
|
|
6239
|
-
function derivePositionAddress(positionNft
|
|
6455
|
+
function derivePositionAddress(positionNft) {
|
|
6240
6456
|
return PublicKey2.findProgramAddressSync(
|
|
6241
6457
|
[Buffer.from("position"), positionNft.toBuffer()],
|
|
6242
|
-
|
|
6458
|
+
CP_AMM_PROGRAM_ID
|
|
6243
6459
|
)[0];
|
|
6244
6460
|
}
|
|
6245
|
-
function deriveTokenVaultAddress(tokenMint, pool
|
|
6461
|
+
function deriveTokenVaultAddress(tokenMint, pool) {
|
|
6246
6462
|
return PublicKey2.findProgramAddressSync(
|
|
6247
6463
|
[Buffer.from("token_vault"), tokenMint.toBuffer(), pool.toBuffer()],
|
|
6248
|
-
|
|
6464
|
+
CP_AMM_PROGRAM_ID
|
|
6249
6465
|
)[0];
|
|
6250
6466
|
}
|
|
6251
|
-
function deriveRewardVaultAddress(pool, rewardIndex
|
|
6467
|
+
function deriveRewardVaultAddress(pool, rewardIndex) {
|
|
6252
6468
|
return PublicKey2.findProgramAddressSync(
|
|
6253
6469
|
[Buffer.from("reward_vault"), pool.toBuffer(), Buffer.from([rewardIndex])],
|
|
6254
|
-
|
|
6470
|
+
CP_AMM_PROGRAM_ID
|
|
6255
6471
|
)[0];
|
|
6256
6472
|
}
|
|
6257
|
-
function deriveCustomizablePoolAddress(tokenAMint, tokenBMint
|
|
6473
|
+
function deriveCustomizablePoolAddress(tokenAMint, tokenBMint) {
|
|
6258
6474
|
return PublicKey2.findProgramAddressSync(
|
|
6259
6475
|
[
|
|
6260
6476
|
Buffer.from("cpool"),
|
|
6261
6477
|
getFirstKey(tokenAMint, tokenBMint),
|
|
6262
6478
|
getSecondKey(tokenAMint, tokenBMint)
|
|
6263
6479
|
],
|
|
6264
|
-
|
|
6480
|
+
CP_AMM_PROGRAM_ID
|
|
6265
6481
|
)[0];
|
|
6266
6482
|
}
|
|
6267
|
-
function deriveTokenBadgeAddress(tokenMint
|
|
6483
|
+
function deriveTokenBadgeAddress(tokenMint) {
|
|
6268
6484
|
return PublicKey2.findProgramAddressSync(
|
|
6269
6485
|
[Buffer.from("token_badge"), tokenMint.toBuffer()],
|
|
6270
|
-
|
|
6486
|
+
CP_AMM_PROGRAM_ID
|
|
6271
6487
|
)[0];
|
|
6272
6488
|
}
|
|
6273
|
-
function deriveClaimFeeOperatorAddress(operator
|
|
6489
|
+
function deriveClaimFeeOperatorAddress(operator) {
|
|
6274
6490
|
return PublicKey2.findProgramAddressSync(
|
|
6275
6491
|
[Buffer.from("cf_operator"), operator.toBuffer()],
|
|
6276
|
-
|
|
6492
|
+
CP_AMM_PROGRAM_ID
|
|
6277
6493
|
)[0];
|
|
6278
6494
|
}
|
|
6279
|
-
function derivePositionNftAccount(positionNftMint
|
|
6495
|
+
function derivePositionNftAccount(positionNftMint) {
|
|
6280
6496
|
return PublicKey2.findProgramAddressSync(
|
|
6281
6497
|
[Buffer.from("position_nft_account"), positionNftMint.toBuffer()],
|
|
6282
|
-
|
|
6498
|
+
CP_AMM_PROGRAM_ID
|
|
6283
6499
|
)[0];
|
|
6284
6500
|
}
|
|
6285
|
-
function deriveEventAuthority(programId) {
|
|
6286
|
-
return PublicKey2.findProgramAddressSync(
|
|
6287
|
-
[Buffer.from("__event_authority")],
|
|
6288
|
-
programId
|
|
6289
|
-
);
|
|
6290
|
-
}
|
|
6291
|
-
|
|
6292
|
-
// src/math/index.ts
|
|
6293
|
-
import { BN as BN2 } from "@coral-xyz/anchor";
|
|
6294
|
-
import Decimal from "decimal.js";
|
|
6295
|
-
|
|
6296
|
-
// src/types.ts
|
|
6297
|
-
var Rounding = /* @__PURE__ */ ((Rounding2) => {
|
|
6298
|
-
Rounding2[Rounding2["Up"] = 0] = "Up";
|
|
6299
|
-
Rounding2[Rounding2["Down"] = 1] = "Down";
|
|
6300
|
-
return Rounding2;
|
|
6301
|
-
})(Rounding || {});
|
|
6302
|
-
var ActivationPoint = /* @__PURE__ */ ((ActivationPoint2) => {
|
|
6303
|
-
ActivationPoint2[ActivationPoint2["Timestamp"] = 0] = "Timestamp";
|
|
6304
|
-
ActivationPoint2[ActivationPoint2["Slot"] = 1] = "Slot";
|
|
6305
|
-
return ActivationPoint2;
|
|
6306
|
-
})(ActivationPoint || {});
|
|
6307
|
-
var FeeSchedulerMode = /* @__PURE__ */ ((FeeSchedulerMode2) => {
|
|
6308
|
-
FeeSchedulerMode2[FeeSchedulerMode2["Linear"] = 0] = "Linear";
|
|
6309
|
-
FeeSchedulerMode2[FeeSchedulerMode2["Exponential"] = 1] = "Exponential";
|
|
6310
|
-
return FeeSchedulerMode2;
|
|
6311
|
-
})(FeeSchedulerMode || {});
|
|
6312
|
-
var CollectFeeMode = /* @__PURE__ */ ((CollectFeeMode2) => {
|
|
6313
|
-
CollectFeeMode2[CollectFeeMode2["BothToken"] = 0] = "BothToken";
|
|
6314
|
-
CollectFeeMode2[CollectFeeMode2["OnlyB"] = 1] = "OnlyB";
|
|
6315
|
-
return CollectFeeMode2;
|
|
6316
|
-
})(CollectFeeMode || {});
|
|
6317
|
-
var TradeDirection = /* @__PURE__ */ ((TradeDirection2) => {
|
|
6318
|
-
TradeDirection2[TradeDirection2["AtoB"] = 0] = "AtoB";
|
|
6319
|
-
TradeDirection2[TradeDirection2["BtoA"] = 1] = "BtoA";
|
|
6320
|
-
return TradeDirection2;
|
|
6321
|
-
})(TradeDirection || {});
|
|
6322
|
-
var ActivationType = /* @__PURE__ */ ((ActivationType2) => {
|
|
6323
|
-
ActivationType2[ActivationType2["Slot"] = 0] = "Slot";
|
|
6324
|
-
ActivationType2[ActivationType2["Timestamp"] = 1] = "Timestamp";
|
|
6325
|
-
return ActivationType2;
|
|
6326
|
-
})(ActivationType || {});
|
|
6327
6501
|
|
|
6328
|
-
// src/
|
|
6329
|
-
|
|
6330
|
-
const { div, mod } = x.mul(y).divmod(denominator);
|
|
6331
|
-
if (rounding == 0 /* Up */ && !mod.isZero()) {
|
|
6332
|
-
return div.add(new BN2(1));
|
|
6333
|
-
}
|
|
6334
|
-
return div;
|
|
6335
|
-
}
|
|
6336
|
-
function divCeil(a, b) {
|
|
6337
|
-
if (a.isZero()) {
|
|
6338
|
-
return new BN2(0);
|
|
6339
|
-
}
|
|
6340
|
-
return a.add(b.sub(new BN2(1))).div(b);
|
|
6341
|
-
}
|
|
6342
|
-
function decimalToQ64(num) {
|
|
6343
|
-
return new BN2(num.mul(Decimal.pow(2, 64)).floor().toFixed());
|
|
6344
|
-
}
|
|
6345
|
-
function priceToSqrtPrice(initPrice, tokenADecimal, tokenBDecimal) {
|
|
6346
|
-
const sqrtPriceQ64 = decimalToQ64(
|
|
6347
|
-
initPrice.mul(Decimal.pow(10, tokenBDecimal - tokenADecimal)).sqrt()
|
|
6348
|
-
);
|
|
6349
|
-
return sqrtPriceQ64;
|
|
6350
|
-
}
|
|
6351
|
-
|
|
6352
|
-
// src/utils/token.ts
|
|
6502
|
+
// src/helpers/token.ts
|
|
6503
|
+
import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";
|
|
6353
6504
|
import {
|
|
6505
|
+
AccountLayout,
|
|
6354
6506
|
createAssociatedTokenAccountIdempotentInstruction,
|
|
6355
6507
|
createCloseAccountInstruction,
|
|
6356
6508
|
getAccount,
|
|
@@ -6436,15 +6588,67 @@ var unwrapSOLInstruction = (owner, allowOwnerOffCurve = true) => __async(void 0,
|
|
|
6436
6588
|
}
|
|
6437
6589
|
return null;
|
|
6438
6590
|
});
|
|
6591
|
+
function getAllUserPositionNftAccount(connection, user) {
|
|
6592
|
+
return __async(this, null, function* () {
|
|
6593
|
+
const filters = [
|
|
6594
|
+
{
|
|
6595
|
+
memcmp: {
|
|
6596
|
+
offset: 32,
|
|
6597
|
+
bytes: user.toBase58()
|
|
6598
|
+
}
|
|
6599
|
+
},
|
|
6600
|
+
{
|
|
6601
|
+
memcmp: {
|
|
6602
|
+
offset: 64,
|
|
6603
|
+
bytes: bs58.encode(Buffer.from([1, 0, 0, 0, 0, 0, 0, 0]))
|
|
6604
|
+
// 1
|
|
6605
|
+
}
|
|
6606
|
+
}
|
|
6607
|
+
];
|
|
6608
|
+
const tokenAccountsRaw = yield connection.getProgramAccounts(
|
|
6609
|
+
TOKEN_2022_PROGRAM_ID,
|
|
6610
|
+
{
|
|
6611
|
+
filters
|
|
6612
|
+
}
|
|
6613
|
+
);
|
|
6614
|
+
const userPositionNftAccount = [];
|
|
6615
|
+
for (const { account, pubkey } of tokenAccountsRaw) {
|
|
6616
|
+
const tokenAccountData = AccountLayout.decode(account.data);
|
|
6617
|
+
userPositionNftAccount.push({
|
|
6618
|
+
positionNft: tokenAccountData.mint,
|
|
6619
|
+
positionNftAccount: pubkey
|
|
6620
|
+
});
|
|
6621
|
+
}
|
|
6622
|
+
return userPositionNftAccount;
|
|
6623
|
+
});
|
|
6624
|
+
}
|
|
6625
|
+
function getAllPositionNftAccountByOwner(connection, user) {
|
|
6626
|
+
return __async(this, null, function* () {
|
|
6627
|
+
const tokenAccounts = yield connection.getTokenAccountsByOwner(user, {
|
|
6628
|
+
programId: TOKEN_2022_PROGRAM_ID
|
|
6629
|
+
});
|
|
6630
|
+
const userPositionNftAccount = [];
|
|
6631
|
+
for (const { account, pubkey } of tokenAccounts.value) {
|
|
6632
|
+
const tokenAccountData = AccountLayout.decode(account.data);
|
|
6633
|
+
if (tokenAccountData.amount.toString() === "1") {
|
|
6634
|
+
userPositionNftAccount.push({
|
|
6635
|
+
positionNft: tokenAccountData.mint,
|
|
6636
|
+
positionNftAccount: pubkey
|
|
6637
|
+
});
|
|
6638
|
+
}
|
|
6639
|
+
}
|
|
6640
|
+
return userPositionNftAccount;
|
|
6641
|
+
});
|
|
6642
|
+
}
|
|
6439
6643
|
|
|
6440
|
-
// src/
|
|
6441
|
-
import { BN as
|
|
6644
|
+
// src/helpers/fee.ts
|
|
6645
|
+
import { BN as BN5 } from "@coral-xyz/anchor";
|
|
6442
6646
|
|
|
6443
6647
|
// src/math/feeMath.ts
|
|
6444
|
-
import { BN as
|
|
6445
|
-
var MAX_EXPONENTIAL = new
|
|
6446
|
-
var ONE = new
|
|
6447
|
-
var MAX = new
|
|
6648
|
+
import { BN as BN2 } from "@coral-xyz/anchor";
|
|
6649
|
+
var MAX_EXPONENTIAL = new BN2(524288);
|
|
6650
|
+
var ONE = new BN2(1).shln(SCALE_OFFSET);
|
|
6651
|
+
var MAX = new BN2(2).pow(new BN2(128)).sub(new BN2(1));
|
|
6448
6652
|
function pow(base, exp) {
|
|
6449
6653
|
let invert = exp.isNeg();
|
|
6450
6654
|
if (exp.isZero()) {
|
|
@@ -6452,7 +6656,7 @@ function pow(base, exp) {
|
|
|
6452
6656
|
}
|
|
6453
6657
|
exp = invert ? exp.abs() : exp;
|
|
6454
6658
|
if (exp.gt(MAX_EXPONENTIAL)) {
|
|
6455
|
-
return new
|
|
6659
|
+
return new BN2(0);
|
|
6456
6660
|
}
|
|
6457
6661
|
let squaredBase = base;
|
|
6458
6662
|
let result = ONE;
|
|
@@ -6460,83 +6664,83 @@ function pow(base, exp) {
|
|
|
6460
6664
|
squaredBase = MAX.div(squaredBase);
|
|
6461
6665
|
invert = !invert;
|
|
6462
6666
|
}
|
|
6463
|
-
if (!exp.and(new
|
|
6667
|
+
if (!exp.and(new BN2(1)).isZero()) {
|
|
6464
6668
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6465
6669
|
}
|
|
6466
6670
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6467
|
-
if (!exp.and(new
|
|
6671
|
+
if (!exp.and(new BN2(2)).isZero()) {
|
|
6468
6672
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6469
6673
|
}
|
|
6470
6674
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6471
|
-
if (!exp.and(new
|
|
6675
|
+
if (!exp.and(new BN2(4)).isZero()) {
|
|
6472
6676
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6473
6677
|
}
|
|
6474
6678
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6475
|
-
if (!exp.and(new
|
|
6679
|
+
if (!exp.and(new BN2(8)).isZero()) {
|
|
6476
6680
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6477
6681
|
}
|
|
6478
6682
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6479
|
-
if (!exp.and(new
|
|
6683
|
+
if (!exp.and(new BN2(16)).isZero()) {
|
|
6480
6684
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6481
6685
|
}
|
|
6482
6686
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6483
|
-
if (!exp.and(new
|
|
6687
|
+
if (!exp.and(new BN2(32)).isZero()) {
|
|
6484
6688
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6485
6689
|
}
|
|
6486
6690
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6487
|
-
if (!exp.and(new
|
|
6691
|
+
if (!exp.and(new BN2(64)).isZero()) {
|
|
6488
6692
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6489
6693
|
}
|
|
6490
6694
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6491
|
-
if (!exp.and(new
|
|
6695
|
+
if (!exp.and(new BN2(128)).isZero()) {
|
|
6492
6696
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6493
6697
|
}
|
|
6494
6698
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6495
|
-
if (!exp.and(new
|
|
6699
|
+
if (!exp.and(new BN2(256)).isZero()) {
|
|
6496
6700
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6497
6701
|
}
|
|
6498
6702
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6499
|
-
if (!exp.and(new
|
|
6703
|
+
if (!exp.and(new BN2(512)).isZero()) {
|
|
6500
6704
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6501
6705
|
}
|
|
6502
6706
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6503
|
-
if (!exp.and(new
|
|
6707
|
+
if (!exp.and(new BN2(1024)).isZero()) {
|
|
6504
6708
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6505
6709
|
}
|
|
6506
6710
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6507
|
-
if (!exp.and(new
|
|
6711
|
+
if (!exp.and(new BN2(2048)).isZero()) {
|
|
6508
6712
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6509
6713
|
}
|
|
6510
6714
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6511
|
-
if (!exp.and(new
|
|
6715
|
+
if (!exp.and(new BN2(4096)).isZero()) {
|
|
6512
6716
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6513
6717
|
}
|
|
6514
6718
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6515
|
-
if (!exp.and(new
|
|
6719
|
+
if (!exp.and(new BN2(8192)).isZero()) {
|
|
6516
6720
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6517
6721
|
}
|
|
6518
6722
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6519
|
-
if (!exp.and(new
|
|
6723
|
+
if (!exp.and(new BN2(16384)).isZero()) {
|
|
6520
6724
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6521
6725
|
}
|
|
6522
6726
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6523
|
-
if (!exp.and(new
|
|
6727
|
+
if (!exp.and(new BN2(32768)).isZero()) {
|
|
6524
6728
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6525
6729
|
}
|
|
6526
6730
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6527
|
-
if (!exp.and(new
|
|
6731
|
+
if (!exp.and(new BN2(65536)).isZero()) {
|
|
6528
6732
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6529
6733
|
}
|
|
6530
6734
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6531
|
-
if (!exp.and(new
|
|
6735
|
+
if (!exp.and(new BN2(131072)).isZero()) {
|
|
6532
6736
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6533
6737
|
}
|
|
6534
6738
|
squaredBase = squaredBase.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6535
|
-
if (!exp.and(new
|
|
6739
|
+
if (!exp.and(new BN2(262144)).isZero()) {
|
|
6536
6740
|
result = result.mul(squaredBase).shrn(SCALE_OFFSET);
|
|
6537
6741
|
}
|
|
6538
6742
|
if (result.isZero()) {
|
|
6539
|
-
return new
|
|
6743
|
+
return new BN2(0);
|
|
6540
6744
|
}
|
|
6541
6745
|
if (invert) {
|
|
6542
6746
|
result = MAX.div(result);
|
|
@@ -6544,13 +6748,74 @@ function pow(base, exp) {
|
|
|
6544
6748
|
return result;
|
|
6545
6749
|
}
|
|
6546
6750
|
|
|
6547
|
-
// src/
|
|
6751
|
+
// src/math/mathUtils.ts
|
|
6752
|
+
import { BN as BN3 } from "@coral-xyz/anchor";
|
|
6753
|
+
import Decimal from "decimal.js";
|
|
6754
|
+
function mulDiv(x, y, denominator, rounding) {
|
|
6755
|
+
const { div, mod } = x.mul(y).divmod(denominator);
|
|
6756
|
+
if (rounding == 0 /* Up */ && !mod.isZero()) {
|
|
6757
|
+
return div.add(new BN3(1));
|
|
6758
|
+
}
|
|
6759
|
+
return div;
|
|
6760
|
+
}
|
|
6761
|
+
function q64ToDecimal(num, decimalPlaces) {
|
|
6762
|
+
return new Decimal(num.toString()).div(Decimal.pow(2, 64)).toDecimalPlaces(decimalPlaces);
|
|
6763
|
+
}
|
|
6764
|
+
function decimalToQ64(num) {
|
|
6765
|
+
return new BN3(num.mul(Decimal.pow(2, 64)).floor().toFixed());
|
|
6766
|
+
}
|
|
6767
|
+
|
|
6768
|
+
// src/helpers/curve.ts
|
|
6769
|
+
import { BN as BN4 } from "@coral-xyz/anchor";
|
|
6770
|
+
function getNextSqrtPrice(amount, sqrtPrice, liquidity, aToB) {
|
|
6771
|
+
let result;
|
|
6772
|
+
if (aToB) {
|
|
6773
|
+
const product = amount.mul(sqrtPrice);
|
|
6774
|
+
const denominator = liquidity.add(product);
|
|
6775
|
+
const numerator = liquidity.mul(sqrtPrice);
|
|
6776
|
+
result = numerator.add(denominator.sub(new BN4(1))).div(denominator);
|
|
6777
|
+
} else {
|
|
6778
|
+
const quotient = amount.shln(SCALE_OFFSET * 2).div(liquidity);
|
|
6779
|
+
result = sqrtPrice.add(quotient);
|
|
6780
|
+
}
|
|
6781
|
+
return result;
|
|
6782
|
+
}
|
|
6783
|
+
function getLiquidityDeltaFromAmountA(amountA, lowerSqrtPrice, upperSqrtPrice) {
|
|
6784
|
+
const product = amountA.mul(lowerSqrtPrice).mul(upperSqrtPrice);
|
|
6785
|
+
const denominator = upperSqrtPrice.sub(lowerSqrtPrice);
|
|
6786
|
+
return product.div(denominator);
|
|
6787
|
+
}
|
|
6788
|
+
function getLiquidityDeltaFromAmountB(amountB, lowerSqrtPrice, upperSqrtPrice) {
|
|
6789
|
+
const denominator = upperSqrtPrice.sub(lowerSqrtPrice);
|
|
6790
|
+
const product = amountB.shln(128);
|
|
6791
|
+
return product.div(denominator);
|
|
6792
|
+
}
|
|
6793
|
+
function getAmountAFromLiquidityDelta(liquidity, currentSqrtPrice, maxSqrtPrice, rounding) {
|
|
6794
|
+
const product = liquidity.mul(maxSqrtPrice.sub(currentSqrtPrice));
|
|
6795
|
+
const denominator = currentSqrtPrice.mul(maxSqrtPrice);
|
|
6796
|
+
if (rounding == 0 /* Up */) {
|
|
6797
|
+
return product.add(denominator.sub(new BN4(1))).div(denominator);
|
|
6798
|
+
}
|
|
6799
|
+
return product.div(denominator);
|
|
6800
|
+
}
|
|
6801
|
+
function getAmountBFromLiquidityDelta(liquidity, currentSqrtPrice, minSqrtPrice, rounding) {
|
|
6802
|
+
const one = new BN4(1).shln(128);
|
|
6803
|
+
const deltaPrice = currentSqrtPrice.sub(minSqrtPrice);
|
|
6804
|
+
const result = liquidity.mul(deltaPrice);
|
|
6805
|
+
if (rounding == 0 /* Up */) {
|
|
6806
|
+
return result.add(one.sub(new BN4(1))).div(one);
|
|
6807
|
+
}
|
|
6808
|
+
return result.shrn(128);
|
|
6809
|
+
}
|
|
6810
|
+
|
|
6811
|
+
// src/helpers/fee.ts
|
|
6812
|
+
import Decimal2 from "decimal.js";
|
|
6548
6813
|
function getBaseFeeNumerator(feeSchedulerMode, cliffFeeNumerator, period, reductionFactor) {
|
|
6549
6814
|
let feeNumerator;
|
|
6550
6815
|
if (feeSchedulerMode == 0 /* Linear */) {
|
|
6551
6816
|
feeNumerator = cliffFeeNumerator.sub(period.mul(reductionFactor));
|
|
6552
6817
|
} else {
|
|
6553
|
-
const bps = reductionFactor.shln(SCALE_OFFSET).div(new
|
|
6818
|
+
const bps = reductionFactor.shln(SCALE_OFFSET).div(new BN5(BASIS_POINT_MAX));
|
|
6554
6819
|
const base = ONE.sub(bps);
|
|
6555
6820
|
const result = pow(base, period);
|
|
6556
6821
|
feeNumerator = cliffFeeNumerator.mul(result).shrn(SCALE_OFFSET);
|
|
@@ -6558,17 +6823,20 @@ function getBaseFeeNumerator(feeSchedulerMode, cliffFeeNumerator, period, reduct
|
|
|
6558
6823
|
return feeNumerator;
|
|
6559
6824
|
}
|
|
6560
6825
|
function getDynamicFeeNumerator(volatilityAccumulator, binStep, variableFeeControl) {
|
|
6561
|
-
|
|
6562
|
-
|
|
6563
|
-
|
|
6826
|
+
if (variableFeeControl.isZero()) {
|
|
6827
|
+
return new BN5(0);
|
|
6828
|
+
}
|
|
6829
|
+
const squareVfaBin = volatilityAccumulator.mul(new BN5(binStep)).pow(new BN5(2));
|
|
6830
|
+
const vFee = variableFeeControl.mul(squareVfaBin);
|
|
6831
|
+
return vFee.add(new BN5(99999999999)).div(new BN5(1e11));
|
|
6564
6832
|
}
|
|
6565
6833
|
function getFeeNumerator(currentPoint, activationPoint, numberOfPeriod, periodFrequency, feeSchedulerMode, cliffFeeNumerator, reductionFactor, dynamicFeeParams) {
|
|
6566
6834
|
if (Number(periodFrequency) == 0) {
|
|
6567
6835
|
return cliffFeeNumerator;
|
|
6568
6836
|
}
|
|
6569
|
-
const period = new
|
|
6570
|
-
new
|
|
6571
|
-
new
|
|
6837
|
+
const period = new BN5(currentPoint).lt(activationPoint) ? new BN5(numberOfPeriod) : BN5.min(
|
|
6838
|
+
new BN5(numberOfPeriod),
|
|
6839
|
+
new BN5(currentPoint).sub(activationPoint).div(periodFrequency)
|
|
6572
6840
|
);
|
|
6573
6841
|
let feeNumerator = getBaseFeeNumerator(
|
|
6574
6842
|
feeSchedulerMode,
|
|
@@ -6580,143 +6848,139 @@ function getFeeNumerator(currentPoint, activationPoint, numberOfPeriod, periodFr
|
|
|
6580
6848
|
const { volatilityAccumulator, binStep, variableFeeControl } = dynamicFeeParams;
|
|
6581
6849
|
const dynamicFeeNumberator = getDynamicFeeNumerator(
|
|
6582
6850
|
volatilityAccumulator,
|
|
6583
|
-
binStep,
|
|
6584
|
-
variableFeeControl
|
|
6851
|
+
new BN5(binStep),
|
|
6852
|
+
new BN5(variableFeeControl)
|
|
6585
6853
|
);
|
|
6586
|
-
feeNumerator.add(dynamicFeeNumberator);
|
|
6854
|
+
feeNumerator = feeNumerator.add(dynamicFeeNumberator);
|
|
6587
6855
|
}
|
|
6588
|
-
return feeNumerator.gt(new
|
|
6856
|
+
return feeNumerator.gt(new BN5(MAX_FEE_NUMERATOR)) ? new BN5(MAX_FEE_NUMERATOR) : feeNumerator;
|
|
6589
6857
|
}
|
|
6590
|
-
|
|
6591
|
-
|
|
6592
|
-
|
|
6593
|
-
|
|
6594
|
-
|
|
6595
|
-
|
|
6596
|
-
|
|
6597
|
-
const denominator = liquidity.add(product);
|
|
6598
|
-
result = mulDiv(liquidity, sqrtPrice, denominator, 0 /* Up */);
|
|
6599
|
-
} else {
|
|
6600
|
-
const quotient = amount.shln(SCALE_OFFSET * 2).div(liquidity);
|
|
6601
|
-
result = sqrtPrice.add(quotient);
|
|
6602
|
-
}
|
|
6603
|
-
return result;
|
|
6858
|
+
function getFeeMode(collectFeeMode, btoA) {
|
|
6859
|
+
const feeOnInput = btoA && collectFeeMode === 1 /* OnlyB */;
|
|
6860
|
+
const feesOnTokenA = btoA && collectFeeMode === 0 /* BothToken */;
|
|
6861
|
+
return {
|
|
6862
|
+
feeOnInput,
|
|
6863
|
+
feesOnTokenA
|
|
6864
|
+
};
|
|
6604
6865
|
}
|
|
6605
|
-
function
|
|
6606
|
-
|
|
6607
|
-
|
|
6608
|
-
|
|
6866
|
+
function getTotalFeeOnAmount(amount, tradeFeeNumerator) {
|
|
6867
|
+
return mulDiv(
|
|
6868
|
+
amount,
|
|
6869
|
+
tradeFeeNumerator,
|
|
6870
|
+
new BN5(FEE_DENOMINATOR),
|
|
6871
|
+
0 /* Up */
|
|
6872
|
+
);
|
|
6609
6873
|
}
|
|
6610
|
-
function
|
|
6611
|
-
|
|
6612
|
-
|
|
6613
|
-
let
|
|
6614
|
-
if (
|
|
6615
|
-
|
|
6616
|
-
|
|
6617
|
-
} else {
|
|
6618
|
-
result = prod.shrn(SCALE_OFFSET * 2);
|
|
6874
|
+
function getSwapAmount(inAmount, sqrtPrice, liquidity, tradeFeeNumerator, aToB, collectFeeMode) {
|
|
6875
|
+
let feeMode = getFeeMode(collectFeeMode, !aToB);
|
|
6876
|
+
let actualInAmount = inAmount;
|
|
6877
|
+
let totalFee = new BN5(0);
|
|
6878
|
+
if (feeMode.feeOnInput) {
|
|
6879
|
+
totalFee = getTotalFeeOnAmount(inAmount, tradeFeeNumerator);
|
|
6880
|
+
actualInAmount = inAmount.sub(totalFee);
|
|
6619
6881
|
}
|
|
6620
|
-
|
|
6882
|
+
const outAmount = aToB ? getAmountBFromLiquidityDelta(
|
|
6883
|
+
liquidity,
|
|
6884
|
+
sqrtPrice,
|
|
6885
|
+
getNextSqrtPrice(actualInAmount, sqrtPrice, liquidity, true),
|
|
6886
|
+
1 /* Down */
|
|
6887
|
+
) : getAmountAFromLiquidityDelta(
|
|
6888
|
+
liquidity,
|
|
6889
|
+
sqrtPrice,
|
|
6890
|
+
getNextSqrtPrice(actualInAmount, sqrtPrice, liquidity, false),
|
|
6891
|
+
1 /* Down */
|
|
6892
|
+
);
|
|
6893
|
+
const amountOut = feeMode.feeOnInput ? outAmount : (totalFee = getTotalFeeOnAmount(outAmount, tradeFeeNumerator), outAmount.sub(totalFee));
|
|
6894
|
+
return { amountOut, totalFee };
|
|
6621
6895
|
}
|
|
6622
|
-
function
|
|
6623
|
-
|
|
6624
|
-
|
|
6625
|
-
|
|
6626
|
-
|
|
6627
|
-
|
|
6628
|
-
|
|
6629
|
-
|
|
6630
|
-
|
|
6631
|
-
|
|
6632
|
-
);
|
|
6633
|
-
outAmount = getDeltaAmountB(
|
|
6634
|
-
nextSqrtPrice,
|
|
6635
|
-
sqrtPrice,
|
|
6636
|
-
liquidity,
|
|
6637
|
-
1 /* Down */
|
|
6638
|
-
);
|
|
6639
|
-
lpFee = mulDiv(
|
|
6640
|
-
outAmount,
|
|
6641
|
-
tradeFeeNumerator,
|
|
6642
|
-
new BN5(FEE_DENOMINATOR),
|
|
6643
|
-
1 /* Down */
|
|
6644
|
-
);
|
|
6645
|
-
} else {
|
|
6646
|
-
const nextSqrtPrice = getNextSqrtPrice(
|
|
6647
|
-
inAmount,
|
|
6648
|
-
sqrtPrice,
|
|
6649
|
-
liquidity,
|
|
6650
|
-
false
|
|
6651
|
-
);
|
|
6652
|
-
outAmount = getDeltaAmountA(
|
|
6653
|
-
sqrtPrice,
|
|
6654
|
-
nextSqrtPrice,
|
|
6655
|
-
liquidity,
|
|
6656
|
-
1 /* Down */
|
|
6657
|
-
);
|
|
6658
|
-
lpFee = mulDiv(
|
|
6659
|
-
outAmount,
|
|
6660
|
-
tradeFeeNumerator,
|
|
6661
|
-
new BN5(new BN5(FEE_DENOMINATOR)),
|
|
6662
|
-
1 /* Down */
|
|
6663
|
-
);
|
|
6896
|
+
function bpsToFeeNumerator(bps) {
|
|
6897
|
+
return new BN5(bps * FEE_DENOMINATOR).divn(BASIS_POINT_MAX);
|
|
6898
|
+
}
|
|
6899
|
+
function feeNumeratorToBps(feeNumerator) {
|
|
6900
|
+
return feeNumerator.muln(BASIS_POINT_MAX).div(new BN5(FEE_DENOMINATOR)).toNumber();
|
|
6901
|
+
}
|
|
6902
|
+
function getBaseFeeParams(maxBaseFeeBps, minBaseFeeBps, feeSchedulerMode, numberOfPeriod, totalDuration) {
|
|
6903
|
+
if (maxBaseFeeBps == minBaseFeeBps) {
|
|
6904
|
+
if (numberOfPeriod != 0 || totalDuration != 0) {
|
|
6905
|
+
throw new Error("numberOfPeriod and totalDuration must both be zero");
|
|
6664
6906
|
}
|
|
6665
|
-
|
|
6666
|
-
|
|
6667
|
-
|
|
6668
|
-
|
|
6669
|
-
|
|
6670
|
-
|
|
6907
|
+
return {
|
|
6908
|
+
cliffFeeNumerator: bpsToFeeNumerator(maxBaseFeeBps),
|
|
6909
|
+
numberOfPeriod: 0,
|
|
6910
|
+
periodFrequency: new BN5(0),
|
|
6911
|
+
reductionFactor: new BN5(0),
|
|
6912
|
+
feeSchedulerMode: 0
|
|
6913
|
+
};
|
|
6914
|
+
}
|
|
6915
|
+
if (numberOfPeriod <= 0) {
|
|
6916
|
+
throw new Error("Total periods must be greater than zero");
|
|
6917
|
+
}
|
|
6918
|
+
if (maxBaseFeeBps > feeNumeratorToBps(new BN5(MAX_FEE_NUMERATOR))) {
|
|
6919
|
+
throw new Error(
|
|
6920
|
+
`maxBaseFeeBps (${maxBaseFeeBps} bps) exceeds maximum allowed value of ${feeNumeratorToBps(
|
|
6921
|
+
new BN5(MAX_FEE_NUMERATOR)
|
|
6922
|
+
)} bps`
|
|
6671
6923
|
);
|
|
6672
|
-
|
|
6673
|
-
|
|
6674
|
-
|
|
6675
|
-
|
|
6676
|
-
1 /* Down */
|
|
6924
|
+
}
|
|
6925
|
+
if (minBaseFeeBps > maxBaseFeeBps) {
|
|
6926
|
+
throw new Error(
|
|
6927
|
+
"minBaseFee bps must be less than or equal to maxBaseFee bps"
|
|
6677
6928
|
);
|
|
6678
|
-
|
|
6679
|
-
|
|
6680
|
-
|
|
6681
|
-
|
|
6682
|
-
1 /* Down */
|
|
6929
|
+
}
|
|
6930
|
+
if (numberOfPeriod == 0 || totalDuration == 0) {
|
|
6931
|
+
throw new Error(
|
|
6932
|
+
"numberOfPeriod and totalDuration must both greater than zero"
|
|
6683
6933
|
);
|
|
6684
|
-
if (aToB) {
|
|
6685
|
-
} else {
|
|
6686
|
-
lpFee = mulDiv(
|
|
6687
|
-
inAmount,
|
|
6688
|
-
tradeFeeNumerator,
|
|
6689
|
-
new BN5(FEE_DENOMINATOR),
|
|
6690
|
-
1 /* Down */
|
|
6691
|
-
);
|
|
6692
|
-
const nextSqrtPrice2 = getNextSqrtPrice(
|
|
6693
|
-
inAmount.sub(lpFee),
|
|
6694
|
-
sqrtPrice,
|
|
6695
|
-
liquidity,
|
|
6696
|
-
false
|
|
6697
|
-
);
|
|
6698
|
-
outAmount = getDeltaAmountA(
|
|
6699
|
-
sqrtPrice,
|
|
6700
|
-
nextSqrtPrice2,
|
|
6701
|
-
liquidity,
|
|
6702
|
-
1 /* Down */
|
|
6703
|
-
);
|
|
6704
|
-
}
|
|
6705
6934
|
}
|
|
6706
|
-
|
|
6707
|
-
|
|
6708
|
-
|
|
6709
|
-
|
|
6710
|
-
|
|
6711
|
-
|
|
6935
|
+
const maxBaseFeeNumerator = bpsToFeeNumerator(maxBaseFeeBps);
|
|
6936
|
+
const minBaseFeeNumerator = bpsToFeeNumerator(minBaseFeeBps);
|
|
6937
|
+
const periodFrequency = new BN5(totalDuration / numberOfPeriod);
|
|
6938
|
+
let reductionFactor;
|
|
6939
|
+
if (feeSchedulerMode == 0 /* Linear */) {
|
|
6940
|
+
const totalReduction = maxBaseFeeNumerator.sub(minBaseFeeNumerator);
|
|
6941
|
+
reductionFactor = totalReduction.divn(numberOfPeriod);
|
|
6942
|
+
} else {
|
|
6943
|
+
const ratio = minBaseFeeNumerator.toNumber() / maxBaseFeeNumerator.toNumber();
|
|
6944
|
+
const decayBase = Math.pow(ratio, 1 / numberOfPeriod);
|
|
6945
|
+
reductionFactor = new BN5(BASIS_POINT_MAX * (1 - decayBase));
|
|
6946
|
+
}
|
|
6947
|
+
return {
|
|
6948
|
+
cliffFeeNumerator: maxBaseFeeNumerator,
|
|
6949
|
+
numberOfPeriod,
|
|
6950
|
+
periodFrequency,
|
|
6951
|
+
reductionFactor,
|
|
6952
|
+
feeSchedulerMode
|
|
6953
|
+
};
|
|
6712
6954
|
}
|
|
6713
|
-
function
|
|
6714
|
-
|
|
6715
|
-
|
|
6716
|
-
|
|
6955
|
+
function getDynamicFeeParams(baseFeeBps, maxPriceChangeBps = MAX_PRICE_CHANGE_BPS_DEFAULT) {
|
|
6956
|
+
if (maxPriceChangeBps > MAX_PRICE_CHANGE_BPS_DEFAULT) {
|
|
6957
|
+
throw new Error(
|
|
6958
|
+
`maxPriceChangeBps (${maxPriceChangeBps} bps) must be less than or equal to ${MAX_PRICE_CHANGE_BPS_DEFAULT}`
|
|
6959
|
+
);
|
|
6960
|
+
}
|
|
6961
|
+
const priceRatio = maxPriceChangeBps / BASIS_POINT_MAX + 1;
|
|
6962
|
+
const sqrtPriceRatioQ64 = new BN5(
|
|
6963
|
+
Decimal2.sqrt(priceRatio.toString()).mul(Decimal2.pow(2, 64)).floor().toFixed()
|
|
6964
|
+
);
|
|
6965
|
+
const deltaBinId = sqrtPriceRatioQ64.sub(ONE).div(BIN_STEP_BPS_U128_DEFAULT).muln(2);
|
|
6966
|
+
const maxVolatilityAccumulator = new BN5(deltaBinId.muln(BASIS_POINT_MAX));
|
|
6967
|
+
const squareVfaBin = maxVolatilityAccumulator.mul(new BN5(BIN_STEP_BPS_DEFAULT)).pow(new BN5(2));
|
|
6968
|
+
const baseFeeNumerator = new BN5(bpsToFeeNumerator(baseFeeBps));
|
|
6969
|
+
const maxDynamicFeeNumerator = baseFeeNumerator.muln(20).divn(100);
|
|
6970
|
+
const vFee = maxDynamicFeeNumerator.mul(new BN5(1e11)).sub(new BN5(99999999999));
|
|
6971
|
+
const variableFeeControl = vFee.div(squareVfaBin);
|
|
6972
|
+
return {
|
|
6973
|
+
binStep: BIN_STEP_BPS_DEFAULT,
|
|
6974
|
+
binStepU128: BIN_STEP_BPS_U128_DEFAULT,
|
|
6975
|
+
filterPeriod: DYNAMIC_FEE_FILTER_PERIOD_DEFAULT,
|
|
6976
|
+
decayPeriod: DYNAMIC_FEE_DECAY_PERIOD_DEFAULT,
|
|
6977
|
+
reductionFactor: DYNAMIC_FEE_REDUCTION_FACTOR_DEFAULT,
|
|
6978
|
+
maxVolatilityAccumulator: maxVolatilityAccumulator.toNumber(),
|
|
6979
|
+
variableFeeControl: variableFeeControl.toNumber()
|
|
6980
|
+
};
|
|
6717
6981
|
}
|
|
6718
6982
|
|
|
6719
|
-
// src/
|
|
6983
|
+
// src/helpers/computeUnits.ts
|
|
6720
6984
|
import {
|
|
6721
6985
|
ComputeBudgetProgram,
|
|
6722
6986
|
PublicKey as PublicKey4,
|
|
@@ -6791,233 +7055,1080 @@ var getEstimatedComputeUnitIxWithBuffer = (connection, instructions, feePayer, b
|
|
|
6791
7055
|
return ComputeBudgetProgram.setComputeUnitLimit({ units });
|
|
6792
7056
|
});
|
|
6793
7057
|
|
|
6794
|
-
// src/
|
|
7058
|
+
// src/helpers/utils.ts
|
|
6795
7059
|
import { BN as BN6 } from "@coral-xyz/anchor";
|
|
6796
|
-
import
|
|
6797
|
-
var
|
|
6798
|
-
const slippage = (100
|
|
7060
|
+
import Decimal3 from "decimal.js";
|
|
7061
|
+
var getMaxAmountWithSlippage = (amount, rate) => {
|
|
7062
|
+
const slippage = (100 + rate) / 100 * BASIS_POINT_MAX;
|
|
6799
7063
|
return amount.mul(new BN6(slippage)).div(new BN6(BASIS_POINT_MAX));
|
|
6800
7064
|
};
|
|
6801
|
-
var
|
|
6802
|
-
const
|
|
6803
|
-
return new
|
|
7065
|
+
var getMinAmountWithSlippage = (amount, rate) => {
|
|
7066
|
+
const slippage = (100 - rate) / 100 * BASIS_POINT_MAX;
|
|
7067
|
+
return amount.mul(new BN6(slippage)).div(new BN6(BASIS_POINT_MAX));
|
|
7068
|
+
};
|
|
7069
|
+
var getPriceImpact = (actualAmount, idealAmount) => {
|
|
7070
|
+
const diff = idealAmount.sub(actualAmount);
|
|
7071
|
+
return new Decimal3(diff.toString()).div(new Decimal3(idealAmount.toString())).mul(100).toNumber();
|
|
7072
|
+
};
|
|
7073
|
+
var getPriceFromSqrtPrice = (sqrtPrice, tokenADecimal, tokenBDecimal) => {
|
|
7074
|
+
const decimalSqrtPrice = new Decimal3(sqrtPrice.toString());
|
|
7075
|
+
const price = decimalSqrtPrice.mul(decimalSqrtPrice).mul(new Decimal3(__pow(10, tokenADecimal - tokenBDecimal))).div(Decimal3.pow(2, 128)).toString();
|
|
7076
|
+
return price;
|
|
7077
|
+
};
|
|
7078
|
+
var getSqrtPriceFromPrice = (price, tokenADecimal, tokenBDecimal) => {
|
|
7079
|
+
const decimalPrice = new Decimal3(price);
|
|
7080
|
+
const adjustedByDecimals = decimalPrice.div(
|
|
7081
|
+
new Decimal3(__pow(10, tokenADecimal - tokenBDecimal))
|
|
7082
|
+
);
|
|
7083
|
+
const sqrtValue = Decimal3.sqrt(adjustedByDecimals);
|
|
7084
|
+
const sqrtValueQ64 = sqrtValue.mul(Decimal3.pow(2, 64));
|
|
7085
|
+
return new BN6(sqrtValueQ64.floor().toFixed());
|
|
7086
|
+
};
|
|
7087
|
+
var getUnClaimReward = (poolState, positionState) => {
|
|
7088
|
+
const totalPositionLiquidity = positionState.unlockedLiquidity.add(positionState.vestedLiquidity).add(positionState.permanentLockedLiquidity);
|
|
7089
|
+
const feeAPerTokenStored = new BN6(
|
|
7090
|
+
Buffer.from(poolState.feeAPerLiquidity).reverse()
|
|
7091
|
+
).sub(new BN6(Buffer.from(positionState.feeAPerTokenCheckpoint).reverse()));
|
|
7092
|
+
const feeBPerTokenStored = new BN6(
|
|
7093
|
+
Buffer.from(poolState.feeBPerLiquidity).reverse()
|
|
7094
|
+
).sub(new BN6(Buffer.from(positionState.feeBPerTokenCheckpoint).reverse()));
|
|
7095
|
+
const feeA = totalPositionLiquidity.mul(feeAPerTokenStored).shrn(LIQUIDITY_SCALE);
|
|
7096
|
+
const feeB = totalPositionLiquidity.mul(feeBPerTokenStored).shrn(LIQUIDITY_SCALE);
|
|
7097
|
+
return {
|
|
7098
|
+
feeTokenA: positionState.feeAPending.add(feeA),
|
|
7099
|
+
feeTokenB: positionState.feeBPending.add(feeB),
|
|
7100
|
+
rewards: positionState.rewardInfos.length > 0 ? positionState.rewardInfos.map((item) => item.rewardPendings) : []
|
|
7101
|
+
};
|
|
7102
|
+
};
|
|
7103
|
+
|
|
7104
|
+
// src/helpers/accountFilters.ts
|
|
7105
|
+
var positionByPoolFilter = (pool) => {
|
|
7106
|
+
return {
|
|
7107
|
+
memcmp: {
|
|
7108
|
+
bytes: pool.toBase58(),
|
|
7109
|
+
offset: 8
|
|
7110
|
+
}
|
|
7111
|
+
};
|
|
7112
|
+
};
|
|
7113
|
+
var vestingByPositionFilter = (position) => {
|
|
7114
|
+
return {
|
|
7115
|
+
memcmp: {
|
|
7116
|
+
bytes: position.toBase58(),
|
|
7117
|
+
offset: 8
|
|
7118
|
+
}
|
|
7119
|
+
};
|
|
6804
7120
|
};
|
|
6805
7121
|
|
|
7122
|
+
// src/helpers/priceMath.ts
|
|
7123
|
+
import { BN as BN7 } from "@coral-xyz/anchor";
|
|
7124
|
+
import Decimal4 from "decimal.js";
|
|
7125
|
+
function calculateInitSqrtPrice(tokenAAmount, tokenBAmount, minSqrtPrice, maxSqrtPrice) {
|
|
7126
|
+
if (tokenAAmount.isZero() || tokenBAmount.isZero()) {
|
|
7127
|
+
throw new Error("Amount cannot be zero");
|
|
7128
|
+
}
|
|
7129
|
+
const amountADecimal = new Decimal4(tokenAAmount.toString());
|
|
7130
|
+
const amountBDecimal = new Decimal4(tokenBAmount.toString());
|
|
7131
|
+
const minSqrtPriceDecimal = new Decimal4(minSqrtPrice.toString()).div(
|
|
7132
|
+
Decimal4.pow(2, 64)
|
|
7133
|
+
);
|
|
7134
|
+
const maxSqrtPriceDecimal = new Decimal4(maxSqrtPrice.toString()).div(
|
|
7135
|
+
Decimal4.pow(2, 64)
|
|
7136
|
+
);
|
|
7137
|
+
const x = new Decimal4(1).div(maxSqrtPriceDecimal);
|
|
7138
|
+
const y = amountBDecimal.div(amountADecimal);
|
|
7139
|
+
const xy = x.mul(y);
|
|
7140
|
+
const paMinusXY = minSqrtPriceDecimal.sub(xy);
|
|
7141
|
+
const xyMinusPa = xy.sub(minSqrtPriceDecimal);
|
|
7142
|
+
const fourY = new Decimal4(4).mul(y);
|
|
7143
|
+
const discriminant = xyMinusPa.mul(xyMinusPa).add(fourY);
|
|
7144
|
+
const sqrtDiscriminant = discriminant.sqrt();
|
|
7145
|
+
const result = paMinusXY.add(sqrtDiscriminant).div(new Decimal4(2)).mul(Decimal4.pow(2, 64));
|
|
7146
|
+
return new BN7(result.floor().toFixed());
|
|
7147
|
+
}
|
|
7148
|
+
|
|
7149
|
+
// src/helpers/token2022.ts
|
|
7150
|
+
import { BN as BN8 } from "@coral-xyz/anchor";
|
|
7151
|
+
import {
|
|
7152
|
+
calculateFee,
|
|
7153
|
+
getEpochFee,
|
|
7154
|
+
getTransferFeeConfig,
|
|
7155
|
+
MAX_FEE_BASIS_POINTS
|
|
7156
|
+
} from "@solana/spl-token";
|
|
7157
|
+
function calculatePreFeeAmount(transferFee, postFeeAmount) {
|
|
7158
|
+
if (postFeeAmount.isZero()) {
|
|
7159
|
+
return new BN8(0);
|
|
7160
|
+
}
|
|
7161
|
+
if (transferFee.transferFeeBasisPoints === 0) {
|
|
7162
|
+
return postFeeAmount;
|
|
7163
|
+
}
|
|
7164
|
+
const maximumFee = new BN8(transferFee.maximumFee.toString());
|
|
7165
|
+
if (transferFee.transferFeeBasisPoints === MAX_FEE_BASIS_POINTS) {
|
|
7166
|
+
return postFeeAmount.add(maximumFee);
|
|
7167
|
+
}
|
|
7168
|
+
const ONE_IN_BASIS_POINTS = new BN8(MAX_FEE_BASIS_POINTS);
|
|
7169
|
+
const numerator = postFeeAmount.mul(ONE_IN_BASIS_POINTS);
|
|
7170
|
+
const denominator = ONE_IN_BASIS_POINTS.sub(
|
|
7171
|
+
new BN8(transferFee.transferFeeBasisPoints)
|
|
7172
|
+
);
|
|
7173
|
+
const rawPreFeeAmount = numerator.add(denominator).sub(new BN8(1)).div(denominator);
|
|
7174
|
+
if (rawPreFeeAmount.sub(postFeeAmount).gte(maximumFee)) {
|
|
7175
|
+
return postFeeAmount.add(maximumFee);
|
|
7176
|
+
}
|
|
7177
|
+
return rawPreFeeAmount;
|
|
7178
|
+
}
|
|
7179
|
+
function calculateInverseFee(transferFee, postFeeAmount) {
|
|
7180
|
+
const preFeeAmount = calculatePreFeeAmount(transferFee, postFeeAmount);
|
|
7181
|
+
return new BN8(
|
|
7182
|
+
calculateFee(transferFee, BigInt(preFeeAmount.toString())).toString()
|
|
7183
|
+
);
|
|
7184
|
+
}
|
|
7185
|
+
function calculateTransferFeeIncludedAmount(transferFeeExcludedAmount, mint, currentEpoch) {
|
|
7186
|
+
if (transferFeeExcludedAmount.isZero()) {
|
|
7187
|
+
return {
|
|
7188
|
+
amount: new BN8(0),
|
|
7189
|
+
transferFee: new BN8(0)
|
|
7190
|
+
};
|
|
7191
|
+
}
|
|
7192
|
+
const transferFeeConfig = getTransferFeeConfig(mint);
|
|
7193
|
+
if (transferFeeConfig === null) {
|
|
7194
|
+
return {
|
|
7195
|
+
amount: transferFeeExcludedAmount,
|
|
7196
|
+
transferFee: new BN8(0)
|
|
7197
|
+
};
|
|
7198
|
+
}
|
|
7199
|
+
const epochFee = getEpochFee(transferFeeConfig, BigInt(currentEpoch));
|
|
7200
|
+
const transferFee = epochFee.transferFeeBasisPoints == MAX_FEE_BASIS_POINTS ? new BN8(epochFee.maximumFee.toString()) : calculateInverseFee(epochFee, transferFeeExcludedAmount);
|
|
7201
|
+
const transferFeeIncludedAmount = transferFeeExcludedAmount.add(transferFee);
|
|
7202
|
+
return {
|
|
7203
|
+
amount: transferFeeIncludedAmount,
|
|
7204
|
+
transferFee
|
|
7205
|
+
};
|
|
7206
|
+
}
|
|
7207
|
+
function calculateTransferFeeExcludedAmount(transferFeeIncludedAmount, mint, currentEpoch) {
|
|
7208
|
+
const transferFeeConfig = getTransferFeeConfig(mint);
|
|
7209
|
+
if (transferFeeConfig === null) {
|
|
7210
|
+
return {
|
|
7211
|
+
amount: transferFeeIncludedAmount,
|
|
7212
|
+
transferFee: new BN8(0)
|
|
7213
|
+
};
|
|
7214
|
+
}
|
|
7215
|
+
const transferFeeIncludedAmountN = BigInt(
|
|
7216
|
+
transferFeeIncludedAmount.toString()
|
|
7217
|
+
);
|
|
7218
|
+
const transferFee = calculateFee(
|
|
7219
|
+
getEpochFee(transferFeeConfig, BigInt(currentEpoch)),
|
|
7220
|
+
transferFeeIncludedAmountN
|
|
7221
|
+
);
|
|
7222
|
+
const transferFeeExcludedAmount = new BN8(
|
|
7223
|
+
(transferFeeIncludedAmountN - transferFee).toString()
|
|
7224
|
+
);
|
|
7225
|
+
return {
|
|
7226
|
+
amount: transferFeeExcludedAmount,
|
|
7227
|
+
transferFee: new BN8(transferFee.toString())
|
|
7228
|
+
};
|
|
7229
|
+
}
|
|
7230
|
+
|
|
7231
|
+
// src/helpers/vestings.ts
|
|
7232
|
+
import { BN as BN9 } from "@coral-xyz/anchor";
|
|
7233
|
+
import { min } from "bn.js";
|
|
7234
|
+
function isVestingComplete(vestingData, currentPoint) {
|
|
7235
|
+
const cliffPoint = vestingData.cliffPoint;
|
|
7236
|
+
const periodFrequency = vestingData.periodFrequency;
|
|
7237
|
+
const numberOfPeriods = vestingData.numberOfPeriod;
|
|
7238
|
+
const endPoint = cliffPoint.add(periodFrequency.muln(numberOfPeriods));
|
|
7239
|
+
return currentPoint.gte(endPoint);
|
|
7240
|
+
}
|
|
7241
|
+
function getTotalLockedLiquidity(vestingData) {
|
|
7242
|
+
return vestingData.cliffUnlockLiquidity.add(
|
|
7243
|
+
vestingData.liquidityPerPeriod.mul(new BN9(vestingData.numberOfPeriod))
|
|
7244
|
+
);
|
|
7245
|
+
}
|
|
7246
|
+
function getAvailableVestingLiquidity(vestingData, currentPoint) {
|
|
7247
|
+
const {
|
|
7248
|
+
cliffPoint,
|
|
7249
|
+
periodFrequency,
|
|
7250
|
+
cliffUnlockLiquidity,
|
|
7251
|
+
liquidityPerPeriod,
|
|
7252
|
+
numberOfPeriod,
|
|
7253
|
+
totalReleasedLiquidity
|
|
7254
|
+
} = vestingData;
|
|
7255
|
+
if (currentPoint.lt(cliffPoint)) {
|
|
7256
|
+
return new BN9(0);
|
|
7257
|
+
}
|
|
7258
|
+
if (periodFrequency.isZero()) {
|
|
7259
|
+
return cliffUnlockLiquidity;
|
|
7260
|
+
}
|
|
7261
|
+
let passedPeriod = new BN9(currentPoint).sub(cliffPoint).div(periodFrequency);
|
|
7262
|
+
passedPeriod = min(passedPeriod, new BN9(numberOfPeriod));
|
|
7263
|
+
const unlockedLiquidity = cliffUnlockLiquidity.add(
|
|
7264
|
+
passedPeriod.mul(liquidityPerPeriod)
|
|
7265
|
+
);
|
|
7266
|
+
const availableReleasingLiquidity = unlockedLiquidity.sub(
|
|
7267
|
+
totalReleasedLiquidity
|
|
7268
|
+
);
|
|
7269
|
+
return availableReleasingLiquidity;
|
|
7270
|
+
}
|
|
7271
|
+
|
|
6806
7272
|
// src/CpAmm.ts
|
|
7273
|
+
import { min as min2 } from "bn.js";
|
|
6807
7274
|
var CpAmm = class {
|
|
6808
7275
|
constructor(connection) {
|
|
6809
7276
|
this._program = new Program(cp_amm_default, {
|
|
6810
7277
|
connection
|
|
6811
7278
|
});
|
|
6812
|
-
|
|
6813
|
-
getProgram() {
|
|
6814
|
-
return this._program;
|
|
7279
|
+
this.poolAuthority = derivePoolAuthority();
|
|
6815
7280
|
}
|
|
6816
7281
|
/**
|
|
6817
|
-
|
|
6818
|
-
|
|
6819
|
-
|
|
6820
|
-
|
|
6821
|
-
|
|
6822
|
-
|
|
6823
|
-
|
|
6824
|
-
|
|
6825
|
-
|
|
6826
|
-
|
|
6827
|
-
|
|
6828
|
-
const { tokenAAmount, tokenBAmount, tokenADecimal, tokenBDecimal } = params;
|
|
6829
|
-
const initPrice = tokenBAmount.div(tokenAAmount);
|
|
6830
|
-
const sqrtPriceQ64 = priceToSqrtPrice(
|
|
6831
|
-
new Decimal3(initPrice.toString()),
|
|
6832
|
-
tokenADecimal,
|
|
6833
|
-
tokenBDecimal
|
|
6834
|
-
);
|
|
6835
|
-
if (sqrtPriceQ64.lt(MIN_SQRT_PRICE) || sqrtPriceQ64.gt(MAX_SQRT_PRICE)) {
|
|
6836
|
-
throw new Error(`Invalid sqrt price: ${sqrtPriceQ64.toString()}`);
|
|
6837
|
-
}
|
|
6838
|
-
const liquidityDeltaFromAmountA = getLiquidityDeltaFromAmountA(
|
|
6839
|
-
tokenAAmount,
|
|
6840
|
-
sqrtPriceQ64,
|
|
6841
|
-
MAX_SQRT_PRICE
|
|
6842
|
-
);
|
|
6843
|
-
const liquidityDeltaFromAmountB = getLiquidityDeltaFromAmountB(
|
|
6844
|
-
tokenBAmount,
|
|
6845
|
-
MIN_SQRT_PRICE,
|
|
6846
|
-
sqrtPriceQ64
|
|
6847
|
-
);
|
|
6848
|
-
const liquidityQ64 = liquidityDeltaFromAmountA.gte(
|
|
6849
|
-
liquidityDeltaFromAmountB
|
|
6850
|
-
) ? liquidityDeltaFromAmountB : liquidityDeltaFromAmountA;
|
|
6851
|
-
return {
|
|
6852
|
-
sqrtPriceQ64,
|
|
6853
|
-
liquidityQ64
|
|
6854
|
-
};
|
|
6855
|
-
});
|
|
6856
|
-
}
|
|
6857
|
-
// fetcher
|
|
6858
|
-
fetchConfigState(config) {
|
|
6859
|
-
return __async(this, null, function* () {
|
|
6860
|
-
const configState = yield this._program.account.config.fetchNullable(
|
|
6861
|
-
config
|
|
6862
|
-
);
|
|
6863
|
-
invariant(configState, `Config account: ${config} not found`);
|
|
6864
|
-
return configState;
|
|
6865
|
-
});
|
|
6866
|
-
}
|
|
6867
|
-
fetchPoolState(pool) {
|
|
6868
|
-
return __async(this, null, function* () {
|
|
6869
|
-
const poolState = yield this._program.account.pool.fetchNullable(pool);
|
|
6870
|
-
invariant(poolState, `Pool account: ${pool} not found`);
|
|
6871
|
-
return poolState;
|
|
6872
|
-
});
|
|
6873
|
-
}
|
|
6874
|
-
fetchPositionState(position) {
|
|
6875
|
-
return __async(this, null, function* () {
|
|
6876
|
-
const positionState = yield this._program.account.position.fetchNullable(
|
|
6877
|
-
position
|
|
6878
|
-
);
|
|
6879
|
-
invariant(positionState, `Position account: ${position} not found`);
|
|
6880
|
-
return positionState;
|
|
6881
|
-
});
|
|
6882
|
-
}
|
|
6883
|
-
getAllConfigs() {
|
|
7282
|
+
* Prepares token accounts for a transaction by retrieving or creating associated token accounts.
|
|
7283
|
+
* @private
|
|
7284
|
+
* @param {PublicKey} owner - The owner of the token accounts
|
|
7285
|
+
* @param {PublicKey} tokenAMint - Mint address of token A
|
|
7286
|
+
* @param {PublicKey} tokenBMint - Mint address of token B
|
|
7287
|
+
* @param {PublicKey} tokenAProgram - Program ID for token A (Token or Token2022)
|
|
7288
|
+
* @param {PublicKey} tokenBProgram - Program ID for token B (Token or Token2022)
|
|
7289
|
+
* @returns {Promise<{tokenAAta: PublicKey, tokenBAta: PublicKey, instructions: TransactionInstruction[]}>}
|
|
7290
|
+
* The token account addresses and any instructions needed to create them
|
|
7291
|
+
*/
|
|
7292
|
+
prepareTokenAccounts(owner, tokenAMint, tokenBMint, tokenAProgram, tokenBProgram) {
|
|
6884
7293
|
return __async(this, null, function* () {
|
|
6885
|
-
const
|
|
6886
|
-
|
|
7294
|
+
const instructions = [];
|
|
7295
|
+
const [
|
|
7296
|
+
{ ataPubkey: tokenAAta, ix: createInputTokenAccountIx },
|
|
7297
|
+
{ ataPubkey: tokenBAta, ix: createOutputTokenAccountIx }
|
|
7298
|
+
] = yield Promise.all([
|
|
7299
|
+
getOrCreateATAInstruction(
|
|
7300
|
+
this._program.provider.connection,
|
|
7301
|
+
tokenAMint,
|
|
7302
|
+
owner,
|
|
7303
|
+
owner,
|
|
7304
|
+
true,
|
|
7305
|
+
tokenAProgram
|
|
7306
|
+
),
|
|
7307
|
+
getOrCreateATAInstruction(
|
|
7308
|
+
this._program.provider.connection,
|
|
7309
|
+
tokenBMint,
|
|
7310
|
+
owner,
|
|
7311
|
+
owner,
|
|
7312
|
+
true,
|
|
7313
|
+
tokenBProgram
|
|
7314
|
+
)
|
|
7315
|
+
]);
|
|
7316
|
+
createInputTokenAccountIx && instructions.push(createInputTokenAccountIx);
|
|
7317
|
+
createOutputTokenAccountIx && instructions.push(createOutputTokenAccountIx);
|
|
7318
|
+
return { tokenAAta, tokenBAta, instructions };
|
|
6887
7319
|
});
|
|
6888
7320
|
}
|
|
6889
|
-
|
|
6890
|
-
|
|
6891
|
-
|
|
6892
|
-
|
|
6893
|
-
|
|
7321
|
+
/**
|
|
7322
|
+
* Derives token badge account metadata
|
|
7323
|
+
* @param tokenAMint - Public key of token A mint
|
|
7324
|
+
* @param tokenBMint - Public key of token B mint
|
|
7325
|
+
* @returns Array of account metadata for token badges
|
|
7326
|
+
*/
|
|
7327
|
+
getTokenBadgeAccounts(tokenAMint, tokenBMint) {
|
|
7328
|
+
return [
|
|
7329
|
+
{
|
|
7330
|
+
pubkey: deriveTokenBadgeAddress(tokenAMint),
|
|
7331
|
+
isWritable: false,
|
|
7332
|
+
isSigner: false
|
|
7333
|
+
},
|
|
7334
|
+
{
|
|
7335
|
+
pubkey: deriveTokenBadgeAddress(tokenBMint),
|
|
7336
|
+
isWritable: false,
|
|
7337
|
+
isSigner: false
|
|
7338
|
+
}
|
|
7339
|
+
];
|
|
6894
7340
|
}
|
|
6895
|
-
|
|
7341
|
+
/**
|
|
7342
|
+
* Builds an instruction to add liquidity to a position.
|
|
7343
|
+
* @private
|
|
7344
|
+
* @param {BuildAddLiquidityParams} params - Parameters for adding liquidity
|
|
7345
|
+
* @returns {Promise<TransactionInstruction>} Instruction to add liquidity
|
|
7346
|
+
*/
|
|
7347
|
+
buildAddLiquidityInstruction(params) {
|
|
6896
7348
|
return __async(this, null, function* () {
|
|
6897
|
-
const
|
|
6898
|
-
|
|
7349
|
+
const {
|
|
7350
|
+
pool,
|
|
7351
|
+
position,
|
|
7352
|
+
positionNftAccount,
|
|
7353
|
+
owner,
|
|
7354
|
+
tokenAAccount,
|
|
7355
|
+
tokenBAccount,
|
|
7356
|
+
tokenAMint,
|
|
7357
|
+
tokenBMint,
|
|
7358
|
+
tokenAVault,
|
|
7359
|
+
tokenBVault,
|
|
7360
|
+
tokenAProgram,
|
|
7361
|
+
tokenBProgram,
|
|
7362
|
+
liquidityDelta,
|
|
7363
|
+
tokenAAmountThreshold,
|
|
7364
|
+
tokenBAmountThreshold
|
|
7365
|
+
} = params;
|
|
7366
|
+
return yield this._program.methods.addLiquidity({
|
|
7367
|
+
liquidityDelta,
|
|
7368
|
+
tokenAAmountThreshold,
|
|
7369
|
+
tokenBAmountThreshold
|
|
7370
|
+
}).accountsPartial({
|
|
7371
|
+
pool,
|
|
7372
|
+
position,
|
|
7373
|
+
positionNftAccount,
|
|
7374
|
+
owner,
|
|
7375
|
+
tokenAAccount,
|
|
7376
|
+
tokenBAccount,
|
|
7377
|
+
tokenAMint,
|
|
7378
|
+
tokenBMint,
|
|
7379
|
+
tokenAVault,
|
|
7380
|
+
tokenBVault,
|
|
7381
|
+
tokenAProgram,
|
|
7382
|
+
tokenBProgram
|
|
7383
|
+
}).instruction();
|
|
6899
7384
|
});
|
|
6900
7385
|
}
|
|
6901
|
-
|
|
7386
|
+
/**
|
|
7387
|
+
* Builds an instruction to remove all liquidity from a position.
|
|
7388
|
+
* @private
|
|
7389
|
+
* @param {BuildRemoveAllLiquidityInstructionParams} params - Parameters for removing all liquidity
|
|
7390
|
+
* @returns {Promise<TransactionInstruction>} Instruction to remove all liquidity
|
|
7391
|
+
*/
|
|
7392
|
+
buildRemoveAllLiquidityInstruction(params) {
|
|
6902
7393
|
return __async(this, null, function* () {
|
|
6903
|
-
const
|
|
6904
|
-
|
|
6905
|
-
|
|
6906
|
-
|
|
6907
|
-
|
|
6908
|
-
|
|
6909
|
-
|
|
6910
|
-
|
|
6911
|
-
|
|
6912
|
-
|
|
6913
|
-
|
|
6914
|
-
|
|
6915
|
-
|
|
6916
|
-
|
|
6917
|
-
|
|
6918
|
-
|
|
7394
|
+
const {
|
|
7395
|
+
poolAuthority,
|
|
7396
|
+
owner,
|
|
7397
|
+
pool,
|
|
7398
|
+
position,
|
|
7399
|
+
positionNftAccount,
|
|
7400
|
+
tokenAAccount,
|
|
7401
|
+
tokenBAccount,
|
|
7402
|
+
tokenAAmountThreshold,
|
|
7403
|
+
tokenBAmountThreshold,
|
|
7404
|
+
tokenAMint,
|
|
7405
|
+
tokenBMint,
|
|
7406
|
+
tokenAVault,
|
|
7407
|
+
tokenBVault,
|
|
7408
|
+
tokenAProgram,
|
|
7409
|
+
tokenBProgram
|
|
7410
|
+
} = params;
|
|
7411
|
+
return yield this._program.methods.removeAllLiquidity(tokenAAmountThreshold, tokenBAmountThreshold).accountsPartial({
|
|
7412
|
+
poolAuthority,
|
|
7413
|
+
pool,
|
|
7414
|
+
position,
|
|
7415
|
+
positionNftAccount,
|
|
7416
|
+
owner,
|
|
7417
|
+
tokenAAccount,
|
|
7418
|
+
tokenBAccount,
|
|
7419
|
+
tokenAMint,
|
|
7420
|
+
tokenBMint,
|
|
7421
|
+
tokenAVault,
|
|
7422
|
+
tokenBVault,
|
|
7423
|
+
tokenAProgram,
|
|
7424
|
+
tokenBProgram
|
|
7425
|
+
}).instruction();
|
|
6919
7426
|
});
|
|
6920
7427
|
}
|
|
6921
|
-
|
|
7428
|
+
/**
|
|
7429
|
+
* Builds an instruction to claim fees accumulated by a position.
|
|
7430
|
+
* @private
|
|
7431
|
+
* @param {ClaimPositionFeeInstructionParams} params - Parameters for claiming position fees
|
|
7432
|
+
* @returns {Promise<TransactionInstruction>} Instruction to claim position fees
|
|
7433
|
+
*/
|
|
7434
|
+
buildClaimPositionFeeInstruction(params) {
|
|
6922
7435
|
return __async(this, null, function* () {
|
|
6923
|
-
var _a;
|
|
6924
|
-
const { inAmount, inputTokenMint, slippage, poolState } = params;
|
|
6925
7436
|
const {
|
|
6926
|
-
|
|
6927
|
-
|
|
6928
|
-
|
|
6929
|
-
|
|
6930
|
-
|
|
6931
|
-
|
|
6932
|
-
|
|
7437
|
+
owner,
|
|
7438
|
+
poolAuthority,
|
|
7439
|
+
pool,
|
|
7440
|
+
position,
|
|
7441
|
+
positionNftAccount,
|
|
7442
|
+
tokenAAccount,
|
|
7443
|
+
tokenBAccount,
|
|
7444
|
+
tokenAVault,
|
|
7445
|
+
tokenBVault,
|
|
7446
|
+
tokenAMint,
|
|
7447
|
+
tokenBMint,
|
|
7448
|
+
tokenAProgram,
|
|
7449
|
+
tokenBProgram
|
|
7450
|
+
} = params;
|
|
7451
|
+
return yield this._program.methods.claimPositionFee().accountsPartial({
|
|
7452
|
+
poolAuthority,
|
|
7453
|
+
owner,
|
|
7454
|
+
pool,
|
|
7455
|
+
position,
|
|
7456
|
+
positionNftAccount,
|
|
7457
|
+
tokenAAccount,
|
|
7458
|
+
tokenBAccount,
|
|
7459
|
+
tokenAVault,
|
|
7460
|
+
tokenBVault,
|
|
7461
|
+
tokenAMint,
|
|
7462
|
+
tokenBMint,
|
|
7463
|
+
tokenAProgram,
|
|
7464
|
+
tokenBProgram
|
|
7465
|
+
}).instruction();
|
|
7466
|
+
});
|
|
7467
|
+
}
|
|
7468
|
+
/**
|
|
7469
|
+
* Builds an instruction to close a position.
|
|
7470
|
+
* @private
|
|
7471
|
+
* @param {ClosePositionInstructionParams} params - Parameters for closing a position
|
|
7472
|
+
* @returns {Promise<TransactionInstruction>} Instruction to close the position
|
|
7473
|
+
*/
|
|
7474
|
+
buildClosePositionInstruction(params) {
|
|
7475
|
+
return __async(this, null, function* () {
|
|
6933
7476
|
const {
|
|
6934
|
-
|
|
6935
|
-
|
|
6936
|
-
|
|
6937
|
-
|
|
6938
|
-
|
|
6939
|
-
|
|
6940
|
-
|
|
6941
|
-
|
|
6942
|
-
|
|
6943
|
-
|
|
6944
|
-
|
|
6945
|
-
|
|
6946
|
-
|
|
6947
|
-
|
|
6948
|
-
|
|
6949
|
-
|
|
6950
|
-
|
|
6951
|
-
|
|
7477
|
+
owner,
|
|
7478
|
+
poolAuthority,
|
|
7479
|
+
pool,
|
|
7480
|
+
position,
|
|
7481
|
+
positionNftAccount,
|
|
7482
|
+
positionNftMint
|
|
7483
|
+
} = params;
|
|
7484
|
+
return yield this._program.methods.closePosition().accountsPartial({
|
|
7485
|
+
positionNftMint,
|
|
7486
|
+
positionNftAccount,
|
|
7487
|
+
pool,
|
|
7488
|
+
position,
|
|
7489
|
+
poolAuthority,
|
|
7490
|
+
rentReceiver: owner,
|
|
7491
|
+
owner,
|
|
7492
|
+
tokenProgram: TOKEN_2022_PROGRAM_ID2
|
|
7493
|
+
}).instruction();
|
|
7494
|
+
});
|
|
7495
|
+
}
|
|
7496
|
+
/**
|
|
7497
|
+
* Builds an instruction to refresh vesting for a position
|
|
7498
|
+
* @param params Parameters required for the refresh vesting instruction
|
|
7499
|
+
* @returns Transaction instruction or null if no vestings to refresh
|
|
7500
|
+
*/
|
|
7501
|
+
buildRefreshVestingInstruction(params) {
|
|
7502
|
+
return __async(this, null, function* () {
|
|
7503
|
+
const { owner, position, positionNftAccount, pool, vestingAccounts } = params;
|
|
7504
|
+
if (vestingAccounts.length == 0) {
|
|
7505
|
+
return null;
|
|
6952
7506
|
}
|
|
6953
|
-
|
|
6954
|
-
|
|
6955
|
-
|
|
6956
|
-
|
|
6957
|
-
|
|
6958
|
-
|
|
6959
|
-
|
|
6960
|
-
|
|
6961
|
-
|
|
7507
|
+
return yield this._program.methods.refreshVesting().accountsPartial({
|
|
7508
|
+
position,
|
|
7509
|
+
positionNftAccount,
|
|
7510
|
+
pool,
|
|
7511
|
+
owner
|
|
7512
|
+
}).remainingAccounts(
|
|
7513
|
+
vestingAccounts.map((pubkey) => {
|
|
7514
|
+
return {
|
|
7515
|
+
isSigner: false,
|
|
7516
|
+
isWritable: true,
|
|
7517
|
+
pubkey
|
|
7518
|
+
};
|
|
7519
|
+
})
|
|
7520
|
+
).instruction();
|
|
7521
|
+
});
|
|
7522
|
+
}
|
|
7523
|
+
/**
|
|
7524
|
+
* Helper function that builds instructions to claim fees, remove liquidity, and close a position
|
|
7525
|
+
* @param {BuildLiquidatePositionInstructionParams} params - Parameters for liquidating a position
|
|
7526
|
+
* @returns {Promise<TransactionInstruction[]>} Array of instructions
|
|
7527
|
+
* @private
|
|
7528
|
+
*/
|
|
7529
|
+
buildLiquidatePositionInstruction(params) {
|
|
7530
|
+
return __async(this, null, function* () {
|
|
7531
|
+
const {
|
|
7532
|
+
owner,
|
|
7533
|
+
position,
|
|
7534
|
+
positionNftAccount,
|
|
7535
|
+
positionState,
|
|
7536
|
+
poolState,
|
|
7537
|
+
tokenAAccount,
|
|
7538
|
+
tokenBAccount,
|
|
7539
|
+
tokenAAmountThreshold,
|
|
7540
|
+
tokenBAmountThreshold
|
|
7541
|
+
} = params;
|
|
7542
|
+
const { nftMint: positionNftMint, pool } = positionState;
|
|
7543
|
+
const { tokenAMint, tokenBMint, tokenAVault, tokenBVault } = poolState;
|
|
7544
|
+
const tokenAProgram = getTokenProgram(poolState.tokenAFlag);
|
|
7545
|
+
const tokenBProgram = getTokenProgram(poolState.tokenBFlag);
|
|
7546
|
+
const instructions = [];
|
|
7547
|
+
const claimPositionFeeInstruction = yield this.buildClaimPositionFeeInstruction({
|
|
7548
|
+
owner,
|
|
7549
|
+
poolAuthority: this.poolAuthority,
|
|
7550
|
+
pool,
|
|
7551
|
+
position,
|
|
7552
|
+
positionNftAccount,
|
|
7553
|
+
tokenAAccount,
|
|
7554
|
+
tokenBAccount,
|
|
7555
|
+
tokenAVault,
|
|
7556
|
+
tokenBVault,
|
|
7557
|
+
tokenAMint,
|
|
7558
|
+
tokenBMint,
|
|
7559
|
+
tokenAProgram,
|
|
7560
|
+
tokenBProgram
|
|
7561
|
+
});
|
|
7562
|
+
instructions.push(claimPositionFeeInstruction);
|
|
7563
|
+
const removeAllLiquidityInstruction = yield this.buildRemoveAllLiquidityInstruction({
|
|
7564
|
+
poolAuthority: this.poolAuthority,
|
|
7565
|
+
owner,
|
|
7566
|
+
pool,
|
|
7567
|
+
position,
|
|
7568
|
+
positionNftAccount,
|
|
7569
|
+
tokenAAccount,
|
|
7570
|
+
tokenBAccount,
|
|
7571
|
+
tokenAAmountThreshold,
|
|
7572
|
+
tokenBAmountThreshold,
|
|
7573
|
+
tokenAMint,
|
|
7574
|
+
tokenBMint,
|
|
7575
|
+
tokenAVault,
|
|
7576
|
+
tokenBVault,
|
|
7577
|
+
tokenAProgram,
|
|
7578
|
+
tokenBProgram
|
|
7579
|
+
});
|
|
7580
|
+
instructions.push(removeAllLiquidityInstruction);
|
|
7581
|
+
const closePositionInstruction = yield this.buildClosePositionInstruction({
|
|
7582
|
+
owner,
|
|
7583
|
+
poolAuthority: this.poolAuthority,
|
|
7584
|
+
pool,
|
|
7585
|
+
position,
|
|
7586
|
+
positionNftMint,
|
|
7587
|
+
positionNftAccount
|
|
7588
|
+
});
|
|
7589
|
+
instructions.push(closePositionInstruction);
|
|
7590
|
+
return instructions;
|
|
7591
|
+
});
|
|
7592
|
+
}
|
|
7593
|
+
/**
|
|
7594
|
+
* Builds a instruction to create a position.
|
|
7595
|
+
* @param {CreatePositionParams} params - Parameters for position creation.
|
|
7596
|
+
* @returns Transaction instruction.
|
|
7597
|
+
*/
|
|
7598
|
+
buildCreatePositionInstruction(params) {
|
|
7599
|
+
return __async(this, null, function* () {
|
|
7600
|
+
const { owner, payer, pool, positionNft } = params;
|
|
7601
|
+
const position = derivePositionAddress(positionNft);
|
|
7602
|
+
const positionNftAccount = derivePositionNftAccount(positionNft);
|
|
7603
|
+
const ix = yield this._program.methods.createPosition().accountsPartial({
|
|
7604
|
+
owner,
|
|
7605
|
+
positionNftMint: positionNft,
|
|
7606
|
+
poolAuthority: this.poolAuthority,
|
|
7607
|
+
positionNftAccount,
|
|
7608
|
+
payer,
|
|
7609
|
+
pool,
|
|
7610
|
+
position,
|
|
7611
|
+
tokenProgram: TOKEN_2022_PROGRAM_ID2,
|
|
7612
|
+
systemProgram: SystemProgram2.programId
|
|
7613
|
+
}).instruction();
|
|
7614
|
+
return {
|
|
7615
|
+
ix,
|
|
7616
|
+
position,
|
|
7617
|
+
positionNftAccount
|
|
7618
|
+
};
|
|
7619
|
+
});
|
|
7620
|
+
}
|
|
7621
|
+
/**
|
|
7622
|
+
* Fetches the Config state of the program.
|
|
7623
|
+
* @param config - Public key of the config account.
|
|
7624
|
+
* @returns Parsed ConfigState.
|
|
7625
|
+
*/
|
|
7626
|
+
fetchConfigState(config) {
|
|
7627
|
+
return __async(this, null, function* () {
|
|
7628
|
+
const configState = yield this._program.account.config.fetchNullable(
|
|
7629
|
+
config
|
|
6962
7630
|
);
|
|
6963
|
-
|
|
6964
|
-
|
|
6965
|
-
|
|
6966
|
-
|
|
6967
|
-
|
|
6968
|
-
|
|
6969
|
-
|
|
7631
|
+
invariant(configState, `Config account: ${config} not found`);
|
|
7632
|
+
return configState;
|
|
7633
|
+
});
|
|
7634
|
+
}
|
|
7635
|
+
/**
|
|
7636
|
+
* Fetches the Pool state.
|
|
7637
|
+
* @param pool - Public key of the pool.
|
|
7638
|
+
* @returns Parsed PoolState.
|
|
7639
|
+
*/
|
|
7640
|
+
fetchPoolState(pool) {
|
|
7641
|
+
return __async(this, null, function* () {
|
|
7642
|
+
const poolState = yield this._program.account.pool.fetchNullable(pool);
|
|
7643
|
+
invariant(poolState, `Pool account: ${pool} not found`);
|
|
7644
|
+
return poolState;
|
|
7645
|
+
});
|
|
7646
|
+
}
|
|
7647
|
+
/**
|
|
7648
|
+
* Fetches the Position state.
|
|
7649
|
+
* @param position - Public key of the position.
|
|
7650
|
+
* @returns Parsed PositionState.
|
|
7651
|
+
*/
|
|
7652
|
+
fetchPositionState(position) {
|
|
7653
|
+
return __async(this, null, function* () {
|
|
7654
|
+
const positionState = yield this._program.account.position.fetchNullable(
|
|
7655
|
+
position
|
|
7656
|
+
);
|
|
7657
|
+
invariant(positionState, `Position account: ${position} not found`);
|
|
7658
|
+
return positionState;
|
|
7659
|
+
});
|
|
7660
|
+
}
|
|
7661
|
+
/**
|
|
7662
|
+
* Retrieves all config accounts.
|
|
7663
|
+
* @returns Array of config public keys and their states.
|
|
7664
|
+
*/
|
|
7665
|
+
getAllConfigs() {
|
|
7666
|
+
return __async(this, null, function* () {
|
|
7667
|
+
const configAccounts = yield this._program.account.config.all();
|
|
7668
|
+
return configAccounts;
|
|
7669
|
+
});
|
|
7670
|
+
}
|
|
7671
|
+
/**
|
|
7672
|
+
* Retrieves all pool accounts.
|
|
7673
|
+
* @returns Array of pool public keys and their states.
|
|
7674
|
+
*/
|
|
7675
|
+
getAllPools() {
|
|
7676
|
+
return __async(this, null, function* () {
|
|
7677
|
+
const poolAccounts = yield this._program.account.pool.all();
|
|
7678
|
+
return poolAccounts;
|
|
7679
|
+
});
|
|
7680
|
+
}
|
|
7681
|
+
/**
|
|
7682
|
+
* Retrieves all position accounts.
|
|
7683
|
+
* @returns Array of position public keys and their states.
|
|
7684
|
+
*/
|
|
7685
|
+
getAllPositions() {
|
|
7686
|
+
return __async(this, null, function* () {
|
|
7687
|
+
const poolAccounts = yield this._program.account.position.all();
|
|
7688
|
+
return poolAccounts;
|
|
7689
|
+
});
|
|
7690
|
+
}
|
|
7691
|
+
/**
|
|
7692
|
+
* Gets all positions a specific pool.
|
|
7693
|
+
* @param pool - Public key of the pool.
|
|
7694
|
+
* @returns List of user positions for the pool.
|
|
7695
|
+
*/
|
|
7696
|
+
getAllPositionsByPool(pool) {
|
|
7697
|
+
return __async(this, null, function* () {
|
|
7698
|
+
return yield this._program.account.position.all([
|
|
7699
|
+
positionByPoolFilter(pool)
|
|
7700
|
+
]);
|
|
7701
|
+
});
|
|
7702
|
+
}
|
|
7703
|
+
/**
|
|
7704
|
+
* Gets all positions of a user for a specific pool.
|
|
7705
|
+
* @param pool - Public key of the pool.
|
|
7706
|
+
* @param user - Public key of the user.
|
|
7707
|
+
* @returns List of user positions for the pool.
|
|
7708
|
+
*/
|
|
7709
|
+
getUserPositionByPool(pool, user) {
|
|
7710
|
+
return __async(this, null, function* () {
|
|
7711
|
+
const allUserPositions = yield this.getPositionsByUser(user);
|
|
7712
|
+
return allUserPositions.filter(
|
|
7713
|
+
(position) => position.positionState.pool.equals(pool)
|
|
7714
|
+
);
|
|
7715
|
+
});
|
|
7716
|
+
}
|
|
7717
|
+
/**
|
|
7718
|
+
* Gets all positions of a user across all pools.
|
|
7719
|
+
* @param user - Public key of the user.
|
|
7720
|
+
* @returns Array of user positions already sorted by liquidity
|
|
7721
|
+
*/
|
|
7722
|
+
getPositionsByUser(user) {
|
|
7723
|
+
return __async(this, null, function* () {
|
|
7724
|
+
const userPositionAccounts = yield getAllPositionNftAccountByOwner(
|
|
7725
|
+
this._program.provider.connection,
|
|
7726
|
+
user
|
|
6970
7727
|
);
|
|
6971
|
-
|
|
6972
|
-
|
|
6973
|
-
|
|
7728
|
+
if (userPositionAccounts.length === 0) {
|
|
7729
|
+
return [];
|
|
7730
|
+
}
|
|
7731
|
+
const positionAddresses = userPositionAccounts.map(
|
|
7732
|
+
(account) => derivePositionAddress(account.positionNft)
|
|
6974
7733
|
);
|
|
6975
|
-
|
|
6976
|
-
|
|
6977
|
-
|
|
6978
|
-
|
|
6979
|
-
|
|
6980
|
-
|
|
6981
|
-
|
|
7734
|
+
const positionStates = yield this._program.account.position.fetchMultiple(
|
|
7735
|
+
positionAddresses
|
|
7736
|
+
);
|
|
7737
|
+
const positionResult = userPositionAccounts.map((account, index) => {
|
|
7738
|
+
const positionState = positionStates[index];
|
|
7739
|
+
if (!positionState) return null;
|
|
7740
|
+
return {
|
|
7741
|
+
positionNftAccount: account.positionNftAccount,
|
|
7742
|
+
position: positionAddresses[index],
|
|
7743
|
+
positionState
|
|
7744
|
+
};
|
|
7745
|
+
}).filter(Boolean);
|
|
7746
|
+
positionResult.sort((a, b) => {
|
|
7747
|
+
const totalLiquidityA = a.positionState.vestedLiquidity.add(a.positionState.permanentLockedLiquidity).add(a.positionState.unlockedLiquidity);
|
|
7748
|
+
const totalLiquidityB = b.positionState.vestedLiquidity.add(b.positionState.permanentLockedLiquidity).add(b.positionState.unlockedLiquidity);
|
|
7749
|
+
return totalLiquidityB.cmp(totalLiquidityA);
|
|
7750
|
+
});
|
|
7751
|
+
return positionResult;
|
|
7752
|
+
});
|
|
7753
|
+
}
|
|
7754
|
+
getAllVestingsByPosition(position) {
|
|
7755
|
+
return __async(this, null, function* () {
|
|
7756
|
+
const vestings = yield this._program.account.vesting.all([
|
|
7757
|
+
vestingByPositionFilter(position)
|
|
7758
|
+
]);
|
|
7759
|
+
return vestings;
|
|
6982
7760
|
});
|
|
6983
7761
|
}
|
|
7762
|
+
isLockedPosition(position) {
|
|
7763
|
+
const totalLockedLiquidity = position.vestedLiquidity.add(
|
|
7764
|
+
position.permanentLockedLiquidity
|
|
7765
|
+
);
|
|
7766
|
+
return totalLockedLiquidity.gtn(0);
|
|
7767
|
+
}
|
|
7768
|
+
isPermanentLockedPosition(positionState) {
|
|
7769
|
+
return positionState.permanentLockedLiquidity.gtn(0);
|
|
7770
|
+
}
|
|
6984
7771
|
/**
|
|
6985
|
-
*
|
|
7772
|
+
* Checks if a position can be unlocked based on its locking state and vesting schedules.
|
|
7773
|
+
*
|
|
7774
|
+
* This method evaluates whether a position is eligible for operations that require
|
|
7775
|
+
* unlocked liquidity, such as removing all liquidity or closing the position. It checks both
|
|
7776
|
+
* permanent locks and time-based vesting schedules.
|
|
6986
7777
|
*
|
|
6987
|
-
* @
|
|
6988
|
-
*
|
|
6989
|
-
*
|
|
6990
|
-
*
|
|
6991
|
-
*
|
|
6992
|
-
*
|
|
7778
|
+
* @private
|
|
7779
|
+
* @param {PositionState} positionState - The current state of the position
|
|
7780
|
+
* @param {Array<{account: PublicKey; vestingState: VestingState}>} vestings - Array of vesting accounts and their states
|
|
7781
|
+
* @param {BN} currentPoint - Current timestamp or slot number (depending on activation type of pool)
|
|
7782
|
+
*
|
|
7783
|
+
* @returns {Object} Result object containing unlock status and reason
|
|
7784
|
+
* @returns {boolean} result.canUnlock - Whether the position can be unlocked
|
|
7785
|
+
* @returns {string|undefined} result.reason - Reason why position cannot be unlocked (if applicable)
|
|
7786
|
+
*/
|
|
7787
|
+
canUnlockPosition(positionState, vestings, currentPoint) {
|
|
7788
|
+
if (vestings.length > 0) {
|
|
7789
|
+
if (this.isPermanentLockedPosition(positionState)) {
|
|
7790
|
+
return {
|
|
7791
|
+
canUnlock: false,
|
|
7792
|
+
reason: "Position is permanently locked"
|
|
7793
|
+
};
|
|
7794
|
+
}
|
|
7795
|
+
for (const vesting of vestings) {
|
|
7796
|
+
if (!isVestingComplete(vesting.vestingState, currentPoint)) {
|
|
7797
|
+
return {
|
|
7798
|
+
canUnlock: false,
|
|
7799
|
+
reason: "Position has incomplete vesting schedule"
|
|
7800
|
+
};
|
|
7801
|
+
}
|
|
7802
|
+
}
|
|
7803
|
+
}
|
|
7804
|
+
return { canUnlock: true };
|
|
7805
|
+
}
|
|
7806
|
+
isPoolExist(pool) {
|
|
7807
|
+
return __async(this, null, function* () {
|
|
7808
|
+
const poolState = yield this._program.account.pool.fetchNullable(pool);
|
|
7809
|
+
return poolState !== null;
|
|
7810
|
+
});
|
|
7811
|
+
}
|
|
7812
|
+
/**
|
|
7813
|
+
* Computes the liquidity delta based on the provided token amounts and sqrt price
|
|
6993
7814
|
*
|
|
7815
|
+
* @param {LiquidityDeltaParams} params - The parameters for liquidity calculation
|
|
6994
7816
|
* @returns {Promise<BN>} - The computed liquidity delta in Q64 value.
|
|
6995
7817
|
*/
|
|
6996
7818
|
getLiquidityDelta(params) {
|
|
6997
|
-
|
|
6998
|
-
|
|
6999
|
-
|
|
7000
|
-
|
|
7001
|
-
|
|
7002
|
-
|
|
7003
|
-
|
|
7004
|
-
|
|
7005
|
-
|
|
7006
|
-
|
|
7819
|
+
const {
|
|
7820
|
+
maxAmountTokenA,
|
|
7821
|
+
maxAmountTokenB,
|
|
7822
|
+
sqrtMaxPrice,
|
|
7823
|
+
sqrtMinPrice,
|
|
7824
|
+
sqrtPrice
|
|
7825
|
+
} = params;
|
|
7826
|
+
const liquidityDeltaFromAmountA = getLiquidityDeltaFromAmountA(
|
|
7827
|
+
maxAmountTokenA,
|
|
7828
|
+
sqrtPrice,
|
|
7829
|
+
sqrtMaxPrice
|
|
7830
|
+
);
|
|
7831
|
+
const liquidityDeltaFromAmountB = getLiquidityDeltaFromAmountB(
|
|
7832
|
+
maxAmountTokenB,
|
|
7833
|
+
sqrtMinPrice,
|
|
7834
|
+
sqrtPrice
|
|
7835
|
+
);
|
|
7836
|
+
return min2(liquidityDeltaFromAmountA, liquidityDeltaFromAmountB);
|
|
7837
|
+
}
|
|
7838
|
+
/**
|
|
7839
|
+
* Calculates swap quote based on input amount and pool state.
|
|
7840
|
+
* @param params - Swap parameters including input amount, pool state, slippage, etc.
|
|
7841
|
+
* @returns Swap quote including expected output amount, fee, and price impact.
|
|
7842
|
+
*/
|
|
7843
|
+
getQuote(params) {
|
|
7844
|
+
const {
|
|
7845
|
+
inAmount,
|
|
7846
|
+
inputTokenMint,
|
|
7847
|
+
slippage,
|
|
7848
|
+
poolState,
|
|
7849
|
+
currentTime,
|
|
7850
|
+
currentSlot,
|
|
7851
|
+
inputTokenInfo,
|
|
7852
|
+
outputTokenInfo
|
|
7853
|
+
} = params;
|
|
7854
|
+
const {
|
|
7855
|
+
sqrtPrice: sqrtPriceQ64,
|
|
7856
|
+
liquidity: liquidityQ64,
|
|
7857
|
+
activationType,
|
|
7858
|
+
activationPoint,
|
|
7859
|
+
collectFeeMode,
|
|
7860
|
+
poolFees
|
|
7861
|
+
} = poolState;
|
|
7862
|
+
const {
|
|
7863
|
+
feeSchedulerMode,
|
|
7864
|
+
cliffFeeNumerator,
|
|
7865
|
+
numberOfPeriod,
|
|
7866
|
+
reductionFactor,
|
|
7867
|
+
periodFrequency
|
|
7868
|
+
} = poolFees.baseFee;
|
|
7869
|
+
const dynamicFee = poolFees.dynamicFee;
|
|
7870
|
+
let actualAmountIn = inAmount;
|
|
7871
|
+
if (inputTokenInfo) {
|
|
7872
|
+
actualAmountIn = calculateTransferFeeExcludedAmount(
|
|
7873
|
+
inAmount,
|
|
7874
|
+
inputTokenInfo.mint,
|
|
7875
|
+
inputTokenInfo.currentEpoch
|
|
7876
|
+
).amount;
|
|
7877
|
+
}
|
|
7878
|
+
const aToB = poolState.tokenAMint.equals(inputTokenMint);
|
|
7879
|
+
const currentPoint = activationType ? currentTime : currentSlot;
|
|
7880
|
+
let dynamicFeeParams;
|
|
7881
|
+
if (dynamicFee.initialized) {
|
|
7882
|
+
const { volatilityAccumulator, binStep, variableFeeControl } = dynamicFee;
|
|
7883
|
+
dynamicFeeParams = { volatilityAccumulator, binStep, variableFeeControl };
|
|
7884
|
+
}
|
|
7885
|
+
const tradeFeeNumerator = getFeeNumerator(
|
|
7886
|
+
currentPoint,
|
|
7887
|
+
activationPoint,
|
|
7888
|
+
numberOfPeriod,
|
|
7889
|
+
periodFrequency,
|
|
7890
|
+
feeSchedulerMode,
|
|
7891
|
+
cliffFeeNumerator,
|
|
7892
|
+
reductionFactor,
|
|
7893
|
+
dynamicFeeParams
|
|
7894
|
+
);
|
|
7895
|
+
const { amountOut, totalFee } = getSwapAmount(
|
|
7896
|
+
actualAmountIn,
|
|
7897
|
+
sqrtPriceQ64,
|
|
7898
|
+
liquidityQ64,
|
|
7899
|
+
tradeFeeNumerator,
|
|
7900
|
+
aToB,
|
|
7901
|
+
collectFeeMode
|
|
7902
|
+
);
|
|
7903
|
+
let actualAmountOut = amountOut;
|
|
7904
|
+
if (outputTokenInfo) {
|
|
7905
|
+
actualAmountOut = calculateTransferFeeExcludedAmount(
|
|
7906
|
+
amountOut,
|
|
7907
|
+
outputTokenInfo.mint,
|
|
7908
|
+
outputTokenInfo.currentEpoch
|
|
7909
|
+
).amount;
|
|
7910
|
+
}
|
|
7911
|
+
const minSwapOutAmount = getMinAmountWithSlippage(
|
|
7912
|
+
actualAmountOut,
|
|
7913
|
+
slippage
|
|
7914
|
+
);
|
|
7915
|
+
return {
|
|
7916
|
+
swapInAmount: inAmount,
|
|
7917
|
+
consumedInAmount: actualAmountIn,
|
|
7918
|
+
swapOutAmount: actualAmountOut,
|
|
7919
|
+
minSwapOutAmount,
|
|
7920
|
+
totalFee,
|
|
7921
|
+
priceImpact: getPriceImpact(minSwapOutAmount, actualAmountOut)
|
|
7922
|
+
};
|
|
7923
|
+
}
|
|
7924
|
+
/**
|
|
7925
|
+
* Calculates the deposit quote for liquidity pool.
|
|
7926
|
+
*
|
|
7927
|
+
* @param {GetDepositQuoteParams} params - The parameters for calculating the deposit quote.
|
|
7928
|
+
*
|
|
7929
|
+
* @returns {Promise<Object>} Deposit quote results
|
|
7930
|
+
* @returns {BN} returns.actualInputAmount - The actual amount used as input (after deducting transfer fees).
|
|
7931
|
+
* @returns {BN} returns.outputAmount - The calculated corresponding amount of the other token.
|
|
7932
|
+
* @returns {BN} returns.liquidityDelta - The amount of liquidity that will be added to the pool.
|
|
7933
|
+
*/
|
|
7934
|
+
getDepositQuote(params) {
|
|
7935
|
+
const {
|
|
7936
|
+
inAmount,
|
|
7937
|
+
isTokenA,
|
|
7938
|
+
inputTokenInfo,
|
|
7939
|
+
outputTokenInfo,
|
|
7940
|
+
minSqrtPrice,
|
|
7941
|
+
maxSqrtPrice,
|
|
7942
|
+
sqrtPrice
|
|
7943
|
+
} = params;
|
|
7944
|
+
const actualAmountIn = inputTokenInfo ? inAmount.sub(
|
|
7945
|
+
calculateTransferFeeIncludedAmount(
|
|
7946
|
+
inAmount,
|
|
7947
|
+
inputTokenInfo.mint,
|
|
7948
|
+
inputTokenInfo.currentEpoch
|
|
7949
|
+
).transferFee
|
|
7950
|
+
) : inAmount;
|
|
7951
|
+
const { liquidityDelta, rawAmount } = isTokenA ? {
|
|
7952
|
+
liquidityDelta: getLiquidityDeltaFromAmountA(
|
|
7953
|
+
actualAmountIn,
|
|
7007
7954
|
sqrtPrice,
|
|
7008
|
-
|
|
7009
|
-
)
|
|
7010
|
-
|
|
7011
|
-
|
|
7012
|
-
|
|
7955
|
+
maxSqrtPrice
|
|
7956
|
+
),
|
|
7957
|
+
rawAmount: (delta) => getAmountBFromLiquidityDelta(
|
|
7958
|
+
delta,
|
|
7959
|
+
sqrtPrice,
|
|
7960
|
+
minSqrtPrice,
|
|
7961
|
+
0 /* Up */
|
|
7962
|
+
)
|
|
7963
|
+
} : {
|
|
7964
|
+
liquidityDelta: getLiquidityDeltaFromAmountB(
|
|
7965
|
+
actualAmountIn,
|
|
7966
|
+
minSqrtPrice,
|
|
7013
7967
|
sqrtPrice
|
|
7014
|
-
)
|
|
7015
|
-
|
|
7016
|
-
|
|
7968
|
+
),
|
|
7969
|
+
rawAmount: (delta) => getAmountAFromLiquidityDelta(
|
|
7970
|
+
delta,
|
|
7971
|
+
sqrtPrice,
|
|
7972
|
+
maxSqrtPrice,
|
|
7973
|
+
0 /* Up */
|
|
7974
|
+
)
|
|
7975
|
+
};
|
|
7976
|
+
const rawOutputAmount = new BN10(rawAmount(liquidityDelta));
|
|
7977
|
+
const outputAmount = outputTokenInfo ? calculateTransferFeeIncludedAmount(
|
|
7978
|
+
rawOutputAmount,
|
|
7979
|
+
outputTokenInfo.mint,
|
|
7980
|
+
outputTokenInfo.currentEpoch
|
|
7981
|
+
).amount : rawOutputAmount;
|
|
7982
|
+
return {
|
|
7983
|
+
actualInputAmount: actualAmountIn,
|
|
7984
|
+
consumedInputAmount: inAmount,
|
|
7985
|
+
liquidityDelta,
|
|
7986
|
+
outputAmount
|
|
7987
|
+
};
|
|
7988
|
+
}
|
|
7989
|
+
/**
|
|
7990
|
+
* Calculates the withdrawal quote for removing liquidity from a concentrated liquidity pool.
|
|
7991
|
+
*
|
|
7992
|
+
* @param {GetWithdrawQuoteParams} params - The parameters for calculating the withdraw quote
|
|
7993
|
+
*
|
|
7994
|
+
* @param {Object|null} params.tokenATokenInfo - must provide if token a is token2022
|
|
7995
|
+
* @param {Object|null} params.tokenBTokenInfo - must provide if token b is token2022
|
|
7996
|
+
*
|
|
7997
|
+
* @returns {Promise<Object>} Withdrawal quote results
|
|
7998
|
+
* @returns {BN} returns.liquidityDelta - The amount of liquidity that will be removed from the pool
|
|
7999
|
+
* @returns {BN} returns.outAmountA - The calculated amount of token A to be received (after deducting transfer fees)
|
|
8000
|
+
* @returns {BN} returns.outAmountB - The calculated amount of token B to be received (after deducting transfer fees)
|
|
8001
|
+
*/
|
|
8002
|
+
getWithdrawQuote(params) {
|
|
8003
|
+
const {
|
|
8004
|
+
liquidityDelta,
|
|
8005
|
+
sqrtPrice,
|
|
8006
|
+
maxSqrtPrice,
|
|
8007
|
+
minSqrtPrice,
|
|
8008
|
+
tokenATokenInfo,
|
|
8009
|
+
tokenBTokenInfo
|
|
8010
|
+
} = params;
|
|
8011
|
+
const amountA = getAmountAFromLiquidityDelta(
|
|
8012
|
+
liquidityDelta,
|
|
8013
|
+
sqrtPrice,
|
|
8014
|
+
maxSqrtPrice,
|
|
8015
|
+
1 /* Down */
|
|
8016
|
+
);
|
|
8017
|
+
const amountB = getAmountBFromLiquidityDelta(
|
|
8018
|
+
liquidityDelta,
|
|
8019
|
+
sqrtPrice,
|
|
8020
|
+
minSqrtPrice,
|
|
8021
|
+
1 /* Down */
|
|
8022
|
+
);
|
|
8023
|
+
return {
|
|
8024
|
+
liquidityDelta,
|
|
8025
|
+
outAmountA: tokenATokenInfo ? calculateTransferFeeExcludedAmount(
|
|
8026
|
+
amountA,
|
|
8027
|
+
tokenATokenInfo.mint,
|
|
8028
|
+
tokenATokenInfo.currentEpoch
|
|
8029
|
+
).amount : amountA,
|
|
8030
|
+
outAmountB: tokenBTokenInfo ? calculateTransferFeeExcludedAmount(
|
|
8031
|
+
amountB,
|
|
8032
|
+
tokenBTokenInfo.mint,
|
|
8033
|
+
tokenBTokenInfo.currentEpoch
|
|
8034
|
+
).amount : amountB
|
|
8035
|
+
};
|
|
7017
8036
|
}
|
|
8037
|
+
/**
|
|
8038
|
+
* Calculates liquidity and corresponding token amounts for token A single-sided pool creation
|
|
8039
|
+
* Only supports initialization where initial price equals min sqrt price
|
|
8040
|
+
* @param params Parameters for single-sided pool creation
|
|
8041
|
+
* @returns Calculated liquidity delta
|
|
8042
|
+
*/
|
|
8043
|
+
preparePoolCreationSingleSide(params) {
|
|
8044
|
+
const {
|
|
8045
|
+
tokenAAmount,
|
|
8046
|
+
initSqrtPrice,
|
|
8047
|
+
minSqrtPrice,
|
|
8048
|
+
maxSqrtPrice,
|
|
8049
|
+
tokenAInfo
|
|
8050
|
+
} = params;
|
|
8051
|
+
if (!initSqrtPrice.eq(minSqrtPrice)) {
|
|
8052
|
+
throw new Error("Only support single side for base token.");
|
|
8053
|
+
}
|
|
8054
|
+
const actualAmountIn = tokenAInfo ? tokenAAmount.sub(
|
|
8055
|
+
calculateTransferFeeIncludedAmount(
|
|
8056
|
+
tokenAAmount,
|
|
8057
|
+
tokenAInfo.mint,
|
|
8058
|
+
tokenAInfo.currentEpoch
|
|
8059
|
+
).transferFee
|
|
8060
|
+
) : tokenAAmount;
|
|
8061
|
+
const liquidityDelta = getLiquidityDeltaFromAmountA(
|
|
8062
|
+
actualAmountIn,
|
|
8063
|
+
initSqrtPrice,
|
|
8064
|
+
maxSqrtPrice
|
|
8065
|
+
);
|
|
8066
|
+
return liquidityDelta;
|
|
8067
|
+
}
|
|
8068
|
+
/**
|
|
8069
|
+
* Prepares parameters required for pool creation, including initial sqrt price and liquidity.
|
|
8070
|
+
* @private
|
|
8071
|
+
* @param {PreparePoolCreationParams} params - Initial token amounts for pool creation.
|
|
8072
|
+
* @returns init sqrt price and liquidity in Q64 format.
|
|
8073
|
+
*/
|
|
8074
|
+
preparePoolCreationParams(params) {
|
|
8075
|
+
const {
|
|
8076
|
+
tokenAAmount,
|
|
8077
|
+
tokenBAmount,
|
|
8078
|
+
minSqrtPrice,
|
|
8079
|
+
maxSqrtPrice,
|
|
8080
|
+
tokenAInfo,
|
|
8081
|
+
tokenBInfo
|
|
8082
|
+
} = params;
|
|
8083
|
+
if (tokenAAmount.eq(new BN10(0)) && tokenBAmount.eq(new BN10(0))) {
|
|
8084
|
+
throw new Error("Invalid input amount");
|
|
8085
|
+
}
|
|
8086
|
+
const actualAmountAIn = tokenAInfo ? tokenAAmount.sub(
|
|
8087
|
+
calculateTransferFeeIncludedAmount(
|
|
8088
|
+
tokenAAmount,
|
|
8089
|
+
tokenAInfo.mint,
|
|
8090
|
+
tokenAInfo.currentEpoch
|
|
8091
|
+
).transferFee
|
|
8092
|
+
) : tokenAAmount;
|
|
8093
|
+
const actualAmountBIn = tokenBInfo ? tokenBAmount.sub(
|
|
8094
|
+
calculateTransferFeeIncludedAmount(
|
|
8095
|
+
tokenBAmount,
|
|
8096
|
+
tokenBInfo.mint,
|
|
8097
|
+
tokenBInfo.currentEpoch
|
|
8098
|
+
).transferFee
|
|
8099
|
+
) : tokenBAmount;
|
|
8100
|
+
const initSqrtPrice = calculateInitSqrtPrice(
|
|
8101
|
+
tokenAAmount,
|
|
8102
|
+
tokenBAmount,
|
|
8103
|
+
minSqrtPrice,
|
|
8104
|
+
maxSqrtPrice
|
|
8105
|
+
);
|
|
8106
|
+
const liquidityDeltaFromAmountA = getLiquidityDeltaFromAmountA(
|
|
8107
|
+
actualAmountAIn,
|
|
8108
|
+
initSqrtPrice,
|
|
8109
|
+
maxSqrtPrice
|
|
8110
|
+
);
|
|
8111
|
+
const liquidityDeltaFromAmountB = getLiquidityDeltaFromAmountB(
|
|
8112
|
+
actualAmountBIn,
|
|
8113
|
+
minSqrtPrice,
|
|
8114
|
+
initSqrtPrice
|
|
8115
|
+
);
|
|
8116
|
+
const liquidityDelta = min2(
|
|
8117
|
+
liquidityDeltaFromAmountA,
|
|
8118
|
+
liquidityDeltaFromAmountB
|
|
8119
|
+
);
|
|
8120
|
+
return {
|
|
8121
|
+
initSqrtPrice,
|
|
8122
|
+
liquidityDelta
|
|
8123
|
+
};
|
|
8124
|
+
}
|
|
8125
|
+
/**
|
|
8126
|
+
* Builds a transaction to create a permissionless pool.
|
|
8127
|
+
* @param params - Parameters for pool creation.
|
|
8128
|
+
* @returns Transaction builder.
|
|
8129
|
+
*/
|
|
7018
8130
|
createPool(params) {
|
|
7019
8131
|
return __async(this, null, function* () {
|
|
7020
|
-
var _a, _b, _c, _d;
|
|
7021
8132
|
const {
|
|
7022
8133
|
payer,
|
|
7023
8134
|
creator,
|
|
@@ -7025,70 +8136,61 @@ var CpAmm = class {
|
|
|
7025
8136
|
positionNft,
|
|
7026
8137
|
tokenAMint,
|
|
7027
8138
|
tokenBMint,
|
|
8139
|
+
initSqrtPrice,
|
|
8140
|
+
liquidityDelta,
|
|
7028
8141
|
activationPoint,
|
|
7029
8142
|
tokenAAmount,
|
|
7030
8143
|
tokenBAmount,
|
|
7031
|
-
|
|
7032
|
-
|
|
8144
|
+
tokenAProgram,
|
|
8145
|
+
tokenBProgram
|
|
7033
8146
|
} = params;
|
|
7034
|
-
const
|
|
7035
|
-
|
|
7036
|
-
|
|
7037
|
-
|
|
7038
|
-
|
|
7039
|
-
|
|
7040
|
-
|
|
7041
|
-
|
|
7042
|
-
|
|
7043
|
-
|
|
7044
|
-
config,
|
|
7045
|
-
tokenAMint,
|
|
7046
|
-
tokenBMint,
|
|
7047
|
-
this._program.programId
|
|
7048
|
-
);
|
|
7049
|
-
const position = derivePositionAddress(
|
|
7050
|
-
positionNft,
|
|
7051
|
-
this._program.programId
|
|
7052
|
-
);
|
|
7053
|
-
const positionNftAccount = derivePositionNftAccount(
|
|
7054
|
-
positionNft,
|
|
7055
|
-
this._program.programId
|
|
7056
|
-
);
|
|
7057
|
-
const tokenAProgram = (_b = (_a = yield this._program.provider.connection.getParsedAccountInfo(tokenAMint)) == null ? void 0 : _a.value) == null ? void 0 : _b.owner;
|
|
7058
|
-
const tokenBProgram = (_d = (_c = yield this._program.provider.connection.getParsedAccountInfo(tokenBMint)) == null ? void 0 : _c.value) == null ? void 0 : _d.owner;
|
|
7059
|
-
const payerTokenA = getAssociatedTokenAddressSync2(
|
|
7060
|
-
tokenAMint,
|
|
8147
|
+
const pool = derivePoolAddress(config, tokenAMint, tokenBMint);
|
|
8148
|
+
const position = derivePositionAddress(positionNft);
|
|
8149
|
+
const positionNftAccount = derivePositionNftAccount(positionNft);
|
|
8150
|
+
const tokenAVault = deriveTokenVaultAddress(tokenAMint, pool);
|
|
8151
|
+
const tokenBVault = deriveTokenVaultAddress(tokenBMint, pool);
|
|
8152
|
+
const {
|
|
8153
|
+
tokenAAta: payerTokenA,
|
|
8154
|
+
tokenBAta: payerTokenB,
|
|
8155
|
+
instructions: preInstructions
|
|
8156
|
+
} = yield this.prepareTokenAccounts(
|
|
7061
8157
|
payer,
|
|
7062
|
-
|
|
7063
|
-
tokenAProgram
|
|
7064
|
-
);
|
|
7065
|
-
const payerTokenB = getAssociatedTokenAddressSync2(
|
|
8158
|
+
tokenAMint,
|
|
7066
8159
|
tokenBMint,
|
|
7067
|
-
|
|
7068
|
-
true,
|
|
8160
|
+
tokenAProgram,
|
|
7069
8161
|
tokenBProgram
|
|
7070
8162
|
);
|
|
7071
|
-
|
|
8163
|
+
if (tokenAMint.equals(NATIVE_MINT2)) {
|
|
8164
|
+
const wrapSOLIx = wrapSOLInstruction(
|
|
8165
|
+
payer,
|
|
8166
|
+
payerTokenA,
|
|
8167
|
+
BigInt(tokenAAmount.toString())
|
|
8168
|
+
);
|
|
8169
|
+
preInstructions.push(...wrapSOLIx);
|
|
8170
|
+
}
|
|
8171
|
+
if (tokenBMint.equals(NATIVE_MINT2)) {
|
|
8172
|
+
const wrapSOLIx = wrapSOLInstruction(
|
|
8173
|
+
payer,
|
|
8174
|
+
payerTokenB,
|
|
8175
|
+
BigInt(tokenBAmount.toString())
|
|
8176
|
+
);
|
|
8177
|
+
preInstructions.push(...wrapSOLIx);
|
|
8178
|
+
}
|
|
8179
|
+
const tokenBadgeAccounts = this.getTokenBadgeAccounts(
|
|
7072
8180
|
tokenAMint,
|
|
7073
|
-
|
|
7074
|
-
this._program.programId
|
|
7075
|
-
);
|
|
7076
|
-
const tokenBVault = deriveTokenVaultAddress(
|
|
7077
|
-
tokenBMint,
|
|
7078
|
-
pool,
|
|
7079
|
-
this._program.programId
|
|
8181
|
+
tokenBMint
|
|
7080
8182
|
);
|
|
7081
8183
|
const tx = yield this._program.methods.initializePool({
|
|
7082
|
-
liquidity:
|
|
7083
|
-
sqrtPrice:
|
|
8184
|
+
liquidity: liquidityDelta,
|
|
8185
|
+
sqrtPrice: initSqrtPrice,
|
|
7084
8186
|
activationPoint
|
|
7085
|
-
}).
|
|
8187
|
+
}).accountsPartial({
|
|
7086
8188
|
creator,
|
|
7087
8189
|
positionNftAccount,
|
|
7088
8190
|
positionNftMint: positionNft,
|
|
7089
8191
|
payer,
|
|
7090
8192
|
config,
|
|
7091
|
-
poolAuthority,
|
|
8193
|
+
poolAuthority: this.poolAuthority,
|
|
7092
8194
|
pool,
|
|
7093
8195
|
position,
|
|
7094
8196
|
tokenAMint,
|
|
@@ -7100,13 +8202,16 @@ var CpAmm = class {
|
|
|
7100
8202
|
token2022Program: TOKEN_2022_PROGRAM_ID2,
|
|
7101
8203
|
tokenAProgram,
|
|
7102
8204
|
tokenBProgram,
|
|
7103
|
-
systemProgram: SystemProgram2.programId
|
|
7104
|
-
|
|
7105
|
-
program: this._program.programId
|
|
7106
|
-
}).transaction();
|
|
8205
|
+
systemProgram: SystemProgram2.programId
|
|
8206
|
+
}).preInstructions(preInstructions).remainingAccounts(tokenBadgeAccounts).transaction();
|
|
7107
8207
|
return tx;
|
|
7108
8208
|
});
|
|
7109
8209
|
}
|
|
8210
|
+
/**
|
|
8211
|
+
* Builds a transaction to create a customizable pool.
|
|
8212
|
+
* @param params - Parameters for customizable pool creation.
|
|
8213
|
+
* @returns Transaction and related addresses.
|
|
8214
|
+
*/
|
|
7110
8215
|
createCustomPool(params) {
|
|
7111
8216
|
return __async(this, null, function* () {
|
|
7112
8217
|
const {
|
|
@@ -7114,8 +8219,10 @@ var CpAmm = class {
|
|
|
7114
8219
|
tokenBMint,
|
|
7115
8220
|
tokenAAmount,
|
|
7116
8221
|
tokenBAmount,
|
|
7117
|
-
|
|
7118
|
-
|
|
8222
|
+
sqrtMinPrice,
|
|
8223
|
+
sqrtMaxPrice,
|
|
8224
|
+
liquidityDelta,
|
|
8225
|
+
initSqrtPrice,
|
|
7119
8226
|
payer,
|
|
7120
8227
|
creator,
|
|
7121
8228
|
positionNft,
|
|
@@ -7127,62 +8234,22 @@ var CpAmm = class {
|
|
|
7127
8234
|
tokenAProgram,
|
|
7128
8235
|
tokenBProgram
|
|
7129
8236
|
} = params;
|
|
7130
|
-
const
|
|
7131
|
-
|
|
7132
|
-
|
|
7133
|
-
|
|
7134
|
-
|
|
7135
|
-
|
|
7136
|
-
|
|
7137
|
-
|
|
7138
|
-
|
|
7139
|
-
|
|
7140
|
-
|
|
7141
|
-
tokenBMint,
|
|
7142
|
-
this._program.programId
|
|
7143
|
-
);
|
|
7144
|
-
const position = derivePositionAddress(
|
|
7145
|
-
positionNft,
|
|
7146
|
-
this._program.programId
|
|
7147
|
-
);
|
|
7148
|
-
const positionNftAccount = derivePositionNftAccount(
|
|
7149
|
-
positionNft,
|
|
7150
|
-
this._program.programId
|
|
7151
|
-
);
|
|
7152
|
-
const tokenAVault = deriveTokenVaultAddress(
|
|
8237
|
+
const pool = deriveCustomizablePoolAddress(tokenAMint, tokenBMint);
|
|
8238
|
+
const position = derivePositionAddress(positionNft);
|
|
8239
|
+
const positionNftAccount = derivePositionNftAccount(positionNft);
|
|
8240
|
+
const tokenAVault = deriveTokenVaultAddress(tokenAMint, pool);
|
|
8241
|
+
const tokenBVault = deriveTokenVaultAddress(tokenBMint, pool);
|
|
8242
|
+
const {
|
|
8243
|
+
tokenAAta: payerTokenA,
|
|
8244
|
+
tokenBAta: payerTokenB,
|
|
8245
|
+
instructions: preInstructions
|
|
8246
|
+
} = yield this.prepareTokenAccounts(
|
|
8247
|
+
payer,
|
|
7153
8248
|
tokenAMint,
|
|
7154
|
-
pool,
|
|
7155
|
-
this._program.programId
|
|
7156
|
-
);
|
|
7157
|
-
const tokenBVault = deriveTokenVaultAddress(
|
|
7158
8249
|
tokenBMint,
|
|
7159
|
-
|
|
7160
|
-
|
|
8250
|
+
tokenAProgram,
|
|
8251
|
+
tokenBProgram
|
|
7161
8252
|
);
|
|
7162
|
-
const preInstructions = [];
|
|
7163
|
-
const [
|
|
7164
|
-
{ ataPubkey: payerTokenA, ix: createTokenATokenAccountIx },
|
|
7165
|
-
{ ataPubkey: payerTokenB, ix: createTokenBTokenAccountIx }
|
|
7166
|
-
] = yield Promise.all([
|
|
7167
|
-
getOrCreateATAInstruction(
|
|
7168
|
-
this._program.provider.connection,
|
|
7169
|
-
tokenAMint,
|
|
7170
|
-
payer,
|
|
7171
|
-
payer,
|
|
7172
|
-
true,
|
|
7173
|
-
tokenAProgram
|
|
7174
|
-
),
|
|
7175
|
-
getOrCreateATAInstruction(
|
|
7176
|
-
this._program.provider.connection,
|
|
7177
|
-
tokenBMint,
|
|
7178
|
-
payer,
|
|
7179
|
-
payer,
|
|
7180
|
-
true,
|
|
7181
|
-
tokenBProgram
|
|
7182
|
-
)
|
|
7183
|
-
]);
|
|
7184
|
-
createTokenATokenAccountIx && preInstructions.push(createTokenATokenAccountIx);
|
|
7185
|
-
createTokenBTokenAccountIx && preInstructions.push(createTokenBTokenAccountIx);
|
|
7186
8253
|
if (tokenAMint.equals(NATIVE_MINT2)) {
|
|
7187
8254
|
const wrapSOLIx = wrapSOLInstruction(
|
|
7188
8255
|
payer,
|
|
@@ -7192,29 +8259,34 @@ var CpAmm = class {
|
|
|
7192
8259
|
preInstructions.push(...wrapSOLIx);
|
|
7193
8260
|
}
|
|
7194
8261
|
if (tokenBMint.equals(NATIVE_MINT2)) {
|
|
8262
|
+
const lamports = tokenBAmount.eq(new BN10(0)) ? new BN10(1) : tokenBAmount;
|
|
7195
8263
|
const wrapSOLIx = wrapSOLInstruction(
|
|
7196
8264
|
payer,
|
|
7197
8265
|
payerTokenB,
|
|
7198
|
-
BigInt(
|
|
8266
|
+
BigInt(lamports.toString())
|
|
7199
8267
|
);
|
|
7200
8268
|
preInstructions.push(...wrapSOLIx);
|
|
7201
8269
|
}
|
|
8270
|
+
const tokenBadgeAccounts = this.getTokenBadgeAccounts(
|
|
8271
|
+
tokenAMint,
|
|
8272
|
+
tokenBMint
|
|
8273
|
+
);
|
|
7202
8274
|
const transaction = yield this._program.methods.initializeCustomizablePool({
|
|
7203
8275
|
poolFees,
|
|
7204
|
-
sqrtMinPrice
|
|
7205
|
-
sqrtMaxPrice
|
|
8276
|
+
sqrtMinPrice,
|
|
8277
|
+
sqrtMaxPrice,
|
|
7206
8278
|
hasAlphaVault,
|
|
7207
|
-
liquidity:
|
|
7208
|
-
sqrtPrice:
|
|
8279
|
+
liquidity: liquidityDelta,
|
|
8280
|
+
sqrtPrice: initSqrtPrice,
|
|
7209
8281
|
activationType,
|
|
7210
8282
|
collectFeeMode,
|
|
7211
8283
|
activationPoint
|
|
7212
|
-
}).
|
|
8284
|
+
}).accountsPartial({
|
|
7213
8285
|
creator,
|
|
7214
8286
|
positionNftAccount,
|
|
7215
8287
|
positionNftMint: positionNft,
|
|
7216
8288
|
payer,
|
|
7217
|
-
poolAuthority,
|
|
8289
|
+
poolAuthority: this.poolAuthority,
|
|
7218
8290
|
pool,
|
|
7219
8291
|
position,
|
|
7220
8292
|
tokenAMint,
|
|
@@ -7224,50 +8296,37 @@ var CpAmm = class {
|
|
|
7224
8296
|
payerTokenA,
|
|
7225
8297
|
payerTokenB,
|
|
7226
8298
|
token2022Program: TOKEN_2022_PROGRAM_ID2,
|
|
7227
|
-
tokenAProgram
|
|
7228
|
-
tokenBProgram
|
|
7229
|
-
systemProgram: SystemProgram2.programId
|
|
7230
|
-
|
|
7231
|
-
program: this._program.programId
|
|
7232
|
-
}).preInstructions(preInstructions).transaction();
|
|
8299
|
+
tokenAProgram,
|
|
8300
|
+
tokenBProgram,
|
|
8301
|
+
systemProgram: SystemProgram2.programId
|
|
8302
|
+
}).preInstructions(preInstructions).remainingAccounts(tokenBadgeAccounts).transaction();
|
|
7233
8303
|
return { tx: transaction, pool, position };
|
|
7234
8304
|
});
|
|
7235
8305
|
}
|
|
8306
|
+
/**
|
|
8307
|
+
* Builds a transaction to create a position.
|
|
8308
|
+
* @param {CreatePositionParams} params - Parameters for position creation.
|
|
8309
|
+
* @returns Transaction builder.
|
|
8310
|
+
*/
|
|
7236
8311
|
createPosition(params) {
|
|
7237
8312
|
return __async(this, null, function* () {
|
|
7238
|
-
const {
|
|
7239
|
-
|
|
7240
|
-
const position = derivePositionAddress(
|
|
7241
|
-
positionNft,
|
|
7242
|
-
this._program.programId
|
|
7243
|
-
);
|
|
7244
|
-
const positionNftAccount = derivePositionNftAccount(
|
|
7245
|
-
positionNft,
|
|
7246
|
-
this._program.programId
|
|
7247
|
-
);
|
|
7248
|
-
return yield this._program.methods.createPosition().accountsStrict({
|
|
7249
|
-
owner,
|
|
7250
|
-
positionNftMint: positionNft,
|
|
7251
|
-
poolAuthority,
|
|
7252
|
-
positionNftAccount,
|
|
7253
|
-
payer,
|
|
7254
|
-
pool,
|
|
7255
|
-
position,
|
|
7256
|
-
tokenProgram: TOKEN_2022_PROGRAM_ID2,
|
|
7257
|
-
systemProgram: SystemProgram2.programId,
|
|
7258
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7259
|
-
program: this._program.programId
|
|
7260
|
-
}).transaction();
|
|
8313
|
+
const { ix } = yield this.buildCreatePositionInstruction(params);
|
|
8314
|
+
return new Transaction().add(ix);
|
|
7261
8315
|
});
|
|
7262
8316
|
}
|
|
8317
|
+
/**
|
|
8318
|
+
* Builds a transaction to add liquidity to an existing position.
|
|
8319
|
+
* @param {AddLiquidityParams} params - Parameters for adding liquidity.
|
|
8320
|
+
* @returns Transaction builder.
|
|
8321
|
+
*/
|
|
7263
8322
|
addLiquidity(params) {
|
|
7264
8323
|
return __async(this, null, function* () {
|
|
7265
8324
|
const {
|
|
7266
8325
|
owner,
|
|
7267
8326
|
pool,
|
|
7268
8327
|
position,
|
|
7269
|
-
|
|
7270
|
-
|
|
8328
|
+
positionNftAccount,
|
|
8329
|
+
liquidityDelta,
|
|
7271
8330
|
maxAmountTokenA,
|
|
7272
8331
|
maxAmountTokenB,
|
|
7273
8332
|
tokenAAmountThreshold,
|
|
@@ -7279,34 +8338,17 @@ var CpAmm = class {
|
|
|
7279
8338
|
tokenAProgram,
|
|
7280
8339
|
tokenBProgram
|
|
7281
8340
|
} = params;
|
|
7282
|
-
const
|
|
7283
|
-
|
|
7284
|
-
|
|
8341
|
+
const {
|
|
8342
|
+
tokenAAta: tokenAAccount,
|
|
8343
|
+
tokenBAta: tokenBAccount,
|
|
8344
|
+
instructions: preInstructions
|
|
8345
|
+
} = yield this.prepareTokenAccounts(
|
|
8346
|
+
owner,
|
|
8347
|
+
tokenAMint,
|
|
8348
|
+
tokenBMint,
|
|
8349
|
+
tokenAProgram,
|
|
8350
|
+
tokenBProgram
|
|
7285
8351
|
);
|
|
7286
|
-
const preInstructions = [];
|
|
7287
|
-
const [
|
|
7288
|
-
{ ataPubkey: tokenAAccount, ix: createTokenATokenAccountIx },
|
|
7289
|
-
{ ataPubkey: tokenBAccount, ix: createTokenBTokenAccountIx }
|
|
7290
|
-
] = yield Promise.all([
|
|
7291
|
-
getOrCreateATAInstruction(
|
|
7292
|
-
this._program.provider.connection,
|
|
7293
|
-
tokenAMint,
|
|
7294
|
-
owner,
|
|
7295
|
-
owner,
|
|
7296
|
-
true,
|
|
7297
|
-
tokenAProgram
|
|
7298
|
-
),
|
|
7299
|
-
getOrCreateATAInstruction(
|
|
7300
|
-
this._program.provider.connection,
|
|
7301
|
-
tokenBMint,
|
|
7302
|
-
owner,
|
|
7303
|
-
owner,
|
|
7304
|
-
true,
|
|
7305
|
-
tokenBProgram
|
|
7306
|
-
)
|
|
7307
|
-
]);
|
|
7308
|
-
createTokenATokenAccountIx && preInstructions.push(createTokenATokenAccountIx);
|
|
7309
|
-
createTokenBTokenAccountIx && preInstructions.push(createTokenBTokenAccountIx);
|
|
7310
8352
|
if (tokenAMint.equals(NATIVE_MINT2)) {
|
|
7311
8353
|
const wrapSOLIx = wrapSOLInstruction(
|
|
7312
8354
|
owner,
|
|
@@ -7330,11 +8372,104 @@ var CpAmm = class {
|
|
|
7330
8372
|
const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
|
|
7331
8373
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
7332
8374
|
}
|
|
7333
|
-
|
|
7334
|
-
|
|
8375
|
+
const addLiquidityInstruction = yield this.buildAddLiquidityInstruction({
|
|
8376
|
+
pool,
|
|
8377
|
+
position,
|
|
8378
|
+
positionNftAccount,
|
|
8379
|
+
owner,
|
|
8380
|
+
tokenAAccount,
|
|
8381
|
+
tokenBAccount,
|
|
8382
|
+
tokenAMint,
|
|
8383
|
+
tokenBMint,
|
|
8384
|
+
tokenAVault,
|
|
8385
|
+
tokenBVault,
|
|
8386
|
+
tokenAProgram,
|
|
8387
|
+
tokenBProgram,
|
|
8388
|
+
liquidityDelta,
|
|
7335
8389
|
tokenAAmountThreshold,
|
|
7336
8390
|
tokenBAmountThreshold
|
|
7337
|
-
})
|
|
8391
|
+
});
|
|
8392
|
+
const transaction = new Transaction();
|
|
8393
|
+
transaction.add(
|
|
8394
|
+
...preInstructions.length > 0 ? preInstructions : [],
|
|
8395
|
+
addLiquidityInstruction,
|
|
8396
|
+
...postInstructions.length > 0 ? postInstructions : []
|
|
8397
|
+
);
|
|
8398
|
+
return transaction;
|
|
8399
|
+
});
|
|
8400
|
+
}
|
|
8401
|
+
/**
|
|
8402
|
+
* Creates a new position and add liquidity to position it in a single transaction.
|
|
8403
|
+
* Handles both native SOL and other tokens, automatically wrapping/unwrapping SOL as needed.
|
|
8404
|
+
*
|
|
8405
|
+
* @param {CreatePositionAndAddLiquidity} params - Parameters for creating position and adding liquidity
|
|
8406
|
+
*
|
|
8407
|
+
* @returns {Transaction} A transaction that creates a position and adds liquidity
|
|
8408
|
+
*
|
|
8409
|
+
**/
|
|
8410
|
+
createPositionAndAddLiquidity(params) {
|
|
8411
|
+
return __async(this, null, function* () {
|
|
8412
|
+
const {
|
|
8413
|
+
owner,
|
|
8414
|
+
pool,
|
|
8415
|
+
positionNft,
|
|
8416
|
+
liquidityDelta,
|
|
8417
|
+
maxAmountTokenA,
|
|
8418
|
+
maxAmountTokenB,
|
|
8419
|
+
tokenAAmountThreshold,
|
|
8420
|
+
tokenBAmountThreshold,
|
|
8421
|
+
tokenAMint,
|
|
8422
|
+
tokenBMint,
|
|
8423
|
+
tokenAVault,
|
|
8424
|
+
tokenBVault,
|
|
8425
|
+
tokenAProgram,
|
|
8426
|
+
tokenBProgram
|
|
8427
|
+
} = params;
|
|
8428
|
+
const {
|
|
8429
|
+
tokenAAta: tokenAAccount,
|
|
8430
|
+
tokenBAta: tokenBAccount,
|
|
8431
|
+
instructions: preInstructions
|
|
8432
|
+
} = yield this.prepareTokenAccounts(
|
|
8433
|
+
owner,
|
|
8434
|
+
tokenAMint,
|
|
8435
|
+
tokenBMint,
|
|
8436
|
+
tokenAProgram,
|
|
8437
|
+
tokenBProgram
|
|
8438
|
+
);
|
|
8439
|
+
if (tokenAMint.equals(NATIVE_MINT2)) {
|
|
8440
|
+
const wrapSOLIx = wrapSOLInstruction(
|
|
8441
|
+
owner,
|
|
8442
|
+
tokenAAccount,
|
|
8443
|
+
BigInt(maxAmountTokenA.toString())
|
|
8444
|
+
);
|
|
8445
|
+
preInstructions.push(...wrapSOLIx);
|
|
8446
|
+
}
|
|
8447
|
+
if (tokenBMint.equals(NATIVE_MINT2)) {
|
|
8448
|
+
const wrapSOLIx = wrapSOLInstruction(
|
|
8449
|
+
owner,
|
|
8450
|
+
tokenBAccount,
|
|
8451
|
+
BigInt(maxAmountTokenB.toString())
|
|
8452
|
+
);
|
|
8453
|
+
preInstructions.push(...wrapSOLIx);
|
|
8454
|
+
}
|
|
8455
|
+
const postInstructions = [];
|
|
8456
|
+
if ([tokenAMint.toBase58(), tokenBMint.toBase58()].includes(
|
|
8457
|
+
NATIVE_MINT2.toBase58()
|
|
8458
|
+
)) {
|
|
8459
|
+
const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
|
|
8460
|
+
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
8461
|
+
}
|
|
8462
|
+
const {
|
|
8463
|
+
ix: createPositionIx,
|
|
8464
|
+
position,
|
|
8465
|
+
positionNftAccount
|
|
8466
|
+
} = yield this.buildCreatePositionInstruction({
|
|
8467
|
+
owner,
|
|
8468
|
+
payer: owner,
|
|
8469
|
+
pool,
|
|
8470
|
+
positionNft
|
|
8471
|
+
});
|
|
8472
|
+
const addLiquidityInstruction = yield this.buildAddLiquidityInstruction({
|
|
7338
8473
|
pool,
|
|
7339
8474
|
position,
|
|
7340
8475
|
positionNftAccount,
|
|
@@ -7347,19 +8482,33 @@ var CpAmm = class {
|
|
|
7347
8482
|
tokenBVault,
|
|
7348
8483
|
tokenAProgram,
|
|
7349
8484
|
tokenBProgram,
|
|
7350
|
-
|
|
7351
|
-
|
|
7352
|
-
|
|
8485
|
+
liquidityDelta,
|
|
8486
|
+
tokenAAmountThreshold,
|
|
8487
|
+
tokenBAmountThreshold
|
|
8488
|
+
});
|
|
8489
|
+
const transaction = new Transaction();
|
|
8490
|
+
transaction.add(createPositionIx);
|
|
8491
|
+
transaction.add(
|
|
8492
|
+
...preInstructions.length > 0 ? preInstructions : [],
|
|
8493
|
+
addLiquidityInstruction,
|
|
8494
|
+
...postInstructions.length > 0 ? postInstructions : []
|
|
8495
|
+
);
|
|
8496
|
+
return transaction;
|
|
7353
8497
|
});
|
|
7354
8498
|
}
|
|
8499
|
+
/**
|
|
8500
|
+
* Builds a transaction to remove liquidity from a position.
|
|
8501
|
+
* @param {RemoveLiquidityParams} params - Parameters for removing liquidity.
|
|
8502
|
+
* @returns Transaction builder.
|
|
8503
|
+
*/
|
|
7355
8504
|
removeLiquidity(params) {
|
|
7356
8505
|
return __async(this, null, function* () {
|
|
7357
8506
|
const {
|
|
7358
8507
|
owner,
|
|
7359
8508
|
pool,
|
|
7360
8509
|
position,
|
|
7361
|
-
|
|
7362
|
-
|
|
8510
|
+
positionNftAccount,
|
|
8511
|
+
liquidityDelta,
|
|
7363
8512
|
tokenAAmountThreshold,
|
|
7364
8513
|
tokenBAmountThreshold,
|
|
7365
8514
|
tokenAMint,
|
|
@@ -7367,37 +8516,91 @@ var CpAmm = class {
|
|
|
7367
8516
|
tokenAVault,
|
|
7368
8517
|
tokenBVault,
|
|
7369
8518
|
tokenAProgram,
|
|
7370
|
-
tokenBProgram
|
|
8519
|
+
tokenBProgram,
|
|
8520
|
+
vestings
|
|
7371
8521
|
} = params;
|
|
7372
|
-
const
|
|
7373
|
-
|
|
7374
|
-
|
|
8522
|
+
const {
|
|
8523
|
+
tokenAAta: tokenAAccount,
|
|
8524
|
+
tokenBAta: tokenBAccount,
|
|
8525
|
+
instructions: preInstructions
|
|
8526
|
+
} = yield this.prepareTokenAccounts(
|
|
8527
|
+
owner,
|
|
8528
|
+
tokenAMint,
|
|
8529
|
+
tokenBMint,
|
|
8530
|
+
tokenAProgram,
|
|
8531
|
+
tokenBProgram
|
|
7375
8532
|
);
|
|
7376
|
-
const
|
|
7377
|
-
|
|
7378
|
-
|
|
7379
|
-
|
|
7380
|
-
|
|
7381
|
-
|
|
7382
|
-
|
|
7383
|
-
|
|
7384
|
-
|
|
7385
|
-
owner,
|
|
7386
|
-
owner,
|
|
7387
|
-
true,
|
|
7388
|
-
tokenAProgram
|
|
7389
|
-
),
|
|
7390
|
-
getOrCreateATAInstruction(
|
|
7391
|
-
this._program.provider.connection,
|
|
7392
|
-
tokenBMint,
|
|
7393
|
-
owner,
|
|
8533
|
+
const postInstructions = [];
|
|
8534
|
+
if ([tokenAMint.toBase58(), tokenBMint.toBase58()].includes(
|
|
8535
|
+
NATIVE_MINT2.toBase58()
|
|
8536
|
+
)) {
|
|
8537
|
+
const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
|
|
8538
|
+
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
8539
|
+
}
|
|
8540
|
+
if (vestings.length > 0) {
|
|
8541
|
+
const refreshVestingInstruction = yield this.buildRefreshVestingInstruction({
|
|
7394
8542
|
owner,
|
|
7395
|
-
|
|
7396
|
-
|
|
7397
|
-
|
|
7398
|
-
|
|
7399
|
-
|
|
7400
|
-
|
|
8543
|
+
position,
|
|
8544
|
+
positionNftAccount,
|
|
8545
|
+
pool,
|
|
8546
|
+
vestingAccounts: vestings.map((item) => item.account)
|
|
8547
|
+
});
|
|
8548
|
+
refreshVestingInstruction && preInstructions.push(refreshVestingInstruction);
|
|
8549
|
+
}
|
|
8550
|
+
return yield this._program.methods.removeLiquidity({
|
|
8551
|
+
liquidityDelta,
|
|
8552
|
+
tokenAAmountThreshold,
|
|
8553
|
+
tokenBAmountThreshold
|
|
8554
|
+
}).accountsPartial({
|
|
8555
|
+
poolAuthority: this.poolAuthority,
|
|
8556
|
+
pool,
|
|
8557
|
+
position,
|
|
8558
|
+
positionNftAccount,
|
|
8559
|
+
owner,
|
|
8560
|
+
tokenAAccount,
|
|
8561
|
+
tokenBAccount,
|
|
8562
|
+
tokenAMint,
|
|
8563
|
+
tokenBMint,
|
|
8564
|
+
tokenAVault,
|
|
8565
|
+
tokenBVault,
|
|
8566
|
+
tokenAProgram,
|
|
8567
|
+
tokenBProgram
|
|
8568
|
+
}).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
|
|
8569
|
+
});
|
|
8570
|
+
}
|
|
8571
|
+
/**
|
|
8572
|
+
* Builds a transaction to remove liquidity from a position.
|
|
8573
|
+
* @param {RemoveLiquidityParams} params - Parameters for removing liquidity.
|
|
8574
|
+
* @returns Transaction builder.
|
|
8575
|
+
*/
|
|
8576
|
+
removeAllLiquidity(params) {
|
|
8577
|
+
return __async(this, null, function* () {
|
|
8578
|
+
const {
|
|
8579
|
+
owner,
|
|
8580
|
+
pool,
|
|
8581
|
+
position,
|
|
8582
|
+
positionNftAccount,
|
|
8583
|
+
tokenAAmountThreshold,
|
|
8584
|
+
tokenBAmountThreshold,
|
|
8585
|
+
tokenAMint,
|
|
8586
|
+
tokenBMint,
|
|
8587
|
+
tokenAVault,
|
|
8588
|
+
tokenBVault,
|
|
8589
|
+
tokenAProgram,
|
|
8590
|
+
tokenBProgram,
|
|
8591
|
+
vestings
|
|
8592
|
+
} = params;
|
|
8593
|
+
const {
|
|
8594
|
+
tokenAAta: tokenAAccount,
|
|
8595
|
+
tokenBAta: tokenBAccount,
|
|
8596
|
+
instructions: preInstructions
|
|
8597
|
+
} = yield this.prepareTokenAccounts(
|
|
8598
|
+
owner,
|
|
8599
|
+
tokenAMint,
|
|
8600
|
+
tokenBMint,
|
|
8601
|
+
tokenAProgram,
|
|
8602
|
+
tokenBProgram
|
|
8603
|
+
);
|
|
7401
8604
|
const postInstructions = [];
|
|
7402
8605
|
if ([tokenAMint.toBase58(), tokenBMint.toBase58()].includes(
|
|
7403
8606
|
NATIVE_MINT2.toBase58()
|
|
@@ -7405,29 +8608,47 @@ var CpAmm = class {
|
|
|
7405
8608
|
const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
|
|
7406
8609
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
7407
8610
|
}
|
|
7408
|
-
|
|
7409
|
-
|
|
7410
|
-
|
|
7411
|
-
|
|
7412
|
-
|
|
7413
|
-
|
|
8611
|
+
if (vestings.length > 0) {
|
|
8612
|
+
const refreshVestingInstruction = yield this.buildRefreshVestingInstruction({
|
|
8613
|
+
owner,
|
|
8614
|
+
position,
|
|
8615
|
+
positionNftAccount,
|
|
8616
|
+
pool,
|
|
8617
|
+
vestingAccounts: vestings.map((item) => item.account)
|
|
8618
|
+
});
|
|
8619
|
+
refreshVestingInstruction && preInstructions.push(refreshVestingInstruction);
|
|
8620
|
+
}
|
|
8621
|
+
const removeAllLiquidityInstruction = yield this.buildRemoveAllLiquidityInstruction({
|
|
8622
|
+
poolAuthority: this.poolAuthority,
|
|
8623
|
+
owner,
|
|
7414
8624
|
pool,
|
|
7415
8625
|
position,
|
|
7416
8626
|
positionNftAccount,
|
|
7417
|
-
owner,
|
|
7418
8627
|
tokenAAccount,
|
|
7419
8628
|
tokenBAccount,
|
|
8629
|
+
tokenAAmountThreshold,
|
|
8630
|
+
tokenBAmountThreshold,
|
|
7420
8631
|
tokenAMint,
|
|
7421
8632
|
tokenBMint,
|
|
7422
8633
|
tokenAVault,
|
|
7423
8634
|
tokenBVault,
|
|
7424
8635
|
tokenAProgram,
|
|
7425
|
-
tokenBProgram
|
|
7426
|
-
|
|
7427
|
-
|
|
7428
|
-
|
|
8636
|
+
tokenBProgram
|
|
8637
|
+
});
|
|
8638
|
+
const transaction = new Transaction();
|
|
8639
|
+
transaction.add(
|
|
8640
|
+
...preInstructions.length > 0 ? preInstructions : [],
|
|
8641
|
+
removeAllLiquidityInstruction,
|
|
8642
|
+
...postInstructions.length > 0 ? postInstructions : []
|
|
8643
|
+
);
|
|
8644
|
+
return transaction;
|
|
7429
8645
|
});
|
|
7430
8646
|
}
|
|
8647
|
+
/**
|
|
8648
|
+
* Builds a transaction to perform a swap in the pool.
|
|
8649
|
+
* @param {SwapParams} params - Parameters for swapping tokens.
|
|
8650
|
+
* @returns Transaction builder.
|
|
8651
|
+
*/
|
|
7431
8652
|
swap(params) {
|
|
7432
8653
|
return __async(this, null, function* () {
|
|
7433
8654
|
const {
|
|
@@ -7445,31 +8666,20 @@ var CpAmm = class {
|
|
|
7445
8666
|
tokenBProgram,
|
|
7446
8667
|
referralTokenAccount
|
|
7447
8668
|
} = params;
|
|
7448
|
-
const
|
|
7449
|
-
|
|
7450
|
-
|
|
7451
|
-
|
|
7452
|
-
|
|
7453
|
-
|
|
7454
|
-
|
|
7455
|
-
|
|
7456
|
-
|
|
7457
|
-
|
|
7458
|
-
|
|
7459
|
-
|
|
7460
|
-
|
|
7461
|
-
|
|
7462
|
-
getOrCreateATAInstruction(
|
|
7463
|
-
this._program.provider.connection,
|
|
7464
|
-
outputTokenMint,
|
|
7465
|
-
payer,
|
|
7466
|
-
payer,
|
|
7467
|
-
true,
|
|
7468
|
-
tokenBProgram
|
|
7469
|
-
)
|
|
7470
|
-
]);
|
|
7471
|
-
createInputTokenAccountIx && preInstructions.push(createInputTokenAccountIx);
|
|
7472
|
-
createOutputTokenAccountIx && preInstructions.push(createOutputTokenAccountIx);
|
|
8669
|
+
const [inputTokenProgram, outputTokenProgram] = inputTokenMint.equals(
|
|
8670
|
+
tokenAMint
|
|
8671
|
+
) ? [tokenAProgram, tokenBProgram] : [tokenBProgram, tokenAProgram];
|
|
8672
|
+
const {
|
|
8673
|
+
tokenAAta: inputTokenAccount,
|
|
8674
|
+
tokenBAta: outputTokenAccount,
|
|
8675
|
+
instructions: preInstructions
|
|
8676
|
+
} = yield this.prepareTokenAccounts(
|
|
8677
|
+
payer,
|
|
8678
|
+
inputTokenMint,
|
|
8679
|
+
outputTokenMint,
|
|
8680
|
+
inputTokenProgram,
|
|
8681
|
+
outputTokenProgram
|
|
8682
|
+
);
|
|
7473
8683
|
if (inputTokenMint.equals(NATIVE_MINT2)) {
|
|
7474
8684
|
const wrapSOLIx = wrapSOLInstruction(
|
|
7475
8685
|
payer,
|
|
@@ -7488,8 +8698,8 @@ var CpAmm = class {
|
|
|
7488
8698
|
return yield this._program.methods.swap({
|
|
7489
8699
|
amountIn,
|
|
7490
8700
|
minimumAmountOut
|
|
7491
|
-
}).
|
|
7492
|
-
poolAuthority,
|
|
8701
|
+
}).accountsPartial({
|
|
8702
|
+
poolAuthority: this.poolAuthority,
|
|
7493
8703
|
pool,
|
|
7494
8704
|
payer,
|
|
7495
8705
|
inputTokenAccount,
|
|
@@ -7500,12 +8710,15 @@ var CpAmm = class {
|
|
|
7500
8710
|
tokenBMint,
|
|
7501
8711
|
tokenAProgram,
|
|
7502
8712
|
tokenBProgram,
|
|
7503
|
-
referralTokenAccount
|
|
7504
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7505
|
-
program: this._program.programId
|
|
8713
|
+
referralTokenAccount
|
|
7506
8714
|
}).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
|
|
7507
8715
|
});
|
|
7508
8716
|
}
|
|
8717
|
+
/**
|
|
8718
|
+
* Builds a transaction to lock a position with vesting schedule.
|
|
8719
|
+
* @param {LockPositionParams} params - Locking parameters.
|
|
8720
|
+
* @returns Transaction builder.
|
|
8721
|
+
*/
|
|
7509
8722
|
lockPosition(params) {
|
|
7510
8723
|
return __async(this, null, function* () {
|
|
7511
8724
|
const {
|
|
@@ -7514,86 +8727,70 @@ var CpAmm = class {
|
|
|
7514
8727
|
payer,
|
|
7515
8728
|
vestingAccount,
|
|
7516
8729
|
position,
|
|
7517
|
-
|
|
8730
|
+
positionNftAccount,
|
|
7518
8731
|
cliffPoint,
|
|
7519
8732
|
periodFrequency,
|
|
7520
8733
|
cliffUnlockLiquidity,
|
|
7521
8734
|
liquidityPerPeriod,
|
|
7522
|
-
numberOfPeriod
|
|
7523
|
-
vestings
|
|
8735
|
+
numberOfPeriod
|
|
7524
8736
|
} = params;
|
|
7525
|
-
const positionNftAccount = derivePositionNftAccount(
|
|
7526
|
-
positionNftMint,
|
|
7527
|
-
this._program.programId
|
|
7528
|
-
);
|
|
7529
8737
|
const lockPositionParams = {
|
|
7530
8738
|
cliffPoint,
|
|
7531
8739
|
periodFrequency,
|
|
7532
8740
|
cliffUnlockLiquidity,
|
|
7533
8741
|
liquidityPerPeriod,
|
|
7534
|
-
numberOfPeriod
|
|
7535
|
-
index: vestings.length
|
|
8742
|
+
numberOfPeriod
|
|
7536
8743
|
};
|
|
7537
|
-
return yield this._program.methods.lockPosition(lockPositionParams).
|
|
8744
|
+
return yield this._program.methods.lockPosition(lockPositionParams).accountsPartial({
|
|
7538
8745
|
position,
|
|
7539
8746
|
positionNftAccount,
|
|
7540
8747
|
vesting: vestingAccount,
|
|
7541
8748
|
pool,
|
|
7542
8749
|
owner,
|
|
7543
8750
|
payer,
|
|
7544
|
-
systemProgram: SystemProgram2.programId
|
|
7545
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7546
|
-
program: this._program.programId
|
|
8751
|
+
systemProgram: SystemProgram2.programId
|
|
7547
8752
|
}).transaction();
|
|
7548
8753
|
});
|
|
7549
8754
|
}
|
|
8755
|
+
/**
|
|
8756
|
+
* Builds a transaction to permanently lock a position.
|
|
8757
|
+
* @param {PermanentLockParams} params - Parameters for permanent locking.
|
|
8758
|
+
* @returns Transaction builder.
|
|
8759
|
+
*/
|
|
7550
8760
|
permanentLockPosition(params) {
|
|
7551
8761
|
return __async(this, null, function* () {
|
|
7552
|
-
const { owner, position,
|
|
7553
|
-
|
|
7554
|
-
positionNftMint,
|
|
7555
|
-
this._program.programId
|
|
7556
|
-
);
|
|
7557
|
-
return yield this._program.methods.permanentLockPosition(unlockedLiquidity).accountsStrict({
|
|
8762
|
+
const { owner, position, positionNftAccount, pool, unlockedLiquidity } = params;
|
|
8763
|
+
return yield this._program.methods.permanentLockPosition(unlockedLiquidity).accountsPartial({
|
|
7558
8764
|
position,
|
|
7559
8765
|
positionNftAccount,
|
|
7560
8766
|
pool,
|
|
7561
|
-
owner
|
|
7562
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7563
|
-
program: this._program.programId
|
|
8767
|
+
owner
|
|
7564
8768
|
}).transaction();
|
|
7565
8769
|
});
|
|
7566
8770
|
}
|
|
8771
|
+
/**
|
|
8772
|
+
* Builds a transaction to refresh vesting status of a position.
|
|
8773
|
+
* @param {RefreshVestingParams} params - Refresh vesting parameters.
|
|
8774
|
+
* @returns Transaction builder.
|
|
8775
|
+
*/
|
|
7567
8776
|
refreshVesting(params) {
|
|
7568
8777
|
return __async(this, null, function* () {
|
|
7569
|
-
const
|
|
7570
|
-
|
|
7571
|
-
positionNftMint,
|
|
7572
|
-
this._program.programId
|
|
7573
|
-
);
|
|
7574
|
-
return yield this._program.methods.refreshVesting().accountsStrict({
|
|
7575
|
-
position,
|
|
7576
|
-
positionNftAccount,
|
|
7577
|
-
pool,
|
|
7578
|
-
owner
|
|
7579
|
-
}).remainingAccounts(
|
|
7580
|
-
vestings.map((pubkey) => {
|
|
7581
|
-
return {
|
|
7582
|
-
isSigner: false,
|
|
7583
|
-
isWritable: true,
|
|
7584
|
-
pubkey
|
|
7585
|
-
};
|
|
7586
|
-
})
|
|
7587
|
-
).transaction();
|
|
8778
|
+
const instruction = yield this.buildRefreshVestingInstruction(params);
|
|
8779
|
+
return new Transaction().add(instruction);
|
|
7588
8780
|
});
|
|
7589
8781
|
}
|
|
8782
|
+
/**
|
|
8783
|
+
* Builds a transaction to claim position fee rewards.
|
|
8784
|
+
* @param {ClaimPositionFeeParams} params - Parameters for claiming position fee.
|
|
8785
|
+
* @returns Transaction builder.
|
|
8786
|
+
*/
|
|
7590
8787
|
claimPositionFee(params) {
|
|
7591
8788
|
return __async(this, null, function* () {
|
|
7592
8789
|
const {
|
|
7593
8790
|
owner,
|
|
7594
8791
|
pool,
|
|
7595
8792
|
position,
|
|
7596
|
-
|
|
8793
|
+
positionNftAccount,
|
|
7597
8794
|
tokenAVault,
|
|
7598
8795
|
tokenBVault,
|
|
7599
8796
|
tokenAMint,
|
|
@@ -7601,35 +8798,17 @@ var CpAmm = class {
|
|
|
7601
8798
|
tokenAProgram,
|
|
7602
8799
|
tokenBProgram
|
|
7603
8800
|
} = params;
|
|
7604
|
-
const
|
|
7605
|
-
|
|
7606
|
-
|
|
8801
|
+
const {
|
|
8802
|
+
tokenAAta: tokenAAccount,
|
|
8803
|
+
tokenBAta: tokenBAccount,
|
|
8804
|
+
instructions: preInstructions
|
|
8805
|
+
} = yield this.prepareTokenAccounts(
|
|
8806
|
+
owner,
|
|
8807
|
+
tokenAMint,
|
|
8808
|
+
tokenBMint,
|
|
8809
|
+
tokenAProgram,
|
|
8810
|
+
tokenBProgram
|
|
7607
8811
|
);
|
|
7608
|
-
const poolAuthority = derivePoolAuthority(this._program.programId);
|
|
7609
|
-
const preInstructions = [];
|
|
7610
|
-
const [
|
|
7611
|
-
{ ataPubkey: tokenAAccount, ix: createTokenAAccountIx },
|
|
7612
|
-
{ ataPubkey: tokenBAccount, ix: createTokenBAccountIx }
|
|
7613
|
-
] = yield Promise.all([
|
|
7614
|
-
getOrCreateATAInstruction(
|
|
7615
|
-
this._program.provider.connection,
|
|
7616
|
-
tokenAMint,
|
|
7617
|
-
owner,
|
|
7618
|
-
owner,
|
|
7619
|
-
true,
|
|
7620
|
-
tokenAProgram
|
|
7621
|
-
),
|
|
7622
|
-
getOrCreateATAInstruction(
|
|
7623
|
-
this._program.provider.connection,
|
|
7624
|
-
tokenBMint,
|
|
7625
|
-
owner,
|
|
7626
|
-
owner,
|
|
7627
|
-
true,
|
|
7628
|
-
tokenBProgram
|
|
7629
|
-
)
|
|
7630
|
-
]);
|
|
7631
|
-
createTokenAAccountIx && preInstructions.push(createTokenAAccountIx);
|
|
7632
|
-
createTokenBAccountIx && preInstructions.push(createTokenBAccountIx);
|
|
7633
8812
|
const postInstructions = [];
|
|
7634
8813
|
if ([tokenAMint.toBase58(), tokenBMint.toBase58()].includes(
|
|
7635
8814
|
NATIVE_MINT2.toBase58()
|
|
@@ -7637,9 +8816,9 @@ var CpAmm = class {
|
|
|
7637
8816
|
const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
|
|
7638
8817
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
7639
8818
|
}
|
|
7640
|
-
|
|
7641
|
-
poolAuthority,
|
|
8819
|
+
const claimPositionFeeInstruction = yield this.buildClaimPositionFeeInstruction({
|
|
7642
8820
|
owner,
|
|
8821
|
+
poolAuthority: this.poolAuthority,
|
|
7643
8822
|
pool,
|
|
7644
8823
|
position,
|
|
7645
8824
|
positionNftAccount,
|
|
@@ -7650,34 +8829,288 @@ var CpAmm = class {
|
|
|
7650
8829
|
tokenAMint,
|
|
7651
8830
|
tokenBMint,
|
|
7652
8831
|
tokenAProgram,
|
|
8832
|
+
tokenBProgram
|
|
8833
|
+
});
|
|
8834
|
+
const transaction = new Transaction();
|
|
8835
|
+
transaction.add(
|
|
8836
|
+
...preInstructions.length > 0 ? preInstructions : [],
|
|
8837
|
+
claimPositionFeeInstruction,
|
|
8838
|
+
...postInstructions.length > 0 ? postInstructions : []
|
|
8839
|
+
);
|
|
8840
|
+
return transaction;
|
|
8841
|
+
});
|
|
8842
|
+
}
|
|
8843
|
+
closePosition(params) {
|
|
8844
|
+
return __async(this, null, function* () {
|
|
8845
|
+
const { owner, pool, position, positionNftMint, positionNftAccount } = params;
|
|
8846
|
+
const instruction = yield this.buildClosePositionInstruction({
|
|
8847
|
+
owner,
|
|
8848
|
+
poolAuthority: this.poolAuthority,
|
|
8849
|
+
pool,
|
|
8850
|
+
position,
|
|
8851
|
+
positionNftMint,
|
|
8852
|
+
positionNftAccount
|
|
8853
|
+
});
|
|
8854
|
+
return new Transaction().add(instruction);
|
|
8855
|
+
});
|
|
8856
|
+
}
|
|
8857
|
+
/**
|
|
8858
|
+
* Builds a transaction to remove all liquidity from a position and close it.
|
|
8859
|
+
* This combines several operations in a single transaction:
|
|
8860
|
+
* 1. Claims any accumulated fees
|
|
8861
|
+
* 2. Removes all liquidity
|
|
8862
|
+
* 3. Closes the position
|
|
8863
|
+
*
|
|
8864
|
+
* @param {RemoveAllLiquidityAndClosePositionParams} params - Combined parameters
|
|
8865
|
+
* @returns {TxBuilder} Transaction builder with all required instructions
|
|
8866
|
+
* @throws {Error} If the position is locked or cannot be closed
|
|
8867
|
+
*/
|
|
8868
|
+
removeAllLiquidityAndClosePosition(params) {
|
|
8869
|
+
return __async(this, null, function* () {
|
|
8870
|
+
const {
|
|
8871
|
+
owner,
|
|
8872
|
+
position,
|
|
8873
|
+
positionNftAccount,
|
|
8874
|
+
positionState,
|
|
8875
|
+
poolState,
|
|
8876
|
+
tokenAAmountThreshold,
|
|
8877
|
+
tokenBAmountThreshold,
|
|
8878
|
+
vestings,
|
|
8879
|
+
currentPoint
|
|
8880
|
+
} = params;
|
|
8881
|
+
const { pool } = positionState;
|
|
8882
|
+
const { tokenAMint, tokenBMint } = poolState;
|
|
8883
|
+
const { canUnlock, reason } = this.canUnlockPosition(
|
|
8884
|
+
positionState,
|
|
8885
|
+
vestings,
|
|
8886
|
+
currentPoint
|
|
8887
|
+
);
|
|
8888
|
+
if (!canUnlock) {
|
|
8889
|
+
throw new Error(`Cannot remove liquidity: ${reason}`);
|
|
8890
|
+
}
|
|
8891
|
+
const tokenAProgram = getTokenProgram(poolState.tokenAFlag);
|
|
8892
|
+
const tokenBProgram = getTokenProgram(poolState.tokenBFlag);
|
|
8893
|
+
const {
|
|
8894
|
+
tokenAAta: tokenAAccount,
|
|
8895
|
+
tokenBAta: tokenBAccount,
|
|
8896
|
+
instructions: preInstructions
|
|
8897
|
+
} = yield this.prepareTokenAccounts(
|
|
8898
|
+
owner,
|
|
8899
|
+
tokenAMint,
|
|
8900
|
+
tokenBMint,
|
|
8901
|
+
tokenAProgram,
|
|
8902
|
+
tokenBProgram
|
|
8903
|
+
);
|
|
8904
|
+
const postInstructions = [];
|
|
8905
|
+
if ([tokenAMint.toBase58(), tokenBMint.toBase58()].includes(
|
|
8906
|
+
NATIVE_MINT2.toBase58()
|
|
8907
|
+
)) {
|
|
8908
|
+
const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
|
|
8909
|
+
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
8910
|
+
}
|
|
8911
|
+
if (vestings.length > 0) {
|
|
8912
|
+
const refreshVestingInstruction = yield this.buildRefreshVestingInstruction({
|
|
8913
|
+
owner,
|
|
8914
|
+
position,
|
|
8915
|
+
positionNftAccount,
|
|
8916
|
+
pool,
|
|
8917
|
+
vestingAccounts: vestings.map((item) => item.account)
|
|
8918
|
+
});
|
|
8919
|
+
refreshVestingInstruction && preInstructions.push(refreshVestingInstruction);
|
|
8920
|
+
}
|
|
8921
|
+
const transaction = new Transaction();
|
|
8922
|
+
if (preInstructions.length > 0) {
|
|
8923
|
+
transaction.add(...preInstructions);
|
|
8924
|
+
}
|
|
8925
|
+
const liquidatePositionInstructions = yield this.buildLiquidatePositionInstruction({
|
|
8926
|
+
owner,
|
|
8927
|
+
position,
|
|
8928
|
+
positionNftAccount,
|
|
8929
|
+
positionState,
|
|
8930
|
+
poolState,
|
|
8931
|
+
tokenAAccount,
|
|
8932
|
+
tokenBAccount,
|
|
8933
|
+
tokenAAmountThreshold,
|
|
8934
|
+
tokenBAmountThreshold
|
|
8935
|
+
});
|
|
8936
|
+
transaction.add(...liquidatePositionInstructions);
|
|
8937
|
+
if (postInstructions.length > 0) {
|
|
8938
|
+
transaction.add(...postInstructions);
|
|
8939
|
+
}
|
|
8940
|
+
return transaction;
|
|
8941
|
+
});
|
|
8942
|
+
}
|
|
8943
|
+
/**
|
|
8944
|
+
* Builds a transaction to merge liquidity from one position into another.
|
|
8945
|
+
* This process:
|
|
8946
|
+
* 1. Claims fees from the source position
|
|
8947
|
+
* 2. Removes all liquidity from the source position
|
|
8948
|
+
* 3. Adds that liquidity to the target position
|
|
8949
|
+
* 4. Closes the source position
|
|
8950
|
+
*
|
|
8951
|
+
* @param {MergePositionParams} params - Parameters for merging positions
|
|
8952
|
+
* @returns {TxBuilder} Transaction builder with all required instructions
|
|
8953
|
+
* @throws {Error} If either position is locked or incompatible
|
|
8954
|
+
*/
|
|
8955
|
+
mergePosition(params) {
|
|
8956
|
+
return __async(this, null, function* () {
|
|
8957
|
+
const {
|
|
8958
|
+
owner,
|
|
8959
|
+
positionA,
|
|
8960
|
+
positionB,
|
|
8961
|
+
positionBState,
|
|
8962
|
+
poolState,
|
|
8963
|
+
positionBNftAccount,
|
|
8964
|
+
positionANftAccount,
|
|
8965
|
+
tokenAAmountAddLiquidityThreshold,
|
|
8966
|
+
tokenBAmountAddLiquidityThreshold,
|
|
8967
|
+
tokenAAmountRemoveLiquidityThreshold,
|
|
8968
|
+
tokenBAmountRemoveLiquidityThreshold,
|
|
8969
|
+
positionBVestings,
|
|
8970
|
+
currentPoint
|
|
8971
|
+
} = params;
|
|
8972
|
+
const { canUnlock, reason } = this.canUnlockPosition(
|
|
8973
|
+
positionBState,
|
|
8974
|
+
positionBVestings,
|
|
8975
|
+
currentPoint
|
|
8976
|
+
);
|
|
8977
|
+
if (!canUnlock) {
|
|
8978
|
+
throw new Error(`Cannot remove liquidity: ${reason}`);
|
|
8979
|
+
}
|
|
8980
|
+
const pool = positionBState.pool;
|
|
8981
|
+
const { tokenAMint, tokenBMint, tokenAVault, tokenBVault } = poolState;
|
|
8982
|
+
const tokenAProgram = getTokenProgram(poolState.tokenAFlag);
|
|
8983
|
+
const tokenBProgram = getTokenProgram(poolState.tokenBFlag);
|
|
8984
|
+
const {
|
|
8985
|
+
tokenAAta: tokenAAccount,
|
|
8986
|
+
tokenBAta: tokenBAccount,
|
|
8987
|
+
instructions: preInstructions
|
|
8988
|
+
} = yield this.prepareTokenAccounts(
|
|
8989
|
+
owner,
|
|
8990
|
+
tokenAMint,
|
|
8991
|
+
tokenBMint,
|
|
8992
|
+
tokenAProgram,
|
|
8993
|
+
tokenBProgram
|
|
8994
|
+
);
|
|
8995
|
+
let positionBLiquidityDelta = positionBState.unlockedLiquidity;
|
|
8996
|
+
if (positionBVestings.length > 0) {
|
|
8997
|
+
const totalAvailableVestingLiquidity = positionBVestings.reduce(
|
|
8998
|
+
(total, position) => {
|
|
8999
|
+
const available = getAvailableVestingLiquidity(
|
|
9000
|
+
position.vestingState,
|
|
9001
|
+
currentPoint
|
|
9002
|
+
);
|
|
9003
|
+
return total.add(available);
|
|
9004
|
+
},
|
|
9005
|
+
new BN10(0)
|
|
9006
|
+
);
|
|
9007
|
+
positionBLiquidityDelta = positionBLiquidityDelta.add(
|
|
9008
|
+
totalAvailableVestingLiquidity
|
|
9009
|
+
);
|
|
9010
|
+
const refreshVestingInstruction = yield this.buildRefreshVestingInstruction({
|
|
9011
|
+
owner,
|
|
9012
|
+
position: positionB,
|
|
9013
|
+
positionNftAccount: positionBNftAccount,
|
|
9014
|
+
pool,
|
|
9015
|
+
vestingAccounts: positionBVestings.map((item) => item.account)
|
|
9016
|
+
});
|
|
9017
|
+
refreshVestingInstruction && preInstructions.push(refreshVestingInstruction);
|
|
9018
|
+
}
|
|
9019
|
+
const tokenAWithdrawAmount = getAmountAFromLiquidityDelta(
|
|
9020
|
+
positionBLiquidityDelta,
|
|
9021
|
+
poolState.sqrtPrice,
|
|
9022
|
+
poolState.sqrtMaxPrice,
|
|
9023
|
+
1 /* Down */
|
|
9024
|
+
);
|
|
9025
|
+
const tokenBWithdrawAmount = getAmountBFromLiquidityDelta(
|
|
9026
|
+
positionBLiquidityDelta,
|
|
9027
|
+
poolState.sqrtPrice,
|
|
9028
|
+
poolState.sqrtMinPrice,
|
|
9029
|
+
1 /* Down */
|
|
9030
|
+
);
|
|
9031
|
+
const newLiquidityDelta = this.getLiquidityDelta({
|
|
9032
|
+
maxAmountTokenA: tokenAWithdrawAmount,
|
|
9033
|
+
maxAmountTokenB: tokenBWithdrawAmount,
|
|
9034
|
+
sqrtMaxPrice: poolState.sqrtMaxPrice,
|
|
9035
|
+
sqrtMinPrice: poolState.sqrtMinPrice,
|
|
9036
|
+
sqrtPrice: poolState.sqrtPrice
|
|
9037
|
+
});
|
|
9038
|
+
const transaction = new Transaction();
|
|
9039
|
+
if (preInstructions.length > 0) {
|
|
9040
|
+
transaction.add(...preInstructions);
|
|
9041
|
+
}
|
|
9042
|
+
const liquidatePositionInstructions = yield this.buildLiquidatePositionInstruction({
|
|
9043
|
+
owner,
|
|
9044
|
+
position: positionB,
|
|
9045
|
+
positionNftAccount: positionBNftAccount,
|
|
9046
|
+
positionState: positionBState,
|
|
9047
|
+
poolState,
|
|
9048
|
+
tokenAAccount,
|
|
9049
|
+
tokenBAccount,
|
|
9050
|
+
tokenAAmountThreshold: tokenAAmountRemoveLiquidityThreshold,
|
|
9051
|
+
tokenBAmountThreshold: tokenBAmountRemoveLiquidityThreshold
|
|
9052
|
+
});
|
|
9053
|
+
transaction.add(...liquidatePositionInstructions);
|
|
9054
|
+
const addLiquidityInstruction = yield this.buildAddLiquidityInstruction({
|
|
9055
|
+
pool,
|
|
9056
|
+
position: positionA,
|
|
9057
|
+
positionNftAccount: positionANftAccount,
|
|
9058
|
+
owner,
|
|
9059
|
+
tokenAAccount,
|
|
9060
|
+
tokenBAccount,
|
|
9061
|
+
tokenAMint,
|
|
9062
|
+
tokenBMint,
|
|
9063
|
+
tokenAVault,
|
|
9064
|
+
tokenBVault,
|
|
9065
|
+
tokenAProgram,
|
|
7653
9066
|
tokenBProgram,
|
|
7654
|
-
|
|
7655
|
-
|
|
7656
|
-
|
|
9067
|
+
liquidityDelta: newLiquidityDelta,
|
|
9068
|
+
tokenAAmountThreshold: tokenAAmountAddLiquidityThreshold,
|
|
9069
|
+
tokenBAmountThreshold: tokenBAmountAddLiquidityThreshold
|
|
9070
|
+
});
|
|
9071
|
+
transaction.add(addLiquidityInstruction);
|
|
9072
|
+
if ([tokenAMint.toBase58(), tokenBMint.toBase58()].includes(
|
|
9073
|
+
NATIVE_MINT2.toBase58()
|
|
9074
|
+
)) {
|
|
9075
|
+
const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
|
|
9076
|
+
closeWrappedSOLIx && transaction.add(closeWrappedSOLIx);
|
|
9077
|
+
}
|
|
9078
|
+
return transaction;
|
|
7657
9079
|
});
|
|
7658
9080
|
}
|
|
9081
|
+
/**
|
|
9082
|
+
* Builds a transaction to update reward duration.
|
|
9083
|
+
* @param {UpdateRewardDurationParams} params - Parameters including pool and new duration.
|
|
9084
|
+
* @returns Transaction builder.
|
|
9085
|
+
*/
|
|
7659
9086
|
updateRewardDuration(params) {
|
|
7660
9087
|
return __async(this, null, function* () {
|
|
7661
9088
|
const { pool, admin, rewardIndex, newDuration } = params;
|
|
7662
|
-
return yield this._program.methods.updateRewardDuration(rewardIndex, newDuration).
|
|
9089
|
+
return yield this._program.methods.updateRewardDuration(rewardIndex, newDuration).accountsPartial({
|
|
7663
9090
|
pool,
|
|
7664
|
-
admin
|
|
7665
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7666
|
-
program: this._program.programId
|
|
9091
|
+
admin
|
|
7667
9092
|
}).transaction();
|
|
7668
9093
|
});
|
|
7669
9094
|
}
|
|
9095
|
+
/**
|
|
9096
|
+
* Builds a transaction to update reward funder address.
|
|
9097
|
+
* @param {UpdateRewardFunderParams} params - Parameters including pool and new funder address.
|
|
9098
|
+
* @returns Transaction builder.
|
|
9099
|
+
*/
|
|
7670
9100
|
updateRewardFunder(params) {
|
|
7671
9101
|
return __async(this, null, function* () {
|
|
7672
9102
|
const { pool, admin, rewardIndex, newFunder } = params;
|
|
7673
|
-
return yield this._program.methods.updateRewardFunder(rewardIndex, newFunder).
|
|
9103
|
+
return yield this._program.methods.updateRewardFunder(rewardIndex, newFunder).accountsPartial({
|
|
7674
9104
|
pool,
|
|
7675
|
-
admin
|
|
7676
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7677
|
-
program: this._program.programId
|
|
9105
|
+
admin
|
|
7678
9106
|
}).transaction();
|
|
7679
9107
|
});
|
|
7680
9108
|
}
|
|
9109
|
+
/**
|
|
9110
|
+
* Builds a transaction to fund rewards in a pool.
|
|
9111
|
+
* @param {FundRewardParams} params - Funding parameters.
|
|
9112
|
+
* @returns Transaction builder.
|
|
9113
|
+
*/
|
|
7681
9114
|
fundReward(params) {
|
|
7682
9115
|
return __async(this, null, function* () {
|
|
7683
9116
|
const { rewardIndex, carryForward, pool, funder, amount } = params;
|
|
@@ -7703,22 +9136,24 @@ var CpAmm = class {
|
|
|
7703
9136
|
);
|
|
7704
9137
|
preInstructions.push(...wrapSOLIx);
|
|
7705
9138
|
}
|
|
7706
|
-
return yield this._program.methods.fundReward(rewardIndex, amount, carryForward).
|
|
9139
|
+
return yield this._program.methods.fundReward(rewardIndex, amount, carryForward).accountsPartial({
|
|
7707
9140
|
pool,
|
|
7708
9141
|
rewardVault: vault,
|
|
7709
9142
|
rewardMint: mint,
|
|
7710
9143
|
funderTokenAccount,
|
|
7711
9144
|
funder,
|
|
7712
|
-
tokenProgram
|
|
7713
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7714
|
-
program: this._program.programId
|
|
9145
|
+
tokenProgram
|
|
7715
9146
|
}).transaction();
|
|
7716
9147
|
});
|
|
7717
9148
|
}
|
|
9149
|
+
/**
|
|
9150
|
+
* Builds a transaction to withdraw ineligible rewards from a pool.
|
|
9151
|
+
* @param {WithdrawIneligibleRewardParams} params - Parameters for withdrawal.
|
|
9152
|
+
* @returns Transaction builder.
|
|
9153
|
+
*/
|
|
7718
9154
|
withdrawIneligibleReward(params) {
|
|
7719
9155
|
return __async(this, null, function* () {
|
|
7720
9156
|
const { rewardIndex, pool, funder } = params;
|
|
7721
|
-
const poolAuthority = derivePoolAuthority(this._program.programId);
|
|
7722
9157
|
const poolState = yield this.fetchPoolState(pool);
|
|
7723
9158
|
const rewardInfo = poolState.rewardInfos[rewardIndex];
|
|
7724
9159
|
const { mint, vault, rewardTokenFlag } = rewardInfo;
|
|
@@ -7738,19 +9173,22 @@ var CpAmm = class {
|
|
|
7738
9173
|
const closeWrappedSOLIx = yield unwrapSOLInstruction(funder);
|
|
7739
9174
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
7740
9175
|
}
|
|
7741
|
-
return yield this._program.methods.withdrawIneligibleReward(rewardIndex).
|
|
9176
|
+
return yield this._program.methods.withdrawIneligibleReward(rewardIndex).accountsPartial({
|
|
7742
9177
|
pool,
|
|
7743
9178
|
rewardVault: vault,
|
|
7744
9179
|
rewardMint: mint,
|
|
7745
|
-
poolAuthority,
|
|
9180
|
+
poolAuthority: this.poolAuthority,
|
|
7746
9181
|
funderTokenAccount,
|
|
7747
9182
|
funder,
|
|
7748
|
-
tokenProgram
|
|
7749
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7750
|
-
program: this._program.programId
|
|
9183
|
+
tokenProgram
|
|
7751
9184
|
}).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
|
|
7752
9185
|
});
|
|
7753
9186
|
}
|
|
9187
|
+
/**
|
|
9188
|
+
* Builds a transaction to claim partner fee rewards.
|
|
9189
|
+
* @param {ClaimPartnerFeeParams} params - Claim parameters including amounts and partner address.
|
|
9190
|
+
* @returns Transaction builder.
|
|
9191
|
+
*/
|
|
7754
9192
|
claimPartnerFee(params) {
|
|
7755
9193
|
return __async(this, null, function* () {
|
|
7756
9194
|
const { partner, pool, maxAmountA, maxAmountB } = params;
|
|
@@ -7763,33 +9201,19 @@ var CpAmm = class {
|
|
|
7763
9201
|
tokenAFlag,
|
|
7764
9202
|
tokenBFlag
|
|
7765
9203
|
} = poolState;
|
|
7766
|
-
const poolAuthority = derivePoolAuthority(this._program.programId);
|
|
7767
9204
|
const tokenAProgram = getTokenProgram(tokenAFlag);
|
|
7768
9205
|
const tokenBProgram = getTokenProgram(tokenBFlag);
|
|
7769
|
-
const
|
|
7770
|
-
|
|
7771
|
-
|
|
7772
|
-
|
|
7773
|
-
|
|
7774
|
-
|
|
7775
|
-
|
|
7776
|
-
|
|
7777
|
-
|
|
7778
|
-
|
|
7779
|
-
|
|
7780
|
-
tokenAProgram
|
|
7781
|
-
),
|
|
7782
|
-
getOrCreateATAInstruction(
|
|
7783
|
-
this._program.provider.connection,
|
|
7784
|
-
poolState.tokenBMint,
|
|
7785
|
-
partner,
|
|
7786
|
-
partner,
|
|
7787
|
-
true,
|
|
7788
|
-
tokenBProgram
|
|
7789
|
-
)
|
|
7790
|
-
]);
|
|
7791
|
-
createTokenAAccountIx && preInstructions.push(createTokenAAccountIx);
|
|
7792
|
-
createTokenBAccountIx && preInstructions.push(createTokenBAccountIx);
|
|
9206
|
+
const {
|
|
9207
|
+
tokenAAta: tokenAAccount,
|
|
9208
|
+
tokenBAta: tokenBAccount,
|
|
9209
|
+
instructions: preInstructions
|
|
9210
|
+
} = yield this.prepareTokenAccounts(
|
|
9211
|
+
partner,
|
|
9212
|
+
tokenAMint,
|
|
9213
|
+
tokenBMint,
|
|
9214
|
+
tokenAProgram,
|
|
9215
|
+
tokenBProgram
|
|
9216
|
+
);
|
|
7793
9217
|
const postInstructions = [];
|
|
7794
9218
|
if ([
|
|
7795
9219
|
poolState.tokenAMint.toBase58(),
|
|
@@ -7798,8 +9222,8 @@ var CpAmm = class {
|
|
|
7798
9222
|
const closeWrappedSOLIx = yield unwrapSOLInstruction(partner);
|
|
7799
9223
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
7800
9224
|
}
|
|
7801
|
-
return yield this._program.methods.claimPartnerFee(maxAmountA, maxAmountB).
|
|
7802
|
-
poolAuthority,
|
|
9225
|
+
return yield this._program.methods.claimPartnerFee(maxAmountA, maxAmountB).accountsPartial({
|
|
9226
|
+
poolAuthority: this.poolAuthority,
|
|
7803
9227
|
pool,
|
|
7804
9228
|
tokenAAccount,
|
|
7805
9229
|
tokenBAccount,
|
|
@@ -7809,22 +9233,25 @@ var CpAmm = class {
|
|
|
7809
9233
|
tokenBMint,
|
|
7810
9234
|
partner,
|
|
7811
9235
|
tokenAProgram,
|
|
7812
|
-
tokenBProgram
|
|
7813
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7814
|
-
program: this._program.programId
|
|
9236
|
+
tokenBProgram
|
|
7815
9237
|
}).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
|
|
7816
9238
|
});
|
|
7817
9239
|
}
|
|
9240
|
+
/**
|
|
9241
|
+
* Builds a transaction to claim reward from a position.
|
|
9242
|
+
* @param {ClaimRewardParams} params - Parameters for claiming reward.
|
|
9243
|
+
* @returns Transaction builder.
|
|
9244
|
+
*/
|
|
7818
9245
|
claimReward(params) {
|
|
7819
9246
|
return __async(this, null, function* () {
|
|
7820
|
-
const {
|
|
7821
|
-
|
|
7822
|
-
|
|
7823
|
-
|
|
7824
|
-
|
|
7825
|
-
|
|
7826
|
-
|
|
7827
|
-
|
|
9247
|
+
const {
|
|
9248
|
+
user,
|
|
9249
|
+
position,
|
|
9250
|
+
positionNftAccount,
|
|
9251
|
+
rewardIndex,
|
|
9252
|
+
poolState,
|
|
9253
|
+
positionState
|
|
9254
|
+
} = params;
|
|
7828
9255
|
const rewardInfo = poolState.rewardInfos[rewardIndex];
|
|
7829
9256
|
const tokenProgram = getTokenProgram(rewardInfo.rewardTokenFlag);
|
|
7830
9257
|
const preInstructions = [];
|
|
@@ -7842,18 +9269,16 @@ var CpAmm = class {
|
|
|
7842
9269
|
const closeWrappedSOLIx = yield unwrapSOLInstruction(user);
|
|
7843
9270
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
7844
9271
|
}
|
|
7845
|
-
return yield this._program.methods.claimReward(rewardIndex).
|
|
9272
|
+
return yield this._program.methods.claimReward(rewardIndex).accountsPartial({
|
|
7846
9273
|
pool: positionState.pool,
|
|
7847
9274
|
positionNftAccount,
|
|
7848
9275
|
rewardVault: rewardInfo.vault,
|
|
7849
9276
|
rewardMint: rewardInfo.mint,
|
|
7850
|
-
poolAuthority,
|
|
9277
|
+
poolAuthority: this.poolAuthority,
|
|
7851
9278
|
position,
|
|
7852
9279
|
userTokenAccount,
|
|
7853
9280
|
owner: user,
|
|
7854
|
-
tokenProgram
|
|
7855
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7856
|
-
program: this._program.programId
|
|
9281
|
+
tokenProgram
|
|
7857
9282
|
}).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
|
|
7858
9283
|
});
|
|
7859
9284
|
}
|
|
@@ -7865,25 +9290,36 @@ export {
|
|
|
7865
9290
|
ActivationPoint,
|
|
7866
9291
|
ActivationType,
|
|
7867
9292
|
BASIS_POINT_MAX,
|
|
9293
|
+
BIN_STEP_BPS_DEFAULT,
|
|
9294
|
+
BIN_STEP_BPS_U128_DEFAULT,
|
|
7868
9295
|
CP_AMM_PROGRAM_ID,
|
|
7869
9296
|
CollectFeeMode,
|
|
7870
9297
|
CpAmm,
|
|
9298
|
+
DYNAMIC_FEE_DECAY_PERIOD_DEFAULT,
|
|
9299
|
+
DYNAMIC_FEE_FILTER_PERIOD_DEFAULT,
|
|
9300
|
+
DYNAMIC_FEE_REDUCTION_FACTOR_DEFAULT,
|
|
7871
9301
|
FEE_DENOMINATOR,
|
|
7872
9302
|
FeeSchedulerMode,
|
|
9303
|
+
LIQUIDITY_SCALE,
|
|
7873
9304
|
MAX_CU_BUFFER,
|
|
7874
9305
|
MAX_FEE_NUMERATOR,
|
|
9306
|
+
MAX_PRICE_CHANGE_BPS_DEFAULT,
|
|
7875
9307
|
MAX_SQRT_PRICE,
|
|
7876
9308
|
MIN_CU_BUFFER,
|
|
7877
9309
|
MIN_SQRT_PRICE,
|
|
9310
|
+
ONE,
|
|
7878
9311
|
Rounding,
|
|
7879
9312
|
SCALE_OFFSET,
|
|
7880
9313
|
TradeDirection,
|
|
7881
|
-
|
|
9314
|
+
bpsToFeeNumerator,
|
|
9315
|
+
calculateInitSqrtPrice,
|
|
9316
|
+
calculateTransferFeeExcludedAmount,
|
|
9317
|
+
calculateTransferFeeIncludedAmount,
|
|
9318
|
+
decimalToQ64,
|
|
7882
9319
|
index_default as default,
|
|
7883
9320
|
deriveClaimFeeOperatorAddress,
|
|
7884
9321
|
deriveConfigAddress,
|
|
7885
9322
|
deriveCustomizablePoolAddress,
|
|
7886
|
-
deriveEventAuthority,
|
|
7887
9323
|
derivePoolAddress,
|
|
7888
9324
|
derivePoolAuthority,
|
|
7889
9325
|
derivePositionAddress,
|
|
@@ -7891,23 +9327,43 @@ export {
|
|
|
7891
9327
|
deriveRewardVaultAddress,
|
|
7892
9328
|
deriveTokenBadgeAddress,
|
|
7893
9329
|
deriveTokenVaultAddress,
|
|
9330
|
+
feeNumeratorToBps,
|
|
9331
|
+
getAllPositionNftAccountByOwner,
|
|
9332
|
+
getAllUserPositionNftAccount,
|
|
9333
|
+
getAmountAFromLiquidityDelta,
|
|
9334
|
+
getAmountBFromLiquidityDelta,
|
|
9335
|
+
getAvailableVestingLiquidity,
|
|
7894
9336
|
getBaseFeeNumerator,
|
|
7895
|
-
|
|
7896
|
-
getDeltaAmountB,
|
|
9337
|
+
getBaseFeeParams,
|
|
7897
9338
|
getDynamicFeeNumerator,
|
|
9339
|
+
getDynamicFeeParams,
|
|
7898
9340
|
getEstimatedComputeUnitIxWithBuffer,
|
|
7899
9341
|
getEstimatedComputeUnitUsageWithBuffer,
|
|
7900
9342
|
getFeeNumerator,
|
|
7901
9343
|
getFirstKey,
|
|
7902
9344
|
getLiquidityDeltaFromAmountA,
|
|
7903
9345
|
getLiquidityDeltaFromAmountB,
|
|
9346
|
+
getMaxAmountWithSlippage,
|
|
9347
|
+
getMinAmountWithSlippage,
|
|
7904
9348
|
getNextSqrtPrice,
|
|
7905
9349
|
getOrCreateATAInstruction,
|
|
9350
|
+
getPriceFromSqrtPrice,
|
|
9351
|
+
getPriceImpact,
|
|
7906
9352
|
getSecondKey,
|
|
7907
9353
|
getSimulationComputeUnits,
|
|
9354
|
+
getSqrtPriceFromPrice,
|
|
9355
|
+
getSwapAmount,
|
|
7908
9356
|
getTokenDecimals,
|
|
7909
9357
|
getTokenProgram,
|
|
9358
|
+
getTotalLockedLiquidity,
|
|
9359
|
+
getUnClaimReward,
|
|
9360
|
+
isVestingComplete,
|
|
9361
|
+
mulDiv,
|
|
9362
|
+
positionByPoolFilter,
|
|
9363
|
+
pow,
|
|
9364
|
+
q64ToDecimal,
|
|
7910
9365
|
unwrapSOLInstruction,
|
|
9366
|
+
vestingByPositionFilter,
|
|
7911
9367
|
wrapSOLInstruction
|
|
7912
9368
|
};
|
|
7913
9369
|
//# sourceMappingURL=index.mjs.map
|