@tevm/actions
Version:
A typesafe library for writing forge scripts in typescript
89 lines (87 loc) • 2.59 kB
JavaScript
import { bytesToHex, numberToHex } from '@tevm/utils'
/**
* Request handler for eth_getFilterChanges JSON-RPC requests.
* @param {import('@tevm/node').TevmNode} client
* @returns {import('./EthProcedure.js').EthGetFilterChangesJsonRpcProcedure}
*/
export const ethGetFilterChangesProcedure = (client) => {
return async (request) => {
const getFilterChangesRequest =
/** @type {import('./EthJsonRpcRequest.js').EthGetFilterChangesJsonRpcRequest}*/
(request)
const [id] = getFilterChangesRequest.params
const filter = client.getFilters().get(id)
if (!filter) {
return {
...(request.id ? { id: request.id } : {}),
method: request.method,
jsonrpc: request.jsonrpc,
error: {
code: -32601,
message: 'Method not implemented yet',
},
}
}
switch (filter.type) {
case 'Log': {
const { logs } = filter
/**
* @type {import('./EthJsonRpcResponse.js').EthGetFilterChangesJsonRpcResponse}
*/
const response = {
...(request.id ? { id: request.id } : {}),
method: request.method,
jsonrpc: request.jsonrpc,
result: logs.map((log) => ({
address: log.address,
topics: log.topics,
data: log.data,
blockNumber: numberToHex(log.blockNumber),
transactionHash: log.transactionHash,
transactionIndex: numberToHex(log.transactionIndex),
blockHash: log.blockHash,
logIndex: numberToHex(log.logIndex),
removed: log.removed,
})),
}
filter.logs = []
return response
}
case 'Block': {
const { blocks } = filter
/**
* @type {import('./EthJsonRpcResponse.js').EthGetFilterChangesJsonRpcResponse}
*/
const response = {
...(request.id ? { id: request.id } : {}),
// TODO fix this type
result: /** @type {any} */ (blocks.map((block) => numberToHex(block.header.number))),
method: request.method,
jsonrpc: request.jsonrpc,
}
filter.blocks = []
return response
}
case 'PendingTransaction': {
const { tx } = filter
/**
* @type {import('./EthJsonRpcResponse.js').EthGetFilterChangesJsonRpcResponse}
*/
const response = {
...(request.id ? { id: request.id } : {}),
// TODO fix this type
result: /** @type {any} */ (tx.map((tx) => bytesToHex(tx.hash()))),
method: request.method,
jsonrpc: request.jsonrpc,
}
filter.tx = []
return response
}
default: {
throw new Error(
'InternalError: Unknown filter type. This indicates a bug in tevm or potentially a typo in filter type if manually added',
)
}
}
}
}