@crypticdot/defituna-client 3.1.3 → 3.1.5
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 +53 -6
- package/dist/index.d.ts +53 -6
- package/dist/index.js +229 -119
- package/dist/index.mjs +233 -123
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -10458,6 +10458,7 @@ function calculateProtocolFee(collateralAmount, borrowAmount, protocolFeeRateOnC
|
|
|
10458
10458
|
// src/utils/spotPositionMath.ts
|
|
10459
10459
|
import {
|
|
10460
10460
|
sqrtPriceToPrice,
|
|
10461
|
+
swapQuoteByInputToken,
|
|
10461
10462
|
swapQuoteByOutputToken
|
|
10462
10463
|
} from "@crypticdot/fusionamm-core";
|
|
10463
10464
|
function getIncreaseSpotPositionQuote(args) {
|
|
@@ -10467,7 +10468,7 @@ function getIncreaseSpotPositionQuote(args) {
|
|
|
10467
10468
|
leverage,
|
|
10468
10469
|
protocolFeeRate,
|
|
10469
10470
|
protocolFeeRateOnCollateral,
|
|
10470
|
-
|
|
10471
|
+
increaseAmount,
|
|
10471
10472
|
collateralToken,
|
|
10472
10473
|
positionToken
|
|
10473
10474
|
} = args;
|
|
@@ -10480,52 +10481,48 @@ function getIncreaseSpotPositionQuote(args) {
|
|
|
10480
10481
|
if (protocolFeeRateOnCollateral < 0) {
|
|
10481
10482
|
throw new Error("protocolFeeRate must be greater or equal than zero");
|
|
10482
10483
|
}
|
|
10483
|
-
if (
|
|
10484
|
-
throw new Error("
|
|
10485
|
-
}
|
|
10486
|
-
if (totalAmount == 0n) {
|
|
10487
|
-
return {
|
|
10488
|
-
collateral: 0n,
|
|
10489
|
-
borrow: 0n,
|
|
10490
|
-
protocolFeeA: 0n,
|
|
10491
|
-
protocolFeeB: 0n,
|
|
10492
|
-
priceImpact: 0
|
|
10493
|
-
};
|
|
10484
|
+
if (increaseAmount <= 0) {
|
|
10485
|
+
throw new Error("increaseAmount must be greater than zero");
|
|
10494
10486
|
}
|
|
10495
10487
|
let borrow;
|
|
10496
10488
|
let collateral;
|
|
10489
|
+
let estimatedAmount;
|
|
10497
10490
|
let nextSqrtPrice = fusionPool.sqrtPrice;
|
|
10498
10491
|
if (positionToken != collateralToken) {
|
|
10499
10492
|
const price = sqrtPriceToPrice(fusionPool.sqrtPrice, 1, 1);
|
|
10500
|
-
|
|
10501
|
-
|
|
10493
|
+
collateral = leverage > 1 ? BigInt(Math.floor(Number(increaseAmount) / leverage)) : increaseAmount;
|
|
10494
|
+
borrow = increaseAmount - collateral;
|
|
10495
|
+
estimatedAmount = BigInt(
|
|
10496
|
+
Math.floor(collateralToken == 0 /* A */ ? Number(increaseAmount) * price : Number(increaseAmount) / price)
|
|
10502
10497
|
);
|
|
10503
|
-
|
|
10504
|
-
|
|
10505
|
-
|
|
10498
|
+
nextSqrtPrice = swapQuoteByInputToken(
|
|
10499
|
+
increaseAmount,
|
|
10500
|
+
collateralToken == 0 /* A */,
|
|
10506
10501
|
0,
|
|
10507
10502
|
fusionPool,
|
|
10508
10503
|
tickArrays
|
|
10509
|
-
);
|
|
10510
|
-
nextSqrtPrice = swapQuote.nextSqrtPrice;
|
|
10511
|
-
const inputAmount = swapQuote.tokenEstIn;
|
|
10512
|
-
collateral = leverage > 1 ? BigInt(Math.floor(Number(inputAmount) / leverage)) : inputAmount;
|
|
10513
|
-
borrow = inputAmount - collateral;
|
|
10504
|
+
).nextSqrtPrice;
|
|
10514
10505
|
} else {
|
|
10515
10506
|
if (leverage > 1) {
|
|
10516
|
-
|
|
10517
|
-
|
|
10518
|
-
|
|
10507
|
+
const price = sqrtPriceToPrice(fusionPool.sqrtPrice, 1, 1);
|
|
10508
|
+
collateral = BigInt(Math.floor(Number(increaseAmount) / leverage));
|
|
10509
|
+
borrow = BigInt(
|
|
10510
|
+
Math.floor(
|
|
10511
|
+
collateralToken == 0 /* A */ ? Number(increaseAmount - collateral) * price : Number(increaseAmount - collateral) / price
|
|
10512
|
+
)
|
|
10513
|
+
);
|
|
10514
|
+
estimatedAmount = increaseAmount;
|
|
10515
|
+
nextSqrtPrice = swapQuoteByOutputToken(
|
|
10516
|
+
increaseAmount - collateral,
|
|
10519
10517
|
positionToken == 0 /* A */,
|
|
10520
10518
|
0,
|
|
10521
10519
|
fusionPool,
|
|
10522
10520
|
tickArrays
|
|
10523
|
-
);
|
|
10524
|
-
nextSqrtPrice = swapQuote.nextSqrtPrice;
|
|
10525
|
-
borrow = swapQuote.tokenEstIn;
|
|
10521
|
+
).nextSqrtPrice;
|
|
10526
10522
|
} else {
|
|
10527
|
-
collateral =
|
|
10523
|
+
collateral = increaseAmount;
|
|
10528
10524
|
borrow = 0n;
|
|
10525
|
+
estimatedAmount = increaseAmount;
|
|
10529
10526
|
}
|
|
10530
10527
|
}
|
|
10531
10528
|
const collateralExcludingFee = collateral;
|
|
@@ -10540,6 +10537,124 @@ function getIncreaseSpotPositionQuote(args) {
|
|
|
10540
10537
|
return {
|
|
10541
10538
|
collateral,
|
|
10542
10539
|
borrow,
|
|
10540
|
+
estimatedAmount,
|
|
10541
|
+
protocolFeeA,
|
|
10542
|
+
protocolFeeB,
|
|
10543
|
+
priceImpact
|
|
10544
|
+
};
|
|
10545
|
+
}
|
|
10546
|
+
function getDecreaseSpotPositionQuote(args) {
|
|
10547
|
+
const {
|
|
10548
|
+
fusionPool,
|
|
10549
|
+
tickArrays,
|
|
10550
|
+
leverage,
|
|
10551
|
+
protocolFeeRate,
|
|
10552
|
+
protocolFeeRateOnCollateral,
|
|
10553
|
+
decreaseAmount,
|
|
10554
|
+
collateralToken,
|
|
10555
|
+
positionToken,
|
|
10556
|
+
positionAmount,
|
|
10557
|
+
positionDebt,
|
|
10558
|
+
reduceOnly
|
|
10559
|
+
} = args;
|
|
10560
|
+
if (leverage < 1) {
|
|
10561
|
+
throw new Error("leverage must be greater or equal than 1.0");
|
|
10562
|
+
}
|
|
10563
|
+
if (protocolFeeRate < 0) {
|
|
10564
|
+
throw new Error("protocolFeeRate must be greater or equal than zero");
|
|
10565
|
+
}
|
|
10566
|
+
if (protocolFeeRateOnCollateral < 0) {
|
|
10567
|
+
throw new Error("protocolFeeRate must be greater or equal than zero");
|
|
10568
|
+
}
|
|
10569
|
+
if (decreaseAmount <= 0) {
|
|
10570
|
+
throw new Error("decreaseAmount must be greater than zero");
|
|
10571
|
+
}
|
|
10572
|
+
let collateral = 0n;
|
|
10573
|
+
let borrow = 0n;
|
|
10574
|
+
let estimatedAmount = 0n;
|
|
10575
|
+
let nextSqrtPrice = fusionPool.sqrtPrice;
|
|
10576
|
+
let newPositionToken = positionToken;
|
|
10577
|
+
let decreasePercent;
|
|
10578
|
+
const price = sqrtPriceToPrice(fusionPool.sqrtPrice, 1, 1);
|
|
10579
|
+
if (decreaseAmount <= positionAmount || reduceOnly) {
|
|
10580
|
+
let decreaseAmountInPositionToken;
|
|
10581
|
+
if (collateralToken == positionToken) {
|
|
10582
|
+
decreaseAmountInPositionToken = decreaseAmount;
|
|
10583
|
+
} else {
|
|
10584
|
+
decreaseAmountInPositionToken = BigInt(
|
|
10585
|
+
Math.floor(collateralToken == 0 /* A */ ? Number(decreaseAmount) * price : Number(decreaseAmount) / price)
|
|
10586
|
+
);
|
|
10587
|
+
}
|
|
10588
|
+
decreasePercent = Math.min(
|
|
10589
|
+
Math.floor(Number(decreaseAmountInPositionToken) * HUNDRED_PERCENT / Number(positionAmount)),
|
|
10590
|
+
HUNDRED_PERCENT
|
|
10591
|
+
);
|
|
10592
|
+
const swapOut = BigInt(Math.floor(Number(positionDebt) * decreasePercent / HUNDRED_PERCENT));
|
|
10593
|
+
const swapQuote = swapQuoteByOutputToken(swapOut, positionToken == 1 /* B */, 0, fusionPool, tickArrays);
|
|
10594
|
+
nextSqrtPrice = swapQuote.nextSqrtPrice;
|
|
10595
|
+
estimatedAmount = decreaseAmountInPositionToken <= positionAmount ? positionAmount - decreaseAmountInPositionToken : 0n;
|
|
10596
|
+
} else {
|
|
10597
|
+
decreasePercent = HUNDRED_PERCENT;
|
|
10598
|
+
const positionTokenIsA = positionToken == 0 /* A */;
|
|
10599
|
+
newPositionToken = positionToken == 0 /* A */ ? 1 /* B */ : 0 /* A */;
|
|
10600
|
+
const increaseAmount = decreaseAmount - positionAmount;
|
|
10601
|
+
if (positionToken == collateralToken) {
|
|
10602
|
+
estimatedAmount = BigInt(
|
|
10603
|
+
Math.floor(positionTokenIsA ? Number(increaseAmount) * price : Number(increaseAmount) / price)
|
|
10604
|
+
);
|
|
10605
|
+
collateral = leverage > 1 ? BigInt(Math.floor(Number(increaseAmount) / leverage)) : estimatedAmount;
|
|
10606
|
+
borrow = increaseAmount - collateral;
|
|
10607
|
+
const borrowInNewPositionToken = BigInt(
|
|
10608
|
+
Math.floor(positionTokenIsA ? Number(borrow) * price : Number(borrow) / price)
|
|
10609
|
+
);
|
|
10610
|
+
nextSqrtPrice = swapQuoteByInputToken(
|
|
10611
|
+
positionDebt + borrowInNewPositionToken,
|
|
10612
|
+
!positionTokenIsA,
|
|
10613
|
+
0,
|
|
10614
|
+
fusionPool,
|
|
10615
|
+
tickArrays
|
|
10616
|
+
).nextSqrtPrice;
|
|
10617
|
+
} else {
|
|
10618
|
+
if (leverage > 1) {
|
|
10619
|
+
const collateralInPositionToken = BigInt(Math.floor(Number(increaseAmount) / leverage));
|
|
10620
|
+
borrow = increaseAmount - collateralInPositionToken;
|
|
10621
|
+
collateral = BigInt(
|
|
10622
|
+
Math.floor(
|
|
10623
|
+
positionToken == 0 /* A */ ? Number(collateralInPositionToken) * price : Number(collateralInPositionToken) / price
|
|
10624
|
+
)
|
|
10625
|
+
);
|
|
10626
|
+
const borrowInNewPositionToken = BigInt(
|
|
10627
|
+
Math.floor(newPositionToken == 1 /* B */ ? Number(borrow) * price : Number(borrow) / price)
|
|
10628
|
+
);
|
|
10629
|
+
estimatedAmount = collateral + borrowInNewPositionToken;
|
|
10630
|
+
nextSqrtPrice = swapQuoteByInputToken(
|
|
10631
|
+
positionAmount + borrow,
|
|
10632
|
+
positionTokenIsA,
|
|
10633
|
+
0,
|
|
10634
|
+
fusionPool,
|
|
10635
|
+
tickArrays
|
|
10636
|
+
).nextSqrtPrice;
|
|
10637
|
+
} else {
|
|
10638
|
+
collateral = increaseAmount;
|
|
10639
|
+
borrow = 0n;
|
|
10640
|
+
}
|
|
10641
|
+
}
|
|
10642
|
+
}
|
|
10643
|
+
const collateralExcludingFee = collateral;
|
|
10644
|
+
const borrowExcludingFee = borrow;
|
|
10645
|
+
collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral);
|
|
10646
|
+
borrow = reverseApplyTunaProtocolFee(borrow, protocolFeeRate);
|
|
10647
|
+
const protocolFeeA = (collateralToken == 0 /* A */ ? collateral - collateralExcludingFee : 0n) + (positionToken == 1 /* B */ ? borrow - borrowExcludingFee : 0n);
|
|
10648
|
+
const protocolFeeB = (collateralToken == 1 /* B */ ? collateral - collateralExcludingFee : 0n) + (positionToken == 0 /* A */ ? borrow - borrowExcludingFee : 0n);
|
|
10649
|
+
const newPrice = sqrtPriceToPrice(nextSqrtPrice, 1, 1);
|
|
10650
|
+
const priceImpact = Math.abs(newPrice / price - 1) * 100;
|
|
10651
|
+
return {
|
|
10652
|
+
decreasePercent,
|
|
10653
|
+
collateralToken,
|
|
10654
|
+
positionToken: newPositionToken,
|
|
10655
|
+
collateral,
|
|
10656
|
+
estimatedAmount,
|
|
10657
|
+
borrow,
|
|
10543
10658
|
protocolFeeA,
|
|
10544
10659
|
protocolFeeB,
|
|
10545
10660
|
priceImpact
|
|
@@ -12000,7 +12115,7 @@ import {
|
|
|
12000
12115
|
AccountRole as AccountRole8
|
|
12001
12116
|
} from "@solana/kit";
|
|
12002
12117
|
import { MEMO_PROGRAM_ADDRESS as MEMO_PROGRAM_ADDRESS8 } from "@solana-program/memo";
|
|
12003
|
-
import { fetchAllMaybeMint as fetchAllMaybeMint11, findAssociatedTokenPda as findAssociatedTokenPda13 } from "@solana-program/token-2022";
|
|
12118
|
+
import { fetchAllMaybeMint as fetchAllMaybeMint11, fetchAllToken, findAssociatedTokenPda as findAssociatedTokenPda13 } from "@solana-program/token-2022";
|
|
12004
12119
|
import assert11 from "assert";
|
|
12005
12120
|
async function closeActiveTunaSpotPositionFusionInstructions(rpc, authority, fusionPoolAddress, args, createInstructions, cleanupInstructions) {
|
|
12006
12121
|
const tunaPositionAddress = (await getTunaSpotPositionAddress(authority.address, fusionPoolAddress))[0];
|
|
@@ -12018,30 +12133,42 @@ async function closeActiveTunaSpotPositionFusionInstructions(rpc, authority, fus
|
|
|
12018
12133
|
const instructions = [];
|
|
12019
12134
|
if (!createInstructions) createInstructions = instructions;
|
|
12020
12135
|
if (!cleanupInstructions) cleanupInstructions = instructions;
|
|
12021
|
-
const
|
|
12022
|
-
|
|
12023
|
-
|
|
12024
|
-
|
|
12025
|
-
|
|
12026
|
-
|
|
12027
|
-
|
|
12028
|
-
|
|
12029
|
-
|
|
12136
|
+
const tunaPositionAtaAAddress = (await findAssociatedTokenPda13({
|
|
12137
|
+
owner: tunaPosition.address,
|
|
12138
|
+
mint: mintA.address,
|
|
12139
|
+
tokenProgram: mintA.programAddress
|
|
12140
|
+
}))[0];
|
|
12141
|
+
const tunaPositionAtaBAddress = (await findAssociatedTokenPda13({
|
|
12142
|
+
owner: tunaPosition.address,
|
|
12143
|
+
mint: mintB.address,
|
|
12144
|
+
tokenProgram: mintB.programAddress
|
|
12145
|
+
}))[0];
|
|
12146
|
+
const [tunaPositionAtaA, tunaPositionAtaB] = await fetchAllToken(rpc, [
|
|
12147
|
+
tunaPositionAtaAAddress,
|
|
12148
|
+
tunaPositionAtaBAddress
|
|
12149
|
+
]);
|
|
12150
|
+
const createUserAtaAInstructions = tunaPosition.data.collateralToken == 0 /* A */ || tunaPositionAtaA.data.amount > (tunaPosition.data.positionToken == 0 /* A */ ? tunaPosition.data.amount : 0n) ? await getCreateAtaInstructions(rpc, authority, mintA.address, authority.address, mintA.programAddress) : void 0;
|
|
12151
|
+
if (createUserAtaAInstructions) createInstructions.push(...createUserAtaAInstructions.init);
|
|
12152
|
+
const createUserAtaBInstructions = tunaPosition.data.collateralToken == 1 /* B */ || tunaPositionAtaB.data.amount > (tunaPosition.data.positionToken == 1 /* B */ ? tunaPosition.data.amount : 0n) ? await getCreateAtaInstructions(rpc, authority, mintB.address, authority.address, mintB.programAddress) : void 0;
|
|
12153
|
+
if (createUserAtaBInstructions) createInstructions.push(...createUserAtaBInstructions.init);
|
|
12030
12154
|
const ix = await closeActiveTunaSpotPositionFusionInstruction(
|
|
12031
12155
|
authority,
|
|
12032
|
-
tunaPosition
|
|
12156
|
+
tunaPosition,
|
|
12033
12157
|
mintA,
|
|
12034
12158
|
mintB,
|
|
12035
12159
|
vaultA,
|
|
12036
12160
|
vaultB,
|
|
12037
12161
|
fusionPool,
|
|
12162
|
+
createUserAtaAInstructions != void 0,
|
|
12163
|
+
createUserAtaBInstructions != void 0,
|
|
12038
12164
|
{ ...args }
|
|
12039
12165
|
);
|
|
12040
12166
|
instructions.push(ix);
|
|
12041
|
-
cleanupInstructions.push(...
|
|
12167
|
+
if (createUserAtaAInstructions) cleanupInstructions.push(...createUserAtaAInstructions.cleanup);
|
|
12168
|
+
if (createUserAtaBInstructions) cleanupInstructions.push(...createUserAtaBInstructions.cleanup);
|
|
12042
12169
|
return instructions;
|
|
12043
12170
|
}
|
|
12044
|
-
async function closeActiveTunaSpotPositionFusionInstruction(authority,
|
|
12171
|
+
async function closeActiveTunaSpotPositionFusionInstruction(authority, tunaPosition, mintA, mintB, vaultA, vaultB, fusionPool, setTunaPositionOwnerAtaA, setTunaPositionOwnerAtaB, args) {
|
|
12045
12172
|
const tunaConfig = (await getTunaConfigAddress())[0];
|
|
12046
12173
|
const marketAddress = (await getMarketAddress(fusionPool.address))[0];
|
|
12047
12174
|
const tunaPositionOwnerAtaA = (await findAssociatedTokenPda13({
|
|
@@ -12055,12 +12182,12 @@ async function closeActiveTunaSpotPositionFusionInstruction(authority, tunaPosit
|
|
|
12055
12182
|
tokenProgram: mintB.programAddress
|
|
12056
12183
|
}))[0];
|
|
12057
12184
|
const tunaPositionAtaA = (await findAssociatedTokenPda13({
|
|
12058
|
-
owner:
|
|
12185
|
+
owner: tunaPosition.address,
|
|
12059
12186
|
mint: mintA.address,
|
|
12060
12187
|
tokenProgram: mintA.programAddress
|
|
12061
12188
|
}))[0];
|
|
12062
12189
|
const tunaPositionAtaB = (await findAssociatedTokenPda13({
|
|
12063
|
-
owner:
|
|
12190
|
+
owner: tunaPosition.address,
|
|
12064
12191
|
mint: mintB.address,
|
|
12065
12192
|
tokenProgram: mintB.programAddress
|
|
12066
12193
|
}))[0];
|
|
@@ -12105,9 +12232,9 @@ async function closeActiveTunaSpotPositionFusionInstruction(authority, tunaPosit
|
|
|
12105
12232
|
tunaConfig,
|
|
12106
12233
|
tunaPositionAtaA,
|
|
12107
12234
|
tunaPositionAtaB,
|
|
12108
|
-
tunaPositionOwnerAtaA,
|
|
12109
|
-
tunaPositionOwnerAtaB,
|
|
12110
|
-
tunaPosition:
|
|
12235
|
+
...setTunaPositionOwnerAtaA && { tunaPositionOwnerAtaA },
|
|
12236
|
+
...setTunaPositionOwnerAtaB && { tunaPositionOwnerAtaB },
|
|
12237
|
+
tunaPosition: tunaPosition.address,
|
|
12111
12238
|
fusionPool: fusionPool.address,
|
|
12112
12239
|
fusionammProgram: FUSIONAMM_PROGRAM_ADDRESS6,
|
|
12113
12240
|
tokenProgramA: mintA.programAddress,
|
|
@@ -12130,7 +12257,7 @@ import {
|
|
|
12130
12257
|
AccountRole as AccountRole9
|
|
12131
12258
|
} from "@solana/kit";
|
|
12132
12259
|
import { MEMO_PROGRAM_ADDRESS as MEMO_PROGRAM_ADDRESS9 } from "@solana-program/memo";
|
|
12133
|
-
import { fetchAllMaybeMint as fetchAllMaybeMint12, findAssociatedTokenPda as findAssociatedTokenPda14 } from "@solana-program/token-2022";
|
|
12260
|
+
import { fetchAllMaybeMint as fetchAllMaybeMint12, fetchAllToken as fetchAllToken2, findAssociatedTokenPda as findAssociatedTokenPda14 } from "@solana-program/token-2022";
|
|
12134
12261
|
import assert12 from "assert";
|
|
12135
12262
|
async function closeActiveTunaSpotPositionOrcaInstructions(rpc, authority, whirlpoolAddress, args, createInstructions, cleanupInstructions) {
|
|
12136
12263
|
const tunaPositionAddress = (await getTunaSpotPositionAddress(authority.address, whirlpoolAddress))[0];
|
|
@@ -12148,30 +12275,42 @@ async function closeActiveTunaSpotPositionOrcaInstructions(rpc, authority, whirl
|
|
|
12148
12275
|
const instructions = [];
|
|
12149
12276
|
if (!createInstructions) createInstructions = instructions;
|
|
12150
12277
|
if (!cleanupInstructions) cleanupInstructions = instructions;
|
|
12151
|
-
const
|
|
12152
|
-
|
|
12153
|
-
|
|
12154
|
-
|
|
12155
|
-
|
|
12156
|
-
|
|
12157
|
-
|
|
12158
|
-
|
|
12159
|
-
|
|
12278
|
+
const tunaPositionAtaAAddress = (await findAssociatedTokenPda14({
|
|
12279
|
+
owner: tunaPosition.address,
|
|
12280
|
+
mint: mintA.address,
|
|
12281
|
+
tokenProgram: mintA.programAddress
|
|
12282
|
+
}))[0];
|
|
12283
|
+
const tunaPositionAtaBAddress = (await findAssociatedTokenPda14({
|
|
12284
|
+
owner: tunaPosition.address,
|
|
12285
|
+
mint: mintB.address,
|
|
12286
|
+
tokenProgram: mintB.programAddress
|
|
12287
|
+
}))[0];
|
|
12288
|
+
const [tunaPositionAtaA, tunaPositionAtaB] = await fetchAllToken2(rpc, [
|
|
12289
|
+
tunaPositionAtaAAddress,
|
|
12290
|
+
tunaPositionAtaBAddress
|
|
12291
|
+
]);
|
|
12292
|
+
const createUserAtaAInstructions = tunaPosition.data.collateralToken == 0 /* A */ || tunaPositionAtaA.data.amount > (tunaPosition.data.positionToken == 0 /* A */ ? tunaPosition.data.amount : 0n) ? await getCreateAtaInstructions(rpc, authority, mintA.address, authority.address, mintA.programAddress) : void 0;
|
|
12293
|
+
if (createUserAtaAInstructions) createInstructions.push(...createUserAtaAInstructions.init);
|
|
12294
|
+
const createUserAtaBInstructions = tunaPosition.data.collateralToken == 1 /* B */ || tunaPositionAtaB.data.amount > (tunaPosition.data.positionToken == 1 /* B */ ? tunaPosition.data.amount : 0n) ? await getCreateAtaInstructions(rpc, authority, mintB.address, authority.address, mintB.programAddress) : void 0;
|
|
12295
|
+
if (createUserAtaBInstructions) createInstructions.push(...createUserAtaBInstructions.init);
|
|
12160
12296
|
const ix = await closeActiveTunaSpotPositionOrcaInstruction(
|
|
12161
12297
|
authority,
|
|
12162
|
-
tunaPosition
|
|
12298
|
+
tunaPosition,
|
|
12163
12299
|
mintA,
|
|
12164
12300
|
mintB,
|
|
12165
12301
|
vaultA,
|
|
12166
12302
|
vaultB,
|
|
12167
12303
|
whirlpool,
|
|
12304
|
+
createUserAtaAInstructions != void 0,
|
|
12305
|
+
createUserAtaBInstructions != void 0,
|
|
12168
12306
|
{ ...args }
|
|
12169
12307
|
);
|
|
12170
12308
|
instructions.push(ix);
|
|
12171
|
-
cleanupInstructions.push(...
|
|
12309
|
+
if (createUserAtaAInstructions) cleanupInstructions.push(...createUserAtaAInstructions.cleanup);
|
|
12310
|
+
if (createUserAtaBInstructions) cleanupInstructions.push(...createUserAtaBInstructions.cleanup);
|
|
12172
12311
|
return instructions;
|
|
12173
12312
|
}
|
|
12174
|
-
async function closeActiveTunaSpotPositionOrcaInstruction(authority,
|
|
12313
|
+
async function closeActiveTunaSpotPositionOrcaInstruction(authority, tunaPosition, mintA, mintB, vaultA, vaultB, whirlpool, setTunaPositionOwnerAtaA, setTunaPositionOwnerAtaB, args) {
|
|
12175
12314
|
const tunaConfig = (await getTunaConfigAddress())[0];
|
|
12176
12315
|
const marketAddress = (await getMarketAddress(whirlpool.address))[0];
|
|
12177
12316
|
const orcaOracleAddress = (await getOracleAddress3(whirlpool.address))[0];
|
|
@@ -12186,12 +12325,12 @@ async function closeActiveTunaSpotPositionOrcaInstruction(authority, tunaPositio
|
|
|
12186
12325
|
tokenProgram: mintB.programAddress
|
|
12187
12326
|
}))[0];
|
|
12188
12327
|
const tunaPositionAtaA = (await findAssociatedTokenPda14({
|
|
12189
|
-
owner:
|
|
12328
|
+
owner: tunaPosition.address,
|
|
12190
12329
|
mint: mintA.address,
|
|
12191
12330
|
tokenProgram: mintA.programAddress
|
|
12192
12331
|
}))[0];
|
|
12193
12332
|
const tunaPositionAtaB = (await findAssociatedTokenPda14({
|
|
12194
|
-
owner:
|
|
12333
|
+
owner: tunaPosition.address,
|
|
12195
12334
|
mint: mintB.address,
|
|
12196
12335
|
tokenProgram: mintB.programAddress
|
|
12197
12336
|
}))[0];
|
|
@@ -12238,9 +12377,9 @@ async function closeActiveTunaSpotPositionOrcaInstruction(authority, tunaPositio
|
|
|
12238
12377
|
tunaConfig,
|
|
12239
12378
|
tunaPositionAtaA,
|
|
12240
12379
|
tunaPositionAtaB,
|
|
12241
|
-
tunaPositionOwnerAtaA,
|
|
12242
|
-
tunaPositionOwnerAtaB,
|
|
12243
|
-
tunaPosition:
|
|
12380
|
+
...setTunaPositionOwnerAtaA && { tunaPositionOwnerAtaA },
|
|
12381
|
+
...setTunaPositionOwnerAtaB && { tunaPositionOwnerAtaB },
|
|
12382
|
+
tunaPosition: tunaPosition.address,
|
|
12244
12383
|
whirlpool: whirlpool.address,
|
|
12245
12384
|
whirlpoolProgram: WHIRLPOOL_PROGRAM_ADDRESS6,
|
|
12246
12385
|
tokenProgramA: mintA.programAddress,
|
|
@@ -12572,7 +12711,7 @@ import {
|
|
|
12572
12711
|
AccountRole as AccountRole12
|
|
12573
12712
|
} from "@solana/kit";
|
|
12574
12713
|
import { MEMO_PROGRAM_ADDRESS as MEMO_PROGRAM_ADDRESS12 } from "@solana-program/memo";
|
|
12575
|
-
import { fetchAllMaybeMint as fetchAllMaybeMint15, fetchAllToken, findAssociatedTokenPda as findAssociatedTokenPda17 } from "@solana-program/token-2022";
|
|
12714
|
+
import { fetchAllMaybeMint as fetchAllMaybeMint15, fetchAllToken as fetchAllToken3, findAssociatedTokenPda as findAssociatedTokenPda17 } from "@solana-program/token-2022";
|
|
12576
12715
|
import assert15 from "assert";
|
|
12577
12716
|
async function decreaseTunaSpotPositionOrcaInstructions(rpc, authority, whirlpoolAddress, args, createInstructions, cleanupInstructions) {
|
|
12578
12717
|
const instructions = [];
|
|
@@ -12590,39 +12729,24 @@ async function decreaseTunaSpotPositionOrcaInstructions(rpc, authority, whirlpoo
|
|
|
12590
12729
|
(await getLendingVaultAddress(whirlpool.data.tokenMintA))[0],
|
|
12591
12730
|
(await getLendingVaultAddress(whirlpool.data.tokenMintB))[0]
|
|
12592
12731
|
]);
|
|
12593
|
-
const
|
|
12732
|
+
const tunaPositionAtaAAddress = (await findAssociatedTokenPda17({
|
|
12594
12733
|
owner: tunaPosition.address,
|
|
12595
12734
|
mint: mintA.address,
|
|
12596
12735
|
tokenProgram: mintA.programAddress
|
|
12597
12736
|
}))[0];
|
|
12598
|
-
const
|
|
12737
|
+
const tunaPositionAtaBAddress = (await findAssociatedTokenPda17({
|
|
12599
12738
|
owner: tunaPosition.address,
|
|
12600
12739
|
mint: mintB.address,
|
|
12601
12740
|
tokenProgram: mintB.programAddress
|
|
12602
12741
|
}))[0];
|
|
12603
|
-
const
|
|
12604
|
-
|
|
12605
|
-
|
|
12606
|
-
|
|
12607
|
-
|
|
12608
|
-
|
|
12609
|
-
|
|
12610
|
-
|
|
12611
|
-
mintA.programAddress
|
|
12612
|
-
);
|
|
12613
|
-
createInstructions.push(...createUserAtaAInstructions.init);
|
|
12614
|
-
}
|
|
12615
|
-
let createUserAtaBInstructions = void 0;
|
|
12616
|
-
if (tunaPosition.data.collateralToken == 1 /* B */ || tunaPositionTokenAccounts[1].data.amount > (tunaPosition.data.positionToken == 1 /* B */ ? tunaPosition.data.amount : 0n)) {
|
|
12617
|
-
createUserAtaBInstructions = await getCreateAtaInstructions(
|
|
12618
|
-
rpc,
|
|
12619
|
-
authority,
|
|
12620
|
-
mintB.address,
|
|
12621
|
-
authority.address,
|
|
12622
|
-
mintB.programAddress
|
|
12623
|
-
);
|
|
12624
|
-
createInstructions.push(...createUserAtaBInstructions.init);
|
|
12625
|
-
}
|
|
12742
|
+
const [tunaPositionAtaA, tunaPositionAtaB] = await fetchAllToken3(rpc, [
|
|
12743
|
+
tunaPositionAtaAAddress,
|
|
12744
|
+
tunaPositionAtaBAddress
|
|
12745
|
+
]);
|
|
12746
|
+
const createUserAtaAInstructions = tunaPosition.data.collateralToken == 0 /* A */ || tunaPositionAtaA.data.amount > (tunaPosition.data.positionToken == 0 /* A */ ? tunaPosition.data.amount : 0n) ? await getCreateAtaInstructions(rpc, authority, mintA.address, authority.address, mintA.programAddress) : void 0;
|
|
12747
|
+
if (createUserAtaAInstructions) createInstructions.push(...createUserAtaAInstructions.init);
|
|
12748
|
+
const createUserAtaBInstructions = tunaPosition.data.collateralToken == 1 /* B */ || tunaPositionAtaB.data.amount > (tunaPosition.data.positionToken == 1 /* B */ ? tunaPosition.data.amount : 0n) ? await getCreateAtaInstructions(rpc, authority, mintB.address, authority.address, mintB.programAddress) : void 0;
|
|
12749
|
+
if (createUserAtaBInstructions) createInstructions.push(...createUserAtaBInstructions.init);
|
|
12626
12750
|
const ix = await decreaseTunaSpotPositionOrcaInstruction(
|
|
12627
12751
|
authority,
|
|
12628
12752
|
tunaPosition,
|
|
@@ -12641,7 +12765,7 @@ async function decreaseTunaSpotPositionOrcaInstructions(rpc, authority, whirlpoo
|
|
|
12641
12765
|
if (createUserAtaBInstructions) cleanupInstructions.push(...createUserAtaBInstructions.cleanup);
|
|
12642
12766
|
return instructions;
|
|
12643
12767
|
}
|
|
12644
|
-
async function decreaseTunaSpotPositionOrcaInstruction(authority, tunaPosition, tunaConfig, mintA, mintB, vaultA, vaultB, whirlpool,
|
|
12768
|
+
async function decreaseTunaSpotPositionOrcaInstruction(authority, tunaPosition, tunaConfig, mintA, mintB, vaultA, vaultB, whirlpool, setTunaPositionOwnerAtaA, setTunaPositionOwnerAtaB, args) {
|
|
12645
12769
|
const marketAddress = (await getMarketAddress(whirlpool.address))[0];
|
|
12646
12770
|
const orcaOracleAddress = (await getOracleAddress5(whirlpool.address))[0];
|
|
12647
12771
|
const tunaPositionOwnerAtaA = (await findAssociatedTokenPda17({
|
|
@@ -12710,8 +12834,8 @@ async function decreaseTunaSpotPositionOrcaInstruction(authority, tunaPosition,
|
|
|
12710
12834
|
tunaPosition: tunaPosition.address,
|
|
12711
12835
|
tunaPositionAtaA,
|
|
12712
12836
|
tunaPositionAtaB,
|
|
12713
|
-
|
|
12714
|
-
|
|
12837
|
+
...setTunaPositionOwnerAtaA && { tunaPositionOwnerAtaA },
|
|
12838
|
+
...setTunaPositionOwnerAtaB && { tunaPositionOwnerAtaB },
|
|
12715
12839
|
whirlpool: whirlpool.address,
|
|
12716
12840
|
whirlpoolProgram: WHIRLPOOL_PROGRAM_ADDRESS8,
|
|
12717
12841
|
memoProgram: MEMO_PROGRAM_ADDRESS12,
|
|
@@ -12728,7 +12852,7 @@ import {
|
|
|
12728
12852
|
AccountRole as AccountRole13
|
|
12729
12853
|
} from "@solana/kit";
|
|
12730
12854
|
import { MEMO_PROGRAM_ADDRESS as MEMO_PROGRAM_ADDRESS13 } from "@solana-program/memo";
|
|
12731
|
-
import { fetchAllMaybeMint as fetchAllMaybeMint16, fetchAllToken as
|
|
12855
|
+
import { fetchAllMaybeMint as fetchAllMaybeMint16, fetchAllToken as fetchAllToken4, findAssociatedTokenPda as findAssociatedTokenPda18 } from "@solana-program/token-2022";
|
|
12732
12856
|
import assert16 from "assert";
|
|
12733
12857
|
async function decreaseTunaSpotPositionFusionInstructions(rpc, authority, fusionPoolAddress, args, createInstructions, cleanupInstructions) {
|
|
12734
12858
|
const instructions = [];
|
|
@@ -12746,39 +12870,24 @@ async function decreaseTunaSpotPositionFusionInstructions(rpc, authority, fusion
|
|
|
12746
12870
|
(await getLendingVaultAddress(fusionPool.data.tokenMintA))[0],
|
|
12747
12871
|
(await getLendingVaultAddress(fusionPool.data.tokenMintB))[0]
|
|
12748
12872
|
]);
|
|
12749
|
-
const
|
|
12873
|
+
const tunaPositionAtaAAddress = (await findAssociatedTokenPda18({
|
|
12750
12874
|
owner: tunaPosition.address,
|
|
12751
12875
|
mint: mintA.address,
|
|
12752
12876
|
tokenProgram: mintA.programAddress
|
|
12753
12877
|
}))[0];
|
|
12754
|
-
const
|
|
12878
|
+
const tunaPositionAtaBAddress = (await findAssociatedTokenPda18({
|
|
12755
12879
|
owner: tunaPosition.address,
|
|
12756
12880
|
mint: mintB.address,
|
|
12757
12881
|
tokenProgram: mintB.programAddress
|
|
12758
12882
|
}))[0];
|
|
12759
|
-
const
|
|
12760
|
-
|
|
12761
|
-
|
|
12762
|
-
|
|
12763
|
-
|
|
12764
|
-
|
|
12765
|
-
|
|
12766
|
-
|
|
12767
|
-
mintA.programAddress
|
|
12768
|
-
);
|
|
12769
|
-
createInstructions.push(...createUserAtaAInstructions.init);
|
|
12770
|
-
}
|
|
12771
|
-
let createUserAtaBInstructions = void 0;
|
|
12772
|
-
if (tunaPosition.data.collateralToken == 1 /* B */ || tunaPositionTokenAccounts[1].data.amount > (tunaPosition.data.positionToken == 1 /* B */ ? tunaPosition.data.amount : 0n)) {
|
|
12773
|
-
createUserAtaBInstructions = await getCreateAtaInstructions(
|
|
12774
|
-
rpc,
|
|
12775
|
-
authority,
|
|
12776
|
-
mintB.address,
|
|
12777
|
-
authority.address,
|
|
12778
|
-
mintB.programAddress
|
|
12779
|
-
);
|
|
12780
|
-
createInstructions.push(...createUserAtaBInstructions.init);
|
|
12781
|
-
}
|
|
12883
|
+
const [tunaPositionAtaA, tunaPositionAtaB] = await fetchAllToken4(rpc, [
|
|
12884
|
+
tunaPositionAtaAAddress,
|
|
12885
|
+
tunaPositionAtaBAddress
|
|
12886
|
+
]);
|
|
12887
|
+
const createUserAtaAInstructions = tunaPosition.data.collateralToken == 0 /* A */ || tunaPositionAtaA.data.amount > (tunaPosition.data.positionToken == 0 /* A */ ? tunaPosition.data.amount : 0n) ? await getCreateAtaInstructions(rpc, authority, mintA.address, authority.address, mintA.programAddress) : void 0;
|
|
12888
|
+
if (createUserAtaAInstructions) createInstructions.push(...createUserAtaAInstructions.init);
|
|
12889
|
+
const createUserAtaBInstructions = tunaPosition.data.collateralToken == 1 /* B */ || tunaPositionAtaB.data.amount > (tunaPosition.data.positionToken == 1 /* B */ ? tunaPosition.data.amount : 0n) ? await getCreateAtaInstructions(rpc, authority, mintB.address, authority.address, mintB.programAddress) : void 0;
|
|
12890
|
+
if (createUserAtaBInstructions) createInstructions.push(...createUserAtaBInstructions.init);
|
|
12782
12891
|
const ix = await decreaseTunaSpotPositionFusionInstruction(
|
|
12783
12892
|
authority,
|
|
12784
12893
|
tunaPosition,
|
|
@@ -12797,7 +12906,7 @@ async function decreaseTunaSpotPositionFusionInstructions(rpc, authority, fusion
|
|
|
12797
12906
|
if (createUserAtaBInstructions) cleanupInstructions.push(...createUserAtaBInstructions.cleanup);
|
|
12798
12907
|
return instructions;
|
|
12799
12908
|
}
|
|
12800
|
-
async function decreaseTunaSpotPositionFusionInstruction(authority, tunaPosition, tunaConfig, mintA, mintB, vaultA, vaultB, fusionPool,
|
|
12909
|
+
async function decreaseTunaSpotPositionFusionInstruction(authority, tunaPosition, tunaConfig, mintA, mintB, vaultA, vaultB, fusionPool, setTunaPositionOwnerAtaA, setTunaPositionOwnerAtaB, args) {
|
|
12801
12910
|
const marketAddress = (await getMarketAddress(fusionPool.address))[0];
|
|
12802
12911
|
const tunaPositionOwnerAtaA = (await findAssociatedTokenPda18({
|
|
12803
12912
|
owner: authority.address,
|
|
@@ -12863,8 +12972,8 @@ async function decreaseTunaSpotPositionFusionInstruction(authority, tunaPosition
|
|
|
12863
12972
|
tunaPosition: tunaPosition.address,
|
|
12864
12973
|
tunaPositionAtaA,
|
|
12865
12974
|
tunaPositionAtaB,
|
|
12866
|
-
|
|
12867
|
-
|
|
12975
|
+
...setTunaPositionOwnerAtaA && { tunaPositionOwnerAtaA },
|
|
12976
|
+
...setTunaPositionOwnerAtaB && { tunaPositionOwnerAtaB },
|
|
12868
12977
|
fusionPool: fusionPool.address,
|
|
12869
12978
|
fusionammProgram: FUSIONAMM_PROGRAM_ADDRESS8,
|
|
12870
12979
|
memoProgram: MEMO_PROGRAM_ADDRESS13,
|
|
@@ -16003,6 +16112,7 @@ export {
|
|
|
16003
16112
|
getCreateVaultInstructionDataCodec,
|
|
16004
16113
|
getCreateVaultInstructionDataDecoder,
|
|
16005
16114
|
getCreateVaultInstructionDataEncoder,
|
|
16115
|
+
getDecreaseSpotPositionQuote,
|
|
16006
16116
|
getDecreaseTunaLpPositionFusionDiscriminatorBytes,
|
|
16007
16117
|
getDecreaseTunaLpPositionFusionInstruction,
|
|
16008
16118
|
getDecreaseTunaLpPositionFusionInstructionDataCodec,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crypticdot/defituna-client",
|
|
3
3
|
"description": "Typescript client to interact with DefiTuna's on-chain program.",
|
|
4
|
-
"version": "3.1.
|
|
4
|
+
"version": "3.1.5",
|
|
5
5
|
"private": false,
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE",
|
|
7
7
|
"main": "./dist/index.js",
|
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
"@orca-so/whirlpools-core": "^2.0.0",
|
|
20
20
|
"@orca-so/whirlpools-client": "^3.0.0",
|
|
21
21
|
"@orca-so/whirlpools": "^3.0.0",
|
|
22
|
-
"@crypticdot/fusionamm-core": "^1.0.
|
|
23
|
-
"@crypticdot/fusionamm-client": "^1.0.
|
|
24
|
-
"@crypticdot/fusionamm-sdk": "^1.0.
|
|
22
|
+
"@crypticdot/fusionamm-core": "^1.0.63",
|
|
23
|
+
"@crypticdot/fusionamm-client": "^1.0.63",
|
|
24
|
+
"@crypticdot/fusionamm-sdk": "^1.0.63",
|
|
25
25
|
"@solana/kit": "^2.1.0",
|
|
26
26
|
"@solana/sysvars": "^2.1.0",
|
|
27
27
|
"@solana-program/compute-budget": "^0.7.0",
|