From 72d7ad5c155737dbc8d802c7013c570560e0c99e Mon Sep 17 00:00:00 2001 From: Esen Dzhailobaev Date: Tue, 6 Jul 2021 16:35:02 +0600 Subject: [PATCH] feat(block): add block module --- src/block.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 3 ++- src/query.ts | 12 ++++++++++-- 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 src/block.ts diff --git a/src/block.ts b/src/block.ts new file mode 100644 index 0000000..f921eb2 --- /dev/null +++ b/src/block.ts @@ -0,0 +1,42 @@ +import query, { RequestConfig, QueryParams } from './query' + +function getBlockAndUncleRewardsbyBlockNo(blockno: number, requestConfig?: RequestConfig) { + return query( + { + blockno, + module: 'block', + action: 'getblockreward', + }, + requestConfig + ) +} + +function getEstimatedBlockCountdownTimebyBlockNo(blockno: number, requestConfig?: RequestConfig) { + return query( + { + blockno, + module: 'block', + action: 'getblockcountdown', + }, + requestConfig + ) +} + +function getBlockNumberByTimestamp(timestamp: number, closest: QueryParams['closest'], requestConfig?: RequestConfig) { + return query( + { + timestamp, + closest, + module: 'block', + action: 'getblocknobytime', + }, + requestConfig + ) +} + + +export default { + getBlockAndUncleRewardsbyBlockNo, + getEstimatedBlockCountdownTimebyBlockNo, + getBlockNumberByTimestamp +} diff --git a/src/index.ts b/src/index.ts index 53f1f01..95b70c6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,8 @@ import config from './config' import contract from './contract' import stats from './stats' import token from './token' +import block from './block' -export { account, contract, stats, token } +export { account, contract, stats, token, block } export default config diff --git a/src/query.ts b/src/query.ts index f991941..8390dce 100644 --- a/src/query.ts +++ b/src/query.ts @@ -5,7 +5,7 @@ import qs from 'querystring' import config from './config' -type Module = 'account' | 'contract' | 'stats' +type Module = 'account' | 'contract' | 'stats' | 'block' type AccountAction = 'balance' | 'balancemulti' | 'txlist' | 'txlistinternal' | 'tokentx' | 'getminedblocks' @@ -13,9 +13,11 @@ type ContractAction = 'getabi' | 'getsourcecode' type StatsAction = 'tokensupply' | 'tokenCsupply' | 'tokenbalance' | 'tokenbalancehistory' | 'bnbsupply' | 'validators' +type BlockAction = 'getblockreward' | 'getblockcountdown' | 'getblocknobytime' + export type QueryParams = { module: Module - action: AccountAction | ContractAction | StatsAction + action: AccountAction | ContractAction | StatsAction | BlockAction address?: string contractAddress?: string txhash?: string @@ -27,6 +29,8 @@ export type QueryParams = { sort?: 'asc' | 'desc' tag?: string apiKey?: string + timestamp?: number + closest?: 'before' | 'after' } export type RequestConfig = { @@ -53,6 +57,8 @@ async function query(queryOptions: QueryParams, requestConfig?: RequestConfig offset = 10000, sort = 'asc', apiKey = config.apiKey, + closest, + timestamp } = queryOptions const queryParams = pickBy( @@ -68,6 +74,8 @@ async function query(queryOptions: QueryParams, requestConfig?: RequestConfig offset, sort, apiKey, + closest, + timestamp }, identity )