@debridge-finance/solana-utils 4.5.1 → 4.5.3

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.
@@ -1,4 +1,4 @@
1
- import { MessageV0, VersionedTransaction, Transaction, PublicKey, TransactionInstruction, SystemProgram, Keypair } from '@solana/web3.js';
1
+ import { MessageV0, VersionedTransaction, Transaction, PublicKey, TransactionInstruction, SystemProgram, AddressLookupTableAccount, Keypair } from '@solana/web3.js';
2
2
 
3
3
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
4
4
 
@@ -6664,7 +6664,9 @@ function sendAll(connection, wallet, transactions, options) {
6664
6664
  if (!skipPreflight) {
6665
6665
  yield Promise.all(transactions.map((tx) => simulateTx(connection, tx, simulationCommitment, wallet.publicKey, convertIntoTxV0, skipSign)));
6666
6666
  }
6667
- const blockhashResponse = yield connection.getLatestBlockhashAndContext({ commitment: blockhashCommitment });
6667
+ const blockhashResponse = yield connection.getLatestBlockhashAndContext({
6668
+ commitment: blockhashCommitment,
6669
+ });
6668
6670
  let signed = transactions;
6669
6671
  if (!skipSign) {
6670
6672
  // set real blockhash
@@ -6679,7 +6681,9 @@ function sendAll(connection, wallet, transactions, options) {
6679
6681
  return connection.sendEncodedTransaction(serialized, {
6680
6682
  preflightCommitment: simulationCommitment,
6681
6683
  skipPreflight: false,
6682
- minContextSlot: useMinContextSlot ? blockhashResponse.context.slot + 1 : undefined,
6684
+ minContextSlot: useMinContextSlot
6685
+ ? blockhashResponse.context.slot + 1
6686
+ : undefined,
6683
6687
  });
6684
6688
  }));
6685
6689
  }
@@ -6933,10 +6937,8 @@ function hashExternalCallBytes(data) {
6933
6937
  if (data === undefined || (data === null || data === void 0 ? void 0 : data.length) === 0) {
6934
6938
  // precomputed "empty" hash
6935
6939
  return buffer.Buffer.from([
6936
- 197, 210, 70, 1, 134, 247, 35, 60,
6937
- 146, 126, 125, 178, 220, 199, 3, 192,
6938
- 229, 0, 182, 83, 202, 130, 39, 59,
6939
- 123, 250, 216, 4, 93, 133, 164, 112
6940
+ 197, 210, 70, 1, 134, 247, 35, 60, 146, 126, 125, 178, 220, 199, 3, 192,
6941
+ 229, 0, 182, 83, 202, 130, 39, 59, 123, 250, 216, 4, 93, 133, 164, 112,
6940
6942
  ]);
6941
6943
  }
6942
6944
  return hexToBuffer(solidityKeccak256(["bytes"], [data ? data : buffer.Buffer.from([])]), 32);
@@ -7485,9 +7487,7 @@ const DlnSrc = {
7485
7487
  getFeeLedgerAddress: (DLN_SRC_PROGRAM_ID) => PublicKey.findProgramAddressSync([buffer.Buffer.from(dlnSrc$1.Seeds.FEE_LEDGER)], DLN_SRC_PROGRAM_ID),
7486
7488
  };
7487
7489
  const DlnDst = {
7488
- getStateAddress: (DLN_DST_PROGRAM_ID) => {
7489
- PublicKey.findProgramAddressSync([buffer.Buffer.from(dlnDst$1.Seeds.STATE)], DLN_DST_PROGRAM_ID);
7490
- },
7490
+ getStateAddress: (DLN_DST_PROGRAM_ID) => PublicKey.findProgramAddressSync([buffer.Buffer.from(dlnDst$1.Seeds.STATE)], DLN_DST_PROGRAM_ID),
7491
7491
  getAuthorizedSrcContractAddress: (chainId, DLN_DST_PROGRAM_ID) => PublicKey.findProgramAddressSync([buffer.Buffer.from(dlnDst$1.Seeds.AUTHORIZED_SRC_CONTRACT), chainId], DLN_DST_PROGRAM_ID),
7492
7492
  getAuthorizedDstNativeSenderAddress: (DLN_DST_PROGRAM_ID) => PublicKey.findProgramAddressSync([buffer.Buffer.from(dlnDst$1.Seeds.AUTHORIZED_DST_NATIVE_SENDER)], DLN_DST_PROGRAM_ID),
7493
7493
  getTakeOrderStateAddress: (orderId, DLN_DST_PROGRAM_ID) => PublicKey.findProgramAddressSync([buffer.Buffer.from(dlnDst$1.Seeds.ORDER_STATE), orderId], DLN_DST_PROGRAM_ID),
@@ -7524,37 +7524,37 @@ class ResolverContainer2 {
7524
7524
  }
7525
7525
  const DeBridgeResolver = (programId, settingsProgramId) => {
7526
7526
  const deBridgeMemoizeList = {
7527
- "getClaimMarkerAddress": {},
7528
- "getNonceAddress": {},
7529
- "getMintAuthorityAddress": { maxSize: 20 }
7527
+ getClaimMarkerAddress: {},
7528
+ getNonceAddress: {},
7529
+ getMintAuthorityAddress: { maxSize: 20 },
7530
7530
  };
7531
7531
  const settingsMemoizeList = {
7532
- "getTokenMetadataMasterAddress": {},
7533
- "getTokenMintAddress": { maxSize: 5 },
7534
- "getStateAddress": {},
7535
- "getBridgeFeeAddress": { maxSize: 40 },
7536
- "getChainSupportInfoAddress": { maxSize: 10 },
7537
- "getBridgeAddress": { maxSize: 10 },
7538
- "getBridgeMapAddress": { maxSize: 10 },
7539
- "getNoBridgeFeeAddress": {},
7540
- "getNoDiscountAddress": {},
7532
+ getTokenMetadataMasterAddress: {},
7533
+ getTokenMintAddress: { maxSize: 5 },
7534
+ getStateAddress: {},
7535
+ getBridgeFeeAddress: { maxSize: 40 },
7536
+ getChainSupportInfoAddress: { maxSize: 10 },
7537
+ getBridgeAddress: { maxSize: 10 },
7538
+ getBridgeMapAddress: { maxSize: 10 },
7539
+ getNoBridgeFeeAddress: {},
7540
+ getNoDiscountAddress: {},
7541
7541
  };
7542
7542
  return new ResolverContainer2(new Resolver(programId, deBridge$1, deBridgeMemoizeList), new Resolver(settingsProgramId, deBridgeSettings, settingsMemoizeList));
7543
7543
  };
7544
7544
  const dlnSrcResolver = (programId) => {
7545
7545
  const dlnMemoizeList = {
7546
- "getStateAccount": {},
7547
- "getAuthorizedNativeSenderAddress": { maxSize: 10 },
7548
- "getFeeLedgerAddress": {},
7549
- "getFeeLedgerWalletAddress": { maxSize: 10 }
7546
+ getStateAccount: {},
7547
+ getAuthorizedNativeSenderAddress: { maxSize: 10 },
7548
+ getFeeLedgerAddress: {},
7549
+ getFeeLedgerWalletAddress: { maxSize: 10 },
7550
7550
  };
7551
7551
  return new Resolver(programId, DlnSrc, dlnMemoizeList);
7552
7552
  };
7553
7553
  const dlnDstResolver = (programId) => {
7554
7554
  const dstMemoizeList = {
7555
- "getAuthorizedSrcContractAddress": { maxSize: 10 },
7556
- "getAuthorizedDstNativeSenderAddress": {},
7557
- "getStateAddress": {},
7555
+ getAuthorizedSrcContractAddress: { maxSize: 10 },
7556
+ getAuthorizedDstNativeSenderAddress: {},
7557
+ getStateAddress: {},
7558
7558
  };
7559
7559
  return new Resolver(programId, DlnDst, dstMemoizeList);
7560
7560
  };
@@ -10074,7 +10074,7 @@ function parseSplAccount(dataOrInfo) {
10074
10074
  if (dataOrInfo === null) {
10075
10075
  return null;
10076
10076
  }
10077
- const data = 'executable' in dataOrInfo ? dataOrInfo.data : dataOrInfo;
10077
+ const data = "executable" in dataOrInfo ? dataOrInfo.data : dataOrInfo;
10078
10078
  try {
10079
10079
  return AccountLayout.decode(data);
10080
10080
  }
@@ -10262,6 +10262,128 @@ function checkIfAssociatedWalletExists(connection, mintAccount, originalWallet)
10262
10262
  return accountData !== null;
10263
10263
  });
10264
10264
  }
10265
+ function getTransactionAccountsForSimulationDiff(connection, tx) {
10266
+ var _a, _b;
10267
+ return __awaiter(this, void 0, void 0, function* () {
10268
+ const result = new Set();
10269
+ if ("version" in tx) {
10270
+ const ALTs = tx.message.addressTableLookups.map((alt) => alt.accountKey);
10271
+ const fetchedALTs = yield connection.getMultipleAccountsInfo(ALTs);
10272
+ const parsedALTs = fetchedALTs
10273
+ .map((fetched, i) => fetched
10274
+ ? new AddressLookupTableAccount({
10275
+ state: AddressLookupTableAccount.deserialize(fetched.data),
10276
+ key: ALTs[i],
10277
+ })
10278
+ : null)
10279
+ .filter((x) => x != null);
10280
+ const accGetter = tx.message.getAccountKeys({
10281
+ addressLookupTableAccounts: parsedALTs,
10282
+ });
10283
+ for (const acc of [
10284
+ ...accGetter.staticAccountKeys,
10285
+ ...((_b = (_a = accGetter.accountKeysFromLookups) === null || _a === void 0 ? void 0 : _a.writable) !== null && _b !== void 0 ? _b : []),
10286
+ ]) {
10287
+ result.add(acc.toBase58());
10288
+ }
10289
+ }
10290
+ else {
10291
+ for (const ix of tx.instructions) {
10292
+ // we don't need read-only accounts and instruction.programId since they won't change
10293
+ ix.keys
10294
+ .filter((meta) => meta.isSigner || meta.isWritable)
10295
+ .map((meta) => result.add(meta.pubkey.toBase58()));
10296
+ }
10297
+ }
10298
+ return Array.from(result);
10299
+ });
10300
+ }
10301
+ function parseMultipleAccounts(accounts) {
10302
+ return accounts.map((account) => {
10303
+ var _a, _b;
10304
+ let parsedSplAccount;
10305
+ if (account) {
10306
+ const owner = new PublicKey(account.owner);
10307
+ if (owner.equals(TOKEN_PROGRAM_ID)) {
10308
+ if (account.data instanceof buffer.Buffer) {
10309
+ parsedSplAccount = parseSplAccount(account.data);
10310
+ }
10311
+ else if (account.data instanceof Array) {
10312
+ parsedSplAccount = parseSplAccount(buffer.Buffer.from(account.data[0], account.data[1]));
10313
+ }
10314
+ }
10315
+ }
10316
+ const parsedAccount = {
10317
+ balance: (_a = account === null || account === void 0 ? void 0 : account.lamports) !== null && _a !== void 0 ? _a : 0,
10318
+ };
10319
+ if (parsedSplAccount) {
10320
+ parsedAccount.spl = {
10321
+ mint: parsedSplAccount.mint,
10322
+ owner: parsedSplAccount.owner,
10323
+ balance: Number((_b = parsedSplAccount.amount) !== null && _b !== void 0 ? _b : 0),
10324
+ };
10325
+ }
10326
+ return parsedAccount;
10327
+ });
10328
+ }
10329
+ function getSplDiff(pre, post) {
10330
+ var _a, _b, _c, _d, _e, _f;
10331
+ const preBalance = (_a = pre === null || pre === void 0 ? void 0 : pre.balance) !== null && _a !== void 0 ? _a : 0;
10332
+ const postBalance = (_b = post === null || post === void 0 ? void 0 : post.balance) !== null && _b !== void 0 ? _b : 0;
10333
+ return {
10334
+ mint: (_c = pre === null || pre === void 0 ? void 0 : pre.mint) !== null && _c !== void 0 ? _c : post === null || post === void 0 ? void 0 : post.mint,
10335
+ owner: (_d = pre === null || pre === void 0 ? void 0 : pre.owner) !== null && _d !== void 0 ? _d : post === null || post === void 0 ? void 0 : post.owner,
10336
+ preBalance: (_e = pre === null || pre === void 0 ? void 0 : pre.balance) !== null && _e !== void 0 ? _e : 0,
10337
+ postBalance: (_f = post === null || post === void 0 ? void 0 : post.balance) !== null && _f !== void 0 ? _f : 0,
10338
+ diff: postBalance - preBalance,
10339
+ };
10340
+ }
10341
+ function getTransactionDiff(connection, tx, txAccounts) {
10342
+ return __awaiter(this, void 0, void 0, function* () {
10343
+ const accountsStrings = txAccounts
10344
+ ? txAccounts.map((acc) => acc.toBase58())
10345
+ : yield getTransactionAccountsForSimulationDiff(connection, tx);
10346
+ const accountsPubKeys = txAccounts !== null && txAccounts !== void 0 ? txAccounts : accountsStrings.map((key) => new PublicKey(key));
10347
+ let [accountsInfo, simulatedTx] = yield Promise.all([
10348
+ connection.getMultipleAccountsInfo(accountsPubKeys),
10349
+ connection.simulateTransaction(tx, {
10350
+ replaceRecentBlockhash: true,
10351
+ accounts: {
10352
+ encoding: "base64",
10353
+ addresses: accountsStrings,
10354
+ },
10355
+ }),
10356
+ ]);
10357
+ const simulatesInfo = simulatedTx.value.accounts;
10358
+ const preBalances = parseMultipleAccounts(accountsInfo);
10359
+ const postBalances = parseMultipleAccounts(simulatesInfo);
10360
+ const balancesDiff = accountsPubKeys.reduce((map, pubKey, i) => {
10361
+ var _a;
10362
+ if ((_a = accountsInfo[i]) === null || _a === void 0 ? void 0 : _a.executable) {
10363
+ return map;
10364
+ }
10365
+ const preBalance = preBalances[i];
10366
+ const postBalance = postBalances[i];
10367
+ const diff = {
10368
+ pubKey: pubKey,
10369
+ preBalance: preBalance.balance,
10370
+ postBalance: postBalance.balance,
10371
+ diff: postBalance.balance - preBalance.balance,
10372
+ };
10373
+ if (preBalance.spl || postBalance.spl) {
10374
+ const splDiff = getSplDiff(preBalance.spl, postBalance.spl);
10375
+ if (splDiff.diff) {
10376
+ diff.spl = splDiff;
10377
+ }
10378
+ }
10379
+ if (diff.diff || diff.spl) {
10380
+ map.set(pubKey.toString(), diff);
10381
+ }
10382
+ return map;
10383
+ }, new Map());
10384
+ return balancesDiff;
10385
+ });
10386
+ }
10265
10387
 
10266
10388
  var spl = /*#__PURE__*/Object.freeze({
10267
10389
  __proto__: null,
@@ -10278,7 +10400,11 @@ var spl = /*#__PURE__*/Object.freeze({
10278
10400
  getTokenAccountsWithBalance: getTokenAccountsWithBalance,
10279
10401
  buildReplenishWsolBalanceTransaction: buildReplenishWsolBalanceTransaction,
10280
10402
  updateWsolBalance: updateWsolBalance,
10281
- checkIfAssociatedWalletExists: checkIfAssociatedWalletExists
10403
+ checkIfAssociatedWalletExists: checkIfAssociatedWalletExists,
10404
+ getTransactionAccountsForSimulationDiff: getTransactionAccountsForSimulationDiff,
10405
+ parseMultipleAccounts: parseMultipleAccounts,
10406
+ getSplDiff: getSplDiff,
10407
+ getTransactionDiff: getTransactionDiff
10282
10408
  });
10283
10409
 
10284
10410
  const deBridge = "DEbrdGj3HsRsAzx6uH4MKyREKxVAfBydijLUF3ygsFfh";
@@ -10300,26 +10426,34 @@ function simulateTransaction(connection, tx, simulateConfig) {
10300
10426
  var _a, _b, _c, _d, _e, _f, _g, _h;
10301
10427
  return __awaiter(this, void 0, void 0, function* () {
10302
10428
  const addressesToGetDiffFor = ((_b = (_a = simulateConfig.accounts) === null || _a === void 0 ? void 0 : _a.addresses) !== null && _b !== void 0 ? _b : []).map((a) => new PublicKey(a));
10303
- const accountsToFetch = addressesToGetDiffFor.length > 0 ? yield connection.getMultipleAccountsInfo(addressesToGetDiffFor) : [];
10429
+ const accountsToFetch = addressesToGetDiffFor.length > 0
10430
+ ? yield connection.getMultipleAccountsInfo(addressesToGetDiffFor)
10431
+ : [];
10304
10432
  const result = yield connection.simulateTransaction(tx, simulateConfig);
10305
10433
  const diffMap = new Map();
10306
10434
  for (const [idx, simulatedAccountInfo] of ((_c = result.value.accounts) !== null && _c !== void 0 ? _c : []).entries()) {
10307
10435
  const accountAddress = addressesToGetDiffFor[idx];
10308
10436
  const oldAccountData = (_d = accountsToFetch.at(idx)) !== null && _d !== void 0 ? _d : null;
10309
- const oldParsedMaybeSpl = (oldAccountData === null || oldAccountData === void 0 ? void 0 : oldAccountData.owner.equals(TOKEN_PROGRAM_ID)) ? parseSplAccount(oldAccountData) : null;
10437
+ const oldParsedMaybeSpl = (oldAccountData === null || oldAccountData === void 0 ? void 0 : oldAccountData.owner.equals(TOKEN_PROGRAM_ID))
10438
+ ? parseSplAccount(oldAccountData)
10439
+ : null;
10310
10440
  const oldSolValue = (_e = oldAccountData === null || oldAccountData === void 0 ? void 0 : oldAccountData.lamports) !== null && _e !== void 0 ? _e : 0;
10311
10441
  const newSolValue = (_f = simulatedAccountInfo === null || simulatedAccountInfo === void 0 ? void 0 : simulatedAccountInfo.lamports) !== null && _f !== void 0 ? _f : 0;
10312
- const solDiff = oldSolValue === newSolValue ? null : {
10313
- before: oldSolValue,
10314
- after: newSolValue,
10315
- };
10442
+ const solDiff = oldSolValue === newSolValue
10443
+ ? null
10444
+ : {
10445
+ before: oldSolValue,
10446
+ after: newSolValue,
10447
+ };
10316
10448
  let newParsedMaybeSpl = null;
10317
10449
  if (simulatedAccountInfo) {
10318
10450
  const [rawData, encoding] = simulatedAccountInfo.data;
10319
10451
  if (encoding !== "base64")
10320
10452
  throw new Error("Expected bs64 account data");
10321
10453
  const artificialAccountInfo = Object.assign(Object.assign({}, simulatedAccountInfo), { owner: new PublicKey(simulatedAccountInfo.owner), data: Buffer.from(rawData, "base64") });
10322
- newParsedMaybeSpl = artificialAccountInfo.owner.equals(TOKEN_PROGRAM_ID) ? parseSplAccount(artificialAccountInfo) : null;
10454
+ newParsedMaybeSpl = artificialAccountInfo.owner.equals(TOKEN_PROGRAM_ID)
10455
+ ? parseSplAccount(artificialAccountInfo)
10456
+ : null;
10323
10457
  }
10324
10458
  let splDiff = null;
10325
10459
  // old state is spl token account OR new state is spl token account
@@ -10350,11 +10484,11 @@ function simulateTransaction(connection, tx, simulateConfig) {
10350
10484
  });
10351
10485
  }
10352
10486
  /**
10353
- * Serializes transaction and returns serialized size
10354
- * @param transaction tx to get length
10355
- * @param feePayer optional fee payer
10356
- * @returns transaction size
10357
- */
10487
+ * Serializes transaction and returns serialized size
10488
+ * @param transaction tx to get length
10489
+ * @param feePayer optional fee payer
10490
+ * @returns transaction size
10491
+ */
10358
10492
  function getTransactionSize(tx) {
10359
10493
  try {
10360
10494
  if ("version" in tx) {