关于blockhash
当我们要发送一个tx的时候
要给一个blockhash
这里会有一个歧义
有些人会说recentBlockhash
有些人会说latestBlockhash
其实这里的歧义是因为2个function产生的
一个是新的
一个是旧的
这个是新的
async getLatestBlockhash(
commitmentOrConfig?: Commitment | GetLatestBlockhashConfig,
): Promise<BlockhashWithExpiryBlockHeight> {
try {
const res = await this.getLatestBlockhashAndContext(commitmentOrConfig);
return res.value;
} catch (e) {
throw new Error('failed to get recent blockhash: ' + e);
}
}
然后
这个是旧的
async getRecentBlockhash(
commitment?: Commitment,
): Promise<{blockhash: Blockhash; feeCalculator: FeeCalculator}> {
try {
const res = await this.getRecentBlockhashAndContext(commitment);
return res.value;
} catch (e) {
throw new Error('failed to get recent blockhash: ' + e);
}
}
getRecentBlockhash会直接返回一个string
getLatestBlockhash会返回一个object
export type BlockhashWithExpiryBlockHeight = Readonly<{
blockhash: Blockhash;
lastValidBlockHeight: number;
}>;
所以这就是区别了
也就是产生歧义的原因了
注意
transaction.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;