feat: add getBlockTime method

This commit is contained in:
Justin Starry
2020-05-19 11:27:36 +08:00
committed by Michael Vines
parent e10a5c90ab
commit 4b613a4574
4 changed files with 50 additions and 0 deletions

View File

@ -356,6 +356,16 @@ const GetEpochScheduleRpcResult = struct({
*/
const GetBalanceAndContextRpcResult = jsonRpcResultAndContext('number?');
/**
* Expected JSON RPC response for the "getBlockTime" message
*/
const GetBlockTimeRpcResult = struct({
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: struct.union(['null', 'number']),
});
/**
* Expected JSON RPC response for the "getVersion" message
*/
@ -942,6 +952,21 @@ export class Connection {
});
}
/**
* Fetch the estimated production time of a block
*/
async getBlockTime(slot: number): Promise<number | null> {
const unsafeRes = await this._rpcRequest('getBlockTime', [slot]);
const res = GetBlockTimeRpcResult(unsafeRes);
if (res.error) {
throw new Error(
'failed to get block time for slot ' + slot + ': ' + res.error.message,
);
}
assert(typeof res.result !== 'undefined');
return res.result;
}
/**
* Fetch all the account info for the specified public key, return with context
*/