@0dotxyz/p0-ts-sdk 2.2.2 → 2.2.3-alpha.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.cjs CHANGED
@@ -15629,12 +15629,20 @@ function makeEndFlashLoanIx(mfiProgram, accounts, remainingAccounts = []) {
15629
15629
  }).accountsPartial(optionalAccounts).remainingAccounts(remainingAccounts).instruction();
15630
15630
  }
15631
15631
  async function makeAccountTransferToNewAccountIx(mfProgram, accounts) {
15632
- const { oldMarginfiAccount, newMarginfiAccount, newAuthority, feePayer, ...optionalAccounts } = accounts;
15632
+ const {
15633
+ oldMarginfiAccount,
15634
+ newMarginfiAccount,
15635
+ newAuthority,
15636
+ globalFeeWallet,
15637
+ feePayer,
15638
+ ...optionalAccounts
15639
+ } = accounts;
15633
15640
  return mfProgram.methods.transferToNewAccount().accounts({
15634
15641
  oldMarginfiAccount,
15635
15642
  newMarginfiAccount,
15636
15643
  newAuthority,
15637
- globalFeeWallet: feePayer
15644
+ globalFeeWallet,
15645
+ feePayer
15638
15646
  }).accountsPartial(optionalAccounts).instruction();
15639
15647
  }
15640
15648
  async function makeGroupInitIx(mfProgram, accounts) {
@@ -19996,7 +20004,7 @@ function dtoToOraclePrice(dto) {
19996
20004
  }
19997
20005
 
19998
20006
  // src/services/price/utils/crankability.utils.ts
19999
- async function checkBatchOracleCrankability(feedHashes, crossbarUrl = "https://integrator-crossbar.prod.mrgn.app") {
20007
+ async function checkBatchOracleCrankability(feedHashes, crossbarUrl = "https://crossbar.0.xyz") {
20000
20008
  const results = /* @__PURE__ */ new Map();
20001
20009
  if (feedHashes.length === 0) {
20002
20010
  return results;
@@ -20528,16 +20536,77 @@ function createActiveEmodePairFromPairs(pairs) {
20528
20536
  assetWeightInit: bestPair.assetWeightInit
20529
20537
  };
20530
20538
  }
20539
+ function indexConfiguredPairs(emodePairs) {
20540
+ const configured = [];
20541
+ const liabTagByBank = /* @__PURE__ */ new Map();
20542
+ for (const p of emodePairs) {
20543
+ if (p.collateralBankTag === 0 /* UNSET */ || p.liabilityBankTag === 0 /* UNSET */) {
20544
+ continue;
20545
+ }
20546
+ const liabStr = p.liabilityBank.toBase58();
20547
+ const liabTagStr = p.liabilityBankTag.toString();
20548
+ configured.push({
20549
+ orig: p,
20550
+ liabStr,
20551
+ liabTagStr,
20552
+ collTagStr: p.collateralBankTag.toString(),
20553
+ collStrs: p.collateralBanks.map((b) => b.toBase58())
20554
+ });
20555
+ liabTagByBank.set(liabStr, liabTagStr);
20556
+ }
20557
+ return { configured, liabTagByBank };
20558
+ }
20559
+ function activePairsFromIndex(configured, liabTagByBank, liabSet, collSet) {
20560
+ const requiredTags = /* @__PURE__ */ new Set();
20561
+ for (const liab of liabSet) {
20562
+ const tag = liabTagByBank.get(liab);
20563
+ if (!tag) return [];
20564
+ requiredTags.add(tag);
20565
+ }
20566
+ const possible = configured.filter(
20567
+ (p) => liabSet.has(p.liabStr) && p.collStrs.some((c) => collSet.has(c))
20568
+ );
20569
+ if (possible.length === 0) return [];
20570
+ const byCollTag = {};
20571
+ for (const p of possible) {
20572
+ (byCollTag[p.collTagStr] ||= []).push(p);
20573
+ }
20574
+ const validGroups = [];
20575
+ for (const group of Object.values(byCollTag)) {
20576
+ const supports = new Set(group.map((p) => p.liabTagStr));
20577
+ let coversAll = true;
20578
+ for (const rt of requiredTags) {
20579
+ if (!supports.has(rt)) {
20580
+ coversAll = false;
20581
+ break;
20582
+ }
20583
+ }
20584
+ if (coversAll) validGroups.push(group);
20585
+ }
20586
+ if (validGroups.length === 0) return [];
20587
+ return validGroups.flat().map((p) => p.orig);
20588
+ }
20531
20589
  function computeEmodeImpacts(emodePairs, activeLiabilities, activeCollateral, allBanks) {
20532
- const toKey = (k) => k.toBase58();
20533
- const basePairs = computeActiveEmodePairs(emodePairs, activeLiabilities, activeCollateral);
20534
- const baseOn = basePairs.length > 0;
20535
- const liabTagMap = /* @__PURE__ */ new Map();
20590
+ const activeLiabilitiesSet = new Set(activeLiabilities.map((b) => b.toBase58()));
20591
+ const activeCollateralSet = new Set(activeCollateral.map((b) => b.toBase58()));
20592
+ const { configured, liabTagByBank } = indexConfiguredPairs(emodePairs);
20593
+ const liabTagMapAll = /* @__PURE__ */ new Map();
20536
20594
  for (const p of emodePairs) {
20537
- liabTagMap.set(p.liabilityBank.toBase58(), p.liabilityBankTag.toString());
20595
+ liabTagMapAll.set(p.liabilityBank.toBase58(), p.liabilityBankTag.toString());
20538
20596
  }
20597
+ const allCollateralBankStrs = /* @__PURE__ */ new Set();
20598
+ for (const p of emodePairs) {
20599
+ for (const c of p.collateralBanks) allCollateralBankStrs.add(c.toBase58());
20600
+ }
20601
+ const basePairs = activePairsFromIndex(
20602
+ configured,
20603
+ liabTagByBank,
20604
+ activeLiabilitiesSet,
20605
+ activeCollateralSet
20606
+ );
20607
+ const baseOn = basePairs.length > 0;
20539
20608
  const existingTags = new Set(
20540
- activeLiabilities.map((l) => liabTagMap.get(l.toBase58())).filter((t) => !!t)
20609
+ Array.from(activeLiabilitiesSet).map((l) => liabTagMapAll.get(l)).filter((t) => !!t)
20541
20610
  );
20542
20611
  function minWeight(ps) {
20543
20612
  let m = ps[0].assetWeightInit;
@@ -20554,27 +20623,26 @@ function computeEmodeImpacts(emodePairs, activeLiabilities, activeCollateral, al
20554
20623
  if (aMin.lt(bMin)) return 3 /* ReduceEmode */;
20555
20624
  return 1 /* ExtendEmode */;
20556
20625
  }
20557
- function simulate(bank, action) {
20558
- bank.equals(new web3_js.PublicKey("CCKtUs6Cgwo4aaQUmBPmyoApH2gUDErxNZCAntD6LYGh"));
20559
- let L = [...activeLiabilities], C = [...activeCollateral];
20626
+ function simulate(bankStr, action) {
20627
+ const L = new Set(activeLiabilitiesSet), C = new Set(activeCollateralSet);
20560
20628
  switch (action) {
20561
20629
  case "borrow":
20562
- if (!L.some((x) => x.equals(bank))) L.push(bank);
20630
+ L.add(bankStr);
20563
20631
  break;
20564
20632
  case "repay":
20565
- L = L.filter((x) => !x.equals(bank));
20633
+ L.delete(bankStr);
20566
20634
  break;
20567
20635
  case "supply":
20568
- if (!C.some((x) => x.equals(bank))) C.push(bank);
20636
+ C.add(bankStr);
20569
20637
  break;
20570
20638
  case "withdraw":
20571
- C = C.filter((x) => !x.equals(bank));
20639
+ C.delete(bankStr);
20572
20640
  break;
20573
20641
  }
20574
- const after = computeActiveEmodePairs(emodePairs, L, C);
20642
+ const after = activePairsFromIndex(configured, liabTagByBank, L, C);
20575
20643
  let status = diffState(basePairs, after);
20576
20644
  if (action === "borrow") {
20577
- const tag = liabTagMap.get(bank.toBase58());
20645
+ const tag = liabTagMapAll.get(bankStr);
20578
20646
  if (!tag) {
20579
20647
  status = baseOn ? 4 /* RemoveEmode */ : 5 /* InactiveEmode */;
20580
20648
  } else if (baseOn) {
@@ -20610,66 +20678,29 @@ function computeEmodeImpacts(emodePairs, activeLiabilities, activeCollateral, al
20610
20678
  }
20611
20679
  const result = {};
20612
20680
  for (const bank of allBanks) {
20613
- const key = toKey(bank);
20681
+ const key = bank.toBase58();
20614
20682
  const impact = {};
20615
- if (!activeCollateral.some((x) => x.equals(bank))) {
20616
- impact.borrowImpact = simulate(bank, "borrow");
20683
+ if (!activeCollateralSet.has(key)) {
20684
+ impact.borrowImpact = simulate(key, "borrow");
20617
20685
  }
20618
- const collSet = new Set(emodePairs.flatMap((p) => p.collateralBanks.map((c) => c.toBase58())));
20619
- if (collSet.has(key) && !activeCollateral.some((x) => x.equals(bank)) && !activeLiabilities.some((x) => x.equals(bank))) {
20620
- impact.supplyImpact = simulate(bank, "supply");
20686
+ if (allCollateralBankStrs.has(key) && !activeCollateralSet.has(key) && !activeLiabilitiesSet.has(key)) {
20687
+ impact.supplyImpact = simulate(key, "supply");
20621
20688
  }
20622
- if (activeLiabilities.some((x) => x.equals(bank))) {
20623
- impact.repayAllImpact = simulate(bank, "repay");
20689
+ if (activeLiabilitiesSet.has(key)) {
20690
+ impact.repayAllImpact = simulate(key, "repay");
20624
20691
  }
20625
- if (activeCollateral.some((x) => x.equals(bank))) {
20626
- impact.withdrawAllImpact = simulate(bank, "withdraw");
20692
+ if (activeCollateralSet.has(key)) {
20693
+ impact.withdrawAllImpact = simulate(key, "withdraw");
20627
20694
  }
20628
20695
  result[key] = impact;
20629
20696
  }
20630
20697
  return result;
20631
20698
  }
20632
20699
  function computeActiveEmodePairs(emodePairs, activeLiabilities, activeCollateral) {
20633
- const configured = emodePairs.filter(
20634
- (p) => p.collateralBankTag !== 0 /* UNSET */ && p.liabilityBankTag !== 0 /* UNSET */
20635
- );
20636
- const liabTagByBank = /* @__PURE__ */ new Map();
20637
- for (const p of configured) {
20638
- liabTagByBank.set(p.liabilityBank.toBase58(), p.liabilityBankTag.toString());
20639
- }
20640
- const requiredTags = /* @__PURE__ */ new Set();
20641
- for (const liab of activeLiabilities) {
20642
- const tag = liabTagByBank.get(liab.toBase58());
20643
- if (!tag) {
20644
- return [];
20645
- }
20646
- requiredTags.add(tag);
20647
- }
20648
- const possible = configured.filter(
20649
- (p) => activeLiabilities.some((l) => l.equals(p.liabilityBank)) && p.collateralBanks.some((c) => activeCollateral.some((a) => a.equals(c)))
20650
- );
20651
- if (possible.length === 0) return [];
20652
- const byCollTag = {};
20653
- for (const p of possible) {
20654
- const ct = p.collateralBankTag.toString();
20655
- (byCollTag[ct] ||= []).push(p);
20656
- }
20657
- const validGroups = [];
20658
- for (const group of Object.values(byCollTag)) {
20659
- const supports = new Set(group.map((p) => p.liabilityBankTag.toString()));
20660
- let coversAll = true;
20661
- for (const rt of requiredTags) {
20662
- if (!supports.has(rt)) {
20663
- coversAll = false;
20664
- break;
20665
- }
20666
- }
20667
- if (coversAll) {
20668
- validGroups.push(group);
20669
- }
20670
- }
20671
- if (validGroups.length === 0) return [];
20672
- return validGroups.flat();
20700
+ const { configured, liabTagByBank } = indexConfiguredPairs(emodePairs);
20701
+ const liabSet = new Set(activeLiabilities.map((b) => b.toBase58()));
20702
+ const collSet = new Set(activeCollateral.map((b) => b.toBase58()));
20703
+ return activePairsFromIndex(configured, liabTagByBank, liabSet, collSet);
20673
20704
  }
20674
20705
  function computeBalanceUsdValue(params) {
20675
20706
  const {
@@ -36088,6 +36119,48 @@ async function makeCloseMarginfiAccountTx({
36088
36119
  );
36089
36120
  return closeTx;
36090
36121
  }
36122
+ async function makeAccountTransferToNewAccountTx({
36123
+ connection,
36124
+ program,
36125
+ marginfiAccount,
36126
+ newMarginfiAccount,
36127
+ newAuthority,
36128
+ feePayer,
36129
+ feePayerKeypair
36130
+ }) {
36131
+ const [feeStateKey] = web3_js.PublicKey.findProgramAddressSync(
36132
+ [Buffer.from("feestate", "utf-8")],
36133
+ program.programId
36134
+ );
36135
+ const feeState = await program.account.feeState.fetch(feeStateKey);
36136
+ const transferIx = await instructions_default.makeAccountTransferToNewAccountIx(program, {
36137
+ oldMarginfiAccount: marginfiAccount.address,
36138
+ newMarginfiAccount: newMarginfiAccount.publicKey,
36139
+ newAuthority,
36140
+ globalFeeWallet: feeState.globalFeeWallet,
36141
+ feePayer
36142
+ });
36143
+ const {
36144
+ value: { blockhash }
36145
+ } = await connection.getLatestBlockhashAndContext("confirmed");
36146
+ const signers = [newMarginfiAccount];
36147
+ if (feePayerKeypair) signers.push(feePayerKeypair);
36148
+ const transferTx = addTransactionMetadata(
36149
+ new web3_js.VersionedTransaction(
36150
+ new web3_js.TransactionMessage({
36151
+ instructions: [transferIx],
36152
+ payerKey: feePayer,
36153
+ recentBlockhash: blockhash
36154
+ }).compileToV0Message([])
36155
+ ),
36156
+ {
36157
+ signers,
36158
+ addressLookupTables: [],
36159
+ type: "TRANSFER_AUTH" /* TRANSFER_AUTH */
36160
+ }
36161
+ );
36162
+ return transferTx;
36163
+ }
36091
36164
  async function makeCreateAccountTxWithProjection(props) {
36092
36165
  const [marginfiAccountAddress] = deriveMarginfiAccount(
36093
36166
  props.program.programId,
@@ -48776,7 +48849,8 @@ async function getHealthSimulationTransactions({
48776
48849
  authority,
48777
48850
  luts,
48778
48851
  includeCrankTx,
48779
- blockhash
48852
+ blockhash,
48853
+ crossbarUrl
48780
48854
  }) {
48781
48855
  const additionalTxs = [];
48782
48856
  const computeIx = web3_js.ComputeBudgetProgram.setComputeUnitLimit({
@@ -48788,7 +48862,8 @@ async function getHealthSimulationTransactions({
48788
48862
  marginfiAccount,
48789
48863
  bankMap,
48790
48864
  projectedActiveBanks,
48791
- program.provider
48865
+ program.provider,
48866
+ crossbarUrl
48792
48867
  );
48793
48868
  }
48794
48869
  const activeBanks = marginfiAccount.balances.filter((b) => b.active).map((b) => b.bankPk);
@@ -50481,11 +50556,14 @@ async function makeSmartCrankSwbFeedIx(params) {
50481
50556
  const { instructions: instructions2, luts } = await makeUpdateSwbFeedIx({
50482
50557
  swbPullOracles: oraclesToCrank,
50483
50558
  feePayer: params.marginfiAccount.authority,
50484
- connection: params.connection
50559
+ connection: params.connection,
50560
+ crossbarUrl: params.crossbarUrl
50485
50561
  });
50486
50562
  return { instructions: instructions2, luts };
50487
50563
  }
50488
- async function makeCrankSwbFeedIx(marginfiAccount, bankMap, newBanksPk, provider) {
50564
+ var DEFAULT_CROSSBAR_URL = "https://crossbar.0.xyz";
50565
+ var DEFAULT_FALLBACK_CROSSBAR_URL = "https://crossbar.switchboard.xyz";
50566
+ async function makeCrankSwbFeedIx(marginfiAccount, bankMap, newBanksPk, provider, crossbarUrl) {
50489
50567
  const activeBanksPk = marginfiAccount.balances.filter((balance) => balance.active).map((balance) => balance.bankPk);
50490
50568
  const allActiveBanks = [
50491
50569
  ...(/* @__PURE__ */ new Set([
@@ -50504,7 +50582,8 @@ async function makeCrankSwbFeedIx(marginfiAccount, bankMap, newBanksPk, provider
50504
50582
  const { instructions: instructions2, luts } = await makeUpdateSwbFeedIx({
50505
50583
  swbPullOracles: staleOracles.map((oracle) => ({ key: oracle })),
50506
50584
  feePayer: provider.publicKey,
50507
- connection: provider.connection
50585
+ connection: provider.connection,
50586
+ crossbarUrl
50508
50587
  });
50509
50588
  return { instructions: instructions2, luts };
50510
50589
  }
@@ -50534,16 +50613,26 @@ async function makeUpdateSwbFeedIx(props) {
50534
50613
  if (pullFeedInstances.length === 0) {
50535
50614
  return { instructions: [], luts: [] };
50536
50615
  }
50537
- const crossbarClient = new common.CrossbarClient(
50538
- process.env.NEXT_PUBLIC_SWITCHBOARD_CROSSSBAR_API || "https://integrator-crossbar.prod.mrgn.app"
50539
- );
50540
- const [pullIx, luts] = await onDemand.PullFeed.fetchUpdateManyIx(swbProgram, {
50541
- feeds: pullFeedInstances,
50542
- numSignatures: 1,
50543
- crossbarClient,
50544
- payer: props.feePayer
50545
- });
50546
- return { instructions: pullIx, luts };
50616
+ const primaryUrl = props.crossbarUrl ?? DEFAULT_CROSSBAR_URL;
50617
+ const fallbackUrl = props.fallbackCrossbarUrl ?? DEFAULT_FALLBACK_CROSSBAR_URL;
50618
+ try {
50619
+ const [pullIx, luts] = await onDemand.PullFeed.fetchUpdateManyIx(swbProgram, {
50620
+ feeds: pullFeedInstances,
50621
+ numSignatures: 1,
50622
+ crossbarClient: new common.CrossbarClient(primaryUrl),
50623
+ payer: props.feePayer
50624
+ });
50625
+ return { instructions: pullIx, luts };
50626
+ } catch (primaryError) {
50627
+ console.warn(`Primary crossbar endpoint failed (${primaryUrl}), trying fallback:`, primaryError);
50628
+ const [pullIx, luts] = await onDemand.PullFeed.fetchUpdateManyIx(swbProgram, {
50629
+ feeds: pullFeedInstances,
50630
+ numSignatures: 1,
50631
+ crossbarClient: new common.CrossbarClient(fallbackUrl),
50632
+ payer: props.feePayer
50633
+ });
50634
+ return { instructions: pullIx, luts };
50635
+ }
50547
50636
  }
50548
50637
 
50549
50638
  // src/services/price/actions/klend-reserve-refresh.ts
@@ -51034,7 +51123,7 @@ var fetchSwbOracleData = async (banks, opts) => {
51034
51123
  } else {
51035
51124
  crossbarResponse = await fetchSwbOraclePricesFromCrossbar(
51036
51125
  swbFeedIds,
51037
- opts.crossbarEndpoint || "https://34.97.218.183.sslip.io",
51126
+ opts.crossbarEndpoint || "https://crossbar.0.xyz",
51038
51127
  "https://crossbar.switchboard.xyz"
51039
51128
  );
51040
51129
  birdeyeResponse = {};
@@ -54062,14 +54151,15 @@ var MarginfiAccount = class _MarginfiAccount {
54062
54151
  *
54063
54152
  * @see {@link makeAccountTransferToNewAccountIx} for implementation
54064
54153
  */
54065
- async makeAccountTransferToNewAccountIx(program, newMarginfiAccount, newAuthority) {
54154
+ async makeAccountTransferToNewAccountIx(program, newMarginfiAccount, newAuthority, globalFeeWallet, feePayer) {
54066
54155
  const accountTransferToNewAccountIx = await instructions_default.makeAccountTransferToNewAccountIx(
54067
54156
  program,
54068
54157
  {
54069
54158
  oldMarginfiAccount: this.address,
54070
54159
  newMarginfiAccount,
54071
54160
  newAuthority,
54072
- feePayer: this.authority
54161
+ globalFeeWallet,
54162
+ feePayer
54073
54163
  }
54074
54164
  );
54075
54165
  return { instructions: [accountTransferToNewAccountIx], keys: [] };
@@ -54725,11 +54815,13 @@ var MarginfiAccountWrapper = class {
54725
54815
  * @param newMarginfiAccount - New account public key
54726
54816
  * @param newAuthority - New authority public key
54727
54817
  */
54728
- async makeAccountTransferToNewAccountIx(newMarginfiAccount, newAuthority) {
54818
+ async makeAccountTransferToNewAccountIx(newMarginfiAccount, newAuthority, globalFeeWallet, feePayer) {
54729
54819
  return this.account.makeAccountTransferToNewAccountIx(
54730
54820
  this.client.program,
54731
54821
  newMarginfiAccount,
54732
- newAuthority
54822
+ newAuthority,
54823
+ globalFeeWallet,
54824
+ feePayer
54733
54825
  );
54734
54826
  }
54735
54827
  /**
@@ -55535,6 +55627,8 @@ exports.Bank = Bank;
55535
55627
  exports.BankConfig = BankConfig;
55536
55628
  exports.BankConfigFlag = BankConfigFlag;
55537
55629
  exports.BankVaultType = BankVaultType;
55630
+ exports.DEFAULT_CROSSBAR_URL = DEFAULT_CROSSBAR_URL;
55631
+ exports.DEFAULT_FALLBACK_CROSSBAR_URL = DEFAULT_FALLBACK_CROSSBAR_URL;
55538
55632
  exports.DEFAULT_ORACLE_MAX_AGE = DEFAULT_ORACLE_MAX_AGE;
55539
55633
  exports.DISABLED_FLAG = DISABLED_FLAG;
55540
55634
  exports.EMPTY_HEALTH_CACHE = EMPTY_HEALTH_CACHE;
@@ -55794,6 +55888,7 @@ exports.isFlashloan = isFlashloan;
55794
55888
  exports.isV0Tx = isV0Tx;
55795
55889
  exports.isWeightedPrice = isWeightedPrice;
55796
55890
  exports.isWholePosition = isWholePosition;
55891
+ exports.makeAccountTransferToNewAccountTx = makeAccountTransferToNewAccountTx;
55797
55892
  exports.makeAddPermissionlessStakedBankIx = makeAddPermissionlessStakedBankIx;
55798
55893
  exports.makeBeginFlashLoanIx = makeBeginFlashLoanIx3;
55799
55894
  exports.makeBorrowIx = makeBorrowIx3;