@0dotxyz/p0-ts-sdk 1.2.0 → 1.2.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.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()}`
@@ -44723,7 +44755,9 @@ async function computeSmartCrank({
44723
44755
  };
44724
44756
  }
44725
44757
  async function makeSmartCrankSwbFeedIx(params) {
44758
+ console.log("[makeSmartCrankSwbFeedIx] Called");
44726
44759
  const crankResult = await computeSmartCrank(params);
44760
+ console.log("[makeSmartCrankSwbFeedIx] Crank result:", crankResult);
44727
44761
  if (crankResult.uncrankableLiabilities.length > 0) {
44728
44762
  console.log(
44729
44763
  "Uncrankable liability details:",
@@ -44759,6 +44793,7 @@ async function makeSmartCrankSwbFeedIx(params) {
44759
44793
  );
44760
44794
  }
44761
44795
  const oraclesToCrank = crankResult.requiredOracles;
44796
+ console.log("[makeSmartCrankSwbFeedIx] Oracles to crank:", oraclesToCrank);
44762
44797
  const { instructions: instructions2, luts } = await makeUpdateSwbFeedIx({
44763
44798
  swbPullOracles: oraclesToCrank,
44764
44799
  feePayer: params.marginfiAccount.authority,
@@ -44795,6 +44830,9 @@ async function makeCrankSwbFeedIx(marginfiAccount, bankMap, newBanksPk, provider
44795
44830
  }
44796
44831
  }
44797
44832
  async function makeUpdateSwbFeedIx(props) {
44833
+ console.log(
44834
+ `[makeUpdateSwbFeedIx] Called with ${props.swbPullOracles.length} oracles, feePayer: ${props.feePayer.toBase58()}`
44835
+ );
44798
44836
  const seen = /* @__PURE__ */ new Set();
44799
44837
  const uniqueOracles = props.swbPullOracles.filter((oracle) => {
44800
44838
  const key = oracle.key.toBase58();
@@ -44802,6 +44840,14 @@ async function makeUpdateSwbFeedIx(props) {
44802
44840
  seen.add(key);
44803
44841
  return true;
44804
44842
  });
44843
+ console.log(
44844
+ `[makeUpdateSwbFeedIx] ${uniqueOracles.length} unique oracles after dedup (removed ${props.swbPullOracles.length - uniqueOracles.length})`
44845
+ );
44846
+ uniqueOracles.forEach(
44847
+ (o) => console.log(
44848
+ `[makeUpdateSwbFeedIx] - ${o.key.toBase58()} (hasSwitchboardData: ${!!o.price?.switchboardData})`
44849
+ )
44850
+ );
44805
44851
  const swbProgram = await AnchorUtils.loadProgramFromConnection(props.connection);
44806
44852
  const pullFeedInstances = uniqueOracles.map((oracle) => {
44807
44853
  const pullFeed = new PullFeed(swbProgram, oracle.key);
@@ -44817,6 +44863,7 @@ async function makeUpdateSwbFeedIx(props) {
44817
44863
  return pullFeed;
44818
44864
  });
44819
44865
  if (pullFeedInstances.length === 0) {
44866
+ console.log(`[makeUpdateSwbFeedIx] No pull feed instances, returning early`);
44820
44867
  return { instructions: [], luts: [] };
44821
44868
  }
44822
44869
  const crossbarClient = new CrossbarClient(
@@ -44826,11 +44873,19 @@ async function makeUpdateSwbFeedIx(props) {
44826
44873
  if (!gatewayUrls || gatewayUrls.length === 0) {
44827
44874
  throw new Error(`No gateways available for mainnet`);
44828
44875
  }
44876
+ console.log(`[makeUpdateSwbFeedIx] Fetched ${gatewayUrls.length} gateways`);
44829
44877
  const gatewayUrl = gatewayUrls[0];
44830
44878
  if (!gatewayUrl) {
44831
44879
  throw new Error(`Invalid gateway URL received formainnet`);
44832
44880
  }
44833
44881
  const gateway = new Gateway(swbProgram, gatewayUrl);
44882
+ console.log(
44883
+ `[makeUpdateSwbFeedIx] Fetching update ix for ${pullFeedInstances.length} feeds via gateway: ${gateway.gatewayUrl}`
44884
+ );
44885
+ console.log(
44886
+ `[makeUpdateSwbFeedIx] pullFeedInstances:`,
44887
+ pullFeedInstances.map((f) => ({ key: f.pubkey.toBase58(), hasData: !!f.data }))
44888
+ );
44834
44889
  const [pullIx, luts] = await PullFeed.fetchUpdateManyIx(swbProgram, {
44835
44890
  feeds: pullFeedInstances,
44836
44891
  gateway: gateway.gatewayUrl,
@@ -44838,6 +44893,7 @@ async function makeUpdateSwbFeedIx(props) {
44838
44893
  payer: props.feePayer,
44839
44894
  crossbarClient
44840
44895
  });
44896
+ console.log(`[makeUpdateSwbFeedIx] Got ${pullIx.length} instructions, ${luts.length} LUTs`);
44841
44897
  return { instructions: pullIx, luts };
44842
44898
  }
44843
44899