@debridge-finance/solana-utils 4.5.2 → 4.5.5

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
 
@@ -6943,16 +6943,42 @@ function hashExternalCallBytes(data) {
6943
6943
  }
6944
6944
  return hexToBuffer(solidityKeccak256(["bytes"], [data ? data : buffer.Buffer.from([])]), 32);
6945
6945
  }
6946
+ function isSendHashedFlagSet(packedFlags) {
6947
+ const SEND_HASHED_DATA_FLAG_BIT_NUMBER = BigInt(3);
6948
+ const result = (packedFlags >> SEND_HASHED_DATA_FLAG_BIT_NUMBER) & BigInt(1);
6949
+ return result === BigInt(1);
6950
+ }
6951
+ function getShortcutFromAutoParams(params) {
6952
+ var _a, _b, _c;
6953
+ const shortcut = (_a = params.autoParams) === null || _a === void 0 ? void 0 : _a.shortcut;
6954
+ if (shortcut) {
6955
+ return isBuffer(shortcut) ? shortcut : hexToBuffer(shortcut);
6956
+ }
6957
+ else {
6958
+ const flags = BigInt(new BN(((_b = params.autoParams) === null || _b === void 0 ? void 0 : _b.flags) || 0).toNumber());
6959
+ const data = (_c = params.autoParams) === null || _c === void 0 ? void 0 : _c.data;
6960
+ if (!data)
6961
+ return hashExternalCallBytes(data);
6962
+ if (isSendHashedFlagSet(flags)) {
6963
+ const result = isBuffer(data) ? data : hexToBuffer(data);
6964
+ if (result.length !== 32) {
6965
+ throw new Error(`Incorrect autoParams.data length for SEND_HASHED_DATA_FLAG`);
6966
+ }
6967
+ return result;
6968
+ }
6969
+ return hashExternalCallBytes(data);
6970
+ }
6971
+ }
6946
6972
  /**
6947
6973
  * Hashes submission for provided data
6948
6974
  * @param params data to hash
6949
6975
  * @returns submissionId from provided params
6950
6976
  */
6951
6977
  function hashSubmissionIdRaw(params) {
6952
- var _a, _b, _c, _d;
6978
+ var _a, _b;
6953
6979
  let packParams = [
6954
6980
  "uint256", // submission prefix
6955
- "uint256", // bridge_id
6981
+ "bytes32", // bridge_id
6956
6982
  "uint256", // source_chain_id
6957
6983
  "uint256", // target_chain_id
6958
6984
  "uint256", // amount
@@ -6964,7 +6990,7 @@ function hashSubmissionIdRaw(params) {
6964
6990
  : typeof params.receiver === "string"
6965
6991
  ? params.receiver.startsWith("0x")
6966
6992
  ? hexToBuffer(params.receiver)
6967
- : new PublicKey(params.receiver)
6993
+ : new PublicKey(params.receiver).toBuffer()
6968
6994
  : new PublicKey(params.receiver).toBuffer();
6969
6995
  let packData = [
6970
6996
  SUBMISSION_PREFIX,
@@ -6979,21 +7005,17 @@ function hashSubmissionIdRaw(params) {
6979
7005
  packParams = packParams.concat([
6980
7006
  "uint256", // execution fee
6981
7007
  "uint256", // flag
6982
- "uint256", // fallback hash
7008
+ "bytes32", // fallback hash
6983
7009
  "uint256", // ext call data hash
6984
7010
  "uint256", // native sender hash
6985
7011
  ]);
6986
- const shortcut = ((_a = params.autoParams) === null || _a === void 0 ? void 0 : _a.shortcut)
6987
- ? isBuffer(params.autoParams.shortcut)
6988
- ? params.autoParams.shortcut
6989
- : hexToBuffer(params.autoParams.shortcut)
6990
- : hashExternalCallBytes((_b = params === null || params === void 0 ? void 0 : params.autoParams) === null || _b === void 0 ? void 0 : _b.data);
7012
+ const shortcut = getShortcutFromAutoParams(params);
6991
7013
  const fallback = params.autoParams.fallbackAddress.startsWith("0x")
6992
7014
  ? hexToBuffer(params.autoParams.fallbackAddress)
6993
7015
  : new PublicKey(params.autoParams.fallbackAddress).toBuffer();
6994
7016
  packData = packData.concat([
6995
- new BN(((_c = params === null || params === void 0 ? void 0 : params.autoParams) === null || _c === void 0 ? void 0 : _c.executionFee) || 0).toArray("be", 32),
6996
- new BN(((_d = params.autoParams) === null || _d === void 0 ? void 0 : _d.flags) || 0).toArray("be", 32),
7017
+ new BN(((_a = params === null || params === void 0 ? void 0 : params.autoParams) === null || _a === void 0 ? void 0 : _a.executionFee) || 0).toArray("be", 32),
7018
+ new BN(((_b = params.autoParams) === null || _b === void 0 ? void 0 : _b.flags) || 0).toArray("be", 32),
6997
7019
  hashExternalCallBytes(fallback),
6998
7020
  shortcut,
6999
7021
  hashExternalCallBytes(params.autoParams.nativeSender),
@@ -10262,6 +10284,128 @@ function checkIfAssociatedWalletExists(connection, mintAccount, originalWallet)
10262
10284
  return accountData !== null;
10263
10285
  });
10264
10286
  }
10287
+ function getTransactionAccountsForSimulationDiff(connection, tx) {
10288
+ var _a, _b;
10289
+ return __awaiter(this, void 0, void 0, function* () {
10290
+ const result = new Set();
10291
+ if ("version" in tx) {
10292
+ const ALTs = tx.message.addressTableLookups.map((alt) => alt.accountKey);
10293
+ const fetchedALTs = yield connection.getMultipleAccountsInfo(ALTs);
10294
+ const parsedALTs = fetchedALTs
10295
+ .map((fetched, i) => fetched
10296
+ ? new AddressLookupTableAccount({
10297
+ state: AddressLookupTableAccount.deserialize(fetched.data),
10298
+ key: ALTs[i],
10299
+ })
10300
+ : null)
10301
+ .filter((x) => x != null);
10302
+ const accGetter = tx.message.getAccountKeys({
10303
+ addressLookupTableAccounts: parsedALTs,
10304
+ });
10305
+ for (const acc of [
10306
+ ...accGetter.staticAccountKeys,
10307
+ ...((_b = (_a = accGetter.accountKeysFromLookups) === null || _a === void 0 ? void 0 : _a.writable) !== null && _b !== void 0 ? _b : []),
10308
+ ]) {
10309
+ result.add(acc.toBase58());
10310
+ }
10311
+ }
10312
+ else {
10313
+ for (const ix of tx.instructions) {
10314
+ // we don't need read-only accounts and instruction.programId since they won't change
10315
+ ix.keys
10316
+ .filter((meta) => meta.isSigner || meta.isWritable)
10317
+ .map((meta) => result.add(meta.pubkey.toBase58()));
10318
+ }
10319
+ }
10320
+ return Array.from(result);
10321
+ });
10322
+ }
10323
+ function parseMultipleAccounts(accounts) {
10324
+ return accounts.map((account) => {
10325
+ var _a, _b;
10326
+ let parsedSplAccount;
10327
+ if (account) {
10328
+ const owner = new PublicKey(account.owner);
10329
+ if (owner.equals(TOKEN_PROGRAM_ID)) {
10330
+ if (account.data instanceof buffer.Buffer) {
10331
+ parsedSplAccount = parseSplAccount(account.data);
10332
+ }
10333
+ else if (account.data instanceof Array) {
10334
+ parsedSplAccount = parseSplAccount(buffer.Buffer.from(account.data[0], account.data[1]));
10335
+ }
10336
+ }
10337
+ }
10338
+ const parsedAccount = {
10339
+ balance: (_a = account === null || account === void 0 ? void 0 : account.lamports) !== null && _a !== void 0 ? _a : 0,
10340
+ };
10341
+ if (parsedSplAccount) {
10342
+ parsedAccount.spl = {
10343
+ mint: parsedSplAccount.mint,
10344
+ owner: parsedSplAccount.owner,
10345
+ balance: Number((_b = parsedSplAccount.amount) !== null && _b !== void 0 ? _b : 0),
10346
+ };
10347
+ }
10348
+ return parsedAccount;
10349
+ });
10350
+ }
10351
+ function getSplDiff(pre, post) {
10352
+ var _a, _b, _c, _d, _e, _f;
10353
+ const preBalance = (_a = pre === null || pre === void 0 ? void 0 : pre.balance) !== null && _a !== void 0 ? _a : 0;
10354
+ const postBalance = (_b = post === null || post === void 0 ? void 0 : post.balance) !== null && _b !== void 0 ? _b : 0;
10355
+ return {
10356
+ 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,
10357
+ 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,
10358
+ preBalance: (_e = pre === null || pre === void 0 ? void 0 : pre.balance) !== null && _e !== void 0 ? _e : 0,
10359
+ postBalance: (_f = post === null || post === void 0 ? void 0 : post.balance) !== null && _f !== void 0 ? _f : 0,
10360
+ diff: postBalance - preBalance,
10361
+ };
10362
+ }
10363
+ function getTransactionDiff(connection, tx, txAccounts) {
10364
+ return __awaiter(this, void 0, void 0, function* () {
10365
+ const accountsStrings = txAccounts
10366
+ ? txAccounts.map((acc) => acc.toBase58())
10367
+ : yield getTransactionAccountsForSimulationDiff(connection, tx);
10368
+ const accountsPubKeys = txAccounts !== null && txAccounts !== void 0 ? txAccounts : accountsStrings.map((key) => new PublicKey(key));
10369
+ let [accountsInfo, simulatedTx] = yield Promise.all([
10370
+ connection.getMultipleAccountsInfo(accountsPubKeys),
10371
+ connection.simulateTransaction(tx, {
10372
+ replaceRecentBlockhash: true,
10373
+ accounts: {
10374
+ encoding: "base64",
10375
+ addresses: accountsStrings,
10376
+ },
10377
+ }),
10378
+ ]);
10379
+ const simulatesInfo = simulatedTx.value.accounts;
10380
+ const preBalances = parseMultipleAccounts(accountsInfo);
10381
+ const postBalances = parseMultipleAccounts(simulatesInfo);
10382
+ const balancesDiff = accountsPubKeys.reduce((map, pubKey, i) => {
10383
+ var _a;
10384
+ if ((_a = accountsInfo[i]) === null || _a === void 0 ? void 0 : _a.executable) {
10385
+ return map;
10386
+ }
10387
+ const preBalance = preBalances[i];
10388
+ const postBalance = postBalances[i];
10389
+ const diff = {
10390
+ pubKey: pubKey,
10391
+ preBalance: preBalance.balance,
10392
+ postBalance: postBalance.balance,
10393
+ diff: postBalance.balance - preBalance.balance,
10394
+ };
10395
+ if (preBalance.spl || postBalance.spl) {
10396
+ const splDiff = getSplDiff(preBalance.spl, postBalance.spl);
10397
+ if (splDiff.diff) {
10398
+ diff.spl = splDiff;
10399
+ }
10400
+ }
10401
+ if (diff.diff || diff.spl) {
10402
+ map.set(pubKey.toString(), diff);
10403
+ }
10404
+ return map;
10405
+ }, new Map());
10406
+ return balancesDiff;
10407
+ });
10408
+ }
10265
10409
 
10266
10410
  var spl = /*#__PURE__*/Object.freeze({
10267
10411
  __proto__: null,
@@ -10278,7 +10422,11 @@ var spl = /*#__PURE__*/Object.freeze({
10278
10422
  getTokenAccountsWithBalance: getTokenAccountsWithBalance,
10279
10423
  buildReplenishWsolBalanceTransaction: buildReplenishWsolBalanceTransaction,
10280
10424
  updateWsolBalance: updateWsolBalance,
10281
- checkIfAssociatedWalletExists: checkIfAssociatedWalletExists
10425
+ checkIfAssociatedWalletExists: checkIfAssociatedWalletExists,
10426
+ getTransactionAccountsForSimulationDiff: getTransactionAccountsForSimulationDiff,
10427
+ parseMultipleAccounts: parseMultipleAccounts,
10428
+ getSplDiff: getSplDiff,
10429
+ getTransactionDiff: getTransactionDiff
10282
10430
  });
10283
10431
 
10284
10432
  const deBridge = "DEbrdGj3HsRsAzx6uH4MKyREKxVAfBydijLUF3ygsFfh";