-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript-hdc10102LabQueryRpc.js
executable file
·107 lines (88 loc) · 3.07 KB
/
script-hdc10102LabQueryRpc.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
#!/usr/bin/env node
import dotenv from 'dotenv';
import { createLogger } from '../util/util.js';
const logger = await createLogger({
scriptId: 'hdc10102LabQueryRpc',
scriptCategory: 'task',
});
let client;
async function scriptHdc10102LabQueryRpc() {
logger.logStart('Welcome to the hdc10102LabQueryRpc task!');
// Read in environment variables from `.env` file in parent directory
dotenv.config({ path: '../.env' });
logger.log('Read .env file');
// Read in necessary configuration values from .env file
const rpcRelayRpcUrl = process.env.RPC_URL;
if (!rpcRelayRpcUrl) {
throw new Error(
'Must set RPC_URL environment variable',
);
}
const hashioRpcUrl = 'https://testnet.hashio.io/api';
const thirdPartyRpcUrl = 'https://docs-demo.hedera-testnet.quiknode.pro/';
// (1) via Hashio endpoints
await logger.logSection('Make a JSON-RPC request to Hedera Testnet via Hashio RPC endpoint');
// Construct JSON-RPC request body
const rpcReq1Body = {
method: 'eth_getBlockByNumber',
params: [
'latest',
false,
],
'id': 1,
'jsonrpc': '2.0',
};
// Send HTTP request to RPC endpoint
console.log('Sending RPC request to:', hashioRpcUrl);
const rpcResp1 = await fetch(
hashioRpcUrl,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(rpcReq1Body),
redirect: 'follow',
},
);
// Display HTTP response from RPC endpoint
const rpcResp1Body = await rpcResp1.json();
console.log(rpcResp1Body);
console.log('latest block number:', rpcResp1Body?.result?.number);
// (2) via JSON-RPC relay running on localhost
await logger.logSection('Make a JSON-RPC request to Hedera Testnet via RPC relay instance');
// Construct JSON-RPC request body
const rpcReq2Body = JSON.parse(JSON.stringify(rpcReq1Body));
// Send HTTP request to RPC endpoint
console.log('Sending RPC request to:', rpcRelayRpcUrl);
const rpcResp2 = await fetch(
rpcRelayRpcUrl,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(rpcReq2Body),
redirect: 'follow',
},
);
// Display HTTP response from RPC endpoint
const rpcResp2Body = await rpcResp2.json();
console.log(rpcResp2Body);
console.log('latest block number:', rpcResp2Body?.result?.number);
// (3) via 3rd party RPC endpoints
// (exercise left to student)
await logger.logSection('Make a JSON-RPC request to Hedera Testnet via 3rd party RPC endpoints');
// Construct JSON-RPC request body
// TODO re-use a request body previously constructed
// Send HTTP request to RPC endpoint
console.log('Sending RPC request to:', thirdPartyRpcUrl);
// TODO use fetch to send HTTP request to thirdPartyRpcUrl
// Display HTTP response from RPC endpoint
// TODO parse and display the response body, and the block number extracted from it
logger.logComplete('hdc10102LabQueryRpc task complete!');
}
scriptHdc10102LabQueryRpc().catch((ex) => {
client && client.close();
logger ? logger.logError(ex) : console.error(ex);
});