viem
Version:
157 lines • 6.66 kB
JavaScript
import { getAction } from '../../utils/getAction.js';
import { observe } from '../../utils/observe.js';
import { poll } from '../../utils/poll.js';
import { stringify } from '../../utils/stringify.js';
import { getBlock } from './getBlock.js';
/**
* Watches and returns information for incoming blocks.
*
* - Docs: https://viem.sh/docs/actions/public/watchBlocks
* - Examples: https://stackblitz.com/github/wevm/viem/tree/main/examples/blocks_watching-blocks
* - JSON-RPC Methods:
* - When `poll: true`, calls [`eth_getBlockByNumber`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getBlockByNumber) on a polling interval.
* - When `poll: false` & WebSocket Transport, uses a WebSocket subscription via [`eth_subscribe`](https://docs.alchemy.com/reference/eth-subscribe-polygon) and the `"newHeads"` event.
*
* @param client - Client to use
* @param parameters - {@link WatchBlocksParameters}
* @returns A function that can be invoked to stop watching for new block numbers. {@link WatchBlocksReturnType}
*
* @example
* import { createPublicClient, watchBlocks, http } from 'viem'
* import { mainnet } from 'viem/chains'
*
* const client = createPublicClient({
* chain: mainnet,
* transport: http(),
* })
* const unwatch = watchBlocks(client, {
* onBlock: (block) => console.log(block),
* })
*/
export function watchBlocks(client, { blockTag = 'latest', emitMissed = false, emitOnBegin = false, onBlock, onError, includeTransactions: includeTransactions_, poll: poll_, pollingInterval = client.pollingInterval, }) {
const enablePolling = (() => {
if (typeof poll_ !== 'undefined')
return poll_;
if (client.transport.type === 'webSocket')
return false;
if (client.transport.type === 'fallback' &&
client.transport.transports[0].config.type === 'webSocket')
return false;
return true;
})();
const includeTransactions = includeTransactions_ ?? false;
let prevBlock;
const pollBlocks = () => {
const observerId = stringify([
'watchBlocks',
client.uid,
blockTag,
emitMissed,
emitOnBegin,
includeTransactions,
pollingInterval,
]);
return observe(observerId, { onBlock, onError }, (emit) => poll(async () => {
try {
const block = await getAction(client, getBlock, 'getBlock')({
blockTag,
includeTransactions,
});
if (block.number && prevBlock?.number) {
// If the current block number is the same as the previous,
// we can skip.
if (block.number === prevBlock.number)
return;
// If we have missed out on some previous blocks, and the
// `emitMissed` flag is truthy, let's emit those blocks.
if (block.number - prevBlock.number > 1 && emitMissed) {
for (let i = prevBlock?.number + 1n; i < block.number; i++) {
const block = (await getAction(client, getBlock, 'getBlock')({
blockNumber: i,
includeTransactions,
}));
emit.onBlock(block, prevBlock);
prevBlock = block;
}
}
}
if (
// If no previous block exists, emit.
!prevBlock?.number ||
// If the block tag is "pending" with no block number, emit.
(blockTag === 'pending' && !block?.number) ||
// If the next block number is greater than the previous block number, emit.
// We don't want to emit blocks in the past.
(block.number && block.number > prevBlock.number)) {
emit.onBlock(block, prevBlock);
prevBlock = block;
}
}
catch (err) {
emit.onError?.(err);
}
}, {
emitOnBegin,
interval: pollingInterval,
}));
};
const subscribeBlocks = () => {
let active = true;
let emitFetched = true;
let unsubscribe = () => (active = false);
(async () => {
try {
if (emitOnBegin) {
getAction(client, getBlock, 'getBlock')({
blockTag,
includeTransactions,
}).then((block) => {
if (!active)
return;
if (!emitFetched)
return;
onBlock(block, undefined);
emitFetched = false;
});
}
const transport = (() => {
if (client.transport.type === 'fallback') {
const transport = client.transport.transports.find((transport) => transport.config.type === 'webSocket');
if (!transport)
return client.transport;
return transport.value;
}
return client.transport;
})();
const { unsubscribe: unsubscribe_ } = await transport.subscribe({
params: ['newHeads'],
async onData(data) {
if (!active)
return;
const block = (await getAction(client, getBlock, 'getBlock')({
blockNumber: data.blockNumber,
includeTransactions,
}).catch(() => { }));
if (!active)
return;
onBlock(block, prevBlock);
emitFetched = false;
prevBlock = block;
},
onError(error) {
onError?.(error);
},
});
unsubscribe = unsubscribe_;
if (!active)
unsubscribe();
}
catch (err) {
onError?.(err);
}
})();
return () => unsubscribe();
};
return enablePolling ? pollBlocks() : subscribeBlocks();
}
//# sourceMappingURL=watchBlocks.js.map