Skip to content

Commit

Permalink
getBlockNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Aug 17, 2024
1 parent e1b0ef6 commit dc841be
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export {
type GetBlockNumberParameters,
type GetBlockNumberReturnType,
getBlockNumber,
_getBlockNumber,
} from './public/getBlockNumber.js'
export {
type GetBlockTransactionCountErrorType,
Expand Down
8 changes: 8 additions & 0 deletions src/actions/public/getBlockNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Transport } from '../../clients/transports/createTransport.js'
import type { ErrorType } from '../../errors/utils.js'
import type { Chain } from '../../types/chain.js'
import type { RequestErrorType } from '../../utils/buildRequest.js'
import { getAction } from '../../utils/getAction.js'
import {
type GetCacheErrorType,
getCache,
Expand Down Expand Up @@ -52,6 +53,13 @@ export function getBlockNumberCache(id: string) {
* // 69420n
*/
export async function getBlockNumber<chain extends Chain | undefined>(
client: Client<Transport, chain>,
params: GetBlockNumberParameters = {},
): Promise<GetBlockNumberReturnType> {
return getAction(client, _getBlockNumber, 'getBlockNumber')(params)
}

export async function _getBlockNumber<chain extends Chain | undefined>(
client: Client<Transport, chain>,
{ cacheTime = client.cacheTime }: GetBlockNumberParameters = {},
): Promise<GetBlockNumberReturnType> {
Expand Down
2 changes: 1 addition & 1 deletion src/actions/public/getTransactionConfirmations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function getTransactionConfirmations<
{ hash, transactionReceipt }: GetTransactionConfirmationsParameters<chain>,
): Promise<GetTransactionConfirmationsReturnType> {
const [blockNumber, transaction] = await Promise.all([
getAction(client, getBlockNumber, 'getBlockNumber')({}),
getBlockNumber(client),
hash
? getAction(client, getTransaction, 'getTransaction')({ hash })
: undefined,
Expand Down
8 changes: 1 addition & 7 deletions src/actions/public/watchBlockNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import type { ErrorType } from '../../errors/utils.js'
import type { Chain } from '../../types/chain.js'
import type { HasTransportType } from '../../types/transport.js'
import { hexToBigInt } from '../../utils/encoding/fromHex.js'
import { getAction } from '../../utils/getAction.js'
import { observe } from '../../utils/observe.js'
import { type PollErrorType, poll } from '../../utils/poll.js'
import { stringify } from '../../utils/stringify.js'

import {
type GetBlockNumberReturnType,
getBlockNumber,
Expand Down Expand Up @@ -117,11 +115,7 @@ export function watchBlockNumber<
poll(
async () => {
try {
const blockNumber = await getAction(
client,
getBlockNumber,
'getBlockNumber',
)({ cacheTime: 0 })
const blockNumber = await getBlockNumber(client, { cacheTime: 0 })

if (prevBlockNumber) {
// If the current block number is the same as the previous,
Expand Down
6 changes: 1 addition & 5 deletions src/actions/public/watchContractEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,7 @@ export function watchContractEvent<
// The fall back exists because some RPC Providers do not support filters.

// Fetch the block number to use for `getLogs`.
const blockNumber = await getAction(
client,
getBlockNumber,
'getBlockNumber',
)({})
const blockNumber = await getBlockNumber(client)

// If the block number has changed, we will need to fetch the logs.
// If the block number doesn't exist, we are yet to reach the first poll interval,
Expand Down
6 changes: 1 addition & 5 deletions src/actions/public/watchEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,7 @@ export function watchEvent<
// The fall back exists because some RPC Providers do not support filters.

// Fetch the block number to use for `getLogs`.
const blockNumber = await getAction(
client,
getBlockNumber,
'getBlockNumber',
)({})
const blockNumber = await getBlockNumber(client)

// If the block number has changed, we will need to fetch the logs.
// If the block number doesn't exist, we are yet to reach the first poll interval,
Expand Down

0 comments on commit dc841be

Please sign in to comment.