@meteora-ag/cp-amm-sdk 1.0.1-rc.9 → 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 +3297 -3427
- package/dist/index.d.ts +3297 -3427
- package/dist/index.js +2224 -799
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2262 -837
- 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,19 +7055,53 @@ 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;
|
|
7063
|
+
return amount.mul(new BN6(slippage)).div(new BN6(BASIS_POINT_MAX));
|
|
7064
|
+
};
|
|
7065
|
+
var getMinAmountWithSlippage = (amount, rate) => {
|
|
7066
|
+
const slippage = (100 - rate) / 100 * BASIS_POINT_MAX;
|
|
6799
7067
|
return amount.mul(new BN6(slippage)).div(new BN6(BASIS_POINT_MAX));
|
|
6800
7068
|
};
|
|
6801
|
-
var getPriceImpact = (
|
|
6802
|
-
const diff =
|
|
6803
|
-
return new
|
|
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
|
+
};
|
|
6804
7102
|
};
|
|
6805
7103
|
|
|
6806
|
-
// src/
|
|
7104
|
+
// src/helpers/accountFilters.ts
|
|
6807
7105
|
var positionByPoolFilter = (pool) => {
|
|
6808
7106
|
return {
|
|
6809
7107
|
memcmp: {
|
|
@@ -6812,243 +7110,1025 @@ var positionByPoolFilter = (pool) => {
|
|
|
6812
7110
|
}
|
|
6813
7111
|
};
|
|
6814
7112
|
};
|
|
7113
|
+
var vestingByPositionFilter = (position) => {
|
|
7114
|
+
return {
|
|
7115
|
+
memcmp: {
|
|
7116
|
+
bytes: position.toBase58(),
|
|
7117
|
+
offset: 8
|
|
7118
|
+
}
|
|
7119
|
+
};
|
|
7120
|
+
};
|
|
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
|
+
}
|
|
6815
7271
|
|
|
6816
7272
|
// src/CpAmm.ts
|
|
7273
|
+
import { min as min2 } from "bn.js";
|
|
6817
7274
|
var CpAmm = class {
|
|
6818
7275
|
constructor(connection) {
|
|
6819
7276
|
this._program = new Program(cp_amm_default, {
|
|
6820
7277
|
connection
|
|
6821
7278
|
});
|
|
6822
|
-
|
|
6823
|
-
getProgram() {
|
|
6824
|
-
return this._program;
|
|
7279
|
+
this.poolAuthority = derivePoolAuthority();
|
|
6825
7280
|
}
|
|
6826
7281
|
/**
|
|
6827
|
-
|
|
6828
|
-
|
|
6829
|
-
|
|
6830
|
-
|
|
6831
|
-
|
|
6832
|
-
|
|
6833
|
-
|
|
6834
|
-
|
|
6835
|
-
|
|
6836
|
-
|
|
6837
|
-
|
|
6838
|
-
const { tokenAAmount, tokenBAmount, tokenADecimal, tokenBDecimal } = params;
|
|
6839
|
-
const initPrice = tokenBAmount.div(tokenAAmount);
|
|
6840
|
-
const sqrtPriceQ64 = priceToSqrtPrice(
|
|
6841
|
-
new Decimal3(initPrice.toString()),
|
|
6842
|
-
tokenADecimal,
|
|
6843
|
-
tokenBDecimal
|
|
6844
|
-
);
|
|
6845
|
-
if (sqrtPriceQ64.lt(MIN_SQRT_PRICE) || sqrtPriceQ64.gt(MAX_SQRT_PRICE)) {
|
|
6846
|
-
throw new Error(`Invalid sqrt price: ${sqrtPriceQ64.toString()}`);
|
|
6847
|
-
}
|
|
6848
|
-
const liquidityDeltaFromAmountA = getLiquidityDeltaFromAmountA(
|
|
6849
|
-
tokenAAmount,
|
|
6850
|
-
sqrtPriceQ64,
|
|
6851
|
-
MAX_SQRT_PRICE
|
|
6852
|
-
);
|
|
6853
|
-
const liquidityDeltaFromAmountB = getLiquidityDeltaFromAmountB(
|
|
6854
|
-
tokenBAmount,
|
|
6855
|
-
MIN_SQRT_PRICE,
|
|
6856
|
-
sqrtPriceQ64
|
|
6857
|
-
);
|
|
6858
|
-
const liquidityQ64 = liquidityDeltaFromAmountA.gte(
|
|
6859
|
-
liquidityDeltaFromAmountB
|
|
6860
|
-
) ? liquidityDeltaFromAmountB : liquidityDeltaFromAmountA;
|
|
6861
|
-
return {
|
|
6862
|
-
sqrtPriceQ64,
|
|
6863
|
-
liquidityQ64
|
|
6864
|
-
};
|
|
6865
|
-
});
|
|
6866
|
-
}
|
|
6867
|
-
// fetcher
|
|
6868
|
-
fetchConfigState(config) {
|
|
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) {
|
|
6869
7293
|
return __async(this, null, function* () {
|
|
6870
|
-
const
|
|
6871
|
-
|
|
6872
|
-
|
|
6873
|
-
|
|
6874
|
-
|
|
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 };
|
|
6875
7319
|
});
|
|
6876
7320
|
}
|
|
6877
|
-
|
|
6878
|
-
|
|
6879
|
-
|
|
6880
|
-
|
|
6881
|
-
|
|
6882
|
-
|
|
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
|
+
];
|
|
6883
7340
|
}
|
|
6884
|
-
|
|
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) {
|
|
6885
7348
|
return __async(this, null, function* () {
|
|
6886
|
-
const
|
|
6887
|
-
|
|
6888
|
-
|
|
6889
|
-
|
|
6890
|
-
|
|
6891
|
-
|
|
6892
|
-
|
|
6893
|
-
|
|
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();
|
|
7384
|
+
});
|
|
7385
|
+
}
|
|
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) {
|
|
7393
|
+
return __async(this, null, function* () {
|
|
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();
|
|
7426
|
+
});
|
|
7427
|
+
}
|
|
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) {
|
|
7435
|
+
return __async(this, null, function* () {
|
|
7436
|
+
const {
|
|
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* () {
|
|
7476
|
+
const {
|
|
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;
|
|
7506
|
+
}
|
|
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
|
|
7630
|
+
);
|
|
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() {
|
|
6894
7666
|
return __async(this, null, function* () {
|
|
6895
7667
|
const configAccounts = yield this._program.account.config.all();
|
|
6896
7668
|
return configAccounts;
|
|
6897
7669
|
});
|
|
6898
7670
|
}
|
|
7671
|
+
/**
|
|
7672
|
+
* Retrieves all pool accounts.
|
|
7673
|
+
* @returns Array of pool public keys and their states.
|
|
7674
|
+
*/
|
|
6899
7675
|
getAllPools() {
|
|
6900
7676
|
return __async(this, null, function* () {
|
|
6901
7677
|
const poolAccounts = yield this._program.account.pool.all();
|
|
6902
7678
|
return poolAccounts;
|
|
6903
7679
|
});
|
|
6904
7680
|
}
|
|
7681
|
+
/**
|
|
7682
|
+
* Retrieves all position accounts.
|
|
7683
|
+
* @returns Array of position public keys and their states.
|
|
7684
|
+
*/
|
|
6905
7685
|
getAllPositions() {
|
|
6906
7686
|
return __async(this, null, function* () {
|
|
6907
7687
|
const poolAccounts = yield this._program.account.position.all();
|
|
6908
7688
|
return poolAccounts;
|
|
6909
7689
|
});
|
|
6910
7690
|
}
|
|
6911
|
-
|
|
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) {
|
|
6912
7697
|
return __async(this, null, function* () {
|
|
6913
|
-
|
|
7698
|
+
return yield this._program.account.position.all([
|
|
6914
7699
|
positionByPoolFilter(pool)
|
|
6915
7700
|
]);
|
|
6916
|
-
const result = [];
|
|
6917
|
-
for (const position of positions) {
|
|
6918
|
-
const largesTokenAccount = yield this._program.provider.connection.getTokenLargestAccounts(
|
|
6919
|
-
position.account.nftMint
|
|
6920
|
-
);
|
|
6921
|
-
const accountInfo = yield this._program.provider.connection.getParsedAccountInfo(
|
|
6922
|
-
largesTokenAccount.value[0].address
|
|
6923
|
-
);
|
|
6924
|
-
const owner = new PublicKey5(accountInfo.value.data.parsed.info.owner);
|
|
6925
|
-
if (owner.equals(user)) {
|
|
6926
|
-
result.push(position);
|
|
6927
|
-
}
|
|
6928
|
-
}
|
|
6929
|
-
return result;
|
|
6930
7701
|
});
|
|
6931
7702
|
}
|
|
6932
|
-
|
|
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) {
|
|
6933
7710
|
return __async(this, null, function* () {
|
|
6934
|
-
const
|
|
6935
|
-
|
|
6936
|
-
|
|
6937
|
-
|
|
6938
|
-
const largesTokenAccount = yield this._program.provider.connection.getTokenLargestAccounts(
|
|
6939
|
-
positionNftMints[0]
|
|
6940
|
-
);
|
|
6941
|
-
const accountInfo = yield this._program.provider.connection.getParsedAccountInfo(
|
|
6942
|
-
largesTokenAccount.value[0].address
|
|
6943
|
-
);
|
|
6944
|
-
const owner = new PublicKey5(accountInfo.value.data.parsed.info.owner);
|
|
6945
|
-
if (owner.equals(user)) {
|
|
6946
|
-
result.push(position);
|
|
6947
|
-
}
|
|
6948
|
-
}
|
|
6949
|
-
return result;
|
|
7711
|
+
const allUserPositions = yield this.getPositionsByUser(user);
|
|
7712
|
+
return allUserPositions.filter(
|
|
7713
|
+
(position) => position.positionState.pool.equals(pool)
|
|
7714
|
+
);
|
|
6950
7715
|
});
|
|
6951
7716
|
}
|
|
6952
|
-
|
|
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) {
|
|
6953
7723
|
return __async(this, null, function* () {
|
|
6954
|
-
|
|
6955
|
-
|
|
6956
|
-
|
|
6957
|
-
sqrtPrice: sqrtPriceQ64,
|
|
6958
|
-
liquidity: liquidityQ64,
|
|
6959
|
-
activationType,
|
|
6960
|
-
activationPoint,
|
|
6961
|
-
collectFeeMode,
|
|
6962
|
-
poolFees
|
|
6963
|
-
} = poolState;
|
|
6964
|
-
const {
|
|
6965
|
-
feeSchedulerMode,
|
|
6966
|
-
cliffFeeNumerator,
|
|
6967
|
-
numberOfPeriod,
|
|
6968
|
-
reductionFactor,
|
|
6969
|
-
periodFrequency
|
|
6970
|
-
} = poolFees.baseFee;
|
|
6971
|
-
const dynamicFee = poolFees.dynamicFee;
|
|
6972
|
-
const aToB = poolState.tokenAMint.equals(inputTokenMint);
|
|
6973
|
-
const slot = yield this._program.provider.connection.getSlot();
|
|
6974
|
-
const blockInfo = yield this._program.provider.connection.getBlock(slot, {
|
|
6975
|
-
maxSupportedTransactionVersion: 0
|
|
6976
|
-
});
|
|
6977
|
-
const currentTime = (_a = blockInfo == null ? void 0 : blockInfo.blockTime) != null ? _a : Math.floor(Date.now() / 1e3);
|
|
6978
|
-
const currentPoint = activationType ? currentTime : slot;
|
|
6979
|
-
let dynamicFeeParams;
|
|
6980
|
-
if (dynamicFee.initialized) {
|
|
6981
|
-
const { volatilityAccumulator, binStep, variableFeeControl } = dynamicFee;
|
|
6982
|
-
dynamicFeeParams = { volatilityAccumulator, binStep, variableFeeControl };
|
|
6983
|
-
}
|
|
6984
|
-
const tradeFeeNumerator = getFeeNumerator(
|
|
6985
|
-
currentPoint,
|
|
6986
|
-
activationPoint,
|
|
6987
|
-
numberOfPeriod,
|
|
6988
|
-
periodFrequency,
|
|
6989
|
-
feeSchedulerMode,
|
|
6990
|
-
cliffFeeNumerator,
|
|
6991
|
-
reductionFactor,
|
|
6992
|
-
dynamicFeeParams
|
|
7724
|
+
const userPositionAccounts = yield getAllPositionNftAccountByOwner(
|
|
7725
|
+
this._program.provider.connection,
|
|
7726
|
+
user
|
|
6993
7727
|
);
|
|
6994
|
-
|
|
6995
|
-
|
|
6996
|
-
|
|
6997
|
-
|
|
6998
|
-
|
|
6999
|
-
aToB,
|
|
7000
|
-
collectFeeMode
|
|
7728
|
+
if (userPositionAccounts.length === 0) {
|
|
7729
|
+
return [];
|
|
7730
|
+
}
|
|
7731
|
+
const positionAddresses = userPositionAccounts.map(
|
|
7732
|
+
(account) => derivePositionAddress(account.positionNft)
|
|
7001
7733
|
);
|
|
7002
|
-
const
|
|
7003
|
-
|
|
7004
|
-
slippage
|
|
7734
|
+
const positionStates = yield this._program.account.position.fetchMultiple(
|
|
7735
|
+
positionAddresses
|
|
7005
7736
|
);
|
|
7006
|
-
|
|
7007
|
-
|
|
7008
|
-
|
|
7009
|
-
|
|
7010
|
-
|
|
7011
|
-
|
|
7012
|
-
|
|
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;
|
|
7013
7752
|
});
|
|
7014
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;
|
|
7760
|
+
});
|
|
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
|
+
}
|
|
7015
7771
|
/**
|
|
7016
|
-
*
|
|
7772
|
+
* Checks if a position can be unlocked based on its locking state and vesting schedules.
|
|
7017
7773
|
*
|
|
7018
|
-
*
|
|
7019
|
-
*
|
|
7020
|
-
*
|
|
7021
|
-
* - maxAmountX: The maximum amount of token X available.
|
|
7022
|
-
* - maxAmountY: The maximum amount of token Y available.
|
|
7023
|
-
* - pool: The address of the liquidity pool.
|
|
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.
|
|
7024
7777
|
*
|
|
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
|
|
7814
|
+
*
|
|
7815
|
+
* @param {LiquidityDeltaParams} params - The parameters for liquidity calculation
|
|
7025
7816
|
* @returns {Promise<BN>} - The computed liquidity delta in Q64 value.
|
|
7026
7817
|
*/
|
|
7027
7818
|
getLiquidityDelta(params) {
|
|
7028
|
-
|
|
7029
|
-
|
|
7030
|
-
|
|
7031
|
-
|
|
7032
|
-
|
|
7033
|
-
|
|
7034
|
-
|
|
7035
|
-
|
|
7036
|
-
|
|
7037
|
-
|
|
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,
|
|
7038
7954
|
sqrtPrice,
|
|
7039
|
-
|
|
7040
|
-
)
|
|
7041
|
-
|
|
7042
|
-
|
|
7043
|
-
|
|
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,
|
|
7044
7967
|
sqrtPrice
|
|
7045
|
-
)
|
|
7046
|
-
|
|
7047
|
-
|
|
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
|
+
};
|
|
7048
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
|
+
};
|
|
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
|
+
*/
|
|
7049
8130
|
createPool(params) {
|
|
7050
8131
|
return __async(this, null, function* () {
|
|
7051
|
-
var _a, _b, _c, _d;
|
|
7052
8132
|
const {
|
|
7053
8133
|
payer,
|
|
7054
8134
|
creator,
|
|
@@ -7056,70 +8136,61 @@ var CpAmm = class {
|
|
|
7056
8136
|
positionNft,
|
|
7057
8137
|
tokenAMint,
|
|
7058
8138
|
tokenBMint,
|
|
8139
|
+
initSqrtPrice,
|
|
8140
|
+
liquidityDelta,
|
|
7059
8141
|
activationPoint,
|
|
7060
8142
|
tokenAAmount,
|
|
7061
8143
|
tokenBAmount,
|
|
7062
|
-
|
|
7063
|
-
|
|
8144
|
+
tokenAProgram,
|
|
8145
|
+
tokenBProgram
|
|
7064
8146
|
} = params;
|
|
7065
|
-
const
|
|
7066
|
-
|
|
7067
|
-
|
|
7068
|
-
|
|
7069
|
-
|
|
7070
|
-
|
|
7071
|
-
|
|
7072
|
-
|
|
7073
|
-
|
|
7074
|
-
|
|
7075
|
-
config,
|
|
7076
|
-
tokenAMint,
|
|
7077
|
-
tokenBMint,
|
|
7078
|
-
this._program.programId
|
|
7079
|
-
);
|
|
7080
|
-
const position = derivePositionAddress(
|
|
7081
|
-
positionNft,
|
|
7082
|
-
this._program.programId
|
|
7083
|
-
);
|
|
7084
|
-
const positionNftAccount = derivePositionNftAccount(
|
|
7085
|
-
positionNft,
|
|
7086
|
-
this._program.programId
|
|
7087
|
-
);
|
|
7088
|
-
const tokenAProgram = (_b = (_a = yield this._program.provider.connection.getParsedAccountInfo(tokenAMint)) == null ? void 0 : _a.value) == null ? void 0 : _b.owner;
|
|
7089
|
-
const tokenBProgram = (_d = (_c = yield this._program.provider.connection.getParsedAccountInfo(tokenBMint)) == null ? void 0 : _c.value) == null ? void 0 : _d.owner;
|
|
7090
|
-
const payerTokenA = getAssociatedTokenAddressSync2(
|
|
7091
|
-
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(
|
|
7092
8157
|
payer,
|
|
7093
|
-
|
|
7094
|
-
tokenAProgram
|
|
7095
|
-
);
|
|
7096
|
-
const payerTokenB = getAssociatedTokenAddressSync2(
|
|
8158
|
+
tokenAMint,
|
|
7097
8159
|
tokenBMint,
|
|
7098
|
-
|
|
7099
|
-
true,
|
|
8160
|
+
tokenAProgram,
|
|
7100
8161
|
tokenBProgram
|
|
7101
8162
|
);
|
|
7102
|
-
|
|
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(
|
|
7103
8180
|
tokenAMint,
|
|
7104
|
-
|
|
7105
|
-
this._program.programId
|
|
7106
|
-
);
|
|
7107
|
-
const tokenBVault = deriveTokenVaultAddress(
|
|
7108
|
-
tokenBMint,
|
|
7109
|
-
pool,
|
|
7110
|
-
this._program.programId
|
|
8181
|
+
tokenBMint
|
|
7111
8182
|
);
|
|
7112
8183
|
const tx = yield this._program.methods.initializePool({
|
|
7113
|
-
liquidity:
|
|
7114
|
-
sqrtPrice:
|
|
8184
|
+
liquidity: liquidityDelta,
|
|
8185
|
+
sqrtPrice: initSqrtPrice,
|
|
7115
8186
|
activationPoint
|
|
7116
|
-
}).
|
|
8187
|
+
}).accountsPartial({
|
|
7117
8188
|
creator,
|
|
7118
8189
|
positionNftAccount,
|
|
7119
8190
|
positionNftMint: positionNft,
|
|
7120
8191
|
payer,
|
|
7121
8192
|
config,
|
|
7122
|
-
poolAuthority,
|
|
8193
|
+
poolAuthority: this.poolAuthority,
|
|
7123
8194
|
pool,
|
|
7124
8195
|
position,
|
|
7125
8196
|
tokenAMint,
|
|
@@ -7131,13 +8202,16 @@ var CpAmm = class {
|
|
|
7131
8202
|
token2022Program: TOKEN_2022_PROGRAM_ID2,
|
|
7132
8203
|
tokenAProgram,
|
|
7133
8204
|
tokenBProgram,
|
|
7134
|
-
systemProgram: SystemProgram2.programId
|
|
7135
|
-
|
|
7136
|
-
program: this._program.programId
|
|
7137
|
-
}).transaction();
|
|
8205
|
+
systemProgram: SystemProgram2.programId
|
|
8206
|
+
}).preInstructions(preInstructions).remainingAccounts(tokenBadgeAccounts).transaction();
|
|
7138
8207
|
return tx;
|
|
7139
8208
|
});
|
|
7140
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
|
+
*/
|
|
7141
8215
|
createCustomPool(params) {
|
|
7142
8216
|
return __async(this, null, function* () {
|
|
7143
8217
|
const {
|
|
@@ -7145,8 +8219,10 @@ var CpAmm = class {
|
|
|
7145
8219
|
tokenBMint,
|
|
7146
8220
|
tokenAAmount,
|
|
7147
8221
|
tokenBAmount,
|
|
7148
|
-
|
|
7149
|
-
|
|
8222
|
+
sqrtMinPrice,
|
|
8223
|
+
sqrtMaxPrice,
|
|
8224
|
+
liquidityDelta,
|
|
8225
|
+
initSqrtPrice,
|
|
7150
8226
|
payer,
|
|
7151
8227
|
creator,
|
|
7152
8228
|
positionNft,
|
|
@@ -7158,62 +8234,22 @@ var CpAmm = class {
|
|
|
7158
8234
|
tokenAProgram,
|
|
7159
8235
|
tokenBProgram
|
|
7160
8236
|
} = params;
|
|
7161
|
-
const
|
|
7162
|
-
|
|
7163
|
-
|
|
7164
|
-
|
|
7165
|
-
|
|
7166
|
-
|
|
7167
|
-
|
|
7168
|
-
|
|
7169
|
-
|
|
7170
|
-
|
|
7171
|
-
|
|
7172
|
-
tokenBMint,
|
|
7173
|
-
this._program.programId
|
|
7174
|
-
);
|
|
7175
|
-
const position = derivePositionAddress(
|
|
7176
|
-
positionNft,
|
|
7177
|
-
this._program.programId
|
|
7178
|
-
);
|
|
7179
|
-
const positionNftAccount = derivePositionNftAccount(
|
|
7180
|
-
positionNft,
|
|
7181
|
-
this._program.programId
|
|
7182
|
-
);
|
|
7183
|
-
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,
|
|
7184
8248
|
tokenAMint,
|
|
7185
|
-
pool,
|
|
7186
|
-
this._program.programId
|
|
7187
|
-
);
|
|
7188
|
-
const tokenBVault = deriveTokenVaultAddress(
|
|
7189
8249
|
tokenBMint,
|
|
7190
|
-
|
|
7191
|
-
|
|
8250
|
+
tokenAProgram,
|
|
8251
|
+
tokenBProgram
|
|
7192
8252
|
);
|
|
7193
|
-
const preInstructions = [];
|
|
7194
|
-
const [
|
|
7195
|
-
{ ataPubkey: payerTokenA, ix: createTokenATokenAccountIx },
|
|
7196
|
-
{ ataPubkey: payerTokenB, ix: createTokenBTokenAccountIx }
|
|
7197
|
-
] = yield Promise.all([
|
|
7198
|
-
getOrCreateATAInstruction(
|
|
7199
|
-
this._program.provider.connection,
|
|
7200
|
-
tokenAMint,
|
|
7201
|
-
payer,
|
|
7202
|
-
payer,
|
|
7203
|
-
true,
|
|
7204
|
-
tokenAProgram
|
|
7205
|
-
),
|
|
7206
|
-
getOrCreateATAInstruction(
|
|
7207
|
-
this._program.provider.connection,
|
|
7208
|
-
tokenBMint,
|
|
7209
|
-
payer,
|
|
7210
|
-
payer,
|
|
7211
|
-
true,
|
|
7212
|
-
tokenBProgram
|
|
7213
|
-
)
|
|
7214
|
-
]);
|
|
7215
|
-
createTokenATokenAccountIx && preInstructions.push(createTokenATokenAccountIx);
|
|
7216
|
-
createTokenBTokenAccountIx && preInstructions.push(createTokenBTokenAccountIx);
|
|
7217
8253
|
if (tokenAMint.equals(NATIVE_MINT2)) {
|
|
7218
8254
|
const wrapSOLIx = wrapSOLInstruction(
|
|
7219
8255
|
payer,
|
|
@@ -7223,29 +8259,34 @@ var CpAmm = class {
|
|
|
7223
8259
|
preInstructions.push(...wrapSOLIx);
|
|
7224
8260
|
}
|
|
7225
8261
|
if (tokenBMint.equals(NATIVE_MINT2)) {
|
|
8262
|
+
const lamports = tokenBAmount.eq(new BN10(0)) ? new BN10(1) : tokenBAmount;
|
|
7226
8263
|
const wrapSOLIx = wrapSOLInstruction(
|
|
7227
8264
|
payer,
|
|
7228
8265
|
payerTokenB,
|
|
7229
|
-
BigInt(
|
|
8266
|
+
BigInt(lamports.toString())
|
|
7230
8267
|
);
|
|
7231
8268
|
preInstructions.push(...wrapSOLIx);
|
|
7232
8269
|
}
|
|
8270
|
+
const tokenBadgeAccounts = this.getTokenBadgeAccounts(
|
|
8271
|
+
tokenAMint,
|
|
8272
|
+
tokenBMint
|
|
8273
|
+
);
|
|
7233
8274
|
const transaction = yield this._program.methods.initializeCustomizablePool({
|
|
7234
8275
|
poolFees,
|
|
7235
|
-
sqrtMinPrice
|
|
7236
|
-
sqrtMaxPrice
|
|
8276
|
+
sqrtMinPrice,
|
|
8277
|
+
sqrtMaxPrice,
|
|
7237
8278
|
hasAlphaVault,
|
|
7238
|
-
liquidity:
|
|
7239
|
-
sqrtPrice:
|
|
8279
|
+
liquidity: liquidityDelta,
|
|
8280
|
+
sqrtPrice: initSqrtPrice,
|
|
7240
8281
|
activationType,
|
|
7241
8282
|
collectFeeMode,
|
|
7242
8283
|
activationPoint
|
|
7243
|
-
}).
|
|
8284
|
+
}).accountsPartial({
|
|
7244
8285
|
creator,
|
|
7245
8286
|
positionNftAccount,
|
|
7246
8287
|
positionNftMint: positionNft,
|
|
7247
8288
|
payer,
|
|
7248
|
-
poolAuthority,
|
|
8289
|
+
poolAuthority: this.poolAuthority,
|
|
7249
8290
|
pool,
|
|
7250
8291
|
position,
|
|
7251
8292
|
tokenAMint,
|
|
@@ -7255,50 +8296,37 @@ var CpAmm = class {
|
|
|
7255
8296
|
payerTokenA,
|
|
7256
8297
|
payerTokenB,
|
|
7257
8298
|
token2022Program: TOKEN_2022_PROGRAM_ID2,
|
|
7258
|
-
tokenAProgram
|
|
7259
|
-
tokenBProgram
|
|
7260
|
-
systemProgram: SystemProgram2.programId
|
|
7261
|
-
|
|
7262
|
-
program: this._program.programId
|
|
7263
|
-
}).preInstructions(preInstructions).transaction();
|
|
8299
|
+
tokenAProgram,
|
|
8300
|
+
tokenBProgram,
|
|
8301
|
+
systemProgram: SystemProgram2.programId
|
|
8302
|
+
}).preInstructions(preInstructions).remainingAccounts(tokenBadgeAccounts).transaction();
|
|
7264
8303
|
return { tx: transaction, pool, position };
|
|
7265
8304
|
});
|
|
7266
8305
|
}
|
|
8306
|
+
/**
|
|
8307
|
+
* Builds a transaction to create a position.
|
|
8308
|
+
* @param {CreatePositionParams} params - Parameters for position creation.
|
|
8309
|
+
* @returns Transaction builder.
|
|
8310
|
+
*/
|
|
7267
8311
|
createPosition(params) {
|
|
7268
8312
|
return __async(this, null, function* () {
|
|
7269
|
-
const {
|
|
7270
|
-
|
|
7271
|
-
const position = derivePositionAddress(
|
|
7272
|
-
positionNft,
|
|
7273
|
-
this._program.programId
|
|
7274
|
-
);
|
|
7275
|
-
const positionNftAccount = derivePositionNftAccount(
|
|
7276
|
-
positionNft,
|
|
7277
|
-
this._program.programId
|
|
7278
|
-
);
|
|
7279
|
-
return yield this._program.methods.createPosition().accountsStrict({
|
|
7280
|
-
owner,
|
|
7281
|
-
positionNftMint: positionNft,
|
|
7282
|
-
poolAuthority,
|
|
7283
|
-
positionNftAccount,
|
|
7284
|
-
payer,
|
|
7285
|
-
pool,
|
|
7286
|
-
position,
|
|
7287
|
-
tokenProgram: TOKEN_2022_PROGRAM_ID2,
|
|
7288
|
-
systemProgram: SystemProgram2.programId,
|
|
7289
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7290
|
-
program: this._program.programId
|
|
7291
|
-
}).transaction();
|
|
8313
|
+
const { ix } = yield this.buildCreatePositionInstruction(params);
|
|
8314
|
+
return new Transaction().add(ix);
|
|
7292
8315
|
});
|
|
7293
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
|
+
*/
|
|
7294
8322
|
addLiquidity(params) {
|
|
7295
8323
|
return __async(this, null, function* () {
|
|
7296
8324
|
const {
|
|
7297
8325
|
owner,
|
|
7298
8326
|
pool,
|
|
7299
8327
|
position,
|
|
7300
|
-
|
|
7301
|
-
|
|
8328
|
+
positionNftAccount,
|
|
8329
|
+
liquidityDelta,
|
|
7302
8330
|
maxAmountTokenA,
|
|
7303
8331
|
maxAmountTokenB,
|
|
7304
8332
|
tokenAAmountThreshold,
|
|
@@ -7310,34 +8338,17 @@ var CpAmm = class {
|
|
|
7310
8338
|
tokenAProgram,
|
|
7311
8339
|
tokenBProgram
|
|
7312
8340
|
} = params;
|
|
7313
|
-
const
|
|
7314
|
-
|
|
7315
|
-
|
|
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
|
|
7316
8351
|
);
|
|
7317
|
-
const preInstructions = [];
|
|
7318
|
-
const [
|
|
7319
|
-
{ ataPubkey: tokenAAccount, ix: createTokenATokenAccountIx },
|
|
7320
|
-
{ ataPubkey: tokenBAccount, ix: createTokenBTokenAccountIx }
|
|
7321
|
-
] = yield Promise.all([
|
|
7322
|
-
getOrCreateATAInstruction(
|
|
7323
|
-
this._program.provider.connection,
|
|
7324
|
-
tokenAMint,
|
|
7325
|
-
owner,
|
|
7326
|
-
owner,
|
|
7327
|
-
true,
|
|
7328
|
-
tokenAProgram
|
|
7329
|
-
),
|
|
7330
|
-
getOrCreateATAInstruction(
|
|
7331
|
-
this._program.provider.connection,
|
|
7332
|
-
tokenBMint,
|
|
7333
|
-
owner,
|
|
7334
|
-
owner,
|
|
7335
|
-
true,
|
|
7336
|
-
tokenBProgram
|
|
7337
|
-
)
|
|
7338
|
-
]);
|
|
7339
|
-
createTokenATokenAccountIx && preInstructions.push(createTokenATokenAccountIx);
|
|
7340
|
-
createTokenBTokenAccountIx && preInstructions.push(createTokenBTokenAccountIx);
|
|
7341
8352
|
if (tokenAMint.equals(NATIVE_MINT2)) {
|
|
7342
8353
|
const wrapSOLIx = wrapSOLInstruction(
|
|
7343
8354
|
owner,
|
|
@@ -7361,11 +8372,104 @@ var CpAmm = class {
|
|
|
7361
8372
|
const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
|
|
7362
8373
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
7363
8374
|
}
|
|
7364
|
-
|
|
7365
|
-
|
|
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,
|
|
7366
8389
|
tokenAAmountThreshold,
|
|
7367
8390
|
tokenBAmountThreshold
|
|
7368
|
-
})
|
|
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({
|
|
7369
8473
|
pool,
|
|
7370
8474
|
position,
|
|
7371
8475
|
positionNftAccount,
|
|
@@ -7378,19 +8482,33 @@ var CpAmm = class {
|
|
|
7378
8482
|
tokenBVault,
|
|
7379
8483
|
tokenAProgram,
|
|
7380
8484
|
tokenBProgram,
|
|
7381
|
-
|
|
7382
|
-
|
|
7383
|
-
|
|
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;
|
|
7384
8497
|
});
|
|
7385
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
|
+
*/
|
|
7386
8504
|
removeLiquidity(params) {
|
|
7387
8505
|
return __async(this, null, function* () {
|
|
7388
8506
|
const {
|
|
7389
8507
|
owner,
|
|
7390
8508
|
pool,
|
|
7391
8509
|
position,
|
|
7392
|
-
|
|
7393
|
-
|
|
8510
|
+
positionNftAccount,
|
|
8511
|
+
liquidityDelta,
|
|
7394
8512
|
tokenAAmountThreshold,
|
|
7395
8513
|
tokenBAmountThreshold,
|
|
7396
8514
|
tokenAMint,
|
|
@@ -7398,37 +8516,91 @@ var CpAmm = class {
|
|
|
7398
8516
|
tokenAVault,
|
|
7399
8517
|
tokenBVault,
|
|
7400
8518
|
tokenAProgram,
|
|
7401
|
-
tokenBProgram
|
|
8519
|
+
tokenBProgram,
|
|
8520
|
+
vestings
|
|
7402
8521
|
} = params;
|
|
7403
|
-
const
|
|
7404
|
-
|
|
7405
|
-
|
|
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
|
|
7406
8532
|
);
|
|
7407
|
-
const
|
|
7408
|
-
|
|
7409
|
-
|
|
7410
|
-
|
|
7411
|
-
|
|
7412
|
-
|
|
7413
|
-
|
|
7414
|
-
|
|
7415
|
-
|
|
7416
|
-
owner,
|
|
7417
|
-
owner,
|
|
7418
|
-
true,
|
|
7419
|
-
tokenAProgram
|
|
7420
|
-
),
|
|
7421
|
-
getOrCreateATAInstruction(
|
|
7422
|
-
this._program.provider.connection,
|
|
7423
|
-
tokenBMint,
|
|
7424
|
-
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({
|
|
7425
8542
|
owner,
|
|
7426
|
-
|
|
7427
|
-
|
|
7428
|
-
|
|
7429
|
-
|
|
7430
|
-
|
|
7431
|
-
|
|
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
|
+
);
|
|
7432
8604
|
const postInstructions = [];
|
|
7433
8605
|
if ([tokenAMint.toBase58(), tokenBMint.toBase58()].includes(
|
|
7434
8606
|
NATIVE_MINT2.toBase58()
|
|
@@ -7436,29 +8608,47 @@ var CpAmm = class {
|
|
|
7436
8608
|
const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
|
|
7437
8609
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
7438
8610
|
}
|
|
7439
|
-
|
|
7440
|
-
|
|
7441
|
-
|
|
7442
|
-
|
|
7443
|
-
|
|
7444
|
-
|
|
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,
|
|
7445
8624
|
pool,
|
|
7446
8625
|
position,
|
|
7447
8626
|
positionNftAccount,
|
|
7448
|
-
owner,
|
|
7449
8627
|
tokenAAccount,
|
|
7450
8628
|
tokenBAccount,
|
|
8629
|
+
tokenAAmountThreshold,
|
|
8630
|
+
tokenBAmountThreshold,
|
|
7451
8631
|
tokenAMint,
|
|
7452
8632
|
tokenBMint,
|
|
7453
8633
|
tokenAVault,
|
|
7454
8634
|
tokenBVault,
|
|
7455
8635
|
tokenAProgram,
|
|
7456
|
-
tokenBProgram
|
|
7457
|
-
|
|
7458
|
-
|
|
7459
|
-
|
|
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;
|
|
7460
8645
|
});
|
|
7461
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
|
+
*/
|
|
7462
8652
|
swap(params) {
|
|
7463
8653
|
return __async(this, null, function* () {
|
|
7464
8654
|
const {
|
|
@@ -7476,31 +8666,20 @@ var CpAmm = class {
|
|
|
7476
8666
|
tokenBProgram,
|
|
7477
8667
|
referralTokenAccount
|
|
7478
8668
|
} = params;
|
|
7479
|
-
const
|
|
7480
|
-
|
|
7481
|
-
|
|
7482
|
-
|
|
7483
|
-
|
|
7484
|
-
|
|
7485
|
-
|
|
7486
|
-
|
|
7487
|
-
|
|
7488
|
-
|
|
7489
|
-
|
|
7490
|
-
|
|
7491
|
-
|
|
7492
|
-
|
|
7493
|
-
getOrCreateATAInstruction(
|
|
7494
|
-
this._program.provider.connection,
|
|
7495
|
-
outputTokenMint,
|
|
7496
|
-
payer,
|
|
7497
|
-
payer,
|
|
7498
|
-
true,
|
|
7499
|
-
tokenBProgram
|
|
7500
|
-
)
|
|
7501
|
-
]);
|
|
7502
|
-
createInputTokenAccountIx && preInstructions.push(createInputTokenAccountIx);
|
|
7503
|
-
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
|
+
);
|
|
7504
8683
|
if (inputTokenMint.equals(NATIVE_MINT2)) {
|
|
7505
8684
|
const wrapSOLIx = wrapSOLInstruction(
|
|
7506
8685
|
payer,
|
|
@@ -7519,8 +8698,8 @@ var CpAmm = class {
|
|
|
7519
8698
|
return yield this._program.methods.swap({
|
|
7520
8699
|
amountIn,
|
|
7521
8700
|
minimumAmountOut
|
|
7522
|
-
}).
|
|
7523
|
-
poolAuthority,
|
|
8701
|
+
}).accountsPartial({
|
|
8702
|
+
poolAuthority: this.poolAuthority,
|
|
7524
8703
|
pool,
|
|
7525
8704
|
payer,
|
|
7526
8705
|
inputTokenAccount,
|
|
@@ -7531,12 +8710,15 @@ var CpAmm = class {
|
|
|
7531
8710
|
tokenBMint,
|
|
7532
8711
|
tokenAProgram,
|
|
7533
8712
|
tokenBProgram,
|
|
7534
|
-
referralTokenAccount
|
|
7535
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7536
|
-
program: this._program.programId
|
|
8713
|
+
referralTokenAccount
|
|
7537
8714
|
}).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
|
|
7538
8715
|
});
|
|
7539
8716
|
}
|
|
8717
|
+
/**
|
|
8718
|
+
* Builds a transaction to lock a position with vesting schedule.
|
|
8719
|
+
* @param {LockPositionParams} params - Locking parameters.
|
|
8720
|
+
* @returns Transaction builder.
|
|
8721
|
+
*/
|
|
7540
8722
|
lockPosition(params) {
|
|
7541
8723
|
return __async(this, null, function* () {
|
|
7542
8724
|
const {
|
|
@@ -7545,86 +8727,70 @@ var CpAmm = class {
|
|
|
7545
8727
|
payer,
|
|
7546
8728
|
vestingAccount,
|
|
7547
8729
|
position,
|
|
7548
|
-
|
|
8730
|
+
positionNftAccount,
|
|
7549
8731
|
cliffPoint,
|
|
7550
8732
|
periodFrequency,
|
|
7551
8733
|
cliffUnlockLiquidity,
|
|
7552
8734
|
liquidityPerPeriod,
|
|
7553
|
-
numberOfPeriod
|
|
7554
|
-
vestings
|
|
8735
|
+
numberOfPeriod
|
|
7555
8736
|
} = params;
|
|
7556
|
-
const positionNftAccount = derivePositionNftAccount(
|
|
7557
|
-
positionNftMint,
|
|
7558
|
-
this._program.programId
|
|
7559
|
-
);
|
|
7560
8737
|
const lockPositionParams = {
|
|
7561
8738
|
cliffPoint,
|
|
7562
8739
|
periodFrequency,
|
|
7563
8740
|
cliffUnlockLiquidity,
|
|
7564
8741
|
liquidityPerPeriod,
|
|
7565
|
-
numberOfPeriod
|
|
7566
|
-
index: vestings.length
|
|
8742
|
+
numberOfPeriod
|
|
7567
8743
|
};
|
|
7568
|
-
return yield this._program.methods.lockPosition(lockPositionParams).
|
|
8744
|
+
return yield this._program.methods.lockPosition(lockPositionParams).accountsPartial({
|
|
7569
8745
|
position,
|
|
7570
8746
|
positionNftAccount,
|
|
7571
8747
|
vesting: vestingAccount,
|
|
7572
8748
|
pool,
|
|
7573
8749
|
owner,
|
|
7574
8750
|
payer,
|
|
7575
|
-
systemProgram: SystemProgram2.programId
|
|
7576
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7577
|
-
program: this._program.programId
|
|
8751
|
+
systemProgram: SystemProgram2.programId
|
|
7578
8752
|
}).transaction();
|
|
7579
8753
|
});
|
|
7580
8754
|
}
|
|
8755
|
+
/**
|
|
8756
|
+
* Builds a transaction to permanently lock a position.
|
|
8757
|
+
* @param {PermanentLockParams} params - Parameters for permanent locking.
|
|
8758
|
+
* @returns Transaction builder.
|
|
8759
|
+
*/
|
|
7581
8760
|
permanentLockPosition(params) {
|
|
7582
8761
|
return __async(this, null, function* () {
|
|
7583
|
-
const { owner, position,
|
|
7584
|
-
|
|
7585
|
-
positionNftMint,
|
|
7586
|
-
this._program.programId
|
|
7587
|
-
);
|
|
7588
|
-
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({
|
|
7589
8764
|
position,
|
|
7590
8765
|
positionNftAccount,
|
|
7591
8766
|
pool,
|
|
7592
|
-
owner
|
|
7593
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7594
|
-
program: this._program.programId
|
|
8767
|
+
owner
|
|
7595
8768
|
}).transaction();
|
|
7596
8769
|
});
|
|
7597
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
|
+
*/
|
|
7598
8776
|
refreshVesting(params) {
|
|
7599
8777
|
return __async(this, null, function* () {
|
|
7600
|
-
const
|
|
7601
|
-
|
|
7602
|
-
positionNftMint,
|
|
7603
|
-
this._program.programId
|
|
7604
|
-
);
|
|
7605
|
-
return yield this._program.methods.refreshVesting().accountsStrict({
|
|
7606
|
-
position,
|
|
7607
|
-
positionNftAccount,
|
|
7608
|
-
pool,
|
|
7609
|
-
owner
|
|
7610
|
-
}).remainingAccounts(
|
|
7611
|
-
vestings.map((pubkey) => {
|
|
7612
|
-
return {
|
|
7613
|
-
isSigner: false,
|
|
7614
|
-
isWritable: true,
|
|
7615
|
-
pubkey
|
|
7616
|
-
};
|
|
7617
|
-
})
|
|
7618
|
-
).transaction();
|
|
8778
|
+
const instruction = yield this.buildRefreshVestingInstruction(params);
|
|
8779
|
+
return new Transaction().add(instruction);
|
|
7619
8780
|
});
|
|
7620
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
|
+
*/
|
|
7621
8787
|
claimPositionFee(params) {
|
|
7622
8788
|
return __async(this, null, function* () {
|
|
7623
8789
|
const {
|
|
7624
8790
|
owner,
|
|
7625
8791
|
pool,
|
|
7626
8792
|
position,
|
|
7627
|
-
|
|
8793
|
+
positionNftAccount,
|
|
7628
8794
|
tokenAVault,
|
|
7629
8795
|
tokenBVault,
|
|
7630
8796
|
tokenAMint,
|
|
@@ -7632,35 +8798,17 @@ var CpAmm = class {
|
|
|
7632
8798
|
tokenAProgram,
|
|
7633
8799
|
tokenBProgram
|
|
7634
8800
|
} = params;
|
|
7635
|
-
const
|
|
7636
|
-
|
|
7637
|
-
|
|
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
|
|
7638
8811
|
);
|
|
7639
|
-
const poolAuthority = derivePoolAuthority(this._program.programId);
|
|
7640
|
-
const preInstructions = [];
|
|
7641
|
-
const [
|
|
7642
|
-
{ ataPubkey: tokenAAccount, ix: createTokenAAccountIx },
|
|
7643
|
-
{ ataPubkey: tokenBAccount, ix: createTokenBAccountIx }
|
|
7644
|
-
] = yield Promise.all([
|
|
7645
|
-
getOrCreateATAInstruction(
|
|
7646
|
-
this._program.provider.connection,
|
|
7647
|
-
tokenAMint,
|
|
7648
|
-
owner,
|
|
7649
|
-
owner,
|
|
7650
|
-
true,
|
|
7651
|
-
tokenAProgram
|
|
7652
|
-
),
|
|
7653
|
-
getOrCreateATAInstruction(
|
|
7654
|
-
this._program.provider.connection,
|
|
7655
|
-
tokenBMint,
|
|
7656
|
-
owner,
|
|
7657
|
-
owner,
|
|
7658
|
-
true,
|
|
7659
|
-
tokenBProgram
|
|
7660
|
-
)
|
|
7661
|
-
]);
|
|
7662
|
-
createTokenAAccountIx && preInstructions.push(createTokenAAccountIx);
|
|
7663
|
-
createTokenBAccountIx && preInstructions.push(createTokenBAccountIx);
|
|
7664
8812
|
const postInstructions = [];
|
|
7665
8813
|
if ([tokenAMint.toBase58(), tokenBMint.toBase58()].includes(
|
|
7666
8814
|
NATIVE_MINT2.toBase58()
|
|
@@ -7668,9 +8816,9 @@ var CpAmm = class {
|
|
|
7668
8816
|
const closeWrappedSOLIx = yield unwrapSOLInstruction(owner);
|
|
7669
8817
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
7670
8818
|
}
|
|
7671
|
-
|
|
7672
|
-
poolAuthority,
|
|
8819
|
+
const claimPositionFeeInstruction = yield this.buildClaimPositionFeeInstruction({
|
|
7673
8820
|
owner,
|
|
8821
|
+
poolAuthority: this.poolAuthority,
|
|
7674
8822
|
pool,
|
|
7675
8823
|
position,
|
|
7676
8824
|
positionNftAccount,
|
|
@@ -7681,34 +8829,288 @@ var CpAmm = class {
|
|
|
7681
8829
|
tokenAMint,
|
|
7682
8830
|
tokenBMint,
|
|
7683
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,
|
|
7684
9066
|
tokenBProgram,
|
|
7685
|
-
|
|
7686
|
-
|
|
7687
|
-
|
|
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;
|
|
7688
9079
|
});
|
|
7689
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
|
+
*/
|
|
7690
9086
|
updateRewardDuration(params) {
|
|
7691
9087
|
return __async(this, null, function* () {
|
|
7692
9088
|
const { pool, admin, rewardIndex, newDuration } = params;
|
|
7693
|
-
return yield this._program.methods.updateRewardDuration(rewardIndex, newDuration).
|
|
9089
|
+
return yield this._program.methods.updateRewardDuration(rewardIndex, newDuration).accountsPartial({
|
|
7694
9090
|
pool,
|
|
7695
|
-
admin
|
|
7696
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7697
|
-
program: this._program.programId
|
|
9091
|
+
admin
|
|
7698
9092
|
}).transaction();
|
|
7699
9093
|
});
|
|
7700
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
|
+
*/
|
|
7701
9100
|
updateRewardFunder(params) {
|
|
7702
9101
|
return __async(this, null, function* () {
|
|
7703
9102
|
const { pool, admin, rewardIndex, newFunder } = params;
|
|
7704
|
-
return yield this._program.methods.updateRewardFunder(rewardIndex, newFunder).
|
|
9103
|
+
return yield this._program.methods.updateRewardFunder(rewardIndex, newFunder).accountsPartial({
|
|
7705
9104
|
pool,
|
|
7706
|
-
admin
|
|
7707
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7708
|
-
program: this._program.programId
|
|
9105
|
+
admin
|
|
7709
9106
|
}).transaction();
|
|
7710
9107
|
});
|
|
7711
9108
|
}
|
|
9109
|
+
/**
|
|
9110
|
+
* Builds a transaction to fund rewards in a pool.
|
|
9111
|
+
* @param {FundRewardParams} params - Funding parameters.
|
|
9112
|
+
* @returns Transaction builder.
|
|
9113
|
+
*/
|
|
7712
9114
|
fundReward(params) {
|
|
7713
9115
|
return __async(this, null, function* () {
|
|
7714
9116
|
const { rewardIndex, carryForward, pool, funder, amount } = params;
|
|
@@ -7734,22 +9136,24 @@ var CpAmm = class {
|
|
|
7734
9136
|
);
|
|
7735
9137
|
preInstructions.push(...wrapSOLIx);
|
|
7736
9138
|
}
|
|
7737
|
-
return yield this._program.methods.fundReward(rewardIndex, amount, carryForward).
|
|
9139
|
+
return yield this._program.methods.fundReward(rewardIndex, amount, carryForward).accountsPartial({
|
|
7738
9140
|
pool,
|
|
7739
9141
|
rewardVault: vault,
|
|
7740
9142
|
rewardMint: mint,
|
|
7741
9143
|
funderTokenAccount,
|
|
7742
9144
|
funder,
|
|
7743
|
-
tokenProgram
|
|
7744
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7745
|
-
program: this._program.programId
|
|
9145
|
+
tokenProgram
|
|
7746
9146
|
}).transaction();
|
|
7747
9147
|
});
|
|
7748
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
|
+
*/
|
|
7749
9154
|
withdrawIneligibleReward(params) {
|
|
7750
9155
|
return __async(this, null, function* () {
|
|
7751
9156
|
const { rewardIndex, pool, funder } = params;
|
|
7752
|
-
const poolAuthority = derivePoolAuthority(this._program.programId);
|
|
7753
9157
|
const poolState = yield this.fetchPoolState(pool);
|
|
7754
9158
|
const rewardInfo = poolState.rewardInfos[rewardIndex];
|
|
7755
9159
|
const { mint, vault, rewardTokenFlag } = rewardInfo;
|
|
@@ -7769,19 +9173,22 @@ var CpAmm = class {
|
|
|
7769
9173
|
const closeWrappedSOLIx = yield unwrapSOLInstruction(funder);
|
|
7770
9174
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
7771
9175
|
}
|
|
7772
|
-
return yield this._program.methods.withdrawIneligibleReward(rewardIndex).
|
|
9176
|
+
return yield this._program.methods.withdrawIneligibleReward(rewardIndex).accountsPartial({
|
|
7773
9177
|
pool,
|
|
7774
9178
|
rewardVault: vault,
|
|
7775
9179
|
rewardMint: mint,
|
|
7776
|
-
poolAuthority,
|
|
9180
|
+
poolAuthority: this.poolAuthority,
|
|
7777
9181
|
funderTokenAccount,
|
|
7778
9182
|
funder,
|
|
7779
|
-
tokenProgram
|
|
7780
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7781
|
-
program: this._program.programId
|
|
9183
|
+
tokenProgram
|
|
7782
9184
|
}).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
|
|
7783
9185
|
});
|
|
7784
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
|
+
*/
|
|
7785
9192
|
claimPartnerFee(params) {
|
|
7786
9193
|
return __async(this, null, function* () {
|
|
7787
9194
|
const { partner, pool, maxAmountA, maxAmountB } = params;
|
|
@@ -7794,33 +9201,19 @@ var CpAmm = class {
|
|
|
7794
9201
|
tokenAFlag,
|
|
7795
9202
|
tokenBFlag
|
|
7796
9203
|
} = poolState;
|
|
7797
|
-
const poolAuthority = derivePoolAuthority(this._program.programId);
|
|
7798
9204
|
const tokenAProgram = getTokenProgram(tokenAFlag);
|
|
7799
9205
|
const tokenBProgram = getTokenProgram(tokenBFlag);
|
|
7800
|
-
const
|
|
7801
|
-
|
|
7802
|
-
|
|
7803
|
-
|
|
7804
|
-
|
|
7805
|
-
|
|
7806
|
-
|
|
7807
|
-
|
|
7808
|
-
|
|
7809
|
-
|
|
7810
|
-
|
|
7811
|
-
tokenAProgram
|
|
7812
|
-
),
|
|
7813
|
-
getOrCreateATAInstruction(
|
|
7814
|
-
this._program.provider.connection,
|
|
7815
|
-
poolState.tokenBMint,
|
|
7816
|
-
partner,
|
|
7817
|
-
partner,
|
|
7818
|
-
true,
|
|
7819
|
-
tokenBProgram
|
|
7820
|
-
)
|
|
7821
|
-
]);
|
|
7822
|
-
createTokenAAccountIx && preInstructions.push(createTokenAAccountIx);
|
|
7823
|
-
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
|
+
);
|
|
7824
9217
|
const postInstructions = [];
|
|
7825
9218
|
if ([
|
|
7826
9219
|
poolState.tokenAMint.toBase58(),
|
|
@@ -7829,8 +9222,8 @@ var CpAmm = class {
|
|
|
7829
9222
|
const closeWrappedSOLIx = yield unwrapSOLInstruction(partner);
|
|
7830
9223
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
7831
9224
|
}
|
|
7832
|
-
return yield this._program.methods.claimPartnerFee(maxAmountA, maxAmountB).
|
|
7833
|
-
poolAuthority,
|
|
9225
|
+
return yield this._program.methods.claimPartnerFee(maxAmountA, maxAmountB).accountsPartial({
|
|
9226
|
+
poolAuthority: this.poolAuthority,
|
|
7834
9227
|
pool,
|
|
7835
9228
|
tokenAAccount,
|
|
7836
9229
|
tokenBAccount,
|
|
@@ -7840,22 +9233,25 @@ var CpAmm = class {
|
|
|
7840
9233
|
tokenBMint,
|
|
7841
9234
|
partner,
|
|
7842
9235
|
tokenAProgram,
|
|
7843
|
-
tokenBProgram
|
|
7844
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7845
|
-
program: this._program.programId
|
|
9236
|
+
tokenBProgram
|
|
7846
9237
|
}).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
|
|
7847
9238
|
});
|
|
7848
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
|
+
*/
|
|
7849
9245
|
claimReward(params) {
|
|
7850
9246
|
return __async(this, null, function* () {
|
|
7851
|
-
const {
|
|
7852
|
-
|
|
7853
|
-
|
|
7854
|
-
|
|
7855
|
-
|
|
7856
|
-
|
|
7857
|
-
|
|
7858
|
-
|
|
9247
|
+
const {
|
|
9248
|
+
user,
|
|
9249
|
+
position,
|
|
9250
|
+
positionNftAccount,
|
|
9251
|
+
rewardIndex,
|
|
9252
|
+
poolState,
|
|
9253
|
+
positionState
|
|
9254
|
+
} = params;
|
|
7859
9255
|
const rewardInfo = poolState.rewardInfos[rewardIndex];
|
|
7860
9256
|
const tokenProgram = getTokenProgram(rewardInfo.rewardTokenFlag);
|
|
7861
9257
|
const preInstructions = [];
|
|
@@ -7873,18 +9269,16 @@ var CpAmm = class {
|
|
|
7873
9269
|
const closeWrappedSOLIx = yield unwrapSOLInstruction(user);
|
|
7874
9270
|
closeWrappedSOLIx && postInstructions.push(closeWrappedSOLIx);
|
|
7875
9271
|
}
|
|
7876
|
-
return yield this._program.methods.claimReward(rewardIndex).
|
|
9272
|
+
return yield this._program.methods.claimReward(rewardIndex).accountsPartial({
|
|
7877
9273
|
pool: positionState.pool,
|
|
7878
9274
|
positionNftAccount,
|
|
7879
9275
|
rewardVault: rewardInfo.vault,
|
|
7880
9276
|
rewardMint: rewardInfo.mint,
|
|
7881
|
-
poolAuthority,
|
|
9277
|
+
poolAuthority: this.poolAuthority,
|
|
7882
9278
|
position,
|
|
7883
9279
|
userTokenAccount,
|
|
7884
9280
|
owner: user,
|
|
7885
|
-
tokenProgram
|
|
7886
|
-
eventAuthority: deriveEventAuthority(this._program.programId)[0],
|
|
7887
|
-
program: this._program.programId
|
|
9281
|
+
tokenProgram
|
|
7888
9282
|
}).preInstructions(preInstructions).postInstructions(postInstructions).transaction();
|
|
7889
9283
|
});
|
|
7890
9284
|
}
|
|
@@ -7896,25 +9290,36 @@ export {
|
|
|
7896
9290
|
ActivationPoint,
|
|
7897
9291
|
ActivationType,
|
|
7898
9292
|
BASIS_POINT_MAX,
|
|
9293
|
+
BIN_STEP_BPS_DEFAULT,
|
|
9294
|
+
BIN_STEP_BPS_U128_DEFAULT,
|
|
7899
9295
|
CP_AMM_PROGRAM_ID,
|
|
7900
9296
|
CollectFeeMode,
|
|
7901
9297
|
CpAmm,
|
|
9298
|
+
DYNAMIC_FEE_DECAY_PERIOD_DEFAULT,
|
|
9299
|
+
DYNAMIC_FEE_FILTER_PERIOD_DEFAULT,
|
|
9300
|
+
DYNAMIC_FEE_REDUCTION_FACTOR_DEFAULT,
|
|
7902
9301
|
FEE_DENOMINATOR,
|
|
7903
9302
|
FeeSchedulerMode,
|
|
9303
|
+
LIQUIDITY_SCALE,
|
|
7904
9304
|
MAX_CU_BUFFER,
|
|
7905
9305
|
MAX_FEE_NUMERATOR,
|
|
9306
|
+
MAX_PRICE_CHANGE_BPS_DEFAULT,
|
|
7906
9307
|
MAX_SQRT_PRICE,
|
|
7907
9308
|
MIN_CU_BUFFER,
|
|
7908
9309
|
MIN_SQRT_PRICE,
|
|
9310
|
+
ONE,
|
|
7909
9311
|
Rounding,
|
|
7910
9312
|
SCALE_OFFSET,
|
|
7911
9313
|
TradeDirection,
|
|
7912
|
-
|
|
9314
|
+
bpsToFeeNumerator,
|
|
9315
|
+
calculateInitSqrtPrice,
|
|
9316
|
+
calculateTransferFeeExcludedAmount,
|
|
9317
|
+
calculateTransferFeeIncludedAmount,
|
|
9318
|
+
decimalToQ64,
|
|
7913
9319
|
index_default as default,
|
|
7914
9320
|
deriveClaimFeeOperatorAddress,
|
|
7915
9321
|
deriveConfigAddress,
|
|
7916
9322
|
deriveCustomizablePoolAddress,
|
|
7917
|
-
deriveEventAuthority,
|
|
7918
9323
|
derivePoolAddress,
|
|
7919
9324
|
derivePoolAuthority,
|
|
7920
9325
|
derivePositionAddress,
|
|
@@ -7922,23 +9327,43 @@ export {
|
|
|
7922
9327
|
deriveRewardVaultAddress,
|
|
7923
9328
|
deriveTokenBadgeAddress,
|
|
7924
9329
|
deriveTokenVaultAddress,
|
|
9330
|
+
feeNumeratorToBps,
|
|
9331
|
+
getAllPositionNftAccountByOwner,
|
|
9332
|
+
getAllUserPositionNftAccount,
|
|
9333
|
+
getAmountAFromLiquidityDelta,
|
|
9334
|
+
getAmountBFromLiquidityDelta,
|
|
9335
|
+
getAvailableVestingLiquidity,
|
|
7925
9336
|
getBaseFeeNumerator,
|
|
7926
|
-
|
|
7927
|
-
getDeltaAmountB,
|
|
9337
|
+
getBaseFeeParams,
|
|
7928
9338
|
getDynamicFeeNumerator,
|
|
9339
|
+
getDynamicFeeParams,
|
|
7929
9340
|
getEstimatedComputeUnitIxWithBuffer,
|
|
7930
9341
|
getEstimatedComputeUnitUsageWithBuffer,
|
|
7931
9342
|
getFeeNumerator,
|
|
7932
9343
|
getFirstKey,
|
|
7933
9344
|
getLiquidityDeltaFromAmountA,
|
|
7934
9345
|
getLiquidityDeltaFromAmountB,
|
|
9346
|
+
getMaxAmountWithSlippage,
|
|
9347
|
+
getMinAmountWithSlippage,
|
|
7935
9348
|
getNextSqrtPrice,
|
|
7936
9349
|
getOrCreateATAInstruction,
|
|
9350
|
+
getPriceFromSqrtPrice,
|
|
9351
|
+
getPriceImpact,
|
|
7937
9352
|
getSecondKey,
|
|
7938
9353
|
getSimulationComputeUnits,
|
|
9354
|
+
getSqrtPriceFromPrice,
|
|
9355
|
+
getSwapAmount,
|
|
7939
9356
|
getTokenDecimals,
|
|
7940
9357
|
getTokenProgram,
|
|
9358
|
+
getTotalLockedLiquidity,
|
|
9359
|
+
getUnClaimReward,
|
|
9360
|
+
isVestingComplete,
|
|
9361
|
+
mulDiv,
|
|
9362
|
+
positionByPoolFilter,
|
|
9363
|
+
pow,
|
|
9364
|
+
q64ToDecimal,
|
|
7941
9365
|
unwrapSOLInstruction,
|
|
9366
|
+
vestingByPositionFilter,
|
|
7942
9367
|
wrapSOLInstruction
|
|
7943
9368
|
};
|
|
7944
9369
|
//# sourceMappingURL=index.mjs.map
|