-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-rpc-mock.js
executable file
·324 lines (291 loc) · 11.3 KB
/
json-rpc-mock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env node --watch
/*-
* Hedera Hardhat Forking Plugin
*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// When in `watch` mode,
// changes in the watched files cause the Node.js process to restart.
// See https://nodejs.org/docs/v20.17.0/api/cli.html#--watch for more information.
const { strict: assert } = require('assert');
const http = require('http');
const { parentPort } = require('worker_threads');
const {
HTSAddress,
getHIP719Code,
getHtsCode,
getHtsStorageAt,
} = require('@hashgraph/system-contracts-forking');
const { ZERO_HEX_32_BYTE } = require('../../src/utils');
const { tokens } = require('../data');
/** @import { IMirrorNodeClient } from '@hashgraph/system-contracts-forking' */
/** ANSI colors functions to avoid any external dependency. */
const c = {
dim: (/**@type{unknown}*/ text) => `\x1b[2m${text}\x1b[0m`,
green: (/**@type{unknown}*/ text) => `\x1b[32m${text}\x1b[0m`,
yellow: (/**@type{unknown}*/ text) => `\x1b[33m${text}\x1b[0m`,
blue: (/**@type{unknown}*/ text) => `\x1b[34m${text}\x1b[0m`,
magenta: (/**@type{unknown}*/ text) => `\x1b[35m${text}\x1b[0m`,
cyan: (/**@type{unknown}*/ text) => `\x1b[36m${text}\x1b[0m`,
};
/**
* Determines whether `address` should be treated as a HIP-719 token proxy contract.
*
* This is done by checking whether `address` is one of the Token addresses configured in the mock data.
* The mock data can be found under `test/data/`.
*
* @param {string} address
* @returns {boolean}
*/
const isHIP719Contract = address =>
Object.values(tokens)
.map(({ address }) => address.toLowerCase())
.includes(address.toLowerCase());
/**
* Loads and returns the module indicated by `path` if exists.
* The `path` is relative to `test/data/`.
* In case the module does not exist, it returns `defaultValue`.
*
* @template T
* @param {string} path of the module relative to `test/data/`.
* @param {T} defaultValue The value to return when `path`.
* @returns {T} The loaded module indicated by `path` if exists. Otherwise `defaultValue`.
*/
const requireOrDefault = (path, defaultValue) => {
try {
return require(`../data/${path}`);
} catch (err) {
assert(err instanceof Error);
// https://nodejs.org/api/errors.html#module_not_found
if (/**@type {{code?: string}}*/ (err).code === 'MODULE_NOT_FOUND') return defaultValue;
throw err;
}
};
/**
* Written like an object (instead of `class`) to avoid type-checking errors due to
* - https://github.com/microsoft/TypeScript/issues/49905
* - https://github.com/microsoft/TypeScript/issues/58542
* @type {IMirrorNodeClient & {requests: string[], append(method: string, ...args: unknown[]): void}}
*/
const mirrorNodeClient = {
requests: [],
append(method, ...args) {
this.requests.push(`${method}:${args.join(',')}`);
},
async getTokenById(tokenId) {
this.append('getTokenById', tokenId);
if (tokens[tokenId] === undefined) return null;
return require(`../data/${tokens[tokenId].symbol}/getToken.json`);
},
async getNftByTokenIdAndSerial(tokenId, serialId) {
this.append('getNonFungibleToken', tokenId, serialId);
if (tokens[tokenId] === undefined) return null;
return require(`../data/${tokens[tokenId].symbol}/getNonFungibleToken_${serialId}.json`);
},
async getAccount(idOrAliasOrEvmAddress) {
assert(!idOrAliasOrEvmAddress.startsWith('0x'));
this.append('getAccount', idOrAliasOrEvmAddress);
if (!idOrAliasOrEvmAddress.startsWith('0.0.')) {
idOrAliasOrEvmAddress = `0x${idOrAliasOrEvmAddress}`;
}
return requireOrDefault(`getAccount_${idOrAliasOrEvmAddress.toLowerCase()}.json`, null);
},
async getBalanceOfToken(tokenId, accountId, blockNumber) {
this.append('getBalanceOfToken', tokenId, accountId, blockNumber);
const noBalance = { balances: [] };
if (tokens[tokenId] === undefined) return noBalance;
return requireOrDefault(
`${tokens[tokenId].symbol}/getBalanceOfToken_${accountId}.json`,
noBalance
);
},
async getAllowanceForToken(accountId, tokenId, spenderId) {
this.append('getAllowanceForToken', accountId, tokenId, spenderId);
const noAllowance = { allowances: [] };
if (tokens[tokenId] === undefined) return noAllowance;
return requireOrDefault(
`${tokens[tokenId].symbol}/getAllowanceForToken_${accountId}_${spenderId}.json`,
noAllowance
);
},
async getAllowanceForNFT(accountId, tokenId, operatorId) {
this.append('getAllowanceForNFT', accountId, tokenId, operatorId);
const noAllowance = { allowances: [] };
if (tokens[tokenId] === undefined) return noAllowance;
return requireOrDefault(
`${tokens[tokenId].symbol}/getAllowanceForToken_${accountId}_${operatorId}.json`,
noAllowance
);
},
async getTokenRelationship(idOrAliasOrEvmAddress, tokenId) {
this.append('getTokenRelationship', idOrAliasOrEvmAddress, tokenId);
const noTokens = { tokens: [] };
if (tokens[tokenId] === undefined) return noTokens;
return requireOrDefault(
`${tokens[tokenId].symbol}/getTokenRelationship_${idOrAliasOrEvmAddress}.json`,
noTokens
);
},
};
/**
* Function signature for `eth_*` and `net_*` method handlers.
* Also includes any additional JSON-RPC method used for testing under the `test_*` namespace.
*
* 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[]) => Promise<unknown>} EthHandler
*/
/**
* Mock values taken from `testnet`.
*
* https://docs.infura.io/api/networks/ethereum/json-rpc-methods
* 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
* @type {EthHandler}
*/
eth_blockNumber: async () => '0x811364',
/**
* https://docs.infura.io/api/networks/ethereum/json-rpc-methods/eth_gasprice
* @type {EthHandler}
*/
eth_gasPrice: async () => '0x1802ba9f400',
/**
* https://docs.infura.io/api/networks/ethereum/json-rpc-methods/eth_feehistory
* @type {EthHandler}
*/
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
* @type {EthHandler}
*/
eth_getBlockByNumber: async ([blockNumber, _transactionDetails]) =>
require(`./eth_getBlockByNumber_${blockNumber}.json`),
/**
* https://docs.infura.io/api/networks/ethereum/json-rpc-methods/eth_gettransactioncount
* @type {EthHandler}
*/
eth_getTransactionCount: async ([_address, _blockNumber]) => '0x0',
/**
* https://docs.infura.io/api/networks/ethereum/json-rpc-methods/eth_getcode
* @type {EthHandler}
*/
eth_getCode: async ([address, _blockNumber]) =>
address === HTSAddress
? getHtsCode()
: typeof address === 'string' && isHIP719Contract(address)
? getHIP719Code(address)
: '0x',
/**
* https://docs.infura.io/api/networks/ethereum/json-rpc-methods/eth_getbalance
* @type {EthHandler}
*/
eth_getBalance: async ([_address, _blockNumber]) => '0x0',
/**
* https://docs.infura.io/api/networks/ethereum/json-rpc-methods/eth_getstorageat
* @type {EthHandler}
*/
eth_getStorageAt: async ([address, slot, blockNumber]) => {
assert(typeof address === 'string');
assert(typeof slot === 'string');
const value = await getHtsStorageAt(address, slot, Number(blockNumber), mirrorNodeClient);
return value ?? ZERO_HEX_32_BYTE;
},
/** @type {EthHandler} */
test_getMirrorNodeRequests: async () => {
const requests = mirrorNodeClient.requests;
mirrorNodeClient.requests = [];
return requests;
},
};
console.log(
c.cyan('[INFO]'),
'Tokens mock configuration loaded from',
c.yellow('./test/data/'),
tokens
);
const port = process.env['PORT'] ?? 7546;
console.info(
c.yellow('[HINT]'),
`Remember to disable Foundry's storage cache using the \`${c.yellow('--no-storage-caching')}\` flag`
);
console.info(
c.yellow('[HINT]'),
c.dim('>'),
`forge test --fork-url http://localhost:${port} --no-storage-caching`
);
console.info(
c.yellow('[HINT]'),
c.dim('>'),
'https://book.getfoundry.sh/reference/forge/forge-test#description'
);
const server = http.createServer(function (req, res) {
const truncate = (/**@type{string}*/ str, max = 92) =>
str.length > max
? c.green(str.slice(0, 92)) + c.yellow(`[${str.length - max} more bytes..]`)
: c.green(str);
assert(req.url === '/', 'Only root / url is supported');
assert(req.method === 'POST', 'Only POST allowed');
// https://nodejs.org/en/learn/modules/anatomy-of-an-http-transaction
/** @type {Uint8Array[]} */
const chunks = [];
req.on('data', chunk => {
chunks.push(chunk);
}).on('end', async () => {
const body = Buffer.concat(chunks).toString();
console.info(c.cyan('[INFO]'), body);
const { jsonrpc, id, method, params } = JSON.parse(body);
assert(jsonrpc === '2.0', 'Only JSON-RPC 2.0');
const handler = eth[/**@type{keyof typeof eth}*/ (method)];
assert(handler !== undefined, `Method not supported: ${method}`);
const result = await handler(params);
const response = JSON.stringify({ jsonrpc, id, result });
console.log(c.cyan('[INFO]'), truncate(JSON.stringify(result)));
res.setHeader('Content-Type', 'application/json');
res.writeHead(200);
res.end(response);
});
});
server.listen(port, () => {
const address = server.address();
assert(address !== null);
assert(typeof address === 'object');
parentPort?.postMessage({ listening: true, ...address });
console.info(
c.cyan('[INFO]'),
'\u{1F680}',
c.magenta('JSON-RPC Mock Server'),
`running on http://localhost:${c.yellow(address.port)}`
);
});