Skip to content

Instantly share code, notes, and snippets.

@bmeeder22
Last active March 25, 2022 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmeeder22/2aab80b92f5f3e382ae6606ab891620f to your computer and use it in GitHub Desktop.
Save bmeeder22/2aab80b92f5f3e382ae6606ab891620f to your computer and use it in GitHub Desktop.
import {Connection, Transaction} from '@solana/web3.js';
export async function addBlockhash(
connection: Connection,
transaction: Transaction
): Promise<void> {
for (let i = 0; i < 5; i++) {
try {
transaction.recentBlockhash = await getBlockhash(connection);
break;
} catch (e) {
// retry;
}
}
if (!transaction.recentBlockhash)
throw new Error('Unable to get blockhash, please retry');
}
async function getBlockhash(connection: Connection): Promise<string> {
try {
const {blockhash} = await connection.getLatestBlockhash();
return blockhash;
} catch (e) {
const {blockhash} = await connection.getRecentBlockhash();
return blockhash;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment