@crypticdot/defituna-client 3.1.0 → 3.1.2

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.js CHANGED
@@ -225,6 +225,7 @@ __export(index_exports, {
225
225
  decreaseTunaSpotPositionOrcaInstructions: () => decreaseTunaSpotPositionOrcaInstructions,
226
226
  depositInstruction: () => depositInstruction,
227
227
  depositInstructions: () => depositInstructions,
228
+ extendAddressLookupTableForMarketInstructions: () => extendAddressLookupTableForMarketInstructions,
228
229
  fetchAllLendingPosition: () => fetchAllLendingPosition,
229
230
  fetchAllLendingPositionWithFilter: () => fetchAllLendingPositionWithFilter,
230
231
  fetchAllMarket: () => fetchAllMarket,
@@ -349,6 +350,7 @@ __export(index_exports, {
349
350
  getDepositInstructionDataDecoder: () => getDepositInstructionDataDecoder,
350
351
  getDepositInstructionDataEncoder: () => getDepositInstructionDataEncoder,
351
352
  getIncreaseLpPositionQuote: () => getIncreaseLpPositionQuote,
353
+ getIncreaseSpotPositionQuote: () => getIncreaseSpotPositionQuote,
352
354
  getIncreaseTunaLpPositionFusionDiscriminatorBytes: () => getIncreaseTunaLpPositionFusionDiscriminatorBytes,
353
355
  getIncreaseTunaLpPositionFusionInstruction: () => getIncreaseTunaLpPositionFusionInstruction,
354
356
  getIncreaseTunaLpPositionFusionInstructionDataCodec: () => getIncreaseTunaLpPositionFusionInstructionDataCodec,
@@ -1252,7 +1254,6 @@ function getTunaSpotPositionEncoder() {
1252
1254
  ["positionToken", getPoolTokenEncoder()],
1253
1255
  ["collateralToken", getPoolTokenEncoder()],
1254
1256
  ["flags", (0, import_kit11.getU32Encoder)()],
1255
- ["state", getTunaPositionStateEncoder()],
1256
1257
  ["amount", (0, import_kit11.getU64Encoder)()],
1257
1258
  ["loanShares", (0, import_kit11.getU64Encoder)()],
1258
1259
  ["loanFunds", (0, import_kit11.getU64Encoder)()],
@@ -1277,7 +1278,6 @@ function getTunaSpotPositionDecoder() {
1277
1278
  ["positionToken", getPoolTokenDecoder()],
1278
1279
  ["collateralToken", getPoolTokenDecoder()],
1279
1280
  ["flags", (0, import_kit11.getU32Decoder)()],
1280
- ["state", getTunaPositionStateDecoder()],
1281
1281
  ["amount", (0, import_kit11.getU64Decoder)()],
1282
1282
  ["loanShares", (0, import_kit11.getU64Decoder)()],
1283
1283
  ["loanFunds", (0, import_kit11.getU64Decoder)()],
@@ -1324,7 +1324,7 @@ async function fetchAllMaybeTunaSpotPosition(rpc, addresses, config) {
1324
1324
  );
1325
1325
  }
1326
1326
  function getTunaSpotPositionSize() {
1327
- return 347;
1327
+ return 346;
1328
1328
  }
1329
1329
 
1330
1330
  // src/generated/accounts/vault.ts
@@ -5955,6 +5955,18 @@ function getLiquidateTunaSpotPositionFusionInstruction(input, config) {
5955
5955
  value: input.feeRecipientAtaB ?? null,
5956
5956
  isWritable: true
5957
5957
  },
5958
+ tunaPositionOwner: {
5959
+ value: input.tunaPositionOwner ?? null,
5960
+ isWritable: true
5961
+ },
5962
+ tunaPositionOwnerAtaA: {
5963
+ value: input.tunaPositionOwnerAtaA ?? null,
5964
+ isWritable: true
5965
+ },
5966
+ tunaPositionOwnerAtaB: {
5967
+ value: input.tunaPositionOwnerAtaB ?? null,
5968
+ isWritable: true
5969
+ },
5958
5970
  pythOraclePriceFeedA: {
5959
5971
  value: input.pythOraclePriceFeedA ?? null,
5960
5972
  isWritable: false
@@ -5995,6 +6007,9 @@ function getLiquidateTunaSpotPositionFusionInstruction(input, config) {
5995
6007
  getAccountMeta(accounts.tunaPositionAtaB),
5996
6008
  getAccountMeta(accounts.feeRecipientAtaA),
5997
6009
  getAccountMeta(accounts.feeRecipientAtaB),
6010
+ getAccountMeta(accounts.tunaPositionOwner),
6011
+ getAccountMeta(accounts.tunaPositionOwnerAtaA),
6012
+ getAccountMeta(accounts.tunaPositionOwnerAtaB),
5998
6013
  getAccountMeta(accounts.pythOraclePriceFeedA),
5999
6014
  getAccountMeta(accounts.pythOraclePriceFeedB),
6000
6015
  getAccountMeta(accounts.fusionammProgram),
@@ -6010,7 +6025,7 @@ function getLiquidateTunaSpotPositionFusionInstruction(input, config) {
6010
6025
  return instruction;
6011
6026
  }
6012
6027
  function parseLiquidateTunaSpotPositionFusionInstruction(instruction) {
6013
- if (instruction.accounts.length < 22) {
6028
+ if (instruction.accounts.length < 25) {
6014
6029
  throw new Error("Not enough accounts");
6015
6030
  }
6016
6031
  let accountIndex = 0;
@@ -6019,6 +6034,10 @@ function parseLiquidateTunaSpotPositionFusionInstruction(instruction) {
6019
6034
  accountIndex += 1;
6020
6035
  return accountMeta;
6021
6036
  };
6037
+ const getNextOptionalAccount = () => {
6038
+ const accountMeta = getNextAccount();
6039
+ return accountMeta.address === TUNA_PROGRAM_ADDRESS ? void 0 : accountMeta;
6040
+ };
6022
6041
  return {
6023
6042
  programAddress: instruction.programAddress,
6024
6043
  accounts: {
@@ -6038,6 +6057,9 @@ function parseLiquidateTunaSpotPositionFusionInstruction(instruction) {
6038
6057
  tunaPositionAtaB: getNextAccount(),
6039
6058
  feeRecipientAtaA: getNextAccount(),
6040
6059
  feeRecipientAtaB: getNextAccount(),
6060
+ tunaPositionOwner: getNextAccount(),
6061
+ tunaPositionOwnerAtaA: getNextOptionalAccount(),
6062
+ tunaPositionOwnerAtaB: getNextOptionalAccount(),
6041
6063
  pythOraclePriceFeedA: getNextAccount(),
6042
6064
  pythOraclePriceFeedB: getNextAccount(),
6043
6065
  fusionammProgram: getNextAccount(),
@@ -6125,6 +6147,18 @@ function getLiquidateTunaSpotPositionOrcaInstruction(input, config) {
6125
6147
  value: input.feeRecipientAtaB ?? null,
6126
6148
  isWritable: true
6127
6149
  },
6150
+ tunaPositionOwner: {
6151
+ value: input.tunaPositionOwner ?? null,
6152
+ isWritable: true
6153
+ },
6154
+ tunaPositionOwnerAtaA: {
6155
+ value: input.tunaPositionOwnerAtaA ?? null,
6156
+ isWritable: true
6157
+ },
6158
+ tunaPositionOwnerAtaB: {
6159
+ value: input.tunaPositionOwnerAtaB ?? null,
6160
+ isWritable: true
6161
+ },
6128
6162
  pythOraclePriceFeedA: {
6129
6163
  value: input.pythOraclePriceFeedA ?? null,
6130
6164
  isWritable: false
@@ -6165,6 +6199,9 @@ function getLiquidateTunaSpotPositionOrcaInstruction(input, config) {
6165
6199
  getAccountMeta(accounts.tunaPositionAtaB),
6166
6200
  getAccountMeta(accounts.feeRecipientAtaA),
6167
6201
  getAccountMeta(accounts.feeRecipientAtaB),
6202
+ getAccountMeta(accounts.tunaPositionOwner),
6203
+ getAccountMeta(accounts.tunaPositionOwnerAtaA),
6204
+ getAccountMeta(accounts.tunaPositionOwnerAtaB),
6168
6205
  getAccountMeta(accounts.pythOraclePriceFeedA),
6169
6206
  getAccountMeta(accounts.pythOraclePriceFeedB),
6170
6207
  getAccountMeta(accounts.whirlpoolProgram),
@@ -6180,7 +6217,7 @@ function getLiquidateTunaSpotPositionOrcaInstruction(input, config) {
6180
6217
  return instruction;
6181
6218
  }
6182
6219
  function parseLiquidateTunaSpotPositionOrcaInstruction(instruction) {
6183
- if (instruction.accounts.length < 22) {
6220
+ if (instruction.accounts.length < 25) {
6184
6221
  throw new Error("Not enough accounts");
6185
6222
  }
6186
6223
  let accountIndex = 0;
@@ -6189,6 +6226,10 @@ function parseLiquidateTunaSpotPositionOrcaInstruction(instruction) {
6189
6226
  accountIndex += 1;
6190
6227
  return accountMeta;
6191
6228
  };
6229
+ const getNextOptionalAccount = () => {
6230
+ const accountMeta = getNextAccount();
6231
+ return accountMeta.address === TUNA_PROGRAM_ADDRESS ? void 0 : accountMeta;
6232
+ };
6192
6233
  return {
6193
6234
  programAddress: instruction.programAddress,
6194
6235
  accounts: {
@@ -6208,6 +6249,9 @@ function parseLiquidateTunaSpotPositionOrcaInstruction(instruction) {
6208
6249
  tunaPositionAtaB: getNextAccount(),
6209
6250
  feeRecipientAtaA: getNextAccount(),
6210
6251
  feeRecipientAtaB: getNextAccount(),
6252
+ tunaPositionOwner: getNextAccount(),
6253
+ tunaPositionOwnerAtaA: getNextOptionalAccount(),
6254
+ tunaPositionOwnerAtaB: getNextOptionalAccount(),
6211
6255
  pythOraclePriceFeedA: getNextAccount(),
6212
6256
  pythOraclePriceFeedB: getNextAccount(),
6213
6257
  whirlpoolProgram: getNextAccount(),
@@ -10121,7 +10165,7 @@ async function createAddressLookupTableInstructions(authority, addresses, recent
10121
10165
  authority,
10122
10166
  payer: authority
10123
10167
  });
10124
- return { instructions: [createInstruction, extendInstruction], lookupTableAddress: pda[0] };
10168
+ return { instructions: [createInstruction, extendInstruction], lookupTableAddress: pda[0], addresses };
10125
10169
  }
10126
10170
 
10127
10171
  // src/utils/lpPositionMath.ts
@@ -10229,6 +10273,97 @@ function calculateProtocolFee(collateralAmount, borrowAmount, protocolFeeRateOnC
10229
10273
  return collateralAmount * BigInt(protocolFeeRateOnCollateral) / BigInt(HUNDRED_PERCENT) + borrowAmount * BigInt(protocolFeeRate) / BigInt(HUNDRED_PERCENT);
10230
10274
  }
10231
10275
 
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
+ totalAmount,
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) {
10293
+ throw new Error("protocolFeeRate must be greater or equal than zero");
10294
+ }
10295
+ if (protocolFeeRateOnCollateral < 0) {
10296
+ throw new Error("protocolFeeRate must be greater or equal than zero");
10297
+ }
10298
+ if (totalAmount < 0) {
10299
+ throw new Error("totalAmount must be greater or equal than zero");
10300
+ }
10301
+ if (totalAmount == 0n) {
10302
+ return {
10303
+ collateral: 0n,
10304
+ borrow: 0n,
10305
+ protocolFeeA: 0n,
10306
+ protocolFeeB: 0n,
10307
+ priceImpact: 0
10308
+ };
10309
+ }
10310
+ let borrow;
10311
+ let collateral;
10312
+ let nextSqrtPrice = fusionPool.sqrtPrice;
10313
+ if (positionToken != collateralToken) {
10314
+ const price = (0, import_fusionamm_core4.sqrtPriceToPrice)(fusionPool.sqrtPrice, 1, 1);
10315
+ const totalAmountInOppositeToken = BigInt(
10316
+ Math.floor(positionToken == 0 /* A */ ? Number(totalAmount) / price : Number(totalAmount) * price)
10317
+ );
10318
+ const swapQuote = (0, import_fusionamm_core4.swapQuoteByOutputToken)(
10319
+ totalAmountInOppositeToken,
10320
+ positionToken == 0 /* A */,
10321
+ 0,
10322
+ fusionPool,
10323
+ tickArrays
10324
+ );
10325
+ nextSqrtPrice = swapQuote.nextSqrtPrice;
10326
+ const inputAmount = swapQuote.tokenEstIn;
10327
+ collateral = leverage > 1 ? BigInt(Math.floor(Number(inputAmount) / leverage)) : inputAmount;
10328
+ borrow = inputAmount - collateral;
10329
+ } else {
10330
+ if (leverage > 1) {
10331
+ collateral = BigInt(Math.floor(Number(totalAmount) / leverage));
10332
+ const swapQuote = (0, import_fusionamm_core4.swapQuoteByOutputToken)(
10333
+ totalAmount - collateral,
10334
+ positionToken == 0 /* A */,
10335
+ 0,
10336
+ fusionPool,
10337
+ tickArrays
10338
+ );
10339
+ nextSqrtPrice = swapQuote.nextSqrtPrice;
10340
+ borrow = swapQuote.tokenEstIn;
10341
+ } else {
10342
+ collateral = totalAmount;
10343
+ borrow = 0n;
10344
+ }
10345
+ }
10346
+ const collateralExcludingFee = collateral;
10347
+ const borrowExcludingFee = borrow;
10348
+ collateral = reverseApplyTunaProtocolFee(collateral, protocolFeeRateOnCollateral);
10349
+ borrow = reverseApplyTunaProtocolFee(borrow, protocolFeeRate);
10350
+ const protocolFeeA = (collateralToken == 0 /* A */ ? collateral - collateralExcludingFee : 0n) + (positionToken == 1 /* B */ ? borrow - borrowExcludingFee : 0n);
10351
+ const protocolFeeB = (collateralToken == 1 /* B */ ? collateral - collateralExcludingFee : 0n) + (positionToken == 0 /* A */ ? borrow - borrowExcludingFee : 0n);
10352
+ const oldPrice = (0, import_fusionamm_core4.sqrtPriceToPrice)(fusionPool.sqrtPrice, 1, 1);
10353
+ const newPrice = (0, import_fusionamm_core4.sqrtPriceToPrice)(nextSqrtPrice, 1, 1);
10354
+ const priceImpact = Math.abs(newPrice / oldPrice - 1) * 100;
10355
+ return {
10356
+ collateral,
10357
+ borrow,
10358
+ protocolFeeA,
10359
+ protocolFeeB,
10360
+ priceImpact
10361
+ };
10362
+ }
10363
+ function reverseApplyTunaProtocolFee(amount, protocolFeeRate) {
10364
+ return amount * BigInt(HUNDRED_PERCENT) / BigInt(HUNDRED_PERCENT - protocolFeeRate);
10365
+ }
10366
+
10232
10367
  // src/txbuilder/increaseTunaLpPositionOrca.ts
10233
10368
  var import_whirlpools_client2 = require("@orca-so/whirlpools-client");
10234
10369
  var import_whirlpools_core2 = require("@orca-so/whirlpools-core");
@@ -10462,7 +10597,7 @@ async function increaseTunaLpPositionOrcaInstruction(authority, tunaPosition, tu
10462
10597
 
10463
10598
  // src/txbuilder/increaseTunaLpPositionFusion.ts
10464
10599
  var import_fusionamm_client2 = require("@crypticdot/fusionamm-client");
10465
- var import_fusionamm_core4 = require("@crypticdot/fusionamm-core");
10600
+ var import_fusionamm_core5 = require("@crypticdot/fusionamm-core");
10466
10601
  var import_kit80 = require("@solana/kit");
10467
10602
  var import_memo2 = require("@solana-program/memo");
10468
10603
  var import_token_20223 = require("@solana-program/token-2022");
@@ -10532,12 +10667,12 @@ async function increaseTunaLpPositionFusionInstructions(rpc, authority, position
10532
10667
  mintB.programAddress
10533
10668
  );
10534
10669
  createInstructions.push(...createFeeRecipientAtaBInstructions.init);
10535
- const lowerTickArrayStartIndex = (0, import_fusionamm_core4.getTickArrayStartTickIndex)(
10670
+ const lowerTickArrayStartIndex = (0, import_fusionamm_core5.getTickArrayStartTickIndex)(
10536
10671
  tunaPosition.data.tickLowerIndex,
10537
10672
  fusionPool.data.tickSpacing
10538
10673
  );
10539
10674
  const [lowerTickArrayAddress] = await (0, import_fusionamm_client2.getTickArrayAddress)(fusionPool.address, lowerTickArrayStartIndex);
10540
- const upperTickArrayStartIndex = (0, import_fusionamm_core4.getTickArrayStartTickIndex)(
10675
+ const upperTickArrayStartIndex = (0, import_fusionamm_core5.getTickArrayStartTickIndex)(
10541
10676
  tunaPosition.data.tickUpperIndex,
10542
10677
  fusionPool.data.tickSpacing
10543
10678
  );
@@ -12829,20 +12964,40 @@ var import_fusionamm_client11 = require("@crypticdot/fusionamm-client");
12829
12964
  var import_kit94 = require("@solana/kit");
12830
12965
  var import_memo16 = require("@solana-program/memo");
12831
12966
  var import_token_202223 = require("@solana-program/token-2022");
12832
- async function liquidateTunaSpotPositionFusionInstructions(authority, tunaPosition, tunaConfig, mintA, mintB, vaultA, vaultB, fusionPool, withdrawPercent) {
12967
+ async function liquidateTunaSpotPositionFusionInstructions(authority, tunaPosition, tunaConfig, mintA, mintB, vaultA, vaultB, fusionPool, createTunaPositionOwnerAta, withdrawPercent) {
12833
12968
  const instructions = [];
12834
- const positionTokenMint = tunaPosition.data.positionToken == 0 /* A */ ? mintA : mintB;
12835
- const createFeeRecipientAtaInstructions = await getCreateAtaInstructions(
12969
+ if (createTunaPositionOwnerAta) {
12970
+ const collateralTokenMint = tunaPosition.data.collateralToken == 0 /* A */ ? mintA : mintB;
12971
+ if (collateralTokenMint.address != NATIVE_MINT) {
12972
+ const createPositionOwnerAtaInstructions = await getCreateAtaInstructions(
12973
+ void 0,
12974
+ authority,
12975
+ collateralTokenMint.address,
12976
+ tunaPosition.data.authority,
12977
+ collateralTokenMint.programAddress
12978
+ );
12979
+ instructions.push(...createPositionOwnerAtaInstructions.init);
12980
+ }
12981
+ }
12982
+ const createFeeRecipientAtaAInstructions = await getCreateAtaInstructions(
12983
+ void 0,
12984
+ authority,
12985
+ mintA.address,
12986
+ tunaConfig.data.feeRecipient,
12987
+ mintA.programAddress
12988
+ );
12989
+ instructions.push(...createFeeRecipientAtaAInstructions.init);
12990
+ const createFeeRecipientAtaBInstructions = await getCreateAtaInstructions(
12836
12991
  void 0,
12837
12992
  authority,
12838
- positionTokenMint.address,
12993
+ mintB.address,
12839
12994
  tunaConfig.data.feeRecipient,
12840
- positionTokenMint.programAddress
12995
+ mintB.programAddress
12841
12996
  );
12842
- instructions.push(...createFeeRecipientAtaInstructions.init);
12997
+ instructions.push(...createFeeRecipientAtaBInstructions.init);
12843
12998
  const ix = await liquidateTunaSpotPositionFusionInstruction(
12844
12999
  authority,
12845
- tunaPosition.address,
13000
+ tunaPosition,
12846
13001
  tunaConfig,
12847
13002
  mintA,
12848
13003
  mintB,
@@ -12854,15 +13009,25 @@ async function liquidateTunaSpotPositionFusionInstructions(authority, tunaPositi
12854
13009
  instructions.push(ix);
12855
13010
  return instructions;
12856
13011
  }
12857
- async function liquidateTunaSpotPositionFusionInstruction(authority, tunaPositionAddress, tunaConfig, mintA, mintB, vaultA, vaultB, fusionPool, withdrawPercent) {
13012
+ async function liquidateTunaSpotPositionFusionInstruction(authority, tunaPosition, tunaConfig, mintA, mintB, vaultA, vaultB, fusionPool, withdrawPercent) {
12858
13013
  const marketAddress = (await getMarketAddress(fusionPool.address))[0];
13014
+ const tunaPositionOwnerAtaA = (await (0, import_token_202223.findAssociatedTokenPda)({
13015
+ owner: tunaPosition.data.authority,
13016
+ mint: mintA.address,
13017
+ tokenProgram: mintA.programAddress
13018
+ }))[0];
13019
+ const tunaPositionOwnerAtaB = (await (0, import_token_202223.findAssociatedTokenPda)({
13020
+ owner: tunaPosition.data.authority,
13021
+ mint: mintB.address,
13022
+ tokenProgram: mintB.programAddress
13023
+ }))[0];
12859
13024
  const tunaPositionAtaA = (await (0, import_token_202223.findAssociatedTokenPda)({
12860
- owner: tunaPositionAddress,
13025
+ owner: tunaPosition.address,
12861
13026
  mint: mintA.address,
12862
13027
  tokenProgram: mintA.programAddress
12863
13028
  }))[0];
12864
13029
  const tunaPositionAtaB = (await (0, import_token_202223.findAssociatedTokenPda)({
12865
- owner: tunaPositionAddress,
13030
+ owner: tunaPosition.address,
12866
13031
  mint: mintB.address,
12867
13032
  tokenProgram: mintB.programAddress
12868
13033
  }))[0];
@@ -12917,9 +13082,12 @@ async function liquidateTunaSpotPositionFusionInstruction(authority, tunaPositio
12917
13082
  vaultAAta,
12918
13083
  vaultB: vaultB.address,
12919
13084
  vaultBAta,
12920
- tunaPosition: tunaPositionAddress,
13085
+ tunaPosition: tunaPosition.address,
12921
13086
  tunaPositionAtaA,
12922
13087
  tunaPositionAtaB,
13088
+ tunaPositionOwner: tunaPosition.data.authority,
13089
+ tunaPositionOwnerAtaA: tunaPosition.data.collateralToken == 0 /* A */ && mintA.address != NATIVE_MINT ? tunaPositionOwnerAtaA : void 0,
13090
+ tunaPositionOwnerAtaB: tunaPosition.data.collateralToken == 1 /* B */ && mintB.address != NATIVE_MINT ? tunaPositionOwnerAtaB : void 0,
12923
13091
  feeRecipientAtaA,
12924
13092
  feeRecipientAtaB,
12925
13093
  fusionPool: fusionPool.address,
@@ -12937,20 +13105,40 @@ var import_whirlpools_client11 = require("@orca-so/whirlpools-client");
12937
13105
  var import_kit95 = require("@solana/kit");
12938
13106
  var import_memo17 = require("@solana-program/memo");
12939
13107
  var import_token_202224 = require("@solana-program/token-2022");
12940
- async function liquidateTunaSpotPositionOrcaInstructions(authority, tunaPosition, tunaConfig, mintA, mintB, vaultA, vaultB, whirlpool, withdrawPercent) {
13108
+ async function liquidateTunaSpotPositionOrcaInstructions(authority, tunaPosition, tunaConfig, mintA, mintB, vaultA, vaultB, whirlpool, createTunaPositionOwnerAta, withdrawPercent) {
12941
13109
  const instructions = [];
12942
- const positionTokenMint = tunaPosition.data.positionToken == 0 /* A */ ? mintA : mintB;
12943
- const createFeeRecipientAtaInstructions = await getCreateAtaInstructions(
13110
+ if (createTunaPositionOwnerAta) {
13111
+ const collateralTokenMint = tunaPosition.data.collateralToken == 0 /* A */ ? mintA : mintB;
13112
+ if (collateralTokenMint.address != NATIVE_MINT) {
13113
+ const createPositionOwnerAtaInstructions = await getCreateAtaInstructions(
13114
+ void 0,
13115
+ authority,
13116
+ collateralTokenMint.address,
13117
+ tunaPosition.data.authority,
13118
+ collateralTokenMint.programAddress
13119
+ );
13120
+ instructions.push(...createPositionOwnerAtaInstructions.init);
13121
+ }
13122
+ }
13123
+ const createFeeRecipientAtaAInstructions = await getCreateAtaInstructions(
12944
13124
  void 0,
12945
13125
  authority,
12946
- positionTokenMint.address,
13126
+ mintA.address,
13127
+ tunaConfig.data.feeRecipient,
13128
+ mintA.programAddress
13129
+ );
13130
+ instructions.push(...createFeeRecipientAtaAInstructions.init);
13131
+ const createFeeRecipientAtaBInstructions = await getCreateAtaInstructions(
13132
+ void 0,
13133
+ authority,
13134
+ mintB.address,
12947
13135
  tunaConfig.data.feeRecipient,
12948
- positionTokenMint.programAddress
13136
+ mintB.programAddress
12949
13137
  );
12950
- instructions.push(...createFeeRecipientAtaInstructions.init);
13138
+ instructions.push(...createFeeRecipientAtaBInstructions.init);
12951
13139
  const ix = await liquidateTunaSpotPositionOrcaInstruction(
12952
13140
  authority,
12953
- tunaPosition.address,
13141
+ tunaPosition,
12954
13142
  tunaConfig,
12955
13143
  mintA,
12956
13144
  mintB,
@@ -12962,16 +13150,26 @@ async function liquidateTunaSpotPositionOrcaInstructions(authority, tunaPosition
12962
13150
  instructions.push(ix);
12963
13151
  return instructions;
12964
13152
  }
12965
- async function liquidateTunaSpotPositionOrcaInstruction(authority, tunaPositionAddress, tunaConfig, mintA, mintB, vaultA, vaultB, whirlpool, withdrawPercent) {
13153
+ async function liquidateTunaSpotPositionOrcaInstruction(authority, tunaPosition, tunaConfig, mintA, mintB, vaultA, vaultB, whirlpool, withdrawPercent) {
12966
13154
  const marketAddress = (await getMarketAddress(whirlpool.address))[0];
12967
13155
  const orcaOracleAddress = (await (0, import_whirlpools_client11.getOracleAddress)(whirlpool.address))[0];
13156
+ const tunaPositionOwnerAtaA = (await (0, import_token_202224.findAssociatedTokenPda)({
13157
+ owner: tunaPosition.data.authority,
13158
+ mint: mintA.address,
13159
+ tokenProgram: mintA.programAddress
13160
+ }))[0];
13161
+ const tunaPositionOwnerAtaB = (await (0, import_token_202224.findAssociatedTokenPda)({
13162
+ owner: tunaPosition.data.authority,
13163
+ mint: mintB.address,
13164
+ tokenProgram: mintB.programAddress
13165
+ }))[0];
12968
13166
  const tunaPositionAtaA = (await (0, import_token_202224.findAssociatedTokenPda)({
12969
- owner: tunaPositionAddress,
13167
+ owner: tunaPosition.address,
12970
13168
  mint: mintA.address,
12971
13169
  tokenProgram: mintA.programAddress
12972
13170
  }))[0];
12973
13171
  const tunaPositionAtaB = (await (0, import_token_202224.findAssociatedTokenPda)({
12974
- owner: tunaPositionAddress,
13172
+ owner: tunaPosition.address,
12975
13173
  mint: mintB.address,
12976
13174
  tokenProgram: mintB.programAddress
12977
13175
  }))[0];
@@ -13028,9 +13226,12 @@ async function liquidateTunaSpotPositionOrcaInstruction(authority, tunaPositionA
13028
13226
  vaultAAta,
13029
13227
  vaultB: vaultB.address,
13030
13228
  vaultBAta,
13031
- tunaPosition: tunaPositionAddress,
13229
+ tunaPosition: tunaPosition.address,
13032
13230
  tunaPositionAtaA,
13033
13231
  tunaPositionAtaB,
13232
+ tunaPositionOwner: tunaPosition.data.authority,
13233
+ tunaPositionOwnerAtaA: tunaPosition.data.collateralToken == 0 /* A */ && mintA.address != NATIVE_MINT ? tunaPositionOwnerAtaA : void 0,
13234
+ tunaPositionOwnerAtaB: tunaPosition.data.collateralToken == 1 /* B */ && mintB.address != NATIVE_MINT ? tunaPositionOwnerAtaB : void 0,
13034
13235
  feeRecipientAtaA,
13035
13236
  feeRecipientAtaB,
13036
13237
  whirlpool: whirlpool.address,
@@ -13395,7 +13596,7 @@ async function openAndIncreaseTunaLpPositionOrcaInstruction(authority, positionM
13395
13596
 
13396
13597
  // src/txbuilder/openAndIncreaseTunaLpPositionFusion.ts
13397
13598
  var import_fusionamm_client14 = require("@crypticdot/fusionamm-client");
13398
- var import_fusionamm_core5 = require("@crypticdot/fusionamm-core");
13599
+ var import_fusionamm_core6 = require("@crypticdot/fusionamm-core");
13399
13600
  var import_kit97 = require("@solana/kit");
13400
13601
  var import_sysvars2 = require("@solana/sysvars");
13401
13602
  var import_memo19 = require("@solana-program/memo");
@@ -13467,9 +13668,9 @@ async function openAndIncreaseTunaLpPositionFusionInstructions(rpc, authority, f
13467
13668
  mintB.programAddress
13468
13669
  );
13469
13670
  createInstructions.push(...createFeeRecipientAtaBInstructions.init);
13470
- const lowerTickArrayIndex = (0, import_fusionamm_core5.getTickArrayStartTickIndex)(args.tickLowerIndex, fusionPool.data.tickSpacing);
13671
+ const lowerTickArrayIndex = (0, import_fusionamm_core6.getTickArrayStartTickIndex)(args.tickLowerIndex, fusionPool.data.tickSpacing);
13471
13672
  const [lowerTickArrayAddress] = await (0, import_fusionamm_client14.getTickArrayAddress)(fusionPool.address, lowerTickArrayIndex);
13472
- const upperTickArrayIndex = (0, import_fusionamm_core5.getTickArrayStartTickIndex)(args.tickUpperIndex, fusionPool.data.tickSpacing);
13673
+ const upperTickArrayIndex = (0, import_fusionamm_core6.getTickArrayStartTickIndex)(args.tickUpperIndex, fusionPool.data.tickSpacing);
13473
13674
  const [upperTickArrayAddress] = await (0, import_fusionamm_client14.getTickArrayAddress)(fusionPool.address, upperTickArrayIndex);
13474
13675
  const [lowerTickArray, upperTickArray] = await (0, import_fusionamm_client14.fetchAllMaybeTickArray)(rpc, [
13475
13676
  lowerTickArrayAddress,
@@ -14537,6 +14738,7 @@ async function withdrawInstruction(authority, mint, funds, shares) {
14537
14738
  var import_fusionamm_client18 = require("@crypticdot/fusionamm-client");
14538
14739
  var import_whirlpools_client17 = require("@orca-so/whirlpools-client");
14539
14740
  var import_kit102 = require("@solana/kit");
14741
+ var import_address_lookup_table2 = require("@solana-program/address-lookup-table");
14540
14742
  var import_memo26 = require("@solana-program/memo");
14541
14743
  var import_system2 = require("@solana-program/system");
14542
14744
  var import_token2 = require("@solana-program/token");
@@ -14620,6 +14822,19 @@ async function createAddressLookupTableForMarketInstructions(rpc, poolAddress, m
14620
14822
  const addresses = await getAddressesForMarketLookupTable(rpc, poolAddress, marketMaker);
14621
14823
  return createAddressLookupTableInstructions(authority, addresses, recentSlot);
14622
14824
  }
14825
+ async function extendAddressLookupTableForMarketInstructions(rpc, poolAddress, marketMaker, authority, lookupTableAddress) {
14826
+ const marketAddresses = await getAddressesForMarketLookupTable(rpc, poolAddress, marketMaker);
14827
+ const lookupTable = await (0, import_address_lookup_table2.fetchAddressLookupTable)(rpc, lookupTableAddress);
14828
+ const existingAddresses = lookupTable.data.addresses;
14829
+ const addresses = marketAddresses.filter((addr) => !existingAddresses.includes(addr));
14830
+ const extendInstruction = (0, import_address_lookup_table2.getExtendLookupTableInstruction)({
14831
+ address: lookupTableAddress,
14832
+ addresses,
14833
+ authority,
14834
+ payer: authority
14835
+ });
14836
+ return { instructions: [extendInstruction], lookupTableAddress, addresses };
14837
+ }
14623
14838
 
14624
14839
  // src/txbuilder/repayTunaLpPositionDebt.ts
14625
14840
  var import_memo27 = require("@solana-program/memo");
@@ -15276,6 +15491,7 @@ async function rebalanceTunaLpPositionFusionInstruction(authority, tunaPosition,
15276
15491
  decreaseTunaSpotPositionOrcaInstructions,
15277
15492
  depositInstruction,
15278
15493
  depositInstructions,
15494
+ extendAddressLookupTableForMarketInstructions,
15279
15495
  fetchAllLendingPosition,
15280
15496
  fetchAllLendingPositionWithFilter,
15281
15497
  fetchAllMarket,
@@ -15400,6 +15616,7 @@ async function rebalanceTunaLpPositionFusionInstruction(authority, tunaPosition,
15400
15616
  getDepositInstructionDataDecoder,
15401
15617
  getDepositInstructionDataEncoder,
15402
15618
  getIncreaseLpPositionQuote,
15619
+ getIncreaseSpotPositionQuote,
15403
15620
  getIncreaseTunaLpPositionFusionDiscriminatorBytes,
15404
15621
  getIncreaseTunaLpPositionFusionInstruction,
15405
15622
  getIncreaseTunaLpPositionFusionInstructionDataCodec,