@meteora-ag/cp-amm-sdk 1.0.1-rc.20 → 1.0.1-rc.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +27 -2
- package/dist/index.d.ts +27 -2
- package/dist/index.js +176 -170
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +211 -205
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -6181,6 +6181,7 @@ import { PublicKey } from "@solana/web3.js";
|
|
|
6181
6181
|
var CP_AMM_PROGRAM_ID = new PublicKey(
|
|
6182
6182
|
"cpamdpZCGKUy5JxQXB4dcpGPiikHawvSWAd6mEn1sGG"
|
|
6183
6183
|
);
|
|
6184
|
+
var LIQUIDITY_SCALE = 128;
|
|
6184
6185
|
var SCALE_OFFSET = 64;
|
|
6185
6186
|
var BASIS_POINT_MAX = 1e4;
|
|
6186
6187
|
var MAX_FEE_NUMERATOR = 5e8;
|
|
@@ -6285,6 +6286,140 @@ function deriveEventAuthority(programId) {
|
|
|
6285
6286
|
);
|
|
6286
6287
|
}
|
|
6287
6288
|
|
|
6289
|
+
// src/helpers/token.ts
|
|
6290
|
+
import {
|
|
6291
|
+
createAssociatedTokenAccountIdempotentInstruction,
|
|
6292
|
+
createCloseAccountInstruction,
|
|
6293
|
+
getAccount,
|
|
6294
|
+
getAssociatedTokenAddressSync,
|
|
6295
|
+
getMint,
|
|
6296
|
+
NATIVE_MINT,
|
|
6297
|
+
TOKEN_2022_PROGRAM_ID,
|
|
6298
|
+
TOKEN_PROGRAM_ID,
|
|
6299
|
+
TokenAccountNotFoundError,
|
|
6300
|
+
TokenInvalidAccountOwnerError
|
|
6301
|
+
} from "@solana/spl-token";
|
|
6302
|
+
import {
|
|
6303
|
+
PublicKey as PublicKey3,
|
|
6304
|
+
SystemProgram,
|
|
6305
|
+
TransactionInstruction
|
|
6306
|
+
} from "@solana/web3.js";
|
|
6307
|
+
function getTokenProgram(flag) {
|
|
6308
|
+
return flag == 0 ? TOKEN_PROGRAM_ID : TOKEN_2022_PROGRAM_ID;
|
|
6309
|
+
}
|
|
6310
|
+
var getTokenDecimals = (connection, mint) => __async(void 0, null, function* () {
|
|
6311
|
+
return (yield getMint(connection, mint)).decimals;
|
|
6312
|
+
});
|
|
6313
|
+
var getOrCreateATAInstruction = (_0, _1, _2, ..._3) => __async(void 0, [_0, _1, _2, ..._3], function* (connection, tokenMint, owner, payer = owner, allowOwnerOffCurve = true, tokenProgram) {
|
|
6314
|
+
const toAccount = getAssociatedTokenAddressSync(
|
|
6315
|
+
tokenMint,
|
|
6316
|
+
owner,
|
|
6317
|
+
allowOwnerOffCurve,
|
|
6318
|
+
tokenProgram
|
|
6319
|
+
);
|
|
6320
|
+
try {
|
|
6321
|
+
yield getAccount(connection, toAccount);
|
|
6322
|
+
return { ataPubkey: toAccount, ix: void 0 };
|
|
6323
|
+
} catch (e) {
|
|
6324
|
+
if (e instanceof TokenAccountNotFoundError || e instanceof TokenInvalidAccountOwnerError) {
|
|
6325
|
+
const ix = createAssociatedTokenAccountIdempotentInstruction(
|
|
6326
|
+
payer,
|
|
6327
|
+
toAccount,
|
|
6328
|
+
owner,
|
|
6329
|
+
tokenMint,
|
|
6330
|
+
tokenProgram
|
|
6331
|
+
);
|
|
6332
|
+
return { ataPubkey: toAccount, ix };
|
|
6333
|
+
} else {
|
|
6334
|
+
console.error("Error::getOrCreateATAInstruction", e);
|
|
6335
|
+
throw e;
|
|
6336
|
+
}
|
|
6337
|
+
}
|
|
6338
|
+
});
|
|
6339
|
+
var wrapSOLInstruction = (from, to, amount) => {
|
|
6340
|
+
return [
|
|
6341
|
+
SystemProgram.transfer({
|
|
6342
|
+
fromPubkey: from,
|
|
6343
|
+
toPubkey: to,
|
|
6344
|
+
lamports: amount
|
|
6345
|
+
}),
|
|
6346
|
+
new TransactionInstruction({
|
|
6347
|
+
keys: [
|
|
6348
|
+
{
|
|
6349
|
+
pubkey: to,
|
|
6350
|
+
isSigner: false,
|
|
6351
|
+
isWritable: true
|
|
6352
|
+
}
|
|
6353
|
+
],
|
|
6354
|
+
data: Buffer.from(new Uint8Array([17])),
|
|
6355
|
+
programId: TOKEN_PROGRAM_ID
|
|
6356
|
+
})
|
|
6357
|
+
];
|
|
6358
|
+
};
|
|
6359
|
+
var unwrapSOLInstruction = (owner, allowOwnerOffCurve = true) => __async(void 0, null, function* () {
|
|
6360
|
+
const wSolATAAccount = getAssociatedTokenAddressSync(
|
|
6361
|
+
NATIVE_MINT,
|
|
6362
|
+
owner,
|
|
6363
|
+
allowOwnerOffCurve
|
|
6364
|
+
);
|
|
6365
|
+
if (wSolATAAccount) {
|
|
6366
|
+
const closedWrappedSolInstruction = createCloseAccountInstruction(
|
|
6367
|
+
wSolATAAccount,
|
|
6368
|
+
owner,
|
|
6369
|
+
owner,
|
|
6370
|
+
[],
|
|
6371
|
+
TOKEN_PROGRAM_ID
|
|
6372
|
+
);
|
|
6373
|
+
return closedWrappedSolInstruction;
|
|
6374
|
+
}
|
|
6375
|
+
return null;
|
|
6376
|
+
});
|
|
6377
|
+
function getNftOwner(connection, nftMint) {
|
|
6378
|
+
return __async(this, null, function* () {
|
|
6379
|
+
const largesTokenAccount = yield connection.getTokenLargestAccounts(nftMint);
|
|
6380
|
+
const accountInfo = yield connection.getParsedAccountInfo(
|
|
6381
|
+
largesTokenAccount.value[0].address
|
|
6382
|
+
);
|
|
6383
|
+
const owner = new PublicKey3(accountInfo.value.data.parsed.info.owner);
|
|
6384
|
+
return new PublicKey3(owner);
|
|
6385
|
+
});
|
|
6386
|
+
}
|
|
6387
|
+
|
|
6388
|
+
// src/helpers/fee.ts
|
|
6389
|
+
import { BN as BN5 } from "@coral-xyz/anchor";
|
|
6390
|
+
|
|
6391
|
+
// src/types.ts
|
|
6392
|
+
var Rounding = /* @__PURE__ */ ((Rounding2) => {
|
|
6393
|
+
Rounding2[Rounding2["Up"] = 0] = "Up";
|
|
6394
|
+
Rounding2[Rounding2["Down"] = 1] = "Down";
|
|
6395
|
+
return Rounding2;
|
|
6396
|
+
})(Rounding || {});
|
|
6397
|
+
var ActivationPoint = /* @__PURE__ */ ((ActivationPoint2) => {
|
|
6398
|
+
ActivationPoint2[ActivationPoint2["Timestamp"] = 0] = "Timestamp";
|
|
6399
|
+
ActivationPoint2[ActivationPoint2["Slot"] = 1] = "Slot";
|
|
6400
|
+
return ActivationPoint2;
|
|
6401
|
+
})(ActivationPoint || {});
|
|
6402
|
+
var FeeSchedulerMode = /* @__PURE__ */ ((FeeSchedulerMode2) => {
|
|
6403
|
+
FeeSchedulerMode2[FeeSchedulerMode2["Linear"] = 0] = "Linear";
|
|
6404
|
+
FeeSchedulerMode2[FeeSchedulerMode2["Exponential"] = 1] = "Exponential";
|
|
6405
|
+
return FeeSchedulerMode2;
|
|
6406
|
+
})(FeeSchedulerMode || {});
|
|
6407
|
+
var CollectFeeMode = /* @__PURE__ */ ((CollectFeeMode3) => {
|
|
6408
|
+
CollectFeeMode3[CollectFeeMode3["BothToken"] = 0] = "BothToken";
|
|
6409
|
+
CollectFeeMode3[CollectFeeMode3["OnlyB"] = 1] = "OnlyB";
|
|
6410
|
+
return CollectFeeMode3;
|
|
6411
|
+
})(CollectFeeMode || {});
|
|
6412
|
+
var TradeDirection = /* @__PURE__ */ ((TradeDirection2) => {
|
|
6413
|
+
TradeDirection2[TradeDirection2["AtoB"] = 0] = "AtoB";
|
|
6414
|
+
TradeDirection2[TradeDirection2["BtoA"] = 1] = "BtoA";
|
|
6415
|
+
return TradeDirection2;
|
|
6416
|
+
})(TradeDirection || {});
|
|
6417
|
+
var ActivationType = /* @__PURE__ */ ((ActivationType2) => {
|
|
6418
|
+
ActivationType2[ActivationType2["Slot"] = 0] = "Slot";
|
|
6419
|
+
ActivationType2[ActivationType2["Timestamp"] = 1] = "Timestamp";
|
|
6420
|
+
return ActivationType2;
|
|
6421
|
+
})(ActivationType || {});
|
|
6422
|
+
|
|
6288
6423
|
// src/math/feeMath.ts
|
|
6289
6424
|
import { BN as BN2 } from "@coral-xyz/anchor";
|
|
6290
6425
|
var MAX_EXPONENTIAL = new BN2(524288);
|
|
@@ -6392,40 +6527,6 @@ function pow(base, exp) {
|
|
|
6392
6527
|
// src/math/mathUtils.ts
|
|
6393
6528
|
import { BN as BN3 } from "@coral-xyz/anchor";
|
|
6394
6529
|
import Decimal from "decimal.js";
|
|
6395
|
-
|
|
6396
|
-
// src/types.ts
|
|
6397
|
-
var Rounding = /* @__PURE__ */ ((Rounding2) => {
|
|
6398
|
-
Rounding2[Rounding2["Up"] = 0] = "Up";
|
|
6399
|
-
Rounding2[Rounding2["Down"] = 1] = "Down";
|
|
6400
|
-
return Rounding2;
|
|
6401
|
-
})(Rounding || {});
|
|
6402
|
-
var ActivationPoint = /* @__PURE__ */ ((ActivationPoint2) => {
|
|
6403
|
-
ActivationPoint2[ActivationPoint2["Timestamp"] = 0] = "Timestamp";
|
|
6404
|
-
ActivationPoint2[ActivationPoint2["Slot"] = 1] = "Slot";
|
|
6405
|
-
return ActivationPoint2;
|
|
6406
|
-
})(ActivationPoint || {});
|
|
6407
|
-
var FeeSchedulerMode = /* @__PURE__ */ ((FeeSchedulerMode2) => {
|
|
6408
|
-
FeeSchedulerMode2[FeeSchedulerMode2["Linear"] = 0] = "Linear";
|
|
6409
|
-
FeeSchedulerMode2[FeeSchedulerMode2["Exponential"] = 1] = "Exponential";
|
|
6410
|
-
return FeeSchedulerMode2;
|
|
6411
|
-
})(FeeSchedulerMode || {});
|
|
6412
|
-
var CollectFeeMode = /* @__PURE__ */ ((CollectFeeMode3) => {
|
|
6413
|
-
CollectFeeMode3[CollectFeeMode3["BothToken"] = 0] = "BothToken";
|
|
6414
|
-
CollectFeeMode3[CollectFeeMode3["OnlyB"] = 1] = "OnlyB";
|
|
6415
|
-
return CollectFeeMode3;
|
|
6416
|
-
})(CollectFeeMode || {});
|
|
6417
|
-
var TradeDirection = /* @__PURE__ */ ((TradeDirection3) => {
|
|
6418
|
-
TradeDirection3[TradeDirection3["AtoB"] = 0] = "AtoB";
|
|
6419
|
-
TradeDirection3[TradeDirection3["BtoA"] = 1] = "BtoA";
|
|
6420
|
-
return TradeDirection3;
|
|
6421
|
-
})(TradeDirection || {});
|
|
6422
|
-
var ActivationType = /* @__PURE__ */ ((ActivationType2) => {
|
|
6423
|
-
ActivationType2[ActivationType2["Slot"] = 0] = "Slot";
|
|
6424
|
-
ActivationType2[ActivationType2["Timestamp"] = 1] = "Timestamp";
|
|
6425
|
-
return ActivationType2;
|
|
6426
|
-
})(ActivationType || {});
|
|
6427
|
-
|
|
6428
|
-
// src/math/mathUtils.ts
|
|
6429
6530
|
function mulShr(x, y, offset, rounding) {
|
|
6430
6531
|
const denominator = new BN3(1).shln(offset);
|
|
6431
6532
|
return mulDiv(x, y, denominator, rounding);
|
|
@@ -6454,141 +6555,9 @@ function decimalToQ64(num) {
|
|
|
6454
6555
|
return new BN3(num.mul(Decimal.pow(2, 64)).floor().toFixed());
|
|
6455
6556
|
}
|
|
6456
6557
|
|
|
6457
|
-
// src/helpers/
|
|
6558
|
+
// src/helpers/curve.ts
|
|
6458
6559
|
import { BN as BN4 } from "@coral-xyz/anchor";
|
|
6459
6560
|
import Decimal2 from "decimal.js";
|
|
6460
|
-
function calculateInitSqrtPrice(tokenAAmount, tokenBAmount, minSqrtPrice, maxSqrtPrice) {
|
|
6461
|
-
if (tokenAAmount.isZero() || tokenBAmount.isZero()) {
|
|
6462
|
-
throw new Error("Amount cannot be zero");
|
|
6463
|
-
}
|
|
6464
|
-
const amountADecimal = new Decimal2(tokenAAmount.toString());
|
|
6465
|
-
const amountBDecimal = new Decimal2(tokenBAmount.toString());
|
|
6466
|
-
const minSqrtPriceDecimal = new Decimal2(minSqrtPrice.toString()).div(
|
|
6467
|
-
Decimal2.pow(2, 64)
|
|
6468
|
-
);
|
|
6469
|
-
const maxSqrtPriceDecimal = new Decimal2(maxSqrtPrice.toString()).div(
|
|
6470
|
-
Decimal2.pow(2, 64)
|
|
6471
|
-
);
|
|
6472
|
-
const x = new Decimal2(1).div(maxSqrtPriceDecimal);
|
|
6473
|
-
const y = amountBDecimal.div(amountADecimal);
|
|
6474
|
-
const xy = x.mul(y);
|
|
6475
|
-
const paMinusXY = minSqrtPriceDecimal.sub(xy);
|
|
6476
|
-
const xyMinusPa = xy.sub(minSqrtPriceDecimal);
|
|
6477
|
-
const fourY = new Decimal2(4).mul(y);
|
|
6478
|
-
const discriminant = xyMinusPa.mul(xyMinusPa).add(fourY);
|
|
6479
|
-
if (discriminant.isNeg()) {
|
|
6480
|
-
throw new Error("Calculate sqrt price failed: negative discriminant");
|
|
6481
|
-
}
|
|
6482
|
-
const sqrtDiscriminant = discriminant.sqrt();
|
|
6483
|
-
const result = paMinusXY.add(sqrtDiscriminant).div(new Decimal2(2)).mul(Decimal2.pow(2, 64));
|
|
6484
|
-
return new BN4(result.floor().toFixed());
|
|
6485
|
-
}
|
|
6486
|
-
|
|
6487
|
-
// src/helpers/token.ts
|
|
6488
|
-
import {
|
|
6489
|
-
createAssociatedTokenAccountIdempotentInstruction,
|
|
6490
|
-
createCloseAccountInstruction,
|
|
6491
|
-
getAccount,
|
|
6492
|
-
getAssociatedTokenAddressSync,
|
|
6493
|
-
getMint,
|
|
6494
|
-
NATIVE_MINT,
|
|
6495
|
-
TOKEN_2022_PROGRAM_ID,
|
|
6496
|
-
TOKEN_PROGRAM_ID,
|
|
6497
|
-
TokenAccountNotFoundError,
|
|
6498
|
-
TokenInvalidAccountOwnerError
|
|
6499
|
-
} from "@solana/spl-token";
|
|
6500
|
-
import {
|
|
6501
|
-
PublicKey as PublicKey3,
|
|
6502
|
-
SystemProgram,
|
|
6503
|
-
TransactionInstruction
|
|
6504
|
-
} from "@solana/web3.js";
|
|
6505
|
-
function getTokenProgram(flag) {
|
|
6506
|
-
return flag == 0 ? TOKEN_PROGRAM_ID : TOKEN_2022_PROGRAM_ID;
|
|
6507
|
-
}
|
|
6508
|
-
var getTokenDecimals = (connection, mint) => __async(void 0, null, function* () {
|
|
6509
|
-
return (yield getMint(connection, mint)).decimals;
|
|
6510
|
-
});
|
|
6511
|
-
var getOrCreateATAInstruction = (_0, _1, _2, ..._3) => __async(void 0, [_0, _1, _2, ..._3], function* (connection, tokenMint, owner, payer = owner, allowOwnerOffCurve = true, tokenProgram) {
|
|
6512
|
-
const toAccount = getAssociatedTokenAddressSync(
|
|
6513
|
-
tokenMint,
|
|
6514
|
-
owner,
|
|
6515
|
-
allowOwnerOffCurve,
|
|
6516
|
-
tokenProgram
|
|
6517
|
-
);
|
|
6518
|
-
try {
|
|
6519
|
-
yield getAccount(connection, toAccount);
|
|
6520
|
-
return { ataPubkey: toAccount, ix: void 0 };
|
|
6521
|
-
} catch (e) {
|
|
6522
|
-
if (e instanceof TokenAccountNotFoundError || e instanceof TokenInvalidAccountOwnerError) {
|
|
6523
|
-
const ix = createAssociatedTokenAccountIdempotentInstruction(
|
|
6524
|
-
payer,
|
|
6525
|
-
toAccount,
|
|
6526
|
-
owner,
|
|
6527
|
-
tokenMint,
|
|
6528
|
-
tokenProgram
|
|
6529
|
-
);
|
|
6530
|
-
return { ataPubkey: toAccount, ix };
|
|
6531
|
-
} else {
|
|
6532
|
-
console.error("Error::getOrCreateATAInstruction", e);
|
|
6533
|
-
throw e;
|
|
6534
|
-
}
|
|
6535
|
-
}
|
|
6536
|
-
});
|
|
6537
|
-
var wrapSOLInstruction = (from, to, amount) => {
|
|
6538
|
-
return [
|
|
6539
|
-
SystemProgram.transfer({
|
|
6540
|
-
fromPubkey: from,
|
|
6541
|
-
toPubkey: to,
|
|
6542
|
-
lamports: amount
|
|
6543
|
-
}),
|
|
6544
|
-
new TransactionInstruction({
|
|
6545
|
-
keys: [
|
|
6546
|
-
{
|
|
6547
|
-
pubkey: to,
|
|
6548
|
-
isSigner: false,
|
|
6549
|
-
isWritable: true
|
|
6550
|
-
}
|
|
6551
|
-
],
|
|
6552
|
-
data: Buffer.from(new Uint8Array([17])),
|
|
6553
|
-
programId: TOKEN_PROGRAM_ID
|
|
6554
|
-
})
|
|
6555
|
-
];
|
|
6556
|
-
};
|
|
6557
|
-
var unwrapSOLInstruction = (owner, allowOwnerOffCurve = true) => __async(void 0, null, function* () {
|
|
6558
|
-
const wSolATAAccount = getAssociatedTokenAddressSync(
|
|
6559
|
-
NATIVE_MINT,
|
|
6560
|
-
owner,
|
|
6561
|
-
allowOwnerOffCurve
|
|
6562
|
-
);
|
|
6563
|
-
if (wSolATAAccount) {
|
|
6564
|
-
const closedWrappedSolInstruction = createCloseAccountInstruction(
|
|
6565
|
-
wSolATAAccount,
|
|
6566
|
-
owner,
|
|
6567
|
-
owner,
|
|
6568
|
-
[],
|
|
6569
|
-
TOKEN_PROGRAM_ID
|
|
6570
|
-
);
|
|
6571
|
-
return closedWrappedSolInstruction;
|
|
6572
|
-
}
|
|
6573
|
-
return null;
|
|
6574
|
-
});
|
|
6575
|
-
function getNftOwner(connection, nftMint) {
|
|
6576
|
-
return __async(this, null, function* () {
|
|
6577
|
-
const largesTokenAccount = yield connection.getTokenLargestAccounts(nftMint);
|
|
6578
|
-
const accountInfo = yield connection.getParsedAccountInfo(
|
|
6579
|
-
largesTokenAccount.value[0].address
|
|
6580
|
-
);
|
|
6581
|
-
const owner = new PublicKey3(accountInfo.value.data.parsed.info.owner);
|
|
6582
|
-
return new PublicKey3(owner);
|
|
6583
|
-
});
|
|
6584
|
-
}
|
|
6585
|
-
|
|
6586
|
-
// src/helpers/fee.ts
|
|
6587
|
-
import { BN as BN6 } from "@coral-xyz/anchor";
|
|
6588
|
-
|
|
6589
|
-
// src/helpers/curve.ts
|
|
6590
|
-
import { BN as BN5 } from "@coral-xyz/anchor";
|
|
6591
|
-
import Decimal3 from "decimal.js";
|
|
6592
6561
|
function getNextSqrtPrice(amount, sqrtPrice, liquidity, aToB) {
|
|
6593
6562
|
let result;
|
|
6594
6563
|
if (aToB) {
|
|
@@ -6613,7 +6582,7 @@ function getDeltaAmountB(lowerSqrtPrice, upperSqrtPrice, liquidity, rounding) {
|
|
|
6613
6582
|
const prod = liquidity.mul(deltaSqrtPrice);
|
|
6614
6583
|
let result;
|
|
6615
6584
|
if (rounding == 0 /* Up */) {
|
|
6616
|
-
const denominator = new
|
|
6585
|
+
const denominator = new BN4(1).shln(SCALE_OFFSET * 2);
|
|
6617
6586
|
result = divCeil(prod, denominator);
|
|
6618
6587
|
} else {
|
|
6619
6588
|
result = prod.shrn(SCALE_OFFSET * 2);
|
|
@@ -6621,45 +6590,45 @@ function getDeltaAmountB(lowerSqrtPrice, upperSqrtPrice, liquidity, rounding) {
|
|
|
6621
6590
|
return result;
|
|
6622
6591
|
}
|
|
6623
6592
|
function getLiquidityDeltaFromAmountA(maxAmountA, lowerSqrtPrice, upperSqrtPrice) {
|
|
6624
|
-
const prod = new
|
|
6593
|
+
const prod = new Decimal2(
|
|
6625
6594
|
maxAmountA.mul(upperSqrtPrice.mul(lowerSqrtPrice)).toString()
|
|
6626
6595
|
);
|
|
6627
|
-
const delta = new
|
|
6628
|
-
return new
|
|
6596
|
+
const delta = new Decimal2(upperSqrtPrice.sub(lowerSqrtPrice).toString());
|
|
6597
|
+
return new BN4(prod.div(delta).floor().toFixed());
|
|
6629
6598
|
}
|
|
6630
6599
|
function getLiquidityDeltaFromAmountB(maxAmountB, lowerSqrtPrice, upperSqrtPrice) {
|
|
6631
|
-
const denominator = new
|
|
6600
|
+
const denominator = new Decimal2(
|
|
6632
6601
|
upperSqrtPrice.sub(lowerSqrtPrice).toString()
|
|
6633
6602
|
);
|
|
6634
|
-
const prod = new
|
|
6635
|
-
|
|
6603
|
+
const prod = new Decimal2(maxAmountB.toString()).mul(
|
|
6604
|
+
Decimal2.pow(2, SCALE_OFFSET * 2)
|
|
6636
6605
|
);
|
|
6637
|
-
return new
|
|
6606
|
+
return new BN4(prod.div(denominator).floor().toFixed());
|
|
6638
6607
|
}
|
|
6639
6608
|
function getAmountAFromLiquidityDelta(liquidity, currentSqrtPrice, maxSqrtPrice) {
|
|
6640
|
-
const prod = new
|
|
6641
|
-
new
|
|
6609
|
+
const prod = new Decimal2(liquidity.toString()).mul(
|
|
6610
|
+
new Decimal2(maxSqrtPrice.sub(currentSqrtPrice).toString())
|
|
6642
6611
|
);
|
|
6643
6612
|
const denominator = currentSqrtPrice.mul(maxSqrtPrice);
|
|
6644
|
-
const result = prod.mul(
|
|
6645
|
-
return result.div(
|
|
6613
|
+
const result = prod.mul(Decimal2.pow(2, 64)).div(new Decimal2(denominator.toString()));
|
|
6614
|
+
return result.div(Decimal2.pow(2, 64)).floor().toFixed();
|
|
6646
6615
|
}
|
|
6647
6616
|
function getAmountBFromLiquidityDelta(liquidity, currentSqrtPrice, minSqrtPrice) {
|
|
6648
6617
|
const delta = currentSqrtPrice.sub(minSqrtPrice);
|
|
6649
|
-
const prod = new
|
|
6650
|
-
new
|
|
6618
|
+
const prod = new Decimal2(liquidity.toString()).mul(
|
|
6619
|
+
new Decimal2(delta.toString())
|
|
6651
6620
|
);
|
|
6652
|
-
return prod.div(
|
|
6621
|
+
return prod.div(Decimal2.pow(2, 128)).floor().toFixed();
|
|
6653
6622
|
}
|
|
6654
6623
|
|
|
6655
6624
|
// src/helpers/fee.ts
|
|
6656
|
-
import
|
|
6625
|
+
import Decimal3 from "decimal.js";
|
|
6657
6626
|
function getBaseFeeNumerator(feeSchedulerMode, cliffFeeNumerator, period, reductionFactor) {
|
|
6658
6627
|
let feeNumerator;
|
|
6659
6628
|
if (feeSchedulerMode == 0 /* Linear */) {
|
|
6660
6629
|
feeNumerator = cliffFeeNumerator.sub(period.mul(reductionFactor));
|
|
6661
6630
|
} else {
|
|
6662
|
-
const bps = reductionFactor.shln(SCALE_OFFSET).div(new
|
|
6631
|
+
const bps = reductionFactor.shln(SCALE_OFFSET).div(new BN5(BASIS_POINT_MAX));
|
|
6663
6632
|
const base = ONE.sub(bps);
|
|
6664
6633
|
const result = pow(base, period);
|
|
6665
6634
|
feeNumerator = cliffFeeNumerator.mul(result).shrn(SCALE_OFFSET);
|
|
@@ -6667,12 +6636,12 @@ function getBaseFeeNumerator(feeSchedulerMode, cliffFeeNumerator, period, reduct
|
|
|
6667
6636
|
return feeNumerator;
|
|
6668
6637
|
}
|
|
6669
6638
|
function getDynamicFeeNumerator(volatilityAccumulator, binStep, variableFeeControl) {
|
|
6670
|
-
const volatilityAccumulatorDecimal = new
|
|
6639
|
+
const volatilityAccumulatorDecimal = new Decimal3(
|
|
6671
6640
|
volatilityAccumulator.toString()
|
|
6672
|
-
).div(
|
|
6673
|
-
const squareVfaBin = volatilityAccumulatorDecimal.mul(new
|
|
6674
|
-
const vFee = squareVfaBin.mul(new
|
|
6675
|
-
return new
|
|
6641
|
+
).div(Decimal3.pow(2, 64));
|
|
6642
|
+
const squareVfaBin = volatilityAccumulatorDecimal.mul(new Decimal3(binStep.toString())).pow(2);
|
|
6643
|
+
const vFee = squareVfaBin.mul(new Decimal3(variableFeeControl.toString()));
|
|
6644
|
+
return new BN5(
|
|
6676
6645
|
vFee.add(99999999999).div(1e11).floor().toFixed()
|
|
6677
6646
|
);
|
|
6678
6647
|
}
|
|
@@ -6680,9 +6649,9 @@ function getFeeNumerator(currentPoint, activationPoint, numberOfPeriod, periodFr
|
|
|
6680
6649
|
if (Number(periodFrequency) == 0) {
|
|
6681
6650
|
return cliffFeeNumerator;
|
|
6682
6651
|
}
|
|
6683
|
-
const period = new
|
|
6684
|
-
new
|
|
6685
|
-
new
|
|
6652
|
+
const period = new BN5(currentPoint).lt(activationPoint) ? new BN5(numberOfPeriod) : BN5.min(
|
|
6653
|
+
new BN5(numberOfPeriod),
|
|
6654
|
+
new BN5(currentPoint).sub(activationPoint).div(periodFrequency)
|
|
6686
6655
|
);
|
|
6687
6656
|
let feeNumerator = getBaseFeeNumerator(
|
|
6688
6657
|
feeSchedulerMode,
|
|
@@ -6699,7 +6668,7 @@ function getFeeNumerator(currentPoint, activationPoint, numberOfPeriod, periodFr
|
|
|
6699
6668
|
);
|
|
6700
6669
|
feeNumerator.add(dynamicFeeNumberator);
|
|
6701
6670
|
}
|
|
6702
|
-
return feeNumerator.gt(new
|
|
6671
|
+
return feeNumerator.gt(new BN5(MAX_FEE_NUMERATOR)) ? new BN5(MAX_FEE_NUMERATOR) : feeNumerator;
|
|
6703
6672
|
}
|
|
6704
6673
|
function getFeeMode(collectFeeMode, btoA) {
|
|
6705
6674
|
const feeOnInput = btoA && collectFeeMode === 1 /* OnlyB */;
|
|
@@ -6713,14 +6682,14 @@ function getTotalFeeOnAmount(amount, tradeFeeNumerator) {
|
|
|
6713
6682
|
return mulDiv(
|
|
6714
6683
|
amount,
|
|
6715
6684
|
tradeFeeNumerator,
|
|
6716
|
-
new
|
|
6685
|
+
new BN5(FEE_DENOMINATOR),
|
|
6717
6686
|
0 /* Up */
|
|
6718
6687
|
);
|
|
6719
6688
|
}
|
|
6720
6689
|
function getSwapAmount(inAmount, sqrtPrice, liquidity, tradeFeeNumerator, aToB, collectFeeMode) {
|
|
6721
6690
|
let feeMode = getFeeMode(collectFeeMode, !aToB);
|
|
6722
6691
|
let actualInAmount = inAmount;
|
|
6723
|
-
let totalFee = new
|
|
6692
|
+
let totalFee = new BN5(0);
|
|
6724
6693
|
if (feeMode.feeOnInput) {
|
|
6725
6694
|
totalFee = getTotalFeeOnAmount(inAmount, tradeFeeNumerator);
|
|
6726
6695
|
actualInAmount = inAmount.sub(totalFee);
|
|
@@ -6816,29 +6785,38 @@ var getEstimatedComputeUnitIxWithBuffer = (connection, instructions, feePayer, b
|
|
|
6816
6785
|
});
|
|
6817
6786
|
|
|
6818
6787
|
// src/helpers/utils.ts
|
|
6819
|
-
import { BN as
|
|
6820
|
-
import
|
|
6788
|
+
import { BN as BN6 } from "@coral-xyz/anchor";
|
|
6789
|
+
import Decimal4 from "decimal.js";
|
|
6821
6790
|
var getMaxAmountWithSlippage = (amount, rate) => {
|
|
6822
6791
|
const slippage = (100 + rate) / 100 * BASIS_POINT_MAX;
|
|
6823
|
-
return amount.mul(new
|
|
6792
|
+
return amount.mul(new BN6(slippage)).div(new BN6(BASIS_POINT_MAX));
|
|
6824
6793
|
};
|
|
6825
6794
|
var getMinAmountWithSlippage = (amount, rate) => {
|
|
6826
6795
|
const slippage = (100 - rate) / 100 * BASIS_POINT_MAX;
|
|
6827
|
-
return amount.mul(new
|
|
6796
|
+
return amount.mul(new BN6(slippage)).div(new BN6(BASIS_POINT_MAX));
|
|
6828
6797
|
};
|
|
6829
6798
|
var getPriceImpact = (actualAmount, idealAmount) => {
|
|
6830
6799
|
const diff = idealAmount.sub(actualAmount);
|
|
6831
|
-
return new
|
|
6800
|
+
return new Decimal4(diff.toString()).div(new Decimal4(idealAmount.toString())).mul(100).toNumber();
|
|
6832
6801
|
};
|
|
6833
6802
|
var getCurrentPrice = (sqrtPrice, tokenADecimal, tokenBDecimal) => {
|
|
6834
|
-
const decimalSqrtPrice = new
|
|
6835
|
-
const price = decimalSqrtPrice.mul(decimalSqrtPrice).mul(new
|
|
6803
|
+
const decimalSqrtPrice = new Decimal4(sqrtPrice.toString());
|
|
6804
|
+
const price = decimalSqrtPrice.mul(decimalSqrtPrice).mul(new Decimal4(__pow(10, tokenADecimal - tokenBDecimal))).div(Decimal4.pow(2, 128)).toString();
|
|
6836
6805
|
return price;
|
|
6837
6806
|
};
|
|
6838
|
-
var getUnClaimReward = (positionState) => {
|
|
6807
|
+
var getUnClaimReward = (poolState, positionState) => {
|
|
6808
|
+
const totalPositionLiquidity = positionState.unlockedLiquidity.add(positionState.vestedLiquidity).add(positionState.permanentLockedLiquidity);
|
|
6809
|
+
const feeAPerTokenStored = new BN6(
|
|
6810
|
+
Buffer.from(poolState.feeAPerLiquidity).reverse()
|
|
6811
|
+
).sub(new BN6(Buffer.from(positionState.feeAPerTokenCheckpoint).reverse()));
|
|
6812
|
+
const feeBPerTokenStored = new BN6(
|
|
6813
|
+
Buffer.from(poolState.feeBPerLiquidity).reverse()
|
|
6814
|
+
).sub(new BN6(Buffer.from(positionState.feeBPerTokenCheckpoint).reverse()));
|
|
6815
|
+
const feeA = totalPositionLiquidity.mul(feeAPerTokenStored).shrn(LIQUIDITY_SCALE);
|
|
6816
|
+
const feeB = totalPositionLiquidity.mul(feeBPerTokenStored).shrn(LIQUIDITY_SCALE);
|
|
6839
6817
|
return {
|
|
6840
|
-
feeTokenA: positionState.feeAPending,
|
|
6841
|
-
feeTokenB: positionState.feeBPending,
|
|
6818
|
+
feeTokenA: positionState.feeAPending.add(feeA),
|
|
6819
|
+
feeTokenB: positionState.feeBPending.add(feeB),
|
|
6842
6820
|
rewards: positionState.rewardInfos.length > 0 ? positionState.rewardInfos.map((item) => item.rewardPendings) : []
|
|
6843
6821
|
};
|
|
6844
6822
|
};
|
|
@@ -6861,6 +6839,33 @@ var vestingByPositionFilter = (position) => {
|
|
|
6861
6839
|
};
|
|
6862
6840
|
};
|
|
6863
6841
|
|
|
6842
|
+
// src/helpers/priceMath.ts
|
|
6843
|
+
import { BN as BN7 } from "@coral-xyz/anchor";
|
|
6844
|
+
import Decimal5 from "decimal.js";
|
|
6845
|
+
function calculateInitSqrtPrice(tokenAAmount, tokenBAmount, minSqrtPrice, maxSqrtPrice) {
|
|
6846
|
+
if (tokenAAmount.isZero() || tokenBAmount.isZero()) {
|
|
6847
|
+
throw new Error("Amount cannot be zero");
|
|
6848
|
+
}
|
|
6849
|
+
const amountADecimal = new Decimal5(tokenAAmount.toString());
|
|
6850
|
+
const amountBDecimal = new Decimal5(tokenBAmount.toString());
|
|
6851
|
+
const minSqrtPriceDecimal = new Decimal5(minSqrtPrice.toString()).div(
|
|
6852
|
+
Decimal5.pow(2, 64)
|
|
6853
|
+
);
|
|
6854
|
+
const maxSqrtPriceDecimal = new Decimal5(maxSqrtPrice.toString()).div(
|
|
6855
|
+
Decimal5.pow(2, 64)
|
|
6856
|
+
);
|
|
6857
|
+
const x = new Decimal5(1).div(maxSqrtPriceDecimal);
|
|
6858
|
+
const y = amountBDecimal.div(amountADecimal);
|
|
6859
|
+
const xy = x.mul(y);
|
|
6860
|
+
const paMinusXY = minSqrtPriceDecimal.sub(xy);
|
|
6861
|
+
const xyMinusPa = xy.sub(minSqrtPriceDecimal);
|
|
6862
|
+
const fourY = new Decimal5(4).mul(y);
|
|
6863
|
+
const discriminant = xyMinusPa.mul(xyMinusPa).add(fourY);
|
|
6864
|
+
const sqrtDiscriminant = discriminant.sqrt();
|
|
6865
|
+
const result = paMinusXY.add(sqrtDiscriminant).div(new Decimal5(2)).mul(Decimal5.pow(2, 64));
|
|
6866
|
+
return new BN7(result.floor().toFixed());
|
|
6867
|
+
}
|
|
6868
|
+
|
|
6864
6869
|
// src/CpAmm.ts
|
|
6865
6870
|
var CpAmm = class {
|
|
6866
6871
|
constructor(connection) {
|
|
@@ -8114,6 +8119,7 @@ export {
|
|
|
8114
8119
|
CpAmm,
|
|
8115
8120
|
FEE_DENOMINATOR,
|
|
8116
8121
|
FeeSchedulerMode,
|
|
8122
|
+
LIQUIDITY_SCALE,
|
|
8117
8123
|
MAX_CU_BUFFER,
|
|
8118
8124
|
MAX_FEE_NUMERATOR,
|
|
8119
8125
|
MAX_SQRT_PRICE,
|