Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import query, { RequestConfig, QueryParams } from './query'

function getBlockAndUncleRewardsbyBlockNo(blockno: number, requestConfig?: RequestConfig) {
return query<string>(
{
blockno,
module: 'block',
action: 'getblockreward',
},
requestConfig
)
}

function getEstimatedBlockCountdownTimebyBlockNo(blockno: number, requestConfig?: RequestConfig) {
return query<string>(
{
blockno,
module: 'block',
action: 'getblockcountdown',
},
requestConfig
)
}

function getBlockNumberByTimestamp(timestamp: number, closest: QueryParams['closest'], requestConfig?: RequestConfig) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closest: QueryParams['closest']

not sure about this, but didn't want to duplicate the declaration

return query<string>(
{
timestamp,
closest,
module: 'block',
action: 'getblocknobytime',
},
requestConfig
)
}


export default {
getBlockAndUncleRewardsbyBlockNo,
getEstimatedBlockCountdownTimebyBlockNo,
getBlockNumberByTimestamp
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 10 additions & 2 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ 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'

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
Expand All @@ -27,6 +29,8 @@ export type QueryParams = {
sort?: 'asc' | 'desc'
tag?: string
apiKey?: string
timestamp?: number
closest?: 'before' | 'after'
}

export type RequestConfig = {
Expand All @@ -53,6 +57,8 @@ async function query<T>(queryOptions: QueryParams, requestConfig?: RequestConfig
offset = 10000,
sort = 'asc',
apiKey = config.apiKey,
closest,
timestamp
} = queryOptions

const queryParams = pickBy(
Expand All @@ -68,6 +74,8 @@ async function query<T>(queryOptions: QueryParams, requestConfig?: RequestConfig
offset,
sort,
apiKey,
closest,
timestamp
},
identity
)
Expand Down