Skip to content

Commit

Permalink
Update bulk send to support versioned txns
Browse files Browse the repository at this point in the history
  • Loading branch information
Perronef5 committed Oct 2, 2023
1 parent ed374bd commit 29959a9
Showing 1 changed file with 45 additions and 15 deletions.
60 changes: 45 additions & 15 deletions packages/spl-utils/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
TransactionInstruction,
TransactionResponse,
TransactionSignature,
VersionedTransaction,
VersionedTransactionResponse,
} from "@solana/web3.js";
import bs58 from "bs58";
Expand Down Expand Up @@ -270,7 +271,7 @@ export const awaitTransactionSignatureConfirmation = async (
slot: context.slot,
confirmations: 0,
};
done = true
done = true;
if (result.err) {
console.log("Rejected via websocket", result.err);
reject(status);
Expand All @@ -283,7 +284,7 @@ export const awaitTransactionSignatureConfirmation = async (
} catch (e) {
console.error("WS error in setup", txid, e);
} finally {
done = true
done = true;
clearTimeout(t);
}
while (!done && queryStatus) {
Expand Down Expand Up @@ -381,7 +382,7 @@ export async function sendAndConfirmWithRetry(
let done = false;
let slot = 0;
const txid = await connection.sendRawTransaction(txn, sendOptions);
console.log("txid", txid)
console.log("txid", txid);
const startTime = getUnixTime();
(async () => {
while (!done && getUnixTime() - startTime < timeout) {
Expand Down Expand Up @@ -457,7 +458,7 @@ async function withRetries<A>(
console.log(`Retrying ${i}...`, e);
}
}
throw new Error("Failed after retries")
throw new Error("Failed after retries");
}

type Status = {
Expand All @@ -468,7 +469,7 @@ type Status = {
const TX_BATCH_SIZE = 200;
export async function bulkSendTransactions(
provider: Provider,
txs: Transaction[],
txs: (Transaction | VersionedTransaction)[],
onProgress?: (status: Status) => void,
triesRemaining: number = 10 // Number of blockhashes to try resending txs with before giving up
): Promise<string[]> {
Expand All @@ -484,26 +485,53 @@ export async function bulkSendTransactions(
);
const blockhashedTxs = await Promise.all(
chunk.map(async (tx) => {
tx.recentBlockhash = recentBlockhash.blockhash;
return tx;
let isVersionedTransaction = false;
const legacyTxn = tx as Transaction;
const versionedTxn = tx as VersionedTransaction;
if (!legacyTxn?.partialSign) {
isVersionedTransaction = true;
}

if (!isVersionedTransaction) {
legacyTxn.recentBlockhash = recentBlockhash.blockhash;
return legacyTxn;
}

versionedTxn.message.recentBlockhash = recentBlockhash.blockhash;
return versionedTxn;
})
);
const signedTxs = await (provider as AnchorProvider).wallet.signAllTransactions(
blockhashedTxs
);
const signedTxs = await (
provider as AnchorProvider
).wallet.signAllTransactions(blockhashedTxs);

const txsWithSigs = signedTxs.map((tx, index) => {
let isVersionedTransaction = false;
const legacyTxn = tx as Transaction;
const versionedTxn = tx as VersionedTransaction;

if (!legacyTxn?.partialSign) {
isVersionedTransaction = true;
}

return {
transaction: chunk[index],
sig: bs58.encode(tx.signatures[0]!.signature!),
sig: bs58.encode(
!isVersionedTransaction
? legacyTxn.signatures[0]!.signature!
: versionedTxn.signatures[0]!
),
};
});
const confirmedTxs = await bulkSendRawTransactions(
provider.connection,
signedTxs.map((s) => s.serialize()),
signedTxs.map((s) => Buffer.from(s.serialize())),
({ totalProgress, ...rest }) =>
onProgress &&
onProgress({ ...rest, totalProgress: totalProgress + ret.length + thisRet.length}),
onProgress({
...rest,
totalProgress: totalProgress + ret.length + thisRet.length,
}),
recentBlockhash.lastValidBlockHeight,
// Hail mary, try with preflight enabled. Sometimes this causes
// errors that wouldn't otherwise happen
Expand All @@ -523,7 +551,9 @@ export async function bulkSendTransactions(
triesRemaining--;
if (triesRemaining <= 0) {
throw new Error(
`Failed to submit all txs after blockhashes expired, ${signedTxs.length - confirmedTxs.length} remain`
`Failed to submit all txs after blockhashes expired, ${
signedTxs.length - confirmedTxs.length
} remain`
);
}
}
Expand Down Expand Up @@ -601,7 +631,7 @@ export async function bulkSendRawTransactions(
}
ret.push(
...txids
.map((txid, idx) => statuses[idx] == null ? null : txid)
.map((txid, idx) => (statuses[idx] == null ? null : txid))
.filter(truthy)
);
chunk = chunk.filter((_, index) => statuses[index] === null);
Expand Down

0 comments on commit 29959a9

Please sign in to comment.