forked from txstreet/tmp-bulk-geth-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
130 lines (106 loc) · 3.26 KB
/
index.ts
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
import express, {Express, Request, Response} from "express";
const cors = require('cors');
const net = require('net');
const { Web3 } = require('web3');
const dotenv = require('dotenv');
const { IpcProvider } = require('web3-providers-ipc');
dotenv.config({});
const provider = () => {
const rpcUrl = process.env.RPC_URL; // Example: "http://eth-node:8545"
const ipcPath = process.env.IPC || '/mnt/geth/geth.ipc';
if (rpcUrl) {
console.log(`Connecting to RPC endpoint: ${rpcUrl}`);
return rpcUrl;
}
console.log(`Connecting to IPC: ${ipcPath}`);
return new IpcProvider(ipcPath, net);
}
const web3 = new Web3(provider());
const app: Express =
express().
use(express.json({ limit: '50mb' })).
use(cors());
// Enable BigInt JSON serialization.
//
// Avoids `TypeError: Do not know how to serialize a BigInt`
(BigInt.prototype as any).toJSON = function() {
return this.toString()
};
type NonceResult = {
account: string;
count: number;
};
app.post('/nonces', async (request: Request, response: Response) => {
const accounts = request.body.accounts;
const tasks: Promise<NonceResult>[] = [];
accounts.forEach((account: string) => {
tasks.push(
new Promise(async (resolve, reject) => {
try {
const count = await web3.eth.getTransactionCount(account);
resolve({ account, count });
} catch (error) {
console.error(
`Error while getting transaction count of ${account}: ${error}`
);
resolve({ account, count: 0 });
}
})
)
});
response.send(await Promise.all(tasks));
});
type ContractCodeResult = {
contract: string;
code: string;
};
app.post('/contract-codes', async (request: Request, response: Response) => {
const contracts = request.body.contracts;
const tasks: Promise<ContractCodeResult>[] = [];
contracts.forEach((contract: string) => {
tasks.push(
new Promise(async (resolve, reject) => {
try {
const code = await web3.eth.getCode(contract);
resolve({ contract, code });
} catch (error) {
console.error(
`Error while getting code of contract ${contract}: ${error}`
);
resolve({ contract, code: "0x" });
}
})
)
});
response.send(await Promise.all(tasks));
});
type TransactionReceiptResult = {
hash: string;
receipt: string | null;
};
app.post('/transaction-receipts', async (request: Request, response: Response) => {
const hashes = request.body.hashes;
const tasks: Promise<TransactionReceiptResult>[] = [];
hashes.forEach((hash: string) => {
tasks.push(
new Promise(async (resolve, reject) => {
try {
const receipt = await web3.eth.getTransactionReceipt(hash);
resolve({ hash, receipt });
} catch (error) {
console.error(
`Error while getting transaction receipt of ${hash}: ${error}`
);
resolve({ hash, receipt: null });
}
})
)
});
response.send(await Promise.all(tasks));
});
app.get('/ping', async (request: Request, response: Response) => {
response.send("Pong\n");
});
const port = process.env.PORT || 80;
app.listen(port);
console.log("listening on port " + port);