@0dotxyz/p0-ts-sdk 2.5.5-alpha.3 → 2.5.5-alpha.4

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
@@ -20084,12 +20084,8 @@ var NATIVE_STAKE_LUT_KEYS = new Set(
20084
20084
  Object.values(ADDRESS_LOOKUP_TABLE_FOR_GROUP_NATIVE_STAKE).flat().map((key) => key.toBase58())
20085
20085
  );
20086
20086
  function selectLutsForBanks(luts, banks) {
20087
- const nativeStakeLuts = luts.filter(
20088
- (lut) => NATIVE_STAKE_LUT_KEYS.has(lut.key.toBase58())
20089
- );
20090
- const generalLuts = luts.filter(
20091
- (lut) => !NATIVE_STAKE_LUT_KEYS.has(lut.key.toBase58())
20092
- );
20087
+ const nativeStakeLuts = luts.filter((lut) => NATIVE_STAKE_LUT_KEYS.has(lut.key.toBase58()));
20088
+ const generalLuts = luts.filter((lut) => !NATIVE_STAKE_LUT_KEYS.has(lut.key.toBase58()));
20093
20089
  const allStakedOrSol = banks.length > 0 && banks.every(
20094
20090
  (bank) => bank.config.assetTag === 2 /* STAKED */ || bank.config.assetTag === 1 /* SOL */
20095
20091
  );
@@ -20136,37 +20132,54 @@ async function makeVersionedTransaction(blockhash, transaction, payer, addressLo
20136
20132
  const versionedMessage = addressLookupTables ? message.compileToV0Message(addressLookupTables) : message.compileToLegacyMessage();
20137
20133
  return new web3_js.VersionedTransaction(versionedMessage);
20138
20134
  }
20135
+ function countTxAccountLocks(tx) {
20136
+ const message = tx.message;
20137
+ const lookupKeys = (message.addressTableLookups ?? []).reduce(
20138
+ (count, lookup) => count + lookup.writableIndexes.length + lookup.readonlyIndexes.length,
20139
+ 0
20140
+ );
20141
+ return message.staticAccountKeys.length + lookupKeys;
20142
+ }
20139
20143
  function splitInstructionsToFitTransactions(mandatoryIxs, ixs, opts) {
20140
20144
  const result = [];
20141
20145
  let buffer = [];
20142
- function buildTx(mandatoryIxs2, extraIxs, opts2) {
20146
+ const maxSize = MAX_TX_SIZE - (opts.sizeMargin ?? 0);
20147
+ function buildTx(extraIxs) {
20143
20148
  const messageV0 = new web3_js.TransactionMessage({
20144
- payerKey: opts2.payerKey,
20145
- recentBlockhash: opts2.blockhash,
20146
- instructions: [...mandatoryIxs2, ...extraIxs]
20147
- }).compileToV0Message(opts2.luts);
20149
+ payerKey: opts.payerKey,
20150
+ recentBlockhash: opts.blockhash,
20151
+ instructions: [...mandatoryIxs, ...extraIxs]
20152
+ }).compileToV0Message(opts.luts);
20148
20153
  return new web3_js.VersionedTransaction(messageV0);
20149
20154
  }
20155
+ function fits(extraIxs) {
20156
+ try {
20157
+ const tx = buildTx(extraIxs);
20158
+ if (getTxSize(tx) > maxSize) return false;
20159
+ if (opts.maxAccountLocks !== void 0 && countTxAccountLocks(tx) > opts.maxAccountLocks) {
20160
+ return false;
20161
+ }
20162
+ return true;
20163
+ } catch {
20164
+ return false;
20165
+ }
20166
+ }
20150
20167
  for (const ix of ixs) {
20151
- const trial = buildTx(mandatoryIxs, [...buffer, ix], opts);
20152
- if (getTxSize(trial) <= MAX_TX_SIZE) {
20168
+ if (fits([...buffer, ix])) {
20153
20169
  buffer.push(ix);
20154
- } else {
20155
- if (buffer.length === 0) {
20156
- throw new Error("Single instruction too large to fit in a transaction");
20157
- }
20158
- const tx = buildTx(mandatoryIxs, buffer, opts);
20159
- result.push(tx);
20160
- buffer = [ix];
20161
- const solo = buildTx(mandatoryIxs, buffer, opts);
20162
- if (getTxSize(solo) > MAX_TX_SIZE) {
20163
- throw new Error("Single instruction too large to fit in a transaction");
20164
- }
20170
+ continue;
20171
+ }
20172
+ if (buffer.length === 0) {
20173
+ throw new Error("Single instruction too large to fit in a transaction");
20174
+ }
20175
+ result.push(buildTx(buffer));
20176
+ buffer = [ix];
20177
+ if (!fits(buffer)) {
20178
+ throw new Error("Single instruction too large to fit in a transaction");
20165
20179
  }
20166
20180
  }
20167
20181
  if (buffer.length > 0) {
20168
- const tx = buildTx(mandatoryIxs, buffer, opts);
20169
- result.push(tx);
20182
+ result.push(buildTx(buffer));
20170
20183
  }
20171
20184
  return result;
20172
20185
  }
@@ -26425,7 +26438,13 @@ BigInt(200);
26425
26438
  BigInt(82);
26426
26439
 
26427
26440
  // src/services/account/utils/compute/transaction-projection.utils.ts
26428
- function computeHealthCheckAccounts(balances, banksMap, mandatoryBanks = [], excludedBanks = []) {
26441
+ function computeHealthCheckAccounts({
26442
+ account,
26443
+ banksMap,
26444
+ mandatoryBanks = [],
26445
+ excludedBanks = []
26446
+ }) {
26447
+ const balances = account.balances;
26429
26448
  const activeBalances = balances.filter((b) => b.active);
26430
26449
  const mandatoryBanksSet = new Set(mandatoryBanks.map((b) => b.toBase58()));
26431
26450
  const excludedBanksSet = new Set(excludedBanks.map((b) => b.toBase58()));
@@ -26455,7 +26474,11 @@ function computeHealthCheckAccounts(balances, banksMap, mandatoryBanks = [], exc
26455
26474
  });
26456
26475
  return projectedActiveBanks;
26457
26476
  }
26458
- function computeHealthAccountMetas(banksToInclude, enableSorting = true, trailingBanks = []) {
26477
+ function computeHealthAccountMetas({
26478
+ banksToInclude,
26479
+ enableSorting = true,
26480
+ trailingBanks = []
26481
+ }) {
26459
26482
  let wrapperFn = enableSorting ? composeRemainingAccounts : (banksAndOracles) => banksAndOracles.flat();
26460
26483
  const accounts = wrapperFn(banksToInclude.map(computeBankRiskAccountKeys));
26461
26484
  for (const bank of trailingBanks) {
@@ -26485,14 +26508,22 @@ function computeBankRiskAccountKeys(bank) {
26485
26508
  }
26486
26509
  return keys;
26487
26510
  }
26488
- function computeProjectedActiveBanksNoCpi(balances, instructions2, program) {
26489
- let projectedBalances = [...balances.map((b) => ({ active: b.active, bankPk: b.bankPk }))];
26511
+ function computeProjectedActiveBanksNoCpi({
26512
+ account,
26513
+ instructions: instructions2,
26514
+ program
26515
+ }) {
26516
+ let projectedBalances = [
26517
+ ...account.balances.map((b) => ({ active: b.active, bankPk: b.bankPk }))
26518
+ ];
26490
26519
  for (let index = 0; index < instructions2.length; index++) {
26491
26520
  const ix = instructions2[index];
26492
26521
  if (!ix?.programId.equals(program.programId)) continue;
26493
26522
  const borshCoder = new anchor.BorshInstructionCoder(program.idl);
26494
26523
  const decoded = borshCoder.decode(ix.data, "base58");
26495
26524
  if (!decoded) continue;
26525
+ const ixMarginfiAccount = ix.keys[1]?.pubkey;
26526
+ if (!ixMarginfiAccount?.equals(account.address)) continue;
26496
26527
  const ixArgs = decoded.data;
26497
26528
  switch (decoded.name) {
26498
26529
  case "lendingAccountBorrow":
@@ -26540,8 +26571,14 @@ function computeProjectedActiveBanksNoCpi(balances, instructions2, program) {
26540
26571
  }
26541
26572
  return projectedBalances.filter((b) => b.active).map((b) => b.bankPk);
26542
26573
  }
26543
- function computeProjectedActiveBalancesNoCpi(balances, instructions2, program, banksMap, assetShareValueMultiplierByBank) {
26544
- let projectedBalances = balances.map((b) => ({
26574
+ function computeProjectedActiveBalancesNoCpi({
26575
+ account,
26576
+ instructions: instructions2,
26577
+ program,
26578
+ banksMap,
26579
+ assetShareValueMultiplierByBank
26580
+ }) {
26581
+ let projectedBalances = account.balances.map((b) => ({
26545
26582
  active: b.active,
26546
26583
  bankPk: b.bankPk,
26547
26584
  assetShares: new BigNumber3__default.default(b.assetShares),
@@ -26558,6 +26595,8 @@ function computeProjectedActiveBalancesNoCpi(balances, instructions2, program, b
26558
26595
  const borshCoder = new anchor.BorshInstructionCoder(program.idl);
26559
26596
  const decoded = borshCoder.decode(ix.data, "base58");
26560
26597
  if (!decoded) continue;
26598
+ const ixMarginfiAccount = ix.keys[1]?.pubkey;
26599
+ if (!ixMarginfiAccount?.equals(account.address)) continue;
26561
26600
  const ixArgs = decoded.data;
26562
26601
  switch (decoded.name) {
26563
26602
  // Instructions that open or add to a position
@@ -41567,13 +41606,18 @@ async function makeSetupIx({ connection, authority, tokens }) {
41567
41606
  return [];
41568
41607
  }
41569
41608
  }
41570
- async function makePulseHealthIx2(program, marginfiAccountPk, banks, balances, mandatoryBanks, excludedBanks) {
41571
- const healthAccounts = computeHealthCheckAccounts(balances, banks, mandatoryBanks, excludedBanks);
41572
- const accountMetas = computeHealthAccountMetas(healthAccounts);
41609
+ async function makePulseHealthIx2(program, marginfiAccount, banks, mandatoryBanks, excludedBanks) {
41610
+ const healthAccounts = computeHealthCheckAccounts({
41611
+ account: marginfiAccount,
41612
+ banksMap: banks,
41613
+ mandatoryBanks,
41614
+ excludedBanks
41615
+ });
41616
+ const accountMetas = computeHealthAccountMetas({ banksToInclude: healthAccounts });
41573
41617
  const ix = await instructions_default.makePulseHealthIx(
41574
41618
  program,
41575
41619
  {
41576
- marginfiAccount: marginfiAccountPk
41620
+ marginfiAccount: marginfiAccount.address
41577
41621
  },
41578
41622
  accountMetas.map((account) => ({
41579
41623
  pubkey: account,
@@ -63746,7 +63790,15 @@ async function makeDriftWithdrawIx3({
63746
63790
  );
63747
63791
  withdrawIxs.push(createAtaIdempotentIx);
63748
63792
  }
63749
- const healthAccounts = withdrawAll ? computeHealthCheckAccounts(marginfiAccount.balances, bankMap, [], [bank.address]) : computeHealthCheckAccounts(marginfiAccount.balances, bankMap, [bank.address], []);
63793
+ const healthAccounts = withdrawAll ? computeHealthCheckAccounts({
63794
+ account: marginfiAccount,
63795
+ banksMap: bankMap,
63796
+ excludedBanks: [bank.address]
63797
+ }) : computeHealthCheckAccounts({
63798
+ account: marginfiAccount,
63799
+ banksMap: bankMap,
63800
+ mandatoryBanks: [bank.address]
63801
+ });
63750
63802
  const marketIndex = driftSpotMarket.marketIndex;
63751
63803
  const driftOracle = driftSpotMarket.oracle;
63752
63804
  const { driftState, driftSigner, driftSpotMarketVault } = getAllDerivedDriftAccounts(marketIndex);
@@ -63756,7 +63808,10 @@ async function makeDriftWithdrawIx3({
63756
63808
  if (opts.observationBanksOverride) {
63757
63809
  remainingAccounts.push(...opts.observationBanksOverride);
63758
63810
  } else {
63759
- const accountMetas = computeHealthAccountMetas(healthAccounts, true, withdrawAll ? [bank] : []);
63811
+ const accountMetas = computeHealthAccountMetas({
63812
+ banksToInclude: healthAccounts,
63813
+ trailingBanks: withdrawAll ? [bank] : []
63814
+ });
63760
63815
  remainingAccounts.push(...accountMetas);
63761
63816
  }
63762
63817
  if (userRewards.length > 2) {
@@ -63871,19 +63926,7 @@ async function makeDriftWithdrawTx(params) {
63871
63926
  updateFeedIxs = _updateFeedIxs;
63872
63927
  feedLuts = _feedLuts;
63873
63928
  }
63874
- const updateJupLendRateIxs = makeUpdateJupLendRateIxs(
63875
- params.marginfiAccount,
63876
- params.bankMap,
63877
- [withdrawIxParams.bank.address],
63878
- params.bankMetadataMap
63879
- );
63880
- const updateDriftMarketIxs = makeUpdateDriftMarketIxs(
63881
- params.marginfiAccount,
63882
- params.bankMap,
63883
- [withdrawIxParams.bank.address],
63884
- params.bankMetadataMap
63885
- );
63886
- const kaminoRefreshIxs = makeRefreshKaminoBanksIxs(
63929
+ const refreshIntegrationIxs = makeRefreshIntegrationBanksIxs(
63887
63930
  params.marginfiAccount,
63888
63931
  params.bankMap,
63889
63932
  [withdrawIxParams.bank.address],
@@ -63913,23 +63956,13 @@ async function makeDriftWithdrawTx(params) {
63913
63956
  const withdrawTx = addTransactionMetadata(
63914
63957
  new web3_js.VersionedTransaction(
63915
63958
  new web3_js.TransactionMessage({
63916
- instructions: [
63917
- ...kaminoRefreshIxs.instructions,
63918
- ...updateDriftMarketIxs.instructions,
63919
- ...updateJupLendRateIxs.instructions,
63920
- ...withdrawIxs.instructions
63921
- ],
63959
+ instructions: [...refreshIntegrationIxs.instructions, ...withdrawIxs.instructions],
63922
63960
  payerKey: params.authority,
63923
63961
  recentBlockhash: blockhash
63924
63962
  }).compileToV0Message(selectedLuts)
63925
63963
  ),
63926
63964
  {
63927
- signers: [
63928
- ...kaminoRefreshIxs.keys,
63929
- ...updateDriftMarketIxs.keys,
63930
- ...updateJupLendRateIxs.keys,
63931
- ...withdrawIxs.keys
63932
- ],
63965
+ signers: [...refreshIntegrationIxs.keys, ...withdrawIxs.keys],
63933
63966
  addressLookupTables: selectedLuts,
63934
63967
  type: "WITHDRAW" /* WITHDRAW */
63935
63968
  }
@@ -63965,7 +63998,15 @@ async function makeKaminoWithdrawIx3({
63965
63998
  );
63966
63999
  withdrawIxs.push(createAtaIdempotentIx);
63967
64000
  }
63968
- const healthAccounts = withdrawAll ? computeHealthCheckAccounts(marginfiAccount.balances, bankMap, [], [bank.address]) : computeHealthCheckAccounts(marginfiAccount.balances, bankMap, [bank.address], []);
64001
+ const healthAccounts = withdrawAll ? computeHealthCheckAccounts({
64002
+ account: marginfiAccount,
64003
+ banksMap: bankMap,
64004
+ excludedBanks: [bank.address]
64005
+ }) : computeHealthCheckAccounts({
64006
+ account: marginfiAccount,
64007
+ banksMap: bankMap,
64008
+ mandatoryBanks: [bank.address]
64009
+ });
63969
64010
  const lendingMarket = reserve.lendingMarket;
63970
64011
  const reserveLiquiditySupply = reserve.liquidity.supplyVault;
63971
64012
  const reserveCollateralMint = reserve.collateral.mintPubkey;
@@ -63985,7 +64026,10 @@ async function makeKaminoWithdrawIx3({
63985
64026
  if (opts.observationBanksOverride) {
63986
64027
  remainingAccounts.push(...opts.observationBanksOverride);
63987
64028
  } else {
63988
- const accountMetas = computeHealthAccountMetas(healthAccounts, true, withdrawAll ? [bank] : []);
64029
+ const accountMetas = computeHealthAccountMetas({
64030
+ banksToInclude: healthAccounts,
64031
+ trailingBanks: withdrawAll ? [bank] : []
64032
+ });
63989
64033
  remainingAccounts.push(...accountMetas);
63990
64034
  }
63991
64035
  const withdrawIx = isSync ? sync_instructions_default.makeKaminoWithdrawIx(
@@ -64080,7 +64124,15 @@ async function makeWithdrawIx3({
64080
64124
  );
64081
64125
  withdrawIxs.push(createAtaIdempotentIx);
64082
64126
  }
64083
- const healthAccounts = withdrawAll ? computeHealthCheckAccounts(marginfiAccount.balances, bankMap, [], [bank.address]) : computeHealthCheckAccounts(marginfiAccount.balances, bankMap, [bank.address], []);
64127
+ const healthAccounts = withdrawAll ? computeHealthCheckAccounts({
64128
+ account: marginfiAccount,
64129
+ banksMap: bankMap,
64130
+ excludedBanks: [bank.address]
64131
+ }) : computeHealthCheckAccounts({
64132
+ account: marginfiAccount,
64133
+ banksMap: bankMap,
64134
+ mandatoryBanks: [bank.address]
64135
+ });
64084
64136
  const remainingAccounts = [];
64085
64137
  if (tokenProgram.equals(TOKEN_2022_PROGRAM_ID)) {
64086
64138
  remainingAccounts.push(bank.mint);
@@ -64088,7 +64140,10 @@ async function makeWithdrawIx3({
64088
64140
  if (opts.observationBanksOverride) {
64089
64141
  remainingAccounts.push(...opts.observationBanksOverride);
64090
64142
  } else {
64091
- const accountMetas = computeHealthAccountMetas(healthAccounts, true, withdrawAll ? [bank] : []);
64143
+ const accountMetas = computeHealthAccountMetas({
64144
+ banksToInclude: healthAccounts,
64145
+ trailingBanks: withdrawAll ? [bank] : []
64146
+ });
64092
64147
  remainingAccounts.push(...accountMetas);
64093
64148
  }
64094
64149
  const withdrawIx = isSync ? sync_instructions_default.makeWithdrawIx(
@@ -64161,19 +64216,7 @@ async function makeWithdrawTx(params) {
64161
64216
  updateFeedIxs = _updateFeedIxs;
64162
64217
  feedLuts = _feedLuts;
64163
64218
  }
64164
- const updateJupLendRateIxs = makeUpdateJupLendRateIxs(
64165
- params.marginfiAccount,
64166
- params.bankMap,
64167
- [withdrawIxParams.bank.address],
64168
- params.bankMetadataMap
64169
- );
64170
- const updateDriftMarketIxs = makeUpdateDriftMarketIxs(
64171
- params.marginfiAccount,
64172
- params.bankMap,
64173
- [withdrawIxParams.bank.address],
64174
- params.bankMetadataMap
64175
- );
64176
- const refreshIxs = makeRefreshKaminoBanksIxs(
64219
+ const refreshIntegrationIxs = makeRefreshIntegrationBanksIxs(
64177
64220
  params.marginfiAccount,
64178
64221
  params.bankMap,
64179
64222
  [withdrawIxParams.bank.address],
@@ -64203,23 +64246,13 @@ async function makeWithdrawTx(params) {
64203
64246
  const withdrawTx = addTransactionMetadata(
64204
64247
  new web3_js.VersionedTransaction(
64205
64248
  new web3_js.TransactionMessage({
64206
- instructions: [
64207
- ...refreshIxs.instructions,
64208
- ...updateDriftMarketIxs.instructions,
64209
- ...updateJupLendRateIxs.instructions,
64210
- ...withdrawIxs.instructions
64211
- ],
64249
+ instructions: [...refreshIntegrationIxs.instructions, ...withdrawIxs.instructions],
64212
64250
  payerKey: params.authority,
64213
64251
  recentBlockhash: blockhash
64214
64252
  }).compileToV0Message(selectedLuts)
64215
64253
  ),
64216
64254
  {
64217
- signers: [
64218
- ...refreshIxs.keys,
64219
- ...updateDriftMarketIxs.keys,
64220
- ...updateJupLendRateIxs.keys,
64221
- ...withdrawIxs.keys
64222
- ],
64255
+ signers: [...refreshIntegrationIxs.keys, ...withdrawIxs.keys],
64223
64256
  addressLookupTables: selectedLuts,
64224
64257
  type: "WITHDRAW" /* WITHDRAW */
64225
64258
  }
@@ -64241,19 +64274,7 @@ async function makeKaminoWithdrawTx(params) {
64241
64274
  const { value: amountValue, type: amountType } = resolveAmount(amount);
64242
64275
  const multiplier = assetShareValueMultiplierByBank.get(withdrawIxParams.bank.address.toBase58()) ?? new BigNumber3.BigNumber(1);
64243
64276
  const adjustedAmount = amountType === "cToken" ? new BigNumber3.BigNumber(amountValue).toNumber() : new BigNumber3.BigNumber(amountValue).div(multiplier).toNumber();
64244
- const updateJupLendRateIxs = makeUpdateJupLendRateIxs(
64245
- params.marginfiAccount,
64246
- params.bankMap,
64247
- [withdrawIxParams.bank.address],
64248
- params.bankMetadataMap
64249
- );
64250
- const refreshIxs = makeRefreshKaminoBanksIxs(
64251
- params.marginfiAccount,
64252
- params.bankMap,
64253
- [withdrawIxParams.bank.address],
64254
- params.bankMetadataMap
64255
- );
64256
- const updateDriftMarketIxs = makeUpdateDriftMarketIxs(
64277
+ const refreshIntegrationIxs = makeRefreshIntegrationBanksIxs(
64257
64278
  params.marginfiAccount,
64258
64279
  params.bankMap,
64259
64280
  [withdrawIxParams.bank.address],
@@ -64297,23 +64318,13 @@ async function makeKaminoWithdrawTx(params) {
64297
64318
  const withdrawTx = addTransactionMetadata(
64298
64319
  new web3_js.VersionedTransaction(
64299
64320
  new web3_js.TransactionMessage({
64300
- instructions: [
64301
- ...refreshIxs.instructions,
64302
- ...updateDriftMarketIxs.instructions,
64303
- ...updateJupLendRateIxs.instructions,
64304
- ...withdrawIxs.instructions
64305
- ],
64321
+ instructions: [...refreshIntegrationIxs.instructions, ...withdrawIxs.instructions],
64306
64322
  payerKey: params.authority,
64307
64323
  recentBlockhash: blockhash
64308
64324
  }).compileToV0Message(selectedLuts)
64309
64325
  ),
64310
64326
  {
64311
- signers: [
64312
- ...refreshIxs.keys,
64313
- ...updateDriftMarketIxs.keys,
64314
- ...updateJupLendRateIxs.keys,
64315
- ...withdrawIxs.keys
64316
- ],
64327
+ signers: [...refreshIntegrationIxs.keys, ...withdrawIxs.keys],
64317
64328
  addressLookupTables: selectedLuts,
64318
64329
  type: "WITHDRAW" /* WITHDRAW */
64319
64330
  }
@@ -64348,7 +64359,15 @@ async function makeJuplendWithdrawIx2({
64348
64359
  );
64349
64360
  withdrawIxs.push(createAtaIdempotentIx);
64350
64361
  }
64351
- const healthAccounts = withdrawAll ? computeHealthCheckAccounts(marginfiAccount.balances, bankMap, [], [bank.address]) : computeHealthCheckAccounts(marginfiAccount.balances, bankMap, [bank.address], []);
64362
+ const healthAccounts = withdrawAll ? computeHealthCheckAccounts({
64363
+ account: marginfiAccount,
64364
+ banksMap: bankMap,
64365
+ excludedBanks: [bank.address]
64366
+ }) : computeHealthCheckAccounts({
64367
+ account: marginfiAccount,
64368
+ banksMap: bankMap,
64369
+ mandatoryBanks: [bank.address]
64370
+ });
64352
64371
  if (!bank.jupLendIntegrationAccounts) {
64353
64372
  throw new Error("Bank has no JupLend integration accounts");
64354
64373
  }
@@ -64361,7 +64380,10 @@ async function makeJuplendWithdrawIx2({
64361
64380
  if (opts.observationBanksOverride) {
64362
64381
  remainingAccounts.push(...opts.observationBanksOverride);
64363
64382
  } else {
64364
- const accountMetas = computeHealthAccountMetas(healthAccounts, true, withdrawAll ? [bank] : []);
64383
+ const accountMetas = computeHealthAccountMetas({
64384
+ banksToInclude: healthAccounts,
64385
+ trailingBanks: withdrawAll ? [bank] : []
64386
+ });
64365
64387
  remainingAccounts.push(...accountMetas);
64366
64388
  }
64367
64389
  const withdrawIx = await instructions_default.makeJuplendWithdrawIx(
@@ -64435,19 +64457,7 @@ async function makeJuplendWithdrawTx(params) {
64435
64457
  updateFeedIxs = _updateFeedIxs;
64436
64458
  feedLuts = _feedLuts;
64437
64459
  }
64438
- const updateJupLendRateIxs = makeUpdateJupLendRateIxs(
64439
- params.marginfiAccount,
64440
- params.bankMap,
64441
- [withdrawIxParams.bank.address],
64442
- params.bankMetadataMap
64443
- );
64444
- const updateDriftMarketIxs = makeUpdateDriftMarketIxs(
64445
- params.marginfiAccount,
64446
- params.bankMap,
64447
- [withdrawIxParams.bank.address],
64448
- params.bankMetadataMap
64449
- );
64450
- const refreshIxs = makeRefreshKaminoBanksIxs(
64460
+ const refreshIntegrationIxs = makeRefreshIntegrationBanksIxs(
64451
64461
  params.marginfiAccount,
64452
64462
  params.bankMap,
64453
64463
  [withdrawIxParams.bank.address],
@@ -64477,18 +64487,13 @@ async function makeJuplendWithdrawTx(params) {
64477
64487
  const withdrawTx = addTransactionMetadata(
64478
64488
  new web3_js.VersionedTransaction(
64479
64489
  new web3_js.TransactionMessage({
64480
- instructions: [
64481
- ...refreshIxs.instructions,
64482
- ...updateDriftMarketIxs.instructions,
64483
- ...updateJupLendRateIxs.instructions,
64484
- ...withdrawIxs.instructions
64485
- ],
64490
+ instructions: [...refreshIntegrationIxs.instructions, ...withdrawIxs.instructions],
64486
64491
  payerKey: params.authority,
64487
64492
  recentBlockhash: blockhash
64488
64493
  }).compileToV0Message(selectedLuts)
64489
64494
  ),
64490
64495
  {
64491
- signers: [...refreshIxs.keys, ...withdrawIxs.keys],
64496
+ signers: [...refreshIntegrationIxs.keys, ...withdrawIxs.keys],
64492
64497
  addressLookupTables: selectedLuts,
64493
64498
  type: "WITHDRAW" /* WITHDRAW */
64494
64499
  }
@@ -64522,12 +64527,11 @@ async function makeBorrowIx3({
64522
64527
  borrowIxs.push(createAtaIdempotentIx);
64523
64528
  }
64524
64529
  const mandatoryBanks = [bank.address, ...opts.additionalHealthCheckBanks ?? []];
64525
- const healthAccounts = computeHealthCheckAccounts(
64526
- marginfiAccount.balances,
64527
- bankMap,
64528
- mandatoryBanks,
64529
- []
64530
- );
64530
+ const healthAccounts = computeHealthCheckAccounts({
64531
+ account: marginfiAccount,
64532
+ banksMap: bankMap,
64533
+ mandatoryBanks
64534
+ });
64531
64535
  const remainingAccounts = [];
64532
64536
  if (tokenProgram.equals(TOKEN_2022_PROGRAM_ID)) {
64533
64537
  remainingAccounts.push(bank.mint);
@@ -64535,7 +64539,7 @@ async function makeBorrowIx3({
64535
64539
  if (opts?.observationBanksOverride) {
64536
64540
  remainingAccounts.push(...opts.observationBanksOverride);
64537
64541
  } else {
64538
- const accountMetas = computeHealthAccountMetas(healthAccounts);
64542
+ const accountMetas = computeHealthAccountMetas({ banksToInclude: healthAccounts });
64539
64543
  remainingAccounts.push(...accountMetas);
64540
64544
  }
64541
64545
  const borrowIx = isSync ? sync_instructions_default.makeBorrowIx(
@@ -64589,19 +64593,7 @@ async function makeBorrowTx(params) {
64589
64593
  params.bankMap,
64590
64594
  borrowIxParams.opts?.additionalHealthCheckBanks
64591
64595
  );
64592
- const updateJupLendRateIxs = makeUpdateJupLendRateIxs(
64593
- params.marginfiAccount,
64594
- params.bankMap,
64595
- [borrowIxParams.bank.address],
64596
- params.bankMetadataMap
64597
- );
64598
- const updateDriftMarketIxs = makeUpdateDriftMarketIxs(
64599
- params.marginfiAccount,
64600
- params.bankMap,
64601
- [borrowIxParams.bank.address],
64602
- params.bankMetadataMap
64603
- );
64604
- const kaminoRefreshIxs = makeRefreshKaminoBanksIxs(
64596
+ const refreshIntegrationIxs = makeRefreshIntegrationBanksIxs(
64605
64597
  params.marginfiAccount,
64606
64598
  params.bankMap,
64607
64599
  [borrowIxParams.bank.address],
@@ -64642,18 +64634,13 @@ async function makeBorrowTx(params) {
64642
64634
  const borrowTx = addTransactionMetadata(
64643
64635
  new web3_js.VersionedTransaction(
64644
64636
  new web3_js.TransactionMessage({
64645
- instructions: [
64646
- ...kaminoRefreshIxs.instructions,
64647
- ...updateDriftMarketIxs.instructions,
64648
- ...updateJupLendRateIxs.instructions,
64649
- ...borrowIxs.instructions
64650
- ],
64637
+ instructions: [...refreshIntegrationIxs.instructions, ...borrowIxs.instructions],
64651
64638
  payerKey: params.authority,
64652
64639
  recentBlockhash: blockhash
64653
64640
  }).compileToV0Message(selectedLuts)
64654
64641
  ),
64655
64642
  {
64656
- signers: [...kaminoRefreshIxs.keys, ...borrowIxs.keys],
64643
+ signers: [...refreshIntegrationIxs.keys, ...borrowIxs.keys],
64657
64644
  addressLookupTables: selectedLuts,
64658
64645
  type: "BORROW" /* BORROW */
64659
64646
  }
@@ -66157,7 +66144,7 @@ async function makeBeginFlashLoanIx3(program, marginfiAccountPk, endIndex, autho
66157
66144
  return { instructions: [ix], keys: [] };
66158
66145
  }
66159
66146
  async function makeEndFlashLoanIx3(program, marginfiAccountPk, projectedActiveBanks, authority, isSync) {
66160
- const remainingAccounts = computeHealthAccountMetas(projectedActiveBanks);
66147
+ const remainingAccounts = computeHealthAccountMetas({ banksToInclude: projectedActiveBanks });
66161
66148
  const ix = isSync && authority ? sync_instructions_default.makeEndFlashLoanIx(
66162
66149
  program.programId,
66163
66150
  {
@@ -66194,11 +66181,11 @@ async function makeFlashLoanTx({
66194
66181
  isSync
66195
66182
  }) {
66196
66183
  const endIndex = ixs.length + 1;
66197
- const projectedActiveBanksKeys = computeProjectedActiveBanksNoCpi(
66198
- marginfiAccount.balances,
66199
- ixs,
66184
+ const projectedActiveBanksKeys = computeProjectedActiveBanksNoCpi({
66185
+ account: marginfiAccount,
66186
+ instructions: ixs,
66200
66187
  program
66201
- );
66188
+ });
66202
66189
  const projectedActiveBanks = projectedActiveBanksKeys.map((account) => {
66203
66190
  const b = bankMap.get(account.toBase58());
66204
66191
  if (!b) throw Error(`Bank ${account.toBase58()} not found, in makeFlashLoanTx function`);
@@ -66327,23 +66314,12 @@ async function makeRepayWithCollatTx(params) {
66327
66314
  }
66328
66315
  ]
66329
66316
  });
66330
- const updateJuplendMarketIxs = makeUpdateJupLendRateIxs(
66317
+ const refreshIntegrationIxs = makeRefreshIntegrationBanksIxs(
66331
66318
  marginfiAccount,
66332
66319
  bankMap,
66333
66320
  [withdrawOpts.withdrawBank.address],
66334
- bankMetadataMap
66335
- );
66336
- const updateDriftMarketIxs = makeUpdateDriftMarketIxs(
66337
- marginfiAccount,
66338
- bankMap,
66339
- [withdrawOpts.withdrawBank.address],
66340
- bankMetadataMap
66341
- );
66342
- const kaminoRefreshIxs = makeRefreshKaminoBanksIxs(
66343
- marginfiAccount,
66344
- bankMap,
66345
- [withdrawOpts.withdrawBank.address, repayOpts.repayBank.address],
66346
- bankMetadataMap
66321
+ bankMetadataMap,
66322
+ [withdrawOpts.withdrawBank.address, repayOpts.repayBank.address]
66347
66323
  );
66348
66324
  const { flashloanTx, setupInstructions, swapQuote, amountToRepay, withdrawIxs, repayIxs } = await buildRepayWithCollatFlashloanTx({
66349
66325
  ...params,
@@ -66373,13 +66349,8 @@ async function makeRepayWithCollatTx(params) {
66373
66349
  crossbarUrl
66374
66350
  });
66375
66351
  let additionalTxs = [];
66376
- if (setupIxs.length > 0 || kaminoRefreshIxs.instructions.length > 0 || updateDriftMarketIxs.instructions.length > 0 || updateJuplendMarketIxs.instructions.length > 0) {
66377
- const ixs = [
66378
- ...setupIxs,
66379
- ...kaminoRefreshIxs.instructions,
66380
- ...updateDriftMarketIxs.instructions,
66381
- ...updateJuplendMarketIxs.instructions
66382
- ];
66352
+ if (setupIxs.length > 0 || refreshIntegrationIxs.instructions.length > 0) {
66353
+ const ixs = [...setupIxs, ...refreshIntegrationIxs.instructions];
66383
66354
  const txs = splitInstructionsToFitTransactions([], ixs, {
66384
66355
  blockhash,
66385
66356
  payerKey: marginfiAccount.authority,
@@ -66817,11 +66788,11 @@ function computeFlashLoanNonSwapBudget({
66817
66788
  bankMap,
66818
66789
  addressLookupTableAccounts
66819
66790
  }) {
66820
- const projectedActiveBanksKeys = computeProjectedActiveBanksNoCpi(
66821
- marginfiAccount.balances,
66822
- ixs,
66791
+ const projectedActiveBanksKeys = computeProjectedActiveBanksNoCpi({
66792
+ account: marginfiAccount,
66793
+ instructions: ixs,
66823
66794
  program
66824
- );
66795
+ });
66825
66796
  const projectedActiveBanks = projectedActiveBanksKeys.map((key) => {
66826
66797
  const b = bankMap.get(key.toBase58());
66827
66798
  if (!b) throw new Error(`Bank ${key.toBase58()} not found in computeFlashLoanNonSwapBudget`);
@@ -66833,7 +66804,9 @@ function computeFlashLoanNonSwapBudget({
66833
66804
  { marginfiAccount: marginfiAccount.address, authority: marginfiAccount.authority },
66834
66805
  { endIndex: new BN9__default.default(endIndex) }
66835
66806
  );
66836
- const endFlRemainingAccounts = computeHealthAccountMetas(projectedActiveBanks);
66807
+ const endFlRemainingAccounts = computeHealthAccountMetas({
66808
+ banksToInclude: projectedActiveBanks
66809
+ });
66837
66810
  const endFlIx = sync_instructions_default.makeEndFlashLoanIx(
66838
66811
  program.programId,
66839
66812
  { marginfiAccount: marginfiAccount.address, authority: marginfiAccount.authority },
@@ -67294,23 +67267,12 @@ async function makeLoopTx(params) {
67294
67267
  }
67295
67268
  ]
67296
67269
  });
67297
- const updateJupLendRateIxs = makeUpdateJupLendRateIxs(
67298
- params.marginfiAccount,
67299
- params.bankMap,
67300
- [depositOpts.depositBank.address],
67301
- params.bankMetadataMap
67302
- );
67303
- const updateDriftMarketIxs = makeUpdateDriftMarketIxs(
67270
+ const refreshIntegrationIxs = makeRefreshIntegrationBanksIxs(
67304
67271
  params.marginfiAccount,
67305
67272
  params.bankMap,
67306
67273
  [depositOpts.depositBank.address],
67307
- params.bankMetadataMap
67308
- );
67309
- const kaminoRefreshIxs = makeRefreshKaminoBanksIxs(
67310
- marginfiAccount,
67311
- bankMap,
67312
- [borrowOpts.borrowBank.address, depositOpts.depositBank.address],
67313
- bankMetadataMap
67274
+ params.bankMetadataMap,
67275
+ [borrowOpts.borrowBank.address, depositOpts.depositBank.address]
67314
67276
  );
67315
67277
  const { flashloanTx, setupInstructions, swapQuote, depositIxs, borrowIxs } = await buildLoopFlashloanTx({
67316
67278
  ...params,
@@ -67345,14 +67307,8 @@ async function makeLoopTx(params) {
67345
67307
  ...makeWrapSolIxs(marginfiAccount.authority, new BigNumber3.BigNumber(depositOpts.inputDepositAmount))
67346
67308
  );
67347
67309
  }
67348
- if (setupIxs.length > 0 || additionalIxs.length > 0 || kaminoRefreshIxs.instructions.length > 0 || updateDriftMarketIxs.instructions.length > 0 || updateJupLendRateIxs.instructions.length > 0) {
67349
- const ixs = [
67350
- ...additionalIxs,
67351
- ...setupIxs,
67352
- ...kaminoRefreshIxs.instructions,
67353
- ...updateDriftMarketIxs.instructions,
67354
- ...updateJupLendRateIxs.instructions
67355
- ];
67310
+ if (setupIxs.length > 0 || additionalIxs.length > 0 || refreshIntegrationIxs.instructions.length > 0) {
67311
+ const ixs = [...additionalIxs, ...setupIxs, ...refreshIntegrationIxs.instructions];
67356
67312
  const txs = splitInstructionsToFitTransactions([], ixs, {
67357
67313
  blockhash,
67358
67314
  payerKey: marginfiAccount.authority,
@@ -67504,7 +67460,9 @@ async function buildLoopNonSwapIxs(params) {
67504
67460
  const innerIxs = [...innerIxsBeforeSwap, ...depositIxs.instructions];
67505
67461
  const depositIxIndex = innerIxs.findIndex(isDepositIx);
67506
67462
  if (depositIxIndex < 0) {
67507
- throw new Error("buildLoopNonSwapIxs: could not locate deposit instruction for amount patching");
67463
+ throw new Error(
67464
+ "buildLoopNonSwapIxs: could not locate deposit instruction for amount patching"
67465
+ );
67508
67466
  }
67509
67467
  const descriptor = {
67510
67468
  innerIxs,
@@ -67715,19 +67673,7 @@ async function makeSwapCollateralTx(params) {
67715
67673
  { mint: depositOpts.depositBank.mint, tokenProgram: depositOpts.tokenProgram }
67716
67674
  ]
67717
67675
  });
67718
- const updateJupLendRateIxs = makeUpdateJupLendRateIxs(
67719
- params.marginfiAccount,
67720
- params.bankMap,
67721
- [depositOpts.depositBank.address],
67722
- params.bankMetadataMap
67723
- );
67724
- const updateDriftMarketIxs = makeUpdateDriftMarketIxs(
67725
- marginfiAccount,
67726
- bankMap,
67727
- [withdrawOpts.withdrawBank.address],
67728
- bankMetadataMap
67729
- );
67730
- const kaminoRefreshIxs = makeRefreshKaminoBanksIxs(
67676
+ const refreshIntegrationIxs = makeRefreshIntegrationBanksIxs(
67731
67677
  marginfiAccount,
67732
67678
  bankMap,
67733
67679
  [withdrawOpts.withdrawBank.address, depositOpts.depositBank.address],
@@ -67761,13 +67707,8 @@ async function makeSwapCollateralTx(params) {
67761
67707
  crossbarUrl
67762
67708
  });
67763
67709
  let additionalTxs = [];
67764
- if (setupIxs.length > 0 || kaminoRefreshIxs.instructions.length > 0 || updateDriftMarketIxs.instructions.length > 0 || updateJupLendRateIxs.instructions.length > 0) {
67765
- const ixs = [
67766
- ...setupIxs,
67767
- ...kaminoRefreshIxs.instructions,
67768
- ...updateDriftMarketIxs.instructions,
67769
- ...updateJupLendRateIxs.instructions
67770
- ];
67710
+ if (setupIxs.length > 0 || refreshIntegrationIxs.instructions.length > 0) {
67711
+ const ixs = [...setupIxs, ...refreshIntegrationIxs.instructions];
67771
67712
  const txs = splitInstructionsToFitTransactions([], ixs, {
67772
67713
  blockhash,
67773
67714
  payerKey: marginfiAccount.authority,
@@ -68156,23 +68097,12 @@ async function makeSwapDebtTx(params) {
68156
68097
  { mint: borrowOpts.borrowBank.mint, tokenProgram: borrowOpts.tokenProgram }
68157
68098
  ]
68158
68099
  });
68159
- const updateJupLendRateIxs = makeUpdateJupLendRateIxs(
68160
- params.marginfiAccount,
68161
- params.bankMap,
68162
- [],
68163
- params.bankMetadataMap
68164
- );
68165
- const updateDriftMarketIxs = makeUpdateDriftMarketIxs(
68100
+ const refreshIntegrationIxs = makeRefreshIntegrationBanksIxs(
68166
68101
  marginfiAccount,
68167
68102
  bankMap,
68168
68103
  [],
68169
- bankMetadataMap
68170
- );
68171
- const kaminoRefreshIxs = makeRefreshKaminoBanksIxs(
68172
- marginfiAccount,
68173
- bankMap,
68174
- [repayOpts.repayBank.address, borrowOpts.borrowBank.address],
68175
- bankMetadataMap
68104
+ bankMetadataMap,
68105
+ [repayOpts.repayBank.address, borrowOpts.borrowBank.address]
68176
68106
  );
68177
68107
  const { flashloanTx, setupInstructions, swapQuote, borrowIxs, repayIxs } = await buildSwapDebtFlashloanTx({
68178
68108
  ...params,
@@ -68202,14 +68132,8 @@ async function makeSwapDebtTx(params) {
68202
68132
  crossbarUrl
68203
68133
  });
68204
68134
  let additionalTxs = [];
68205
- if (setupIxs.length > 0 || kaminoRefreshIxs.instructions.length > 0 || updateDriftMarketIxs.instructions.length > 0 || updateJupLendRateIxs.instructions.length > 0) {
68206
- const ixs = [
68207
- ...additionalIxs,
68208
- ...setupIxs,
68209
- ...kaminoRefreshIxs.instructions,
68210
- ...updateDriftMarketIxs.instructions,
68211
- ...updateJupLendRateIxs.instructions
68212
- ];
68135
+ if (setupIxs.length > 0 || refreshIntegrationIxs.instructions.length > 0) {
68136
+ const ixs = [...additionalIxs, ...setupIxs, ...refreshIntegrationIxs.instructions];
68213
68137
  const txs = splitInstructionsToFitTransactions([], ixs, {
68214
68138
  blockhash,
68215
68139
  payerKey: marginfiAccount.authority,
@@ -69160,7 +69084,12 @@ var MarginfiAccount = class _MarginfiAccount {
69160
69084
  * @returns Array of banks needed for health checks
69161
69085
  */
69162
69086
  getHealthCheckAccounts(banks, mandatoryBanks = [], excludedBanks = []) {
69163
- return computeHealthCheckAccounts(this.balances, banks, mandatoryBanks, excludedBanks);
69087
+ return computeHealthCheckAccounts({
69088
+ account: this,
69089
+ banksMap: banks,
69090
+ mandatoryBanks,
69091
+ excludedBanks
69092
+ });
69164
69093
  }
69165
69094
  /**
69166
69095
  * Determines which E-mode pairs are currently active for this account.
@@ -69360,14 +69289,7 @@ var MarginfiAccount = class _MarginfiAccount {
69360
69289
  * @see {@link makePulseHealthIx} for implementation
69361
69290
  */
69362
69291
  async makePulseHealthIx(program, banks, mandatoryBanks, excludedBanks) {
69363
- return makePulseHealthIx2(
69364
- program,
69365
- this.address,
69366
- banks,
69367
- this.balances,
69368
- mandatoryBanks,
69369
- excludedBanks
69370
- );
69292
+ return makePulseHealthIx2(program, this, banks, mandatoryBanks, excludedBanks);
69371
69293
  }
69372
69294
  /**
69373
69295
  * Computes which banks will be active after executing the given instructions.
@@ -69383,7 +69305,7 @@ var MarginfiAccount = class _MarginfiAccount {
69383
69305
  * @see {@link computeProjectedActiveBanksNoCpi} for implementation
69384
69306
  */
69385
69307
  computeProjectedActiveBanksNoCpi(program, instructions2) {
69386
- return computeProjectedActiveBanksNoCpi(this.balances, instructions2, program);
69308
+ return computeProjectedActiveBanksNoCpi({ account: this, instructions: instructions2, program });
69387
69309
  }
69388
69310
  /**
69389
69311
  * Computes projected active balances after executing the given instructions.
@@ -69400,13 +69322,13 @@ var MarginfiAccount = class _MarginfiAccount {
69400
69322
  * @see {@link computeProjectedActiveBalancesNoCpi} for implementation
69401
69323
  */
69402
69324
  computeProjectedActiveBalancesNoCpi(program, instructions2, banksMap, assetShareValueMultiplierByBank) {
69403
- const { projectedBalances, ...rest } = computeProjectedActiveBalancesNoCpi(
69404
- this.balances,
69405
- instructions2,
69325
+ const { projectedBalances, ...rest } = computeProjectedActiveBalancesNoCpi({
69326
+ account: this,
69327
+ instructions: instructions2,
69406
69328
  program,
69407
69329
  banksMap,
69408
69330
  assetShareValueMultiplierByBank
69409
- );
69331
+ });
69410
69332
  return {
69411
69333
  projectedBalances: projectedBalances.map(Balance.fromBalanceType),
69412
69334
  ...rest
@@ -69837,13 +69759,13 @@ function projectAccountAfterFirstLeg(account, firstLegFlashloanTxs, program, ban
69837
69759
  const luts = tx.addressLookupTables ?? [];
69838
69760
  ixs.push(...decompileV0Transaction(tx, luts).instructions);
69839
69761
  }
69840
- const { projectedBalances } = computeProjectedActiveBalancesNoCpi(
69841
- account.balances,
69842
- ixs,
69762
+ const { projectedBalances } = computeProjectedActiveBalancesNoCpi({
69763
+ account,
69764
+ instructions: ixs,
69843
69765
  program,
69844
69766
  banksMap,
69845
- multipliers
69846
- );
69767
+ assetShareValueMultiplierByBank: multipliers
69768
+ });
69847
69769
  return new MarginfiAccount(
69848
69770
  account.address,
69849
69771
  account.group,
@@ -70203,7 +70125,7 @@ async function buildInnerIxs(ctx, positions, isSync) {
70203
70125
  const borrowIxs = [];
70204
70126
  const repayIxs = [];
70205
70127
  for (const position of collateral) {
70206
- const observationBanksOverride = ctx.groupRateLimiterEnabled ? computeHealthAccountMetas([], true, [position.bank]) : [];
70128
+ const observationBanksOverride = ctx.groupRateLimiterEnabled ? computeHealthAccountMetas({ banksToInclude: [], trailingBanks: [position.bank] }) : [];
70207
70129
  const legs = await buildCollateralLegIxs(ctx, position, isSync, observationBanksOverride);
70208
70130
  withdrawIxs.push(...legs.withdrawIxs);
70209
70131
  depositIxs.push(...legs.depositIxs);
@@ -70224,7 +70146,7 @@ async function buildInnerIxs(ctx, positions, isSync) {
70224
70146
  ...collateralBanks,
70225
70147
  ...borrowedSoFar
70226
70148
  ]);
70227
- const observationBanksOverride = computeHealthAccountMetas(activeBanks);
70149
+ const observationBanksOverride = computeHealthAccountMetas({ banksToInclude: activeBanks });
70228
70150
  const borrowUi = position.uiAmount.times(1 + ctx.borrowPaddingBps / 1e4);
70229
70151
  const borrow = await makeBorrowIx3({
70230
70152
  program: ctx.program,
@@ -70393,15 +70315,8 @@ async function makeTransferPositionsTx(params) {
70393
70315
  )
70394
70316
  );
70395
70317
  }
70396
- const destinationOnlyBalances = accountB.balances.filter(
70397
- (b) => b.active && !accountA.balances.some((a) => a.active && a.bankPk.equals(b.bankPk))
70398
- );
70399
- const crankBalanceView = {
70400
- ...accountA,
70401
- balances: [...accountA.balances, ...destinationOnlyBalances]
70402
- };
70403
- const { instructions: updateFeedIxs, luts: feedLuts } = await makeSmartCrankSwbFeedIx({
70404
- marginfiAccount: crankBalanceView,
70318
+ const { instructions: updateFeedIxs, luts: feedLuts } = await makeSmartCrankSwbFeedIxForAccounts({
70319
+ marginfiAccounts: [accountA, accountB],
70405
70320
  bankMap,
70406
70321
  oraclePrices,
70407
70322
  assetShareValueMultiplierByBank,
@@ -70431,34 +70346,6 @@ async function makeTransferPositionsTx(params) {
70431
70346
  };
70432
70347
  }
70433
70348
  var BULK_TX_SIZE_MARGIN = 128;
70434
- function computeV0TxSizeSafe(ixs, payer, luts) {
70435
- try {
70436
- return computeV0TxSize(ixs, payer, luts);
70437
- } catch {
70438
- return { size: Number.MAX_SAFE_INTEGER, accountCount: Number.MAX_SAFE_INTEGER };
70439
- }
70440
- }
70441
- function fitsInTx(ixs, payer, luts) {
70442
- const { size, accountCount } = computeV0TxSizeSafe(ixs, payer, luts);
70443
- return size <= MAX_TX_SIZE - BULK_TX_SIZE_MARGIN && accountCount <= MAX_ACCOUNT_LOCKS;
70444
- }
70445
- function buildTxRefreshIxs(args) {
70446
- const { marginfiAccount, bankMap, bankMetadataMap, txBanks } = args;
70447
- const ixs = [];
70448
- const kaminoPks = txBanks.filter((b) => b.config.assetTag === 3 /* KAMINO */).map((b) => b.address);
70449
- if (kaminoPks.length > 0) {
70450
- ixs.push(
70451
- ...makeRefreshKaminoBanksIxs(marginfiAccount, bankMap, kaminoPks, bankMetadataMap).instructions
70452
- );
70453
- }
70454
- const jupPksInTx = txBanks.filter((b) => b.config.assetTag === 6 /* JUPLEND */).map((b) => b.address);
70455
- if (jupPksInTx.length > 0) {
70456
- ixs.push(
70457
- ...makeUpdateJupLendRateIxs(marginfiAccount, bankMap, jupPksInTx, bankMetadataMap).instructions
70458
- );
70459
- }
70460
- return ixs;
70461
- }
70462
70349
  async function makeBulkWithdrawTx(params) {
70463
70350
  const {
70464
70351
  program,
@@ -70470,14 +70357,19 @@ async function makeBulkWithdrawTx(params) {
70470
70357
  oraclePrices,
70471
70358
  assetShareValueMultiplierByBank,
70472
70359
  tokenProgramsByBank,
70473
- overrideInferAccounts
70360
+ overrideInferAccounts,
70361
+ luts
70474
70362
  } = params;
70475
- const luts = params.addressLookupTableAccounts ?? [];
70476
- const groupRateLimiterEnabled = params.groupRateLimiterEnabled ?? false;
70477
70363
  const authority = marginfiAccount.authority;
70478
70364
  if (bankAddresses.length === 0) throw new Error("no banks to withdraw");
70479
70365
  const activeBalances = marginfiAccount.balances.filter((b) => b.active);
70480
- const legs = [];
70366
+ const involvedBanks = [
70367
+ ...bankAddresses.map((pk2) => requireBank(bankMap, pk2)),
70368
+ ...activeBalances.flatMap((b) => bankMap.get(b.bankPk.toBase58()) ?? [])
70369
+ ];
70370
+ const selectedLuts = selectLutsForBanks(luts, involvedBanks);
70371
+ const withdrawIxs = [];
70372
+ const setupTokens = [];
70481
70373
  const withdrawnSoFar = [];
70482
70374
  for (const bankAddress of bankAddresses) {
70483
70375
  const bank = requireBank(bankMap, bankAddress);
@@ -70486,15 +70378,15 @@ async function makeBulkWithdrawTx(params) {
70486
70378
  if (!balance || !balance.assetShares.gt(0)) {
70487
70379
  throw new Error(`no active deposit for bank ${bankAddress.toBase58()}`);
70488
70380
  }
70489
- const multiplier = assetShareValueMultiplierByBank.get(bankAddress.toBase58());
70490
- const uiAmount = computeQuantityUi(balance, bank, multiplier).assets;
70491
- const packBanks = computeHealthCheckAccounts(
70492
- marginfiAccount.balances,
70493
- bankMap,
70494
- [],
70495
- [...withdrawnSoFar, bankAddress]
70496
- );
70497
- const observationBanksOverride = computeHealthAccountMetas(packBanks, true, [bank]);
70381
+ const packBanks = computeHealthCheckAccounts({
70382
+ account: marginfiAccount,
70383
+ banksMap: bankMap,
70384
+ excludedBanks: [...withdrawnSoFar, bankAddress]
70385
+ });
70386
+ const observationBanksOverride = computeHealthAccountMetas({
70387
+ banksToInclude: packBanks,
70388
+ trailingBanks: [bank]
70389
+ });
70498
70390
  const shared = {
70499
70391
  program,
70500
70392
  bank,
@@ -70503,11 +70395,14 @@ async function makeBulkWithdrawTx(params) {
70503
70395
  marginfiAccount,
70504
70396
  authority,
70505
70397
  bankMetadataMap,
70398
+ // every venue's withdraw ix ignores the amount when the withdraw-all flag is
70399
+ // set and derives the full position on-chain, so all legs pass amount 0
70506
70400
  withdrawAll: true,
70507
70401
  opts: {
70508
70402
  createAtas: false,
70509
70403
  // ATAs are created in the prelude txs
70510
- wrapAndUnwrapSol: true,
70404
+ wrapAndUnwrapSol: false,
70405
+ // one unwrap ix is appended after the last withdraw
70511
70406
  overrideInferAccounts,
70512
70407
  observationBanksOverride
70513
70408
  }
@@ -70521,7 +70416,7 @@ async function makeBulkWithdrawTx(params) {
70521
70416
  }
70522
70417
  const withdraw = await makeKaminoWithdrawIx3({
70523
70418
  ...shared,
70524
- cTokenAmount: uiAmount.div(multiplier ?? new BigNumber3.BigNumber(1)),
70419
+ cTokenAmount: 0,
70525
70420
  reserve
70526
70421
  });
70527
70422
  instructions2 = withdraw.instructions;
@@ -70534,7 +70429,7 @@ async function makeBulkWithdrawTx(params) {
70534
70429
  }
70535
70430
  const withdraw = await makeJuplendWithdrawIx2({
70536
70431
  ...shared,
70537
- amount: uiAmount,
70432
+ amount: 0,
70538
70433
  jupLendingState
70539
70434
  });
70540
70435
  instructions2 = withdraw.instructions;
@@ -70547,7 +70442,7 @@ async function makeBulkWithdrawTx(params) {
70547
70442
  }
70548
70443
  const withdraw = await makeDriftWithdrawIx3({
70549
70444
  ...shared,
70550
- amount: uiAmount,
70445
+ amount: 0,
70551
70446
  driftSpotMarket: driftState.spotMarketState,
70552
70447
  userRewards: driftState.userRewards
70553
70448
  });
@@ -70557,76 +70452,49 @@ async function makeBulkWithdrawTx(params) {
70557
70452
  default: {
70558
70453
  const withdraw = await makeWithdrawIx3({
70559
70454
  ...shared,
70560
- amount: uiAmount
70455
+ amount: 0
70561
70456
  });
70562
70457
  instructions2 = withdraw.instructions;
70563
70458
  break;
70564
70459
  }
70565
70460
  }
70566
- legs.push({ bank, instructions: instructions2 });
70461
+ withdrawIxs.push(...instructions2);
70462
+ setupTokens.push({ mint: bank.mint, tokenProgram });
70567
70463
  withdrawnSoFar.push(bankAddress);
70568
70464
  }
70569
- const bundles = [];
70570
- let current = [];
70571
- const assembleTxIxs = (bundle) => [
70572
- ...buildTxRefreshIxs({
70573
- marginfiAccount,
70574
- bankMap,
70575
- bankMetadataMap,
70576
- txBanks: bundle.map((l) => l.bank)
70577
- }),
70578
- ...bundle.flatMap((l) => l.instructions)
70579
- ];
70580
- for (const leg of legs) {
70581
- const candidate = [...current, leg];
70582
- if (current.length > 0 && !fitsInTx(assembleTxIxs(candidate), authority, luts)) {
70583
- bundles.push(current);
70584
- current = [leg];
70585
- } else {
70586
- current = candidate;
70587
- }
70588
- }
70589
- if (current.length > 0) bundles.push(current);
70590
- for (const bundle of bundles) {
70591
- const ixs = assembleTxIxs(bundle);
70592
- if (bundle.length === 1 && !fitsInTx(ixs, authority, luts)) {
70593
- throw new Error(
70594
- `withdraw for bank ${bundle[0].bank.address.toBase58()} does not fit one transaction`
70595
- );
70596
- }
70465
+ if (setupTokens.some((t) => t.mint.equals(NATIVE_MINT))) {
70466
+ withdrawIxs.push(makeUnwrapSolIx(authority));
70597
70467
  }
70598
70468
  const { blockhash } = await connection.getLatestBlockhash("confirmed");
70599
- const withdrawTxs = bundles.map((bundle) => {
70600
- const message = new web3_js.TransactionMessage({
70601
- payerKey: authority,
70602
- recentBlockhash: blockhash,
70603
- instructions: assembleTxIxs(bundle)
70604
- }).compileToV0Message(luts);
70605
- return addTransactionMetadata(new web3_js.VersionedTransaction(message), {
70606
- addressLookupTables: luts,
70469
+ const withdrawTxs = splitInstructionsToFitTransactions([], withdrawIxs, {
70470
+ blockhash,
70471
+ payerKey: authority,
70472
+ luts: selectedLuts,
70473
+ sizeMargin: BULK_TX_SIZE_MARGIN,
70474
+ maxAccountLocks: MAX_ACCOUNT_LOCKS
70475
+ }).map(
70476
+ (tx) => addTransactionMetadata(tx, {
70477
+ addressLookupTables: selectedLuts,
70607
70478
  type: "WITHDRAW" /* WITHDRAW */
70608
- });
70609
- });
70479
+ })
70480
+ );
70610
70481
  const additionalTxs = [];
70611
70482
  const setupIxs = await makeSetupIx({
70612
70483
  connection,
70613
70484
  authority,
70614
- tokens: legs.map((l) => ({
70615
- mint: l.bank.mint,
70616
- tokenProgram: requireTokenProgram(tokenProgramsByBank, l.bank.address)
70617
- }))
70485
+ tokens: setupTokens
70618
70486
  });
70619
70487
  if (setupIxs.length > 0) {
70620
70488
  const setupTxs = splitInstructionsToFitTransactions([], setupIxs, {
70621
70489
  blockhash,
70622
70490
  payerKey: authority,
70623
- luts
70491
+ luts: selectedLuts
70624
70492
  });
70625
70493
  additionalTxs.push(
70626
70494
  ...setupTxs.map(
70627
70495
  (tx) => addTransactionMetadata(tx, {
70628
70496
  type: "CREATE_ATA" /* CREATE_ATA */,
70629
- addressLookupTables: luts
70497
+ addressLookupTables: selectedLuts
70630
70498
  })
70631
70499
  )
70632
70500
  );
@@ -70636,11 +70504,10 @@ async function makeBulkWithdrawTx(params) {
70636
70504
  bankMap,
70637
70505
  oraclePrices,
70638
70506
  assetShareValueMultiplierByBank,
70639
- instructions: legs.flatMap((l) => l.instructions),
70507
+ instructions: withdrawIxs,
70640
70508
  program,
70641
70509
  connection,
70642
- crossbarUrl: params.crossbarUrl,
70643
- groupRateLimiterEnabled
70510
+ crossbarUrl: params.crossbarUrl
70644
70511
  });
70645
70512
  if (crankIxs.length > 0) {
70646
70513
  const message = new web3_js.TransactionMessage({
@@ -70655,6 +70522,27 @@ async function makeBulkWithdrawTx(params) {
70655
70522
  })
70656
70523
  );
70657
70524
  }
70525
+ const refreshIxs = makeRefreshIntegrationBanksIxs(
70526
+ marginfiAccount,
70527
+ bankMap,
70528
+ bankAddresses,
70529
+ bankMetadataMap
70530
+ ).instructions;
70531
+ if (refreshIxs.length > 0) {
70532
+ const refreshTxs = splitInstructionsToFitTransactions([], refreshIxs, {
70533
+ blockhash,
70534
+ payerKey: authority,
70535
+ luts: selectedLuts
70536
+ });
70537
+ additionalTxs.push(
70538
+ ...refreshTxs.map(
70539
+ (tx) => addTransactionMetadata(tx, {
70540
+ type: "CRANK" /* CRANK */,
70541
+ addressLookupTables: selectedLuts
70542
+ })
70543
+ )
70544
+ );
70545
+ }
70658
70546
  return {
70659
70547
  transactions: [...additionalTxs, ...withdrawTxs],
70660
70548
  actionTxIndex: additionalTxs.length
@@ -70674,7 +70562,7 @@ async function makeBulkRepayTx(params) {
70674
70562
  const authority = marginfiAccount.authority;
70675
70563
  if (bankAddresses.length === 0) throw new Error("no banks to repay");
70676
70564
  const activeBalances = marginfiAccount.balances.filter((b) => b.active);
70677
- const legs = [];
70565
+ const repayIxs = [];
70678
70566
  for (const bankAddress of bankAddresses) {
70679
70567
  const bank = requireBank(bankMap, bankAddress);
70680
70568
  const tokenProgram = requireTokenProgram(tokenProgramsByBank, bankAddress);
@@ -70696,33 +70584,21 @@ async function makeBulkRepayTx(params) {
70696
70584
  overrideInferAccounts
70697
70585
  }
70698
70586
  });
70699
- legs.push({ bank, instructions: repay.instructions });
70700
- }
70701
- const bundles = [];
70702
- let current = [];
70703
- for (const leg of legs) {
70704
- const candidate = [...current, leg];
70705
- const candidateIxs = candidate.flatMap((l) => l.instructions);
70706
- if (current.length > 0 && !fitsInTx(candidateIxs, authority, luts)) {
70707
- bundles.push(current);
70708
- current = [leg];
70709
- } else {
70710
- current = candidate;
70711
- }
70587
+ repayIxs.push(...repay.instructions);
70712
70588
  }
70713
- if (current.length > 0) bundles.push(current);
70714
70589
  const { blockhash } = await connection.getLatestBlockhash("confirmed");
70715
- const transactions = bundles.map((bundle) => {
70716
- const message = new web3_js.TransactionMessage({
70717
- payerKey: authority,
70718
- recentBlockhash: blockhash,
70719
- instructions: bundle.flatMap((l) => l.instructions)
70720
- }).compileToV0Message(luts);
70721
- return addTransactionMetadata(new web3_js.VersionedTransaction(message), {
70590
+ const transactions = splitInstructionsToFitTransactions([], repayIxs, {
70591
+ blockhash,
70592
+ payerKey: authority,
70593
+ luts,
70594
+ sizeMargin: BULK_TX_SIZE_MARGIN,
70595
+ maxAccountLocks: MAX_ACCOUNT_LOCKS
70596
+ }).map(
70597
+ (tx) => addTransactionMetadata(tx, {
70722
70598
  addressLookupTables: luts,
70723
70599
  type: "REPAY" /* REPAY */
70724
- });
70725
- });
70600
+ })
70601
+ );
70726
70602
  return { transactions, actionTxIndex: 0 };
70727
70603
  }
70728
70604
 
@@ -70861,9 +70737,8 @@ async function simulateAccountHealthCache(params) {
70861
70737
  );
70862
70738
  const healthPulseIxs = await makePulseHealthIx2(
70863
70739
  program,
70864
- marginfiAccount.address,
70740
+ marginfiAccount,
70865
70741
  banksMap,
70866
- marginfiAccount.balances,
70867
70742
  activeBalances.map((b) => b.bankPk),
70868
70743
  []
70869
70744
  );
@@ -71051,9 +70926,8 @@ async function getHealthSimulationTransactions({
71051
70926
  );
71052
70927
  const healthPulseIx = await makePulseHealthIx2(
71053
70928
  program,
71054
- marginfiAccount.address,
70929
+ marginfiAccount,
71055
70930
  bankMap,
71056
- marginfiAccount.balances,
71057
70931
  mandatoryBanks,
71058
70932
  excludedBanks
71059
70933
  );
@@ -71514,13 +71388,13 @@ async function computeSmartCrank({
71514
71388
  assetShareValueMultiplierByBank,
71515
71389
  groupRateLimiterEnabled = false
71516
71390
  }) {
71517
- const { projectedBalances, withdrawnBanks } = computeProjectedActiveBalancesNoCpi(
71518
- marginfiAccount.balances,
71519
- instructions2,
71391
+ const { projectedBalances, withdrawnBanks } = computeProjectedActiveBalancesNoCpi({
71392
+ account: marginfiAccount,
71393
+ instructions: instructions2,
71520
71394
  program,
71521
- bankMap,
71395
+ banksMap: bankMap,
71522
71396
  assetShareValueMultiplierByBank
71523
- );
71397
+ });
71524
71398
  const isSwitchboard = (bank) => getOracleSourceFromBank(bank).key === "switchboard";
71525
71399
  const rateLimitBanks = groupRateLimiterEnabled ? withdrawnBanks.map((pk2) => bankMap.get(pk2)).filter((bank) => bank !== void 0).filter(isSwitchboard) : [];
71526
71400
  let rateLimitOracles = [];
@@ -71828,6 +71702,55 @@ async function makeSmartCrankSwbFeedIx(params) {
71828
71702
  });
71829
71703
  return { instructions: instructions2, luts };
71830
71704
  }
71705
+ async function makeSmartCrankSwbFeedIxForAccounts(params) {
71706
+ const crankResults = await Promise.all(
71707
+ params.marginfiAccounts.map(
71708
+ (marginfiAccount) => computeSmartCrank({ ...params, marginfiAccount })
71709
+ )
71710
+ );
71711
+ const uncrankableLiabilities = crankResults.flatMap((r) => r.uncrankableLiabilities);
71712
+ const uncrankableAssets = crankResults.flatMap((r) => r.uncrankableAssets);
71713
+ if (uncrankableLiabilities.length > 0) {
71714
+ console.log(
71715
+ "Uncrankable liability details:",
71716
+ uncrankableLiabilities.map((l) => ({
71717
+ symbol: l.bank.tokenSymbol,
71718
+ reason: l.reason
71719
+ }))
71720
+ );
71721
+ }
71722
+ if (uncrankableAssets.length > 0) {
71723
+ console.log(
71724
+ "Uncrankable asset details:",
71725
+ uncrankableAssets.map((a) => ({
71726
+ symbol: a.bank.tokenSymbol,
71727
+ reason: a.reason
71728
+ }))
71729
+ );
71730
+ }
71731
+ if (crankResults.some((r) => !r.isCrankable)) {
71732
+ throw TransactionBuildingError.oracleCrankFailed(
71733
+ uncrankableLiabilities.map((liability) => ({
71734
+ bankAddress: liability.bank.address.toBase58(),
71735
+ mint: liability.bank.mint.toBase58(),
71736
+ symbol: liability.bank.tokenSymbol,
71737
+ reason: liability.reason
71738
+ })),
71739
+ uncrankableAssets.map((asset) => ({
71740
+ bankAddress: asset.bank.address.toBase58(),
71741
+ mint: asset.bank.mint.toBase58(),
71742
+ symbol: asset.bank.tokenSymbol,
71743
+ reason: asset.reason
71744
+ }))
71745
+ );
71746
+ }
71747
+ return makeUpdateSwbFeedIx({
71748
+ swbPullOracles: crankResults.flatMap((r) => r.requiredOracles),
71749
+ feePayer: params.marginfiAccounts[0].authority,
71750
+ connection: params.connection,
71751
+ crossbarUrl: params.crossbarUrl
71752
+ });
71753
+ }
71831
71754
  var DEFAULT_CROSSBAR_URL = "https://crossbar.0.xyz";
71832
71755
  var DEFAULT_FALLBACK_CROSSBAR_URL = "https://crossbar.switchboard.xyz";
71833
71756
  async function makeCrankSwbFeedIx(marginfiAccount, bankMap, newBanksPk, provider, crossbarUrl) {
@@ -71998,6 +71921,36 @@ function makeUpdateJupLendRateIxs(marginfiAccount, bankMap, banksToExclude, bank
71998
71921
  keys: []
71999
71922
  };
72000
71923
  }
71924
+
71925
+ // src/services/price/actions/refresh-integration-banks.ts
71926
+ function makeRefreshIntegrationBanksIxs(marginfiAccount, bankMap, banksToExclude, bankMetadataMap, kaminoNewBanksPk = banksToExclude) {
71927
+ const kaminoRefreshIxs = makeRefreshKaminoBanksIxs(
71928
+ marginfiAccount,
71929
+ bankMap,
71930
+ kaminoNewBanksPk,
71931
+ bankMetadataMap
71932
+ );
71933
+ const updateDriftMarketIxs = makeUpdateDriftMarketIxs(
71934
+ marginfiAccount,
71935
+ bankMap,
71936
+ banksToExclude,
71937
+ bankMetadataMap
71938
+ );
71939
+ const updateJupLendRateIxs = makeUpdateJupLendRateIxs(
71940
+ marginfiAccount,
71941
+ bankMap,
71942
+ banksToExclude,
71943
+ bankMetadataMap
71944
+ );
71945
+ return {
71946
+ instructions: [
71947
+ ...kaminoRefreshIxs.instructions,
71948
+ ...updateDriftMarketIxs.instructions,
71949
+ ...updateJupLendRateIxs.instructions
71950
+ ],
71951
+ keys: [...kaminoRefreshIxs.keys, ...updateDriftMarketIxs.keys, ...updateJupLendRateIxs.keys]
71952
+ };
71953
+ }
72001
71954
  function bankMetadataMapToDto(bankMetadataMap) {
72002
71955
  return Object.fromEntries(
72003
71956
  Object.entries(bankMetadataMap).map(([bankPk, bankMetadata]) => [
@@ -76403,6 +76356,7 @@ exports.makePriorityFeeMicroIx = makePriorityFeeMicroIx;
76403
76356
  exports.makePulseHealthIx = makePulseHealthIx2;
76404
76357
  exports.makeRedeemStakedLstIx = makeRedeemStakedLstIx;
76405
76358
  exports.makeRedeemStakedLstTx = makeRedeemStakedLstTx;
76359
+ exports.makeRefreshIntegrationBanksIxs = makeRefreshIntegrationBanksIxs;
76406
76360
  exports.makeRefreshKaminoBanksIxs = makeRefreshKaminoBanksIxs;
76407
76361
  exports.makeRepayIx = makeRepayIx3;
76408
76362
  exports.makeRepayTx = makeRepayTx;
@@ -76410,6 +76364,7 @@ exports.makeRepayWithCollatTx = makeRepayWithCollatTx;
76410
76364
  exports.makeRollPtTx = makeRollPtTx;
76411
76365
  exports.makeSetupIx = makeSetupIx;
76412
76366
  exports.makeSmartCrankSwbFeedIx = makeSmartCrankSwbFeedIx;
76367
+ exports.makeSmartCrankSwbFeedIxForAccounts = makeSmartCrankSwbFeedIxForAccounts;
76413
76368
  exports.makeSwapCollateralTx = makeSwapCollateralTx;
76414
76369
  exports.makeSwapDebtTx = makeSwapDebtTx;
76415
76370
  exports.makeTransferPositionsTx = makeTransferPositionsTx;