@0dotxyz/p0-ts-sdk 1.2.0 → 1.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.cts CHANGED
@@ -13545,6 +13545,7 @@ interface MakeSwapCollateralTxParams {
13545
13545
  bankMetadataMap: BankIntegrationMetadataMap;
13546
13546
  withdrawOpts: {
13547
13547
  totalPositionAmount: number;
13548
+ withdrawAmount?: number;
13548
13549
  withdrawBank: BankType;
13549
13550
  tokenProgram: PublicKey;
13550
13551
  };
@@ -13578,6 +13579,7 @@ interface MakeSwapDebtTxParams {
13578
13579
  bankMetadataMap: BankIntegrationMetadataMap;
13579
13580
  repayOpts: {
13580
13581
  totalPositionAmount: number;
13582
+ repayAmount?: number;
13581
13583
  repayBank: BankType;
13582
13584
  tokenProgram: PublicKey;
13583
13585
  };
package/dist/index.d.ts CHANGED
@@ -13545,6 +13545,7 @@ interface MakeSwapCollateralTxParams {
13545
13545
  bankMetadataMap: BankIntegrationMetadataMap;
13546
13546
  withdrawOpts: {
13547
13547
  totalPositionAmount: number;
13548
+ withdrawAmount?: number;
13548
13549
  withdrawBank: BankType;
13549
13550
  tokenProgram: PublicKey;
13550
13551
  };
@@ -13578,6 +13579,7 @@ interface MakeSwapDebtTxParams {
13578
13579
  bankMetadataMap: BankIntegrationMetadataMap;
13579
13580
  repayOpts: {
13580
13581
  totalPositionAmount: number;
13582
+ repayAmount?: number;
13581
13583
  repayBank: BankType;
13582
13584
  tokenProgram: PublicKey;
13583
13585
  };
package/dist/index.js CHANGED
@@ -43328,8 +43328,22 @@ async function buildSwapCollateralFlashloanTx({
43328
43328
  overrideInferAccounts,
43329
43329
  blockhash
43330
43330
  }) {
43331
- const { withdrawBank, tokenProgram: withdrawTokenProgram, totalPositionAmount } = withdrawOpts;
43331
+ const {
43332
+ withdrawBank,
43333
+ tokenProgram: withdrawTokenProgram,
43334
+ totalPositionAmount,
43335
+ withdrawAmount
43336
+ } = withdrawOpts;
43332
43337
  const { depositBank, tokenProgram: depositTokenProgram } = depositOpts;
43338
+ if (withdrawAmount !== void 0 && withdrawAmount <= 0) {
43339
+ throw new Error("withdrawAmount must be greater than 0");
43340
+ }
43341
+ const actualWithdrawAmount = Math.min(withdrawAmount ?? totalPositionAmount, totalPositionAmount);
43342
+ const isFullWithdraw = isWholePosition(
43343
+ { amount: totalPositionAmount, isLending: true },
43344
+ actualWithdrawAmount,
43345
+ withdrawBank.mintDecimals
43346
+ );
43333
43347
  const swapResult = [];
43334
43348
  const cuRequestIxs = [
43335
43349
  ComputeBudgetProgram.setComputeUnitLimit({ units: 12e5 }),
@@ -43346,7 +43360,7 @@ async function buildSwapCollateralFlashloanTx({
43346
43360
  withdrawOpts.withdrawBank.tokenSymbol
43347
43361
  );
43348
43362
  }
43349
- const adjustedAmount = new BigNumber(totalPositionAmount).div(withdrawOpts.withdrawBank.assetShareValue).times(1.0001).toNumber();
43363
+ const adjustedAmount = new BigNumber(actualWithdrawAmount).div(withdrawOpts.withdrawBank.assetShareValue).times(1.0001).toNumber();
43350
43364
  withdrawIxs = await makeKaminoWithdrawIx3({
43351
43365
  program,
43352
43366
  bank: withdrawBank,
@@ -43356,7 +43370,7 @@ async function buildSwapCollateralFlashloanTx({
43356
43370
  marginfiAccount,
43357
43371
  authority: marginfiAccount.authority,
43358
43372
  reserve,
43359
- withdrawAll: true,
43373
+ withdrawAll: isFullWithdraw,
43360
43374
  isSync: true,
43361
43375
  opts: {
43362
43376
  createAtas: false,
@@ -43380,12 +43394,12 @@ async function buildSwapCollateralFlashloanTx({
43380
43394
  bank: withdrawOpts.withdrawBank,
43381
43395
  bankMap,
43382
43396
  tokenProgram: withdrawOpts.tokenProgram,
43383
- amount: totalPositionAmount,
43397
+ amount: actualWithdrawAmount,
43384
43398
  marginfiAccount,
43385
43399
  authority: marginfiAccount.authority,
43386
43400
  driftSpotMarket: driftState.spotMarketState,
43387
43401
  userRewards: driftState.userRewards,
43388
- withdrawAll: true,
43402
+ withdrawAll: isFullWithdraw,
43389
43403
  isSync: false,
43390
43404
  opts: {
43391
43405
  createAtas: false,
@@ -43401,10 +43415,10 @@ async function buildSwapCollateralFlashloanTx({
43401
43415
  bank: withdrawBank,
43402
43416
  bankMap,
43403
43417
  tokenProgram: withdrawTokenProgram,
43404
- amount: totalPositionAmount,
43418
+ amount: actualWithdrawAmount,
43405
43419
  marginfiAccount,
43406
43420
  authority: marginfiAccount.authority,
43407
- withdrawAll: true,
43421
+ withdrawAll: isFullWithdraw,
43408
43422
  isSync: true,
43409
43423
  opts: {
43410
43424
  createAtas: false,
@@ -43417,7 +43431,7 @@ async function buildSwapCollateralFlashloanTx({
43417
43431
  }
43418
43432
  if (depositBank.mint.equals(withdrawBank.mint)) {
43419
43433
  swapResult.push({
43420
- amountToDeposit: totalPositionAmount,
43434
+ amountToDeposit: actualWithdrawAmount,
43421
43435
  swapInstructions: [],
43422
43436
  setupInstructions: [],
43423
43437
  swapLookupTables: []
@@ -43433,7 +43447,7 @@ async function buildSwapCollateralFlashloanTx({
43433
43447
  quoteParams: {
43434
43448
  inputMint: withdrawBank.mint.toBase58(),
43435
43449
  outputMint: depositBank.mint.toBase58(),
43436
- amount: uiToNative(totalPositionAmount, withdrawBank.mintDecimals).toNumber(),
43450
+ amount: uiToNative(actualWithdrawAmount, withdrawBank.mintDecimals).toNumber(),
43437
43451
  dynamicSlippage: swapOpts.jupiterOptions ? swapOpts.jupiterOptions.slippageMode === "DYNAMIC" : true,
43438
43452
  slippageBps: swapOpts.jupiterOptions?.slippageBps,
43439
43453
  swapMode: "ExactIn",
@@ -43700,56 +43714,74 @@ async function buildSwapDebtFlashloanTx({
43700
43714
  overrideInferAccounts,
43701
43715
  blockhash
43702
43716
  }) {
43703
- const { repayBank, tokenProgram: repayTokenProgram, totalPositionAmount } = repayOpts;
43717
+ const {
43718
+ repayBank,
43719
+ tokenProgram: repayTokenProgram,
43720
+ totalPositionAmount,
43721
+ repayAmount
43722
+ } = repayOpts;
43704
43723
  const { borrowBank, tokenProgram: borrowTokenProgram } = borrowOpts;
43724
+ if (repayAmount !== void 0 && repayAmount <= 0) {
43725
+ throw new Error("repayAmount must be greater than 0");
43726
+ }
43727
+ const actualRepayAmount = Math.min(repayAmount ?? totalPositionAmount, totalPositionAmount);
43705
43728
  const swapResult = [];
43706
43729
  const cuRequestIxs = [
43707
43730
  ComputeBudgetProgram.setComputeUnitLimit({ units: 12e5 }),
43708
43731
  ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 1 })
43709
43732
  ];
43710
- if (borrowBank.mint.equals(repayBank.mint)) {
43711
- swapResult.push({
43712
- amountToRepay: totalPositionAmount,
43713
- borrowAmount: totalPositionAmount,
43714
- swapInstructions: [],
43715
- setupInstructions: [],
43716
- swapLookupTables: []
43717
- });
43718
- } else {
43719
- const destinationTokenAccount = getAssociatedTokenAddressSync(
43720
- repayBank.mint,
43721
- marginfiAccount.authority,
43722
- true,
43723
- repayTokenProgram.equals(TOKEN_2022_PROGRAM_ID) ? TOKEN_2022_PROGRAM_ID : void 0
43733
+ const destinationTokenAccount = getAssociatedTokenAddressSync(
43734
+ repayBank.mint,
43735
+ marginfiAccount.authority,
43736
+ true,
43737
+ repayTokenProgram.equals(TOKEN_2022_PROGRAM_ID) ? TOKEN_2022_PROGRAM_ID : void 0
43738
+ );
43739
+ const jupiterApiClient = swapOpts.jupiterOptions?.configParams?.basePath ? new SwapApi(new Configuration(swapOpts.jupiterOptions.configParams)) : createJupiterApiClient(swapOpts.jupiterOptions?.configParams);
43740
+ const estimateQuote = await jupiterApiClient.quoteGet({
43741
+ inputMint: borrowBank.mint.toBase58(),
43742
+ outputMint: repayBank.mint.toBase58(),
43743
+ amount: uiToNative(actualRepayAmount, repayBank.mintDecimals).toNumber(),
43744
+ swapMode: "ExactOut",
43745
+ dynamicSlippage: swapOpts.jupiterOptions ? swapOpts.jupiterOptions.slippageMode === "DYNAMIC" : true,
43746
+ slippageBps: swapOpts.jupiterOptions?.slippageBps
43747
+ });
43748
+ const estimatedBorrowAmount = nativeToUi(
43749
+ estimateQuote.otherAmountThreshold,
43750
+ borrowBank.mintDecimals
43751
+ );
43752
+ const swapResponses = await getJupiterSwapIxsForFlashloan({
43753
+ quoteParams: {
43754
+ inputMint: borrowBank.mint.toBase58(),
43755
+ outputMint: repayBank.mint.toBase58(),
43756
+ amount: uiToNative(estimatedBorrowAmount, borrowBank.mintDecimals).toNumber(),
43757
+ dynamicSlippage: swapOpts.jupiterOptions ? swapOpts.jupiterOptions.slippageMode === "DYNAMIC" : true,
43758
+ slippageBps: swapOpts.jupiterOptions?.slippageBps,
43759
+ swapMode: "ExactIn",
43760
+ platformFeeBps: swapOpts.jupiterOptions?.platformFeeBps,
43761
+ onlyDirectRoutes: swapOpts.jupiterOptions?.directRoutesOnly ?? false
43762
+ },
43763
+ authority: marginfiAccount.authority,
43764
+ connection,
43765
+ destinationTokenAccount,
43766
+ configParams: swapOpts.jupiterOptions?.configParams
43767
+ });
43768
+ swapResponses.forEach((response) => {
43769
+ const outAmount = nativeToUi(response.quoteResponse.outAmount, repayBank.mintDecimals);
43770
+ const outAmountThreshold = nativeToUi(
43771
+ response.quoteResponse.otherAmountThreshold,
43772
+ repayBank.mintDecimals
43724
43773
  );
43725
- const swapResponses = await getJupiterSwapIxsForFlashloan({
43726
- quoteParams: {
43727
- inputMint: borrowBank.mint.toBase58(),
43728
- outputMint: repayBank.mint.toBase58(),
43729
- amount: uiToNative(totalPositionAmount, repayBank.mintDecimals).toNumber(),
43730
- dynamicSlippage: swapOpts.jupiterOptions ? swapOpts.jupiterOptions.slippageMode === "DYNAMIC" : true,
43731
- slippageBps: swapOpts.jupiterOptions?.slippageBps,
43732
- swapMode: "ExactOut",
43733
- platformFeeBps: swapOpts.jupiterOptions?.platformFeeBps,
43734
- onlyDirectRoutes: swapOpts.jupiterOptions?.directRoutesOnly ?? false
43735
- },
43736
- authority: marginfiAccount.authority,
43737
- connection,
43738
- destinationTokenAccount,
43739
- configParams: swapOpts.jupiterOptions?.configParams
43740
- });
43741
- swapResponses.forEach((response) => {
43742
- const borrowAmount = nativeToUi(response.quoteResponse.inAmount, borrowBank.mintDecimals);
43743
- swapResult.push({
43744
- amountToRepay: totalPositionAmount,
43745
- borrowAmount,
43746
- swapInstructions: [response.swapInstruction],
43747
- setupInstructions: response.setupInstructions,
43748
- swapLookupTables: response.addressLookupTableAddresses,
43749
- quoteResponse: response.quoteResponse
43750
- });
43774
+ const amountToRepay = outAmount > totalPositionAmount ? totalPositionAmount : outAmountThreshold;
43775
+ const borrowAmount = nativeToUi(response.quoteResponse.inAmount, borrowBank.mintDecimals);
43776
+ swapResult.push({
43777
+ amountToRepay,
43778
+ borrowAmount,
43779
+ swapInstructions: [response.swapInstruction],
43780
+ setupInstructions: response.setupInstructions,
43781
+ swapLookupTables: response.addressLookupTableAddresses,
43782
+ quoteResponse: response.quoteResponse
43751
43783
  });
43752
- }
43784
+ });
43753
43785
  if (swapResult.length === 0) {
43754
43786
  throw new Error(
43755
43787
  `No swap routes found for ${borrowBank.mint.toBase58()} -> ${repayBank.mint.toBase58()}`