@crypticdot/defituna-client 3.1.19 → 3.2.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/dist/index.d.mts +5 -116
- package/dist/index.d.ts +5 -116
- package/dist/index.js +28 -277
- package/dist/index.mjs +20 -271
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -255,6 +255,7 @@ __export(index_exports, {
|
|
|
255
255
|
fetchTunaLpPosition: () => fetchTunaLpPosition,
|
|
256
256
|
fetchTunaSpotPosition: () => fetchTunaSpotPosition,
|
|
257
257
|
fetchVault: () => fetchVault,
|
|
258
|
+
fromPoolTokenFacade: () => fromPoolTokenFacade,
|
|
258
259
|
getAccountsTypeCodec: () => getAccountsTypeCodec,
|
|
259
260
|
getAccountsTypeDecoder: () => getAccountsTypeDecoder,
|
|
260
261
|
getAccountsTypeEncoder: () => getAccountsTypeEncoder,
|
|
@@ -325,7 +326,6 @@ __export(index_exports, {
|
|
|
325
326
|
getCreateVaultInstructionDataCodec: () => getCreateVaultInstructionDataCodec,
|
|
326
327
|
getCreateVaultInstructionDataDecoder: () => getCreateVaultInstructionDataDecoder,
|
|
327
328
|
getCreateVaultInstructionDataEncoder: () => getCreateVaultInstructionDataEncoder,
|
|
328
|
-
getDecreaseSpotPositionQuote: () => getDecreaseSpotPositionQuote,
|
|
329
329
|
getDecreaseTunaLpPositionFusionDiscriminatorBytes: () => getDecreaseTunaLpPositionFusionDiscriminatorBytes,
|
|
330
330
|
getDecreaseTunaLpPositionFusionInstruction: () => getDecreaseTunaLpPositionFusionInstruction,
|
|
331
331
|
getDecreaseTunaLpPositionFusionInstructionDataCodec: () => getDecreaseTunaLpPositionFusionInstructionDataCodec,
|
|
@@ -352,7 +352,6 @@ __export(index_exports, {
|
|
|
352
352
|
getDepositInstructionDataDecoder: () => getDepositInstructionDataDecoder,
|
|
353
353
|
getDepositInstructionDataEncoder: () => getDepositInstructionDataEncoder,
|
|
354
354
|
getIncreaseLpPositionQuote: () => getIncreaseLpPositionQuote,
|
|
355
|
-
getIncreaseSpotPositionQuote: () => getIncreaseSpotPositionQuote,
|
|
356
355
|
getIncreaseTunaLpPositionFusionDiscriminatorBytes: () => getIncreaseTunaLpPositionFusionDiscriminatorBytes,
|
|
357
356
|
getIncreaseTunaLpPositionFusionInstruction: () => getIncreaseTunaLpPositionFusionInstruction,
|
|
358
357
|
getIncreaseTunaLpPositionFusionInstructionDataCodec: () => getIncreaseTunaLpPositionFusionInstructionDataCodec,
|
|
@@ -400,7 +399,6 @@ __export(index_exports, {
|
|
|
400
399
|
getLiquidateTunaSpotPositionOrcaInstructionDataCodec: () => getLiquidateTunaSpotPositionOrcaInstructionDataCodec,
|
|
401
400
|
getLiquidateTunaSpotPositionOrcaInstructionDataDecoder: () => getLiquidateTunaSpotPositionOrcaInstructionDataDecoder,
|
|
402
401
|
getLiquidateTunaSpotPositionOrcaInstructionDataEncoder: () => getLiquidateTunaSpotPositionOrcaInstructionDataEncoder,
|
|
403
|
-
getLiquidationPrice: () => getLiquidationPrice,
|
|
404
402
|
getMarketAddress: () => getMarketAddress,
|
|
405
403
|
getMarketCodec: () => getMarketCodec,
|
|
406
404
|
getMarketDecoder: () => getMarketDecoder,
|
|
@@ -550,7 +548,6 @@ __export(index_exports, {
|
|
|
550
548
|
getSetTunaSpotPositionLimitOrdersInstructionDataCodec: () => getSetTunaSpotPositionLimitOrdersInstructionDataCodec,
|
|
551
549
|
getSetTunaSpotPositionLimitOrdersInstructionDataDecoder: () => getSetTunaSpotPositionLimitOrdersInstructionDataDecoder,
|
|
552
550
|
getSetTunaSpotPositionLimitOrdersInstructionDataEncoder: () => getSetTunaSpotPositionLimitOrdersInstructionDataEncoder,
|
|
553
|
-
getTradableAmount: () => getTradableAmount,
|
|
554
551
|
getTunaConfigAddress: () => getTunaConfigAddress,
|
|
555
552
|
getTunaConfigCodec: () => getTunaConfigCodec,
|
|
556
553
|
getTunaConfigDecoder: () => getTunaConfigDecoder,
|
|
@@ -696,6 +693,7 @@ __export(index_exports, {
|
|
|
696
693
|
resetTunaSpotPositionInstruction: () => resetTunaSpotPositionInstruction,
|
|
697
694
|
setTunaLpPositionLimitOrdersInstruction: () => setTunaLpPositionLimitOrdersInstruction,
|
|
698
695
|
setTunaSpotPositionLimitOrdersInstruction: () => setTunaSpotPositionLimitOrdersInstruction,
|
|
696
|
+
toPoolTokenFacade: () => toPoolTokenFacade,
|
|
699
697
|
tunaLpPositionAuthorityFilter: () => tunaLpPositionAuthorityFilter,
|
|
700
698
|
tunaLpPositionMarketMakerFilter: () => tunaLpPositionMarketMakerFilter,
|
|
701
699
|
tunaLpPositionMintAFilter: () => tunaLpPositionMintAFilter,
|
|
@@ -10273,269 +10271,6 @@ function calculateProtocolFee(collateralAmount, borrowAmount, protocolFeeRateOnC
|
|
|
10273
10271
|
return collateralAmount * BigInt(protocolFeeRateOnCollateral) / BigInt(HUNDRED_PERCENT) + borrowAmount * BigInt(protocolFeeRate) / BigInt(HUNDRED_PERCENT);
|
|
10274
10272
|
}
|
|
10275
10273
|
|
|
10276
|
-
// src/utils/spotPositionMath.ts
|
|
10277
|
-
var import_fusionamm_core4 = require("@crypticdot/fusionamm-core");
|
|
10278
|
-
function getIncreaseSpotPositionQuote(args) {
|
|
10279
|
-
const {
|
|
10280
|
-
fusionPool,
|
|
10281
|
-
tickArrays,
|
|
10282
|
-
leverage,
|
|
10283
|
-
protocolFeeRate,
|
|
10284
|
-
protocolFeeRateOnCollateral,
|
|
10285
|
-
increaseAmount,
|
|
10286
|
-
collateralToken,
|
|
10287
|
-
positionToken
|
|
10288
|
-
} = args;
|
|
10289
|
-
if (leverage < 1) {
|
|
10290
|
-
throw new Error("leverage must be greater or equal than 1.0");
|
|
10291
|
-
}
|
|
10292
|
-
if (protocolFeeRate < 0 || protocolFeeRate >= HUNDRED_PERCENT) {
|
|
10293
|
-
throw new Error("protocolFeeRate must be greater or equal than zero and less than HUNDRED_PERCENT");
|
|
10294
|
-
}
|
|
10295
|
-
if (protocolFeeRateOnCollateral < 0 || protocolFeeRateOnCollateral >= HUNDRED_PERCENT) {
|
|
10296
|
-
throw new Error("protocolFeeRate must be greater or equal than zero and less than HUNDRED_PERCENT");
|
|
10297
|
-
}
|
|
10298
|
-
if (increaseAmount <= 0) {
|
|
10299
|
-
throw new Error("increaseAmount must be greater than zero");
|
|
10300
|
-
}
|
|
10301
|
-
let borrow;
|
|
10302
|
-
let collateral;
|
|
10303
|
-
let estimatedAmount;
|
|
10304
|
-
let swapInputAmount;
|
|
10305
|
-
let nextSqrtPrice = fusionPool.sqrtPrice;
|
|
10306
|
-
if (positionToken != collateralToken) {
|
|
10307
|
-
const price = (0, import_fusionamm_core4.sqrtPriceToPrice)(fusionPool.sqrtPrice, 1, 1);
|
|
10308
|
-
borrow = BigInt(Math.ceil(Number(increaseAmount) * (leverage - 1) / leverage));
|
|
10309
|
-
collateral = increaseAmount - applySwapFee(applyTunaProtocolFee(borrow, protocolFeeRate), fusionPool.feeRate);
|
|
10310
|
-
collateral = reverseApplySwapFee(collateral, fusionPool.feeRate, false);
|
|
10311
|
-
collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral, false);
|
|
10312
|
-
swapInputAmount = increaseAmount;
|
|
10313
|
-
estimatedAmount = BigInt(
|
|
10314
|
-
Math.round(collateralToken == 0 /* A */ ? Number(increaseAmount) * price : Number(increaseAmount) / price)
|
|
10315
|
-
);
|
|
10316
|
-
} else {
|
|
10317
|
-
const price = (0, import_fusionamm_core4.sqrtPriceToPrice)(fusionPool.sqrtPrice, 1, 1);
|
|
10318
|
-
const positionToBorrowedTokenPrice = collateralToken == 0 /* A */ ? price : 1 / price;
|
|
10319
|
-
const borrowInPositionToken = Math.ceil(Number(increaseAmount) * (leverage - 1) / leverage);
|
|
10320
|
-
borrow = BigInt(Math.ceil(borrowInPositionToken * positionToBorrowedTokenPrice));
|
|
10321
|
-
const borrowInPositionTokenWithFeesApplied = applySwapFee(
|
|
10322
|
-
applyTunaProtocolFee(BigInt(borrowInPositionToken), protocolFeeRate),
|
|
10323
|
-
fusionPool.feeRate
|
|
10324
|
-
);
|
|
10325
|
-
collateral = increaseAmount - borrowInPositionTokenWithFeesApplied;
|
|
10326
|
-
collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral, false);
|
|
10327
|
-
swapInputAmount = applyTunaProtocolFee(borrow, protocolFeeRate);
|
|
10328
|
-
estimatedAmount = increaseAmount;
|
|
10329
|
-
}
|
|
10330
|
-
if (swapInputAmount > 0) {
|
|
10331
|
-
const is_token_a = positionToken == 1 /* B */;
|
|
10332
|
-
nextSqrtPrice = (0, import_fusionamm_core4.swapQuoteByInputToken)(swapInputAmount, is_token_a, 0, fusionPool, tickArrays).nextSqrtPrice;
|
|
10333
|
-
}
|
|
10334
|
-
const protocolFeeA = (collateralToken == 0 /* A */ ? collateral - applyTunaProtocolFee(collateral, protocolFeeRateOnCollateral) : 0n) + (positionToken == 1 /* B */ ? borrow - applyTunaProtocolFee(borrow, protocolFeeRate) : 0n);
|
|
10335
|
-
const protocolFeeB = (collateralToken == 1 /* B */ ? collateral - applyTunaProtocolFee(collateral, protocolFeeRateOnCollateral) : 0n) + (positionToken == 0 /* A */ ? borrow - applyTunaProtocolFee(borrow, protocolFeeRate) : 0n);
|
|
10336
|
-
const oldPrice = (0, import_fusionamm_core4.sqrtPriceToPrice)(fusionPool.sqrtPrice, 1, 1);
|
|
10337
|
-
const newPrice = (0, import_fusionamm_core4.sqrtPriceToPrice)(nextSqrtPrice, 1, 1);
|
|
10338
|
-
const priceImpact = Math.abs(newPrice / oldPrice - 1) * 100;
|
|
10339
|
-
return {
|
|
10340
|
-
collateral,
|
|
10341
|
-
borrow,
|
|
10342
|
-
swapInputAmount,
|
|
10343
|
-
estimatedAmount,
|
|
10344
|
-
protocolFeeA,
|
|
10345
|
-
protocolFeeB,
|
|
10346
|
-
priceImpact
|
|
10347
|
-
};
|
|
10348
|
-
}
|
|
10349
|
-
function getDecreaseSpotPositionQuote(args) {
|
|
10350
|
-
const {
|
|
10351
|
-
fusionPool,
|
|
10352
|
-
tickArrays,
|
|
10353
|
-
leverage,
|
|
10354
|
-
protocolFeeRate,
|
|
10355
|
-
protocolFeeRateOnCollateral,
|
|
10356
|
-
decreaseAmount,
|
|
10357
|
-
collateralToken,
|
|
10358
|
-
positionToken,
|
|
10359
|
-
positionAmount,
|
|
10360
|
-
positionDebt,
|
|
10361
|
-
reduceOnly
|
|
10362
|
-
} = args;
|
|
10363
|
-
if (leverage < 1) {
|
|
10364
|
-
throw new Error("leverage must be greater or equal than 1.0");
|
|
10365
|
-
}
|
|
10366
|
-
if (protocolFeeRate < 0 || protocolFeeRate >= HUNDRED_PERCENT) {
|
|
10367
|
-
throw new Error("protocolFeeRate must be greater or equal than zero and less than HUNDRED_PERCENT");
|
|
10368
|
-
}
|
|
10369
|
-
if (protocolFeeRateOnCollateral < 0 || protocolFeeRateOnCollateral >= HUNDRED_PERCENT) {
|
|
10370
|
-
throw new Error("protocolFeeRate must be greater or equal than zero and less than HUNDRED_PERCENT");
|
|
10371
|
-
}
|
|
10372
|
-
if (decreaseAmount <= 0) {
|
|
10373
|
-
throw new Error("decreaseAmount must be greater than zero");
|
|
10374
|
-
}
|
|
10375
|
-
let collateral = 0n;
|
|
10376
|
-
let borrow = 0n;
|
|
10377
|
-
let swapInputAmount = 0n;
|
|
10378
|
-
let estimatedAmount = 0n;
|
|
10379
|
-
let nextSqrtPrice = fusionPool.sqrtPrice;
|
|
10380
|
-
let newPositionToken = positionToken;
|
|
10381
|
-
let decreasePercent;
|
|
10382
|
-
const price = (0, import_fusionamm_core4.sqrtPriceToPrice)(fusionPool.sqrtPrice, 1, 1);
|
|
10383
|
-
const positionToOppositeTokenPrice = positionToken == 0 /* A */ ? price : 1 / price;
|
|
10384
|
-
let decreaseAmountInPositionToken = collateralToken == positionToken ? decreaseAmount : BigInt(Math.round(Number(decreaseAmount) / positionToOppositeTokenPrice));
|
|
10385
|
-
if (reduceOnly && decreaseAmountInPositionToken > positionAmount) {
|
|
10386
|
-
decreaseAmountInPositionToken = positionAmount;
|
|
10387
|
-
}
|
|
10388
|
-
if (decreaseAmountInPositionToken <= positionAmount) {
|
|
10389
|
-
decreasePercent = Math.min(
|
|
10390
|
-
Math.floor(Number(decreaseAmountInPositionToken) * HUNDRED_PERCENT / Number(positionAmount)),
|
|
10391
|
-
HUNDRED_PERCENT
|
|
10392
|
-
);
|
|
10393
|
-
estimatedAmount = positionAmount - decreaseAmountInPositionToken;
|
|
10394
|
-
if (collateralToken == positionToken) {
|
|
10395
|
-
if (positionDebt > 0) {
|
|
10396
|
-
const swapOut = BigInt(Math.floor(Number(positionDebt) * decreasePercent / HUNDRED_PERCENT));
|
|
10397
|
-
const swapQuote = (0, import_fusionamm_core4.swapQuoteByOutputToken)(swapOut, positionToken == 1 /* B */, 0, fusionPool, tickArrays);
|
|
10398
|
-
nextSqrtPrice = swapQuote.nextSqrtPrice;
|
|
10399
|
-
swapInputAmount = swapQuote.tokenEstIn;
|
|
10400
|
-
}
|
|
10401
|
-
} else {
|
|
10402
|
-
swapInputAmount = positionAmount - BigInt(Math.floor(Number(positionAmount) * (HUNDRED_PERCENT - decreasePercent) / HUNDRED_PERCENT));
|
|
10403
|
-
const swapQuote = (0, import_fusionamm_core4.swapQuoteByInputToken)(swapInputAmount, positionToken == 0 /* A */, 0, fusionPool, tickArrays);
|
|
10404
|
-
nextSqrtPrice = swapQuote.nextSqrtPrice;
|
|
10405
|
-
}
|
|
10406
|
-
} else {
|
|
10407
|
-
decreasePercent = HUNDRED_PERCENT;
|
|
10408
|
-
newPositionToken = positionToken == 0 /* A */ ? 1 /* B */ : 0 /* A */;
|
|
10409
|
-
const increaseAmount = decreaseAmountInPositionToken - positionAmount;
|
|
10410
|
-
if (positionToken == collateralToken) {
|
|
10411
|
-
estimatedAmount = BigInt(Math.round(Number(increaseAmount) * positionToOppositeTokenPrice));
|
|
10412
|
-
borrow = BigInt(Math.round(Number(increaseAmount) * (leverage - 1) / leverage));
|
|
10413
|
-
const borrowWithFeesApplied = applySwapFee(applyTunaProtocolFee(borrow, protocolFeeRate), fusionPool.feeRate);
|
|
10414
|
-
collateral = increaseAmount - borrowWithFeesApplied;
|
|
10415
|
-
if (positionDebt > 0) {
|
|
10416
|
-
const swapQuote2 = (0, import_fusionamm_core4.swapQuoteByOutputToken)(positionDebt, positionToken != 0 /* A */, 0, fusionPool, tickArrays);
|
|
10417
|
-
swapInputAmount = swapQuote2.tokenEstIn;
|
|
10418
|
-
}
|
|
10419
|
-
swapInputAmount += collateral + applyTunaProtocolFee(borrow, protocolFeeRate);
|
|
10420
|
-
const swapQuote = (0, import_fusionamm_core4.swapQuoteByInputToken)(swapInputAmount, positionToken == 0 /* A */, 0, fusionPool, tickArrays);
|
|
10421
|
-
nextSqrtPrice = swapQuote.nextSqrtPrice;
|
|
10422
|
-
collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral, false);
|
|
10423
|
-
} else {
|
|
10424
|
-
estimatedAmount = BigInt(Math.round(Number(increaseAmount) * positionToOppositeTokenPrice));
|
|
10425
|
-
borrow = BigInt(Math.round(Number(increaseAmount) * (leverage - 1) / leverage));
|
|
10426
|
-
const borrowWithFeesApplied = applySwapFee(applyTunaProtocolFee(borrow, protocolFeeRate), fusionPool.feeRate);
|
|
10427
|
-
collateral = increaseAmount - borrowWithFeesApplied;
|
|
10428
|
-
collateral = BigInt(Math.round(Number(collateral) * positionToOppositeTokenPrice));
|
|
10429
|
-
collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral, false);
|
|
10430
|
-
swapInputAmount = positionAmount + applyTunaProtocolFee(borrow, protocolFeeRate);
|
|
10431
|
-
const swapQuote = (0, import_fusionamm_core4.swapQuoteByInputToken)(swapInputAmount, positionToken == 0 /* A */, 0, fusionPool, tickArrays);
|
|
10432
|
-
nextSqrtPrice = swapQuote.nextSqrtPrice;
|
|
10433
|
-
}
|
|
10434
|
-
}
|
|
10435
|
-
const protocolFeeA = (collateralToken == 0 /* A */ ? collateral - applyTunaProtocolFee(collateral, protocolFeeRateOnCollateral) : 0n) + (positionToken == 1 /* B */ ? borrow - applyTunaProtocolFee(borrow, protocolFeeRate) : 0n);
|
|
10436
|
-
const protocolFeeB = (collateralToken == 1 /* B */ ? collateral - applyTunaProtocolFee(collateral, protocolFeeRateOnCollateral) : 0n) + (positionToken == 0 /* A */ ? borrow - applyTunaProtocolFee(borrow, protocolFeeRate) : 0n);
|
|
10437
|
-
const newPrice = (0, import_fusionamm_core4.sqrtPriceToPrice)(nextSqrtPrice, 1, 1);
|
|
10438
|
-
const priceImpact = Math.abs(newPrice / price - 1) * 100;
|
|
10439
|
-
return {
|
|
10440
|
-
decreasePercent,
|
|
10441
|
-
collateralToken,
|
|
10442
|
-
positionToken: newPositionToken,
|
|
10443
|
-
collateral,
|
|
10444
|
-
borrow,
|
|
10445
|
-
swapInputAmount,
|
|
10446
|
-
estimatedAmount,
|
|
10447
|
-
protocolFeeA,
|
|
10448
|
-
protocolFeeB,
|
|
10449
|
-
priceImpact
|
|
10450
|
-
};
|
|
10451
|
-
}
|
|
10452
|
-
function getTradableAmount(args) {
|
|
10453
|
-
const {
|
|
10454
|
-
collateralToken,
|
|
10455
|
-
newPositionToken,
|
|
10456
|
-
positionToken,
|
|
10457
|
-
positionAmount,
|
|
10458
|
-
positionDebt,
|
|
10459
|
-
reduceOnly,
|
|
10460
|
-
fusionPool,
|
|
10461
|
-
tickArrays,
|
|
10462
|
-
leverage,
|
|
10463
|
-
availableBalance,
|
|
10464
|
-
protocolFeeRate,
|
|
10465
|
-
protocolFeeRateOnCollateral
|
|
10466
|
-
} = args;
|
|
10467
|
-
if (leverage < 1) {
|
|
10468
|
-
throw new Error("leverage must be greater or equal than 1.0");
|
|
10469
|
-
}
|
|
10470
|
-
if (protocolFeeRateOnCollateral < 0 || protocolFeeRateOnCollateral >= HUNDRED_PERCENT) {
|
|
10471
|
-
throw new Error("protocolFeeRate must be greater or equal than zero and less than HUNDRED_PERCENT");
|
|
10472
|
-
}
|
|
10473
|
-
if (protocolFeeRate < 0 || protocolFeeRate >= HUNDRED_PERCENT) {
|
|
10474
|
-
throw new Error("protocolFeeRate must be greater or equal than zero and less than HUNDRED_PERCENT");
|
|
10475
|
-
}
|
|
10476
|
-
if (positionAmount == 0n && newPositionToken != positionToken) {
|
|
10477
|
-
throw new Error("positionToken must be set to newPositionToken if positionAmount is zero");
|
|
10478
|
-
}
|
|
10479
|
-
let availableToTrade = 0n;
|
|
10480
|
-
const addLeverage = (collateral) => {
|
|
10481
|
-
collateral = applyTunaProtocolFee(collateral, protocolFeeRateOnCollateral);
|
|
10482
|
-
if (collateralToken != newPositionToken) {
|
|
10483
|
-
collateral = applySwapFee(collateral, fusionPool.feeRate);
|
|
10484
|
-
}
|
|
10485
|
-
const feeMultiplier = (1 - protocolFeeRate / HUNDRED_PERCENT) * (1 - fusionPool.feeRate / 1e6);
|
|
10486
|
-
const total = Math.floor(Number(collateral) / (1 - feeMultiplier * (leverage - 1) / leverage));
|
|
10487
|
-
return BigInt(total);
|
|
10488
|
-
};
|
|
10489
|
-
if (newPositionToken == positionToken) {
|
|
10490
|
-
availableToTrade = addLeverage(availableBalance);
|
|
10491
|
-
} else {
|
|
10492
|
-
const price = (0, import_fusionamm_core4.sqrtPriceToPrice)(fusionPool.sqrtPrice, 1, 1);
|
|
10493
|
-
const positionToOppositeTokenPrice = positionToken == 0 /* A */ ? price : 1 / price;
|
|
10494
|
-
if (reduceOnly) {
|
|
10495
|
-
if (collateralToken == positionToken) {
|
|
10496
|
-
availableToTrade = positionAmount;
|
|
10497
|
-
} else {
|
|
10498
|
-
availableToTrade = BigInt(Math.round(Number(positionAmount) * positionToOppositeTokenPrice));
|
|
10499
|
-
}
|
|
10500
|
-
} else {
|
|
10501
|
-
const positionAmountInCollateralToken = collateralToken == positionToken ? positionAmount : BigInt(Math.round(Number(positionAmount) * positionToOppositeTokenPrice));
|
|
10502
|
-
let positionCollateral = collateralToken == positionToken ? positionAmount - (positionDebt > 0n ? (0, import_fusionamm_core4.swapQuoteByOutputToken)(positionDebt, positionToken == 1 /* B */, 0, fusionPool, tickArrays).tokenEstIn : 0n) : positionAmount > 0n ? (0, import_fusionamm_core4.swapQuoteByInputToken)(positionAmount, positionToken == 0 /* A */, 0, fusionPool, tickArrays).tokenEstOut - positionDebt : 0n;
|
|
10503
|
-
if (positionCollateral < 0n) positionCollateral = 0n;
|
|
10504
|
-
availableToTrade = positionAmountInCollateralToken + addLeverage(availableBalance + positionCollateral);
|
|
10505
|
-
}
|
|
10506
|
-
}
|
|
10507
|
-
return availableToTrade;
|
|
10508
|
-
}
|
|
10509
|
-
function getLiquidationPrice(positionToken, amount, debt, liquidationThreshold) {
|
|
10510
|
-
if (debt < 0) {
|
|
10511
|
-
throw new Error("debt must be greater or equal than zero");
|
|
10512
|
-
}
|
|
10513
|
-
if (amount < 0) {
|
|
10514
|
-
throw new Error("position amount must be greater or equal than zero");
|
|
10515
|
-
}
|
|
10516
|
-
if (liquidationThreshold <= 0 || liquidationThreshold >= 1) {
|
|
10517
|
-
throw new Error("liquidationThreshold must be greater than zero and less than one");
|
|
10518
|
-
}
|
|
10519
|
-
if (debt == 0 || amount == 0) return 0;
|
|
10520
|
-
if (positionToken == 0 /* A */) {
|
|
10521
|
-
return debt / (amount * liquidationThreshold);
|
|
10522
|
-
} else {
|
|
10523
|
-
return amount * liquidationThreshold / debt;
|
|
10524
|
-
}
|
|
10525
|
-
}
|
|
10526
|
-
function applyTunaProtocolFee(amount, protocolFeeRate, roundUp = false) {
|
|
10527
|
-
return mulDiv(amount, BigInt(HUNDRED_PERCENT - protocolFeeRate), BigInt(HUNDRED_PERCENT), roundUp);
|
|
10528
|
-
}
|
|
10529
|
-
function reverseApplyTunaProtocolFee(amount, protocolFeeRate, roundUp = true) {
|
|
10530
|
-
return mulDiv(amount, BigInt(HUNDRED_PERCENT), BigInt(HUNDRED_PERCENT - protocolFeeRate), roundUp);
|
|
10531
|
-
}
|
|
10532
|
-
function applySwapFee(amount, feeRate, roundUp = false) {
|
|
10533
|
-
return mulDiv(amount, BigInt(1e6 - feeRate), 1000000n, roundUp);
|
|
10534
|
-
}
|
|
10535
|
-
function reverseApplySwapFee(amount, feeRate, roundUp = true) {
|
|
10536
|
-
return mulDiv(amount, 1000000n, BigInt(1e6 - feeRate), roundUp);
|
|
10537
|
-
}
|
|
10538
|
-
|
|
10539
10274
|
// src/txbuilder/increaseTunaLpPositionOrca.ts
|
|
10540
10275
|
var import_whirlpools_client2 = require("@orca-so/whirlpools-client");
|
|
10541
10276
|
var import_whirlpools_core2 = require("@orca-so/whirlpools-core");
|
|
@@ -10769,7 +10504,7 @@ async function increaseTunaLpPositionOrcaInstruction(authority, tunaPosition, tu
|
|
|
10769
10504
|
|
|
10770
10505
|
// src/txbuilder/increaseTunaLpPositionFusion.ts
|
|
10771
10506
|
var import_fusionamm_client2 = require("@crypticdot/fusionamm-client");
|
|
10772
|
-
var
|
|
10507
|
+
var import_fusionamm_core4 = require("@crypticdot/fusionamm-core");
|
|
10773
10508
|
var import_kit80 = require("@solana/kit");
|
|
10774
10509
|
var import_memo2 = require("@solana-program/memo");
|
|
10775
10510
|
var import_token_20223 = require("@solana-program/token-2022");
|
|
@@ -10839,12 +10574,12 @@ async function increaseTunaLpPositionFusionInstructions(rpc, authority, position
|
|
|
10839
10574
|
mintB.programAddress
|
|
10840
10575
|
);
|
|
10841
10576
|
createInstructions.push(...createFeeRecipientAtaBInstructions.init);
|
|
10842
|
-
const lowerTickArrayStartIndex = (0,
|
|
10577
|
+
const lowerTickArrayStartIndex = (0, import_fusionamm_core4.getTickArrayStartTickIndex)(
|
|
10843
10578
|
tunaPosition.data.tickLowerIndex,
|
|
10844
10579
|
fusionPool.data.tickSpacing
|
|
10845
10580
|
);
|
|
10846
10581
|
const [lowerTickArrayAddress] = await (0, import_fusionamm_client2.getTickArrayAddress)(fusionPool.address, lowerTickArrayStartIndex);
|
|
10847
|
-
const upperTickArrayStartIndex = (0,
|
|
10582
|
+
const upperTickArrayStartIndex = (0, import_fusionamm_core4.getTickArrayStartTickIndex)(
|
|
10848
10583
|
tunaPosition.data.tickUpperIndex,
|
|
10849
10584
|
fusionPool.data.tickSpacing
|
|
10850
10585
|
);
|
|
@@ -13762,7 +13497,7 @@ async function openAndIncreaseTunaLpPositionOrcaInstruction(authority, positionM
|
|
|
13762
13497
|
|
|
13763
13498
|
// src/txbuilder/openAndIncreaseTunaLpPositionFusion.ts
|
|
13764
13499
|
var import_fusionamm_client14 = require("@crypticdot/fusionamm-client");
|
|
13765
|
-
var
|
|
13500
|
+
var import_fusionamm_core5 = require("@crypticdot/fusionamm-core");
|
|
13766
13501
|
var import_kit97 = require("@solana/kit");
|
|
13767
13502
|
var import_sysvars2 = require("@solana/sysvars");
|
|
13768
13503
|
var import_memo19 = require("@solana-program/memo");
|
|
@@ -13834,9 +13569,9 @@ async function openAndIncreaseTunaLpPositionFusionInstructions(rpc, authority, f
|
|
|
13834
13569
|
mintB.programAddress
|
|
13835
13570
|
);
|
|
13836
13571
|
createInstructions.push(...createFeeRecipientAtaBInstructions.init);
|
|
13837
|
-
const lowerTickArrayIndex = (0,
|
|
13572
|
+
const lowerTickArrayIndex = (0, import_fusionamm_core5.getTickArrayStartTickIndex)(args.tickLowerIndex, fusionPool.data.tickSpacing);
|
|
13838
13573
|
const [lowerTickArrayAddress] = await (0, import_fusionamm_client14.getTickArrayAddress)(fusionPool.address, lowerTickArrayIndex);
|
|
13839
|
-
const upperTickArrayIndex = (0,
|
|
13574
|
+
const upperTickArrayIndex = (0, import_fusionamm_core5.getTickArrayStartTickIndex)(args.tickUpperIndex, fusionPool.data.tickSpacing);
|
|
13840
13575
|
const [upperTickArrayAddress] = await (0, import_fusionamm_client14.getTickArrayAddress)(fusionPool.address, upperTickArrayIndex);
|
|
13841
13576
|
const [lowerTickArray, upperTickArray] = await (0, import_fusionamm_client14.fetchAllMaybeTickArray)(rpc, [
|
|
13842
13577
|
lowerTickArrayAddress,
|
|
@@ -15459,6 +15194,24 @@ async function rebalanceTunaLpPositionFusionInstruction(authority, tunaPosition,
|
|
|
15459
15194
|
ix.accounts.push(...remainingAccounts);
|
|
15460
15195
|
return ix;
|
|
15461
15196
|
}
|
|
15197
|
+
|
|
15198
|
+
// src/casts.ts
|
|
15199
|
+
function toPoolTokenFacade(token) {
|
|
15200
|
+
switch (token) {
|
|
15201
|
+
case 0 /* A */:
|
|
15202
|
+
return "a";
|
|
15203
|
+
case 1 /* B */:
|
|
15204
|
+
return "b";
|
|
15205
|
+
}
|
|
15206
|
+
}
|
|
15207
|
+
function fromPoolTokenFacade(token) {
|
|
15208
|
+
switch (token) {
|
|
15209
|
+
case "a":
|
|
15210
|
+
return 0 /* A */;
|
|
15211
|
+
case "b":
|
|
15212
|
+
return 1 /* B */;
|
|
15213
|
+
}
|
|
15214
|
+
}
|
|
15462
15215
|
// Annotate the CommonJS export names for ESM import in node:
|
|
15463
15216
|
0 && (module.exports = {
|
|
15464
15217
|
AccountsType,
|
|
@@ -15687,6 +15440,7 @@ async function rebalanceTunaLpPositionFusionInstruction(authority, tunaPosition,
|
|
|
15687
15440
|
fetchTunaLpPosition,
|
|
15688
15441
|
fetchTunaSpotPosition,
|
|
15689
15442
|
fetchVault,
|
|
15443
|
+
fromPoolTokenFacade,
|
|
15690
15444
|
getAccountsTypeCodec,
|
|
15691
15445
|
getAccountsTypeDecoder,
|
|
15692
15446
|
getAccountsTypeEncoder,
|
|
@@ -15757,7 +15511,6 @@ async function rebalanceTunaLpPositionFusionInstruction(authority, tunaPosition,
|
|
|
15757
15511
|
getCreateVaultInstructionDataCodec,
|
|
15758
15512
|
getCreateVaultInstructionDataDecoder,
|
|
15759
15513
|
getCreateVaultInstructionDataEncoder,
|
|
15760
|
-
getDecreaseSpotPositionQuote,
|
|
15761
15514
|
getDecreaseTunaLpPositionFusionDiscriminatorBytes,
|
|
15762
15515
|
getDecreaseTunaLpPositionFusionInstruction,
|
|
15763
15516
|
getDecreaseTunaLpPositionFusionInstructionDataCodec,
|
|
@@ -15784,7 +15537,6 @@ async function rebalanceTunaLpPositionFusionInstruction(authority, tunaPosition,
|
|
|
15784
15537
|
getDepositInstructionDataDecoder,
|
|
15785
15538
|
getDepositInstructionDataEncoder,
|
|
15786
15539
|
getIncreaseLpPositionQuote,
|
|
15787
|
-
getIncreaseSpotPositionQuote,
|
|
15788
15540
|
getIncreaseTunaLpPositionFusionDiscriminatorBytes,
|
|
15789
15541
|
getIncreaseTunaLpPositionFusionInstruction,
|
|
15790
15542
|
getIncreaseTunaLpPositionFusionInstructionDataCodec,
|
|
@@ -15832,7 +15584,6 @@ async function rebalanceTunaLpPositionFusionInstruction(authority, tunaPosition,
|
|
|
15832
15584
|
getLiquidateTunaSpotPositionOrcaInstructionDataCodec,
|
|
15833
15585
|
getLiquidateTunaSpotPositionOrcaInstructionDataDecoder,
|
|
15834
15586
|
getLiquidateTunaSpotPositionOrcaInstructionDataEncoder,
|
|
15835
|
-
getLiquidationPrice,
|
|
15836
15587
|
getMarketAddress,
|
|
15837
15588
|
getMarketCodec,
|
|
15838
15589
|
getMarketDecoder,
|
|
@@ -15982,7 +15733,6 @@ async function rebalanceTunaLpPositionFusionInstruction(authority, tunaPosition,
|
|
|
15982
15733
|
getSetTunaSpotPositionLimitOrdersInstructionDataCodec,
|
|
15983
15734
|
getSetTunaSpotPositionLimitOrdersInstructionDataDecoder,
|
|
15984
15735
|
getSetTunaSpotPositionLimitOrdersInstructionDataEncoder,
|
|
15985
|
-
getTradableAmount,
|
|
15986
15736
|
getTunaConfigAddress,
|
|
15987
15737
|
getTunaConfigCodec,
|
|
15988
15738
|
getTunaConfigDecoder,
|
|
@@ -16128,6 +15878,7 @@ async function rebalanceTunaLpPositionFusionInstruction(authority, tunaPosition,
|
|
|
16128
15878
|
resetTunaSpotPositionInstruction,
|
|
16129
15879
|
setTunaLpPositionLimitOrdersInstruction,
|
|
16130
15880
|
setTunaSpotPositionLimitOrdersInstruction,
|
|
15881
|
+
toPoolTokenFacade,
|
|
16131
15882
|
tunaLpPositionAuthorityFilter,
|
|
16132
15883
|
tunaLpPositionMarketMakerFilter,
|
|
16133
15884
|
tunaLpPositionMintAFilter,
|