@debridge-finance/solana-utils 1.0.2 → 2.0.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
 
@@ -7552,7 +7552,12 @@ class Wallet {
7552
7552
  }
7553
7553
  signTransaction(tx, additionalSigners) {
7554
7554
  return __awaiter(this, void 0, void 0, function* () {
7555
- tx.sign(this.keypair, ...(additionalSigners || []));
7555
+ if (!isVersionedTx(tx)) {
7556
+ tx.sign(this.keypair, ...(additionalSigners || []));
7557
+ }
7558
+ else {
7559
+ tx.sign([this.keypair, ...(additionalSigners || [])]);
7560
+ }
7556
7561
  return Promise.resolve(tx);
7557
7562
  });
7558
7563
  }
@@ -7565,22 +7570,6 @@ class Wallet {
7565
7570
  return Promise.all(promises);
7566
7571
  });
7567
7572
  }
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
7573
  }
7585
7574
  function sendWithRetries(connection, serialized, lastValidBlockHeight) {
7586
7575
  return __awaiter(this, void 0, void 0, function* () {
@@ -7601,40 +7590,31 @@ function sendWithRetries(connection, serialized, lastValidBlockHeight) {
7601
7590
  return txId;
7602
7591
  });
7603
7592
  }
7604
- function sendAll(connection, wallet, transactions, signers, waitBetweenSend, skipPreflight = false, debug = false) {
7593
+ function txToV0(transaction, payer, blockhash) {
7594
+ if (isVersionedTx(transaction)) {
7595
+ return transaction;
7596
+ }
7597
+ else {
7598
+ const v0Message = MessageV0.compile({
7599
+ instructions: transaction.instructions,
7600
+ payerKey: transaction.feePayer || payer,
7601
+ recentBlockhash: transaction.recentBlockhash || blockhash,
7602
+ });
7603
+ return new VersionedTransaction(v0Message);
7604
+ }
7605
+ }
7606
+ function sendAll(connection, wallet, transactions, signers, waitBetweenSend, skipPreflight = false, debug = false, convertIntoTxV0 = true) {
7607
+ var _a;
7605
7608
  return __awaiter(this, void 0, void 0, function* () {
7609
+ if (convertIntoTxV0) {
7610
+ const fakeBlockHash = "0".repeat(64);
7611
+ transactions = transactions.map((tx) => txToV0(tx, wallet.publicKey, fakeBlockHash));
7612
+ }
7606
7613
  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
7614
  if (waitBetweenSend === undefined || waitBetweenSend === 0) {
7635
7615
  const { blockhash, lastValidBlockHeight } = yield connection.getLatestBlockhash({ commitment: "confirmed" });
7636
7616
  yield Promise.all(transactions.map((tx) => __awaiter(this, void 0, void 0, function* () {
7637
- var _a;
7617
+ var _b;
7638
7618
  let txV0;
7639
7619
  if (isVersionedTx(tx)) {
7640
7620
  tx.message.recentBlockhash = blockhash;
@@ -7649,12 +7629,12 @@ function sendAll(connection, wallet, transactions, signers, waitBetweenSend, ski
7649
7629
  if (!skipPreflight) {
7650
7630
  const simulationResult = yield connection.simulateTransaction(txV0, { sigVerify: false, replaceRecentBlockhash: true, commitment: "confirmed" });
7651
7631
  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")}`);
7632
+ 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
7633
  }
7654
7634
  }
7655
7635
  })));
7656
7636
  // sign after setting correct blockhashes
7657
- const signed = yield signAllTransactions(transactions, signers);
7637
+ const signed = yield wallet.signAllTransactions(transactions, signers);
7658
7638
  txIds = yield Promise.all(signed.map((signedTx) => {
7659
7639
  const serialized = buffer.Buffer.from(signedTx.serialize());
7660
7640
  if (debug)
@@ -7665,23 +7645,29 @@ function sendAll(connection, wallet, transactions, signers, waitBetweenSend, ski
7665
7645
  else {
7666
7646
  for (let i = 0; i < transactions.length; i++) {
7667
7647
  const tx = transactions[i];
7668
- const { blockhash } = yield connection.getLatestBlockhash();
7669
- let signed;
7648
+ const { blockhash, lastValidBlockHeight } = yield connection.getLatestBlockhash();
7649
+ let txV0;
7670
7650
  if (isVersionedTx(tx)) {
7671
7651
  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);
7652
+ txV0 = tx;
7675
7653
  }
7676
7654
  else {
7677
7655
  tx.recentBlockhash = blockhash;
7678
7656
  tx.feePayer = wallet.publicKey;
7679
- signed = yield wallet.signTransaction(tx, signers && signers.length > i ? signers[i] : undefined);
7657
+ const message = Message.from(tx.serializeMessage());
7658
+ txV0 = new VersionedTransaction(new MessageV0({ addressTableLookups: [], compiledInstructions: [], header: message.header, recentBlockhash: blockhash, staticAccountKeys: message.accountKeys }));
7659
+ }
7660
+ const signed = yield wallet.signTransaction(tx, signers && signers.length > i ? signers[i] : undefined);
7661
+ if (!skipPreflight) {
7662
+ const simulationResult = yield connection.simulateTransaction(txV0, { sigVerify: false, replaceRecentBlockhash: true, commitment: "confirmed" });
7663
+ if (simulationResult.value.err !== null) {
7664
+ 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")}`);
7665
+ }
7680
7666
  }
7681
7667
  const serialized = buffer.Buffer.from(signed.serialize());
7682
7668
  if (debug)
7683
7669
  console.debug(serialized.toString("base64"));
7684
- txIds.push(yield sendAndConfirmRawTransaction(connection, serialized, { maxRetries: 20, commitment: "confirmed", skipPreflight }));
7670
+ txIds.push(yield sendWithRetries(connection, serialized, lastValidBlockHeight));
7685
7671
  yield sleep(waitBetweenSend);
7686
7672
  }
7687
7673
  }