@debridge-finance/solana-utils 1.0.2 → 2.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.
@@ -1,4 +1,4 @@
1
- import { Transaction, Message, VersionedTransaction, MessageV0, sendAndConfirmRawTransaction, PublicKey } from '@solana/web3.js';
1
+ import { Transaction, Message, VersionedTransaction, MessageV0, PublicKey } 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
 
@@ -7501,6 +7501,20 @@ function __awaiter(thisArg, _arguments, P, generator) {
7501
7501
  });
7502
7502
  }
7503
7503
 
7504
+ function getAccountInfo(connection, account) {
7505
+ return __awaiter(this, void 0, void 0, function* () {
7506
+ const info = yield connection.getAccountInfo(account);
7507
+ if (info) {
7508
+ // alchemy bug fix
7509
+ if (info.lamports === 0 && info.rentEpoch === 0)
7510
+ return null;
7511
+ return info;
7512
+ }
7513
+ else {
7514
+ return null;
7515
+ }
7516
+ });
7517
+ }
7504
7518
  function hexToBuffer(data, align) {
7505
7519
  if (!data.startsWith("0x"))
7506
7520
  throw new Error("[hexToBuffer]: string must start with 0x");
@@ -7552,7 +7566,12 @@ class Wallet {
7552
7566
  }
7553
7567
  signTransaction(tx, additionalSigners) {
7554
7568
  return __awaiter(this, void 0, void 0, function* () {
7555
- tx.sign(this.keypair, ...(additionalSigners || []));
7569
+ if (!isVersionedTx(tx)) {
7570
+ tx.sign(this.keypair, ...(additionalSigners || []));
7571
+ }
7572
+ else {
7573
+ tx.sign([this.keypair, ...(additionalSigners || [])]);
7574
+ }
7556
7575
  return Promise.resolve(tx);
7557
7576
  });
7558
7577
  }
@@ -7565,30 +7584,13 @@ class Wallet {
7565
7584
  return Promise.all(promises);
7566
7585
  });
7567
7586
  }
7568
- signVersionedTransaction(tx, additionalSigners) {
7569
- return __awaiter(this, void 0, void 0, function* () {
7570
- tx.sign([this.keypair, ...(additionalSigners || [])]);
7571
- return Promise.resolve(tx);
7572
- });
7573
- }
7574
- ;
7575
- signAllVersionedTransactions(txs, additionalSigners) {
7576
- return __awaiter(this, void 0, void 0, function* () {
7577
- const promises = [];
7578
- for (const [id, tx] of txs.entries()) {
7579
- promises.push(this.signVersionedTransaction(tx, additionalSigners && additionalSigners.length > id ? additionalSigners[id] : []));
7580
- }
7581
- return Promise.all(promises);
7582
- });
7583
- }
7584
7587
  }
7585
- function sendWithRetries(connection, serialized, lastValidBlockHeight) {
7588
+ function sendWithRetries(connection, serialized, lastValidBlockHeight, timesToSend = 5) {
7586
7589
  return __awaiter(this, void 0, void 0, function* () {
7587
7590
  const txId = yield connection.sendRawTransaction(serialized, {
7588
7591
  skipPreflight: true,
7589
7592
  });
7590
7593
  let blockheight = yield connection.getBlockHeight();
7591
- const timesToSend = 5;
7592
7594
  let sent = 0;
7593
7595
  while (blockheight < lastValidBlockHeight && sent < timesToSend) {
7594
7596
  yield connection.sendRawTransaction(serialized, {
@@ -7601,40 +7603,31 @@ function sendWithRetries(connection, serialized, lastValidBlockHeight) {
7601
7603
  return txId;
7602
7604
  });
7603
7605
  }
7604
- function sendAll(connection, wallet, transactions, signers, waitBetweenSend, skipPreflight = false, debug = false) {
7606
+ function txToV0(transaction, payer, blockhash) {
7607
+ if (isVersionedTx(transaction)) {
7608
+ return transaction;
7609
+ }
7610
+ else {
7611
+ const v0Message = MessageV0.compile({
7612
+ instructions: transaction.instructions,
7613
+ payerKey: transaction.feePayer || payer,
7614
+ recentBlockhash: transaction.recentBlockhash || blockhash,
7615
+ });
7616
+ return new VersionedTransaction(v0Message);
7617
+ }
7618
+ }
7619
+ function sendAll(connection, wallet, transactions, signers, waitBetweenSend, skipPreflight = false, debug = false, convertIntoTxV0 = true, rpcCalls = 5) {
7620
+ var _a;
7605
7621
  return __awaiter(this, void 0, void 0, function* () {
7622
+ if (convertIntoTxV0) {
7623
+ const fakeBlockHash = "0".repeat(64);
7624
+ transactions = transactions.map((tx) => txToV0(tx, wallet.publicKey, fakeBlockHash));
7625
+ }
7606
7626
  let txIds = [];
7607
- const signAllTransactions = (txs, additionalSigners) => {
7608
- const result = [];
7609
- const txTypes = txs.map(isVersionedTx);
7610
- if (txTypes.every((isVersioned) => isVersioned === true)) {
7611
- // all txs are Versioned
7612
- if (wallet.signAllVersionedTransactions === undefined)
7613
- throw new Error("Trying to sign versioned transactions without implementation for sigining such transactions (signAllVersionedTransactions)");
7614
- return wallet.signAllVersionedTransactions(txs, additionalSigners);
7615
- }
7616
- else if (txTypes.every((isVersioned) => isVersioned === false)) {
7617
- // all txs are Legacy
7618
- return wallet.signAllTransactions(txs, additionalSigners);
7619
- }
7620
- // transactions are mixed
7621
- if (wallet.signVersionedTransaction === undefined)
7622
- throw new Error("Trying to sign versioned transaction without implementation for sigining such transaction (signVersionedTransaction)");
7623
- for (const [i, tx] of txs.entries()) {
7624
- const signers = additionalSigners && additionalSigners.length > i ? additionalSigners[i] : undefined;
7625
- if (isVersionedTx(tx)) {
7626
- result.push(wallet.signVersionedTransaction(tx, signers));
7627
- }
7628
- else {
7629
- result.push(wallet.signTransaction(tx, signers));
7630
- }
7631
- }
7632
- return Promise.all(result);
7633
- };
7634
7627
  if (waitBetweenSend === undefined || waitBetweenSend === 0) {
7635
7628
  const { blockhash, lastValidBlockHeight } = yield connection.getLatestBlockhash({ commitment: "confirmed" });
7636
7629
  yield Promise.all(transactions.map((tx) => __awaiter(this, void 0, void 0, function* () {
7637
- var _a;
7630
+ var _b;
7638
7631
  let txV0;
7639
7632
  if (isVersionedTx(tx)) {
7640
7633
  tx.message.recentBlockhash = blockhash;
@@ -7649,39 +7642,45 @@ function sendAll(connection, wallet, transactions, signers, waitBetweenSend, ski
7649
7642
  if (!skipPreflight) {
7650
7643
  const simulationResult = yield connection.simulateTransaction(txV0, { sigVerify: false, replaceRecentBlockhash: true, commitment: "confirmed" });
7651
7644
  if (simulationResult.value.err !== null) {
7652
- throw new Error(`Tx simulation Error! Transaction message dump: ${buffer.Buffer.from(tx.serialize()).toString("base64")}\n Error: ${JSON.stringify(simulationResult.value.err)}, logs:\n ${(_a = simulationResult.value.logs) === null || _a === void 0 ? void 0 : _a.join("\n")}`);
7645
+ throw new Error(`Tx simulation Error! Transaction message dump: ${buffer.Buffer.from(tx.serialize()).toString("base64")}\n Error: ${JSON.stringify(simulationResult.value.err)}, logs:\n ${(_b = simulationResult.value.logs) === null || _b === void 0 ? void 0 : _b.join("\n")}`);
7653
7646
  }
7654
7647
  }
7655
7648
  })));
7656
7649
  // sign after setting correct blockhashes
7657
- const signed = yield signAllTransactions(transactions, signers);
7650
+ const signed = yield wallet.signAllTransactions(transactions, signers);
7658
7651
  txIds = yield Promise.all(signed.map((signedTx) => {
7659
7652
  const serialized = buffer.Buffer.from(signedTx.serialize());
7660
7653
  if (debug)
7661
7654
  console.debug(serialized.toString("base64"));
7662
- return sendWithRetries(connection, serialized, lastValidBlockHeight);
7655
+ return sendWithRetries(connection, serialized, lastValidBlockHeight, rpcCalls);
7663
7656
  }));
7664
7657
  }
7665
7658
  else {
7666
7659
  for (let i = 0; i < transactions.length; i++) {
7667
7660
  const tx = transactions[i];
7668
- const { blockhash } = yield connection.getLatestBlockhash();
7669
- let signed;
7661
+ const { blockhash, lastValidBlockHeight } = yield connection.getLatestBlockhash();
7662
+ let txV0;
7670
7663
  if (isVersionedTx(tx)) {
7671
7664
  tx.message.recentBlockhash = blockhash;
7672
- if (wallet.signVersionedTransaction === undefined)
7673
- throw new Error("Trying to sign versioned transaction without implementation for sigining such transaction (signVersionedTransaction)");
7674
- signed = yield wallet.signVersionedTransaction(tx, signers && signers.length > i ? signers[i] : undefined);
7665
+ txV0 = tx;
7675
7666
  }
7676
7667
  else {
7677
7668
  tx.recentBlockhash = blockhash;
7678
7669
  tx.feePayer = wallet.publicKey;
7679
- signed = yield wallet.signTransaction(tx, signers && signers.length > i ? signers[i] : undefined);
7670
+ const message = Message.from(tx.serializeMessage());
7671
+ txV0 = new VersionedTransaction(new MessageV0({ addressTableLookups: [], compiledInstructions: [], header: message.header, recentBlockhash: blockhash, staticAccountKeys: message.accountKeys }));
7672
+ }
7673
+ const signed = yield wallet.signTransaction(tx, signers && signers.length > i ? signers[i] : undefined);
7674
+ if (!skipPreflight) {
7675
+ const simulationResult = yield connection.simulateTransaction(txV0, { sigVerify: false, replaceRecentBlockhash: true, commitment: "confirmed" });
7676
+ if (simulationResult.value.err !== null) {
7677
+ throw new Error(`Tx simulation Error! Transaction message dump: ${buffer.Buffer.from(tx.serialize()).toString("base64")}\n Error: ${JSON.stringify(simulationResult.value.err)}, logs:\n ${(_a = simulationResult.value.logs) === null || _a === void 0 ? void 0 : _a.join("\n")}`);
7678
+ }
7680
7679
  }
7681
7680
  const serialized = buffer.Buffer.from(signed.serialize());
7682
7681
  if (debug)
7683
7682
  console.debug(serialized.toString("base64"));
7684
- txIds.push(yield sendAndConfirmRawTransaction(connection, serialized, { maxRetries: 20, commitment: "confirmed", skipPreflight }));
7683
+ txIds.push(yield sendWithRetries(connection, serialized, lastValidBlockHeight, rpcCalls));
7685
7684
  yield sleep(waitBetweenSend);
7686
7685
  }
7687
7686
  }
@@ -7691,6 +7690,7 @@ function sendAll(connection, wallet, transactions, signers, waitBetweenSend, ski
7691
7690
 
7692
7691
  var helpers = /*#__PURE__*/Object.freeze({
7693
7692
  __proto__: null,
7693
+ getAccountInfo: getAccountInfo,
7694
7694
  hexToBuffer: hexToBuffer,
7695
7695
  bufferToHex: bufferToHex,
7696
7696
  sleep: sleep,