@0dotxyz/p0-ts-sdk 2.5.5-alpha.5 → 2.5.5-alpha.7

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.cjs CHANGED
@@ -65603,6 +65603,34 @@ async function getTitanExactOutViaHttpProxy(params) {
65603
65603
  }
65604
65604
 
65605
65605
  // src/services/account/utils/swap.utils.ts
65606
+ function resolvePinnedSwapRoute(swapIxs, expectedInAmountNative) {
65607
+ const { quoteResponse } = swapIxs;
65608
+ const expectedIn = new BN9__default.default(expectedInAmountNative);
65609
+ let minOut;
65610
+ try {
65611
+ minOut = new BN9__default.default(quoteResponse.otherAmountThreshold);
65612
+ } catch {
65613
+ minOut = new BN9__default.default(0);
65614
+ }
65615
+ if (minOut.lten(0)) {
65616
+ throw new Error(
65617
+ `Pinned swap route (swapOpts.swapIxs) has no usable min-out: quoteResponse.otherAmountThreshold is "${quoteResponse.otherAmountThreshold}". The min-out sizes the follow-up amount (e.g. the loop's deposit) \u2014 without it the flow would deposit zero collateral and fail init health.`
65618
+ );
65619
+ }
65620
+ const pinnedIn = new BN9__default.default(quoteResponse.inAmount);
65621
+ if (!pinnedIn.eq(expectedIn)) {
65622
+ throw new Error(
65623
+ `Pinned swap route (swapOpts.swapIxs) was quoted for a different input amount than the flow will swap: quote inAmount=${pinnedIn.toString()}, flow swap input=${expectedIn.toString()} (native units). Re-quote the pinned route for the exact flow amount.`
65624
+ );
65625
+ }
65626
+ return {
65627
+ swapInstructions: swapIxs.instructions,
65628
+ setupInstructions: [],
65629
+ lookupTables: swapIxs.lookupTables,
65630
+ quoteResponse,
65631
+ outputAmountNative: minOut
65632
+ };
65633
+ }
65606
65634
  function getSwapProviderFn({
65607
65635
  attemptProvider,
65608
65636
  maxSwapTotalAccounts,
@@ -65709,16 +65737,12 @@ var getSwapIxsForFlashloan = async (params) => {
65709
65737
  maxSwapTotalAccounts
65710
65738
  } = params;
65711
65739
  if (swapOpts.swapIxs) {
65740
+ const pinned = resolvePinnedSwapRoute(swapOpts.swapIxs, amount);
65712
65741
  return {
65713
- swapInstructions: swapOpts.swapIxs.instructions,
65714
- setupInstructions: [],
65715
- addressLookupTableAddresses: swapOpts.swapIxs.lookupTables,
65716
- quoteResponse: {
65717
- inAmount: String(amount),
65718
- outAmount: "0",
65719
- otherAmountThreshold: "0",
65720
- slippageBps: 0
65721
- }
65742
+ swapInstructions: pinned.swapInstructions,
65743
+ setupInstructions: pinned.setupInstructions,
65744
+ addressLookupTableAddresses: pinned.lookupTables,
65745
+ quoteResponse: pinned.quoteResponse
65722
65746
  };
65723
65747
  }
65724
65748
  const provider = swapOpts.swapConfig?.provider ?? "JUPITER" /* JUPITER */;
@@ -68770,6 +68794,7 @@ async function makeBridgedSwapDebtTx(params) {
68770
68794
  return await makeSwapDebtTx(directParams);
68771
68795
  } catch (directError) {
68772
68796
  if (!isDecomposableSwapError(directError)) throw directError;
68797
+ if (directParams.swapOpts.swapIxs) throw directError;
68773
68798
  const bridged = await tryBridgedDebtSwap(directParams, bridgeOpts);
68774
68799
  if (bridged) return bridged;
68775
68800
  throw directError;
@@ -69187,17 +69212,13 @@ async function buildDepositIxs(params, amountUi) {
69187
69212
  async function runLoopSwapEngine(descriptor, params) {
69188
69213
  const { connection, swapOpts, marginfiAccount, swapEngineRunner } = params;
69189
69214
  if (swapOpts.swapIxs) {
69215
+ const pinned = resolvePinnedSwapRoute(swapOpts.swapIxs, descriptor.inAmountNative);
69190
69216
  return {
69191
- swapInstructions: swapOpts.swapIxs.instructions,
69192
- setupInstructions: [],
69193
- swapLuts: swapOpts.swapIxs.lookupTables,
69194
- quoteResponse: {
69195
- inAmount: String(descriptor.inAmountNative),
69196
- outAmount: "0",
69197
- otherAmountThreshold: "0",
69198
- slippageBps: 0
69199
- },
69200
- outputAmountNative: new BN9__default.default(0)
69217
+ swapInstructions: pinned.swapInstructions,
69218
+ setupInstructions: pinned.setupInstructions,
69219
+ swapLuts: pinned.lookupTables,
69220
+ quoteResponse: pinned.quoteResponse,
69221
+ outputAmountNative: pinned.outputAmountNative
69201
69222
  };
69202
69223
  }
69203
69224
  const runEngine = swapEngineRunner ?? runSwapEngine;
@@ -69272,6 +69293,7 @@ async function makeBridgedLoopTx(params) {
69272
69293
  return await makeLoopTx(loopParams);
69273
69294
  } catch (directError) {
69274
69295
  if (!isDecomposableSwapError(directError)) throw directError;
69296
+ if (loopParams.swapOpts.swapIxs) throw directError;
69275
69297
  const bridged = await tryBridgedLoop(loopParams, bridgeOpts);
69276
69298
  if (bridged) return bridged;
69277
69299
  throw directError;
@@ -69290,7 +69312,7 @@ async function tryBridgedLoop(params, bridgeOpts) {
69290
69312
  });
69291
69313
  const oraclePriceOf = (bank) => params.oraclePrices.get(bank.address.toBase58())?.priceRealtime.price.toNumber() ?? 0;
69292
69314
  const borrowBankPrice = oraclePriceOf(borrowBank);
69293
- if (borrowBankPrice) return null;
69315
+ if (borrowBankPrice <= 0) return null;
69294
69316
  const tokenProgramCache = new Map(bridgeOpts?.tokenProgramByMint);
69295
69317
  return tryBridgeCandidates({
69296
69318
  usableBridgeBanks,
@@ -69298,9 +69320,9 @@ async function tryBridgedLoop(params, bridgeOpts) {
69298
69320
  bridgeTokenSide: "borrow",
69299
69321
  abortSignal: bridgeOpts?.abortSignal,
69300
69322
  buildBundleThroughBridge: async (bridgeBank) => {
69301
- const birdgeBankPrice = oraclePriceOf(bridgeBank);
69302
- if (birdgeBankPrice <= 0) return null;
69303
- const bridgeBorrowUi = params.borrowOpts.borrowAmount * borrowBankPrice / birdgeBankPrice;
69323
+ const bridgeBankPrice = oraclePriceOf(bridgeBank);
69324
+ if (bridgeBankPrice <= 0) return null;
69325
+ const bridgeBorrowUi = params.borrowOpts.borrowAmount * borrowBankPrice / bridgeBankPrice;
69304
69326
  if (bridgeBorrowUi <= 0) return null;
69305
69327
  const bridgeTokenProgram = await resolveTokenProgramForMint(
69306
69328
  bridgeBank.mint,
@@ -69317,7 +69339,7 @@ async function tryBridgedLoop(params, bridgeOpts) {
69317
69339
  borrowAmount: bridgeBorrowUi,
69318
69340
  borrowBank: bridgeBank,
69319
69341
  tokenProgram: bridgeTokenProgram,
69320
- marketPrice: birdgeBankPrice
69342
+ marketPrice: bridgeBankPrice
69321
69343
  }
69322
69344
  });
69323
69345
  if (!firstLeg.quoteResponse) return null;
@@ -69333,7 +69355,7 @@ async function tryBridgedLoop(params, bridgeOpts) {
69333
69355
  repayAmount: bridgeBorrowUi,
69334
69356
  repayBank: bridgeBank,
69335
69357
  tokenProgram: bridgeTokenProgram,
69336
- marketPrice: birdgeBankPrice
69358
+ marketPrice: bridgeBankPrice
69337
69359
  },
69338
69360
  borrowOpts: {
69339
69361
  borrowBank,
@@ -69789,6 +69811,7 @@ async function makeBridgedSwapCollateralTx(params) {
69789
69811
  return await makeSwapCollateralTx(directParams);
69790
69812
  } catch (directError) {
69791
69813
  if (!isDecomposableSwapError(directError)) throw directError;
69814
+ if (directParams.swapOpts.swapIxs) throw directError;
69792
69815
  const bridged = await tryBridgedCollateralSwap(directParams, bridgeOpts);
69793
69816
  if (bridged) return bridged;
69794
69817
  throw directError;
@@ -71774,6 +71797,10 @@ async function tryBridgeCandidates(args) {
71774
71797
  if (result) return result;
71775
71798
  } catch (e) {
71776
71799
  if (isAbortError(e)) throw e;
71800
+ console.warn(
71801
+ `[bridge-routing] candidate ${bridgeBank.tokenSymbol ?? bridgeBank.mint.toBase58()} failed:`,
71802
+ e instanceof Error ? e.message : e
71803
+ );
71777
71804
  }
71778
71805
  }
71779
71806
  if (args.usableBridgeBanks.length === 0 && args.conflictingBridgeBanks.length > 0) {
@@ -76918,6 +76945,7 @@ exports.requireBank = requireBank;
76918
76945
  exports.requireTokenProgram = requireTokenProgram;
76919
76946
  exports.resolveAmount = resolveAmount;
76920
76947
  exports.resolveBridgeCandidateBanks = resolveBridgeCandidateBanks;
76948
+ exports.resolvePinnedSwapRoute = resolvePinnedSwapRoute;
76921
76949
  exports.resolveTokenProgramForMint = resolveTokenProgramForMint;
76922
76950
  exports.runSwapEngine = runSwapEngine;
76923
76951
  exports.selectLutsForAccountAction = selectLutsForAccountAction;