Skip to content

Commit

Permalink
test: add methods to support Hardhat forking with JSON-RPC mock (#52)
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Mastrangelo <luis@swirldslabs.com>
  • Loading branch information
acuarica authored Oct 3, 2024
1 parent eedd6b1 commit b0b35f3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
37 changes: 33 additions & 4 deletions scripts/json-rpc-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ const isHIP719Contract = address => Object
.includes(address.toLowerCase());

/**
* Function signature for `eth_*` method handlers.
* Function signature for `eth_*` and `net_*` method handlers.
* The `params` argument comes from the JSON-RPC request,
* so it must to be declared as `unknown[]`.
* Therefore, each method handler should validate each element of `params` they use.
*
* @typedef {(params: unknown[], reqId: string) => Promise<string>} EthHandler
* @typedef {(params: unknown[], reqId: string) => Promise<unknown>} EthHandler
*/

/**
Expand All @@ -83,6 +83,21 @@ const isHIP719Contract = address => Object
* Official Ethereum JSON-RPC spec can be found at https://ethereum.github.io/execution-apis/api-documentation/.
*/
const eth = {

/**
* https://docs.infura.io/api/networks/ethereum/json-rpc-methods/net_version
*
* @type {EthHandler}
*/
net_version: async () => parseInt('0x12b').toString(),

/**
* https://docs.infura.io/api/networks/ethereum/json-rpc-methods/eth_chainid
*
* @type {EthHandler}
*/
eth_chainId: async () => '0x12b',

/**
* https://docs.infura.io/api/networks/ethereum/json-rpc-methods/eth_blocknumber
*
Expand All @@ -98,11 +113,25 @@ const eth = {
eth_gasPrice: async () => '0x1802ba9f400',

/**
* https://docs.infura.io/api/networks/ethereum/json-rpc-methods/eth_chainid
* https://docs.infura.io/api/networks/ethereum/json-rpc-methods/eth_feehistory
*
* @type {EthHandler}
*/
eth_chainId: async () => '0x12b',
eth_feeHistory: async () => ({
"baseFeePerGas": [
"0x192cc091400",
"0x192cc091400"
],
"gasUsedRatio": [
0.5
],
"oldestBlock": "0x8e1376",
"reward": [
[
"0x0"
]
]
}),

/**
* https://docs.infura.io/api/networks/ethereum/json-rpc-methods/eth_getblockbynumber
Expand Down
25 changes: 25 additions & 0 deletions scripts/mock/eth_getBlockByNumber_0x8112e4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"timestamp": "0x66cf73c4",
"difficulty": "0x0",
"extraData": "0x",
"gasLimit": "0xe4e1c0",
"baseFeePerGas": "0x192cc091400",
"gasUsed": "0x0",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"miner": "0x0000000000000000000000000000000000000000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"nonce": "0x0000000000000000",
"receiptsRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"size": "0x98d",
"stateRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"totalDifficulty": "0x0",
"transactions": [],
"transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"uncles": [],
"withdrawals": [],
"withdrawalsRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"number": "0x8112e4",
"hash": "0x79ec2e97e363c3a78b3ef8e55bc7b0f0968fdf5d693da8180fcd1cb63bb5133e",
"parentHash": "0xfbefad89c5d301dea4fbef73d0df4a13b6fe42c9cefc73742ebaa954e17d5e47"
}

0 comments on commit b0b35f3

Please sign in to comment.