Skip to content

Instantly share code, notes, and snippets.

@bmeeder22
Last active Mar 25, 2022
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