@0dotxyz/p0-ts-sdk 1.2.0-alpha.1 → 1.2.0

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
@@ -43411,7 +43411,7 @@ async function buildSwapCollateralFlashloanTx({
43411
43411
  driftSpotMarket: driftState.spotMarketState,
43412
43412
  userRewards: driftState.userRewards,
43413
43413
  withdrawAll: true,
43414
- isSync: true,
43414
+ isSync: false,
43415
43415
  opts: {
43416
43416
  createAtas: false,
43417
43417
  wrapAndUnwrapSol: false,
@@ -43868,7 +43868,7 @@ async function buildSwapDebtFlashloanTx({
43868
43868
  // src/services/account/services/account-simulation.service.ts
43869
43869
  async function simulateAccountHealthCacheWithFallback(props) {
43870
43870
  let marginfiAccount = props.marginfiAccount;
43871
- const activeBalances = props.balances.filter((b) => b.active);
43871
+ const activeBalances = marginfiAccount.balances.filter((b) => b.active);
43872
43872
  const { assets: assetValueEquity, liabilities: liabilityValueEquity } = computeHealthComponentsWithoutBiasLegacy(
43873
43873
  activeBalances,
43874
43874
  props.bankMap,
@@ -43879,8 +43879,7 @@ async function simulateAccountHealthCacheWithFallback(props) {
43879
43879
  const simulatedAccount = await simulateAccountHealthCache({
43880
43880
  program: props.program,
43881
43881
  bankMap: props.bankMap,
43882
- marginfiAccountPk: props.marginfiAccount.address,
43883
- balances: props.balances,
43882
+ marginfiAccount: props.marginfiAccount,
43884
43883
  bankMetadataMap: props.bankMetadataMap
43885
43884
  });
43886
43885
  simulatedAccount.healthCache.assetValueEquity = bigNumberToWrappedI80F48(assetValueEquity);
@@ -43921,8 +43920,8 @@ async function simulateAccountHealthCacheWithFallback(props) {
43921
43920
  return { marginfiAccount };
43922
43921
  }
43923
43922
  async function simulateAccountHealthCache(props) {
43924
- const { program, bankMap, marginfiAccountPk, balances, bankMetadataMap } = props;
43925
- const activeBalances = balances.filter((b) => b.active);
43923
+ const { program, bankMap, marginfiAccount, bankMetadataMap } = props;
43924
+ const activeBalances = marginfiAccount.balances.filter((b) => b.active);
43926
43925
  const activeBanks = activeBalances.map((balance) => bankMap.get(balance.bankPk.toBase58())).filter((bank) => !!bank);
43927
43926
  const kaminoBanks = activeBanks.filter((bank) => bank.config.assetTag === 3 /* KAMINO */);
43928
43927
  const driftBanks = activeBanks.filter((bank) => bank.config.assetTag === 4 /* DRIFT */);
@@ -43936,7 +43935,7 @@ async function simulateAccountHealthCache(props) {
43936
43935
  const fundAccountIx = web3_js.SystemProgram.transfer({
43937
43936
  fromPubkey: new web3_js.PublicKey("DD3AeAssFvjqTvRTrRAtpfjkBF8FpVKnFuwnMLN9haXD"),
43938
43937
  // marginfi SOL VAULT
43939
- toPubkey: program.provider.publicKey,
43938
+ toPubkey: marginfiAccount.authority,
43940
43939
  lamports: 1e8
43941
43940
  // 0.1 SOL
43942
43941
  });
@@ -43975,7 +43974,7 @@ async function simulateAccountHealthCache(props) {
43975
43974
  swbPullOracles: staleSwbOracles.map((oracle) => ({
43976
43975
  key: oracle.oracleKey
43977
43976
  })),
43978
- feePayer: program.provider.publicKey,
43977
+ feePayer: marginfiAccount.authority,
43979
43978
  connection: program.provider.connection
43980
43979
  }) : { instructions: [], luts: [] };
43981
43980
  const updateDriftMarketIxs = updateDriftMarketData.map((market) => ({
@@ -43985,16 +43984,16 @@ async function simulateAccountHealthCache(props) {
43985
43984
  }));
43986
43985
  const healthPulseIxs = await makePulseHealthIx2(
43987
43986
  program,
43988
- marginfiAccountPk,
43987
+ marginfiAccount.address,
43989
43988
  bankMap,
43990
- balances,
43989
+ marginfiAccount.balances,
43991
43990
  activeBalances.map((b) => b.bankPk),
43992
43991
  []
43993
43992
  );
43994
43993
  const txs = [];
43995
43994
  const additionalTx = new web3_js.VersionedTransaction(
43996
43995
  new web3_js.TransactionMessage({
43997
- payerKey: program.provider.publicKey,
43996
+ payerKey: marginfiAccount.authority,
43998
43997
  recentBlockhash: blockhash,
43999
43998
  instructions: [
44000
43999
  computeIx,
@@ -44007,7 +44006,7 @@ async function simulateAccountHealthCache(props) {
44007
44006
  txs.push(additionalTx);
44008
44007
  const swbTx = new web3_js.VersionedTransaction(
44009
44008
  new web3_js.TransactionMessage({
44010
- payerKey: program.provider.publicKey,
44009
+ payerKey: marginfiAccount.authority,
44011
44010
  recentBlockhash: blockhash,
44012
44011
  instructions: [...crankSwbIxs.instructions]
44013
44012
  }).compileToV0Message([...crankSwbIxs.luts])
@@ -44015,7 +44014,7 @@ async function simulateAccountHealthCache(props) {
44015
44014
  txs.push(swbTx);
44016
44015
  const healthTx = new web3_js.VersionedTransaction(
44017
44016
  new web3_js.TransactionMessage({
44018
- payerKey: program.provider.publicKey,
44017
+ payerKey: marginfiAccount.authority,
44019
44018
  recentBlockhash: blockhash,
44020
44019
  instructions: [computeIx, ...healthPulseIxs.instructions]
44021
44020
  }).compileToV0Message([])
@@ -44026,7 +44025,7 @@ async function simulateAccountHealthCache(props) {
44026
44025
  throw new Error("Too many transactions");
44027
44026
  }
44028
44027
  const simulationResult = await simulateBundle(program.provider.connection.rpcEndpoint, txs, [
44029
- marginfiAccountPk
44028
+ marginfiAccount.address
44030
44029
  ]);
44031
44030
  const postExecutionAccount = simulationResult.find(
44032
44031
  (result) => result.postExecutionAccounts.length > 0
@@ -44241,23 +44240,19 @@ var fetchMarginfiAccountAddresses = async (program, authority, group) => {
44241
44240
  return marginfiAccounts;
44242
44241
  };
44243
44242
  var fetchMarginfiAccountData = async (program, marginfiAccountPk, bankMap, bankMetadataMap) => {
44244
- const marginfiAccountRaw = await program.account.marginfiAccount.fetch(marginfiAccountPk, "confirmed");
44245
- const marginfiAccount = parseMarginfiAccountRaw(
44243
+ const marginfiAccountRaw = await program.account.marginfiAccount.fetch(
44246
44244
  marginfiAccountPk,
44247
- marginfiAccountRaw
44245
+ "confirmed"
44248
44246
  );
44247
+ const marginfiAccount = parseMarginfiAccountRaw(marginfiAccountPk, marginfiAccountRaw);
44249
44248
  try {
44250
44249
  const simulatedAccount = await simulateAccountHealthCache({
44251
44250
  program,
44252
44251
  bankMap,
44253
- marginfiAccountPk,
44254
- balances: marginfiAccount.balances,
44252
+ marginfiAccount,
44255
44253
  bankMetadataMap
44256
44254
  });
44257
- const marginfiAccountWithCache = parseMarginfiAccountRaw(
44258
- marginfiAccountPk,
44259
- simulatedAccount
44260
- );
44255
+ const marginfiAccountWithCache = parseMarginfiAccountRaw(marginfiAccountPk, simulatedAccount);
44261
44256
  return { marginfiAccount: marginfiAccountWithCache };
44262
44257
  } catch (e) {
44263
44258
  console.error("Error simulating account health cache", e);
@@ -44279,10 +44274,7 @@ async function findRandomAvailableAccountIndex(connection, programId, group, aut
44279
44274
  const BATCH_SIZE = 16;
44280
44275
  const MAX_ATTEMPTS = 8;
44281
44276
  for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
44282
- const indices = randomDistinctIndices(
44283
- Math.min(BATCH_SIZE, MAX_INDEX),
44284
- MAX_INDEX
44285
- );
44277
+ const indices = randomDistinctIndices(Math.min(BATCH_SIZE, MAX_INDEX), MAX_INDEX);
44286
44278
  const pdas = indices.map(
44287
44279
  (i) => deriveMarginfiAccount(programId, group, authority, i, thirdPartyId)[0]
44288
44280
  );
@@ -47087,7 +47079,6 @@ var MarginfiAccount = class _MarginfiAccount {
47087
47079
  bankMap,
47088
47080
  oraclePrices,
47089
47081
  marginfiAccount: this,
47090
- balances: this.balances,
47091
47082
  bankMetadataMap
47092
47083
  });
47093
47084
  return {
@@ -48642,16 +48633,23 @@ var Project0Client = class _Project0Client {
48642
48633
  * @param accountAddress - The public key of the marginfi account
48643
48634
  * @returns A wrapped account ready to use
48644
48635
  */
48645
- async fetchAccount(accountAddress) {
48646
- const bankMap = new Map(this.banks.map((b) => [b.address.toBase58(), b]));
48647
- const { marginfiAccount } = await fetchMarginfiAccountData(
48648
- this.program,
48636
+ async fetchAccount(accountAddress, skipHealthCache) {
48637
+ const marginfiAccountRaw = await this.program.account.marginfiAccount.fetch(accountAddress);
48638
+ let marginfiAccountParsed = MarginfiAccount.fromAccountParsed(
48649
48639
  accountAddress,
48650
- bankMap,
48651
- this.bankIntegrationMap
48640
+ marginfiAccountRaw
48652
48641
  );
48653
- const account = MarginfiAccount.fromAccountType(marginfiAccount);
48654
- return new MarginfiAccountWrapper(account, this);
48642
+ if (!skipHealthCache) {
48643
+ const bankMap = new Map(this.banks.map((b) => [b.address.toBase58(), b]));
48644
+ const { account: simulatedAccount } = await marginfiAccountParsed.simulateHealthCache(
48645
+ this.program,
48646
+ bankMap,
48647
+ this.oraclePriceByBank,
48648
+ this.bankIntegrationMap
48649
+ );
48650
+ marginfiAccountParsed = simulatedAccount;
48651
+ }
48652
+ return new MarginfiAccountWrapper(marginfiAccountParsed, this);
48655
48653
  }
48656
48654
  static async initialize(connection, config) {
48657
48655
  const { groupPk, programId } = config;