This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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