Skip to content

Instantly share code, notes, and snippets.

@bmeeder22
Last active March 25, 2022 20:50
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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