-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
102 lines (86 loc) · 3.28 KB
/
index.js
File metadata and controls
102 lines (86 loc) · 3.28 KB
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
const express = require('express');
const PQueue = require('p-queue');
const ip = require('ip');
const EVM = require('./src/EVM');
const Contract = require('./src/Contract');
const EnvironmentInfo = require('./src/utils/environmentInfo');
// Create an instance of our EVM
const evm = new EVM();
let contract = null;
const app = express();
// Create a priority queue with a concurrency limit
const apiQueue = new PQueue({ concurrency: 1 }); // Allows 1 concurrent requests
// Use express.json() for parsing JSON request bodies
app.use(express.json());
// Middleware to enqueue API requests
//app.use((req, res, next) => {
// // Priority could be based on some logic, here we use a default priority of 10
// const priority = parseInt(req.query.priority, 10) || 10;
//
// apiQueue.add(() => processApiCall(req.query.requestId || 'unknown'), { priority })
// .then(() => {
// res.json({ message: 'API call completed' });
// })
// .catch((err) => {
// console.error(err);
// res.status(500).json({ message: 'Error processing API call' });
// });
//});
// JSON-RPC handler
app.post('/', async (req, res) => {
const { jsonrpc, method, params, id } = req.body;
if (!jsonrpc || jsonrpc !== '2.0') {
return res.status(400).send({ error: 'Invalid JSON-RPC version' });
}
try {
if (method === 'eth_deploy') {
const { abi, bytecode } = params;
await deploy(abi, bytecode);
res.send({ jsonrpc: '2.0', result: 'Contract deployed successfully', id });
} else if (method === 'eth_invokeMethod') {
const { methodName, args } = params;
const result = await invokeMethod(methodName, args);
res.send({ jsonrpc: '2.0', result, id });
} else if (method === 'eth_getCallValue') {
const result = EnvironmentInfo.getCallValue();
res.send({ jsonrpc: '2.0', result, id });
} else if (method === 'eth_getAddress') {
const result = EnvironmentInfo.getAddress();
res.send({ jsonrpc: '2.0', result, id });
} else if (method === 'eth_getCaller') {
const result = EnvironmentInfo.getCaller();
res.send({ jsonrpc: '2.0', result, id });
} else if (method === 'eth_getBlockNumber') {
const result = EnvironmentInfo.getBlockNumber();
res.send({ jsonrpc: '2.0', result, id });
} else if (method === 'eth_getTimestamp') {
const result = EnvironmentInfo.getTimestamp();
res.send({ jsonrpc: '2.0', result, id });
} else {
res.status(400).send({ jsonrpc: '2.0', error: 'Method not found', id });
}
} catch (error) {
res.status(500).send({ jsonrpc: '2.0', error: error.message, id });
}
});
async function deploy(contractABI, contractBytecode) {
// Create a contract instance
contract = new Contract(contractABI, contractBytecode);
// Deploy the contract (simplified deployment)
console.log('Deploying contract...');
evm.loadCode(contractBytecode);
await evm.run();
}
async function invokeMethod(methodName, args=[]) {
// Call increment function
console.log('Calling increment...');
const methodCall = contract.encodeFunctionCall(methodName);
evm.loadCode(methodCall);
const result = await evm.run();
return result;
}
// Start server
const PORT = 8545;
app.listen(PORT,ip.address(), () => {
console.log(`EVM JSON-RPC server running on http://${ip.address()}:${PORT}`);
});