-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrcrpcethClient.ts.ts
More file actions
103 lines (84 loc) · 3.32 KB
/
Copy pathsrcrpcethClient.ts.ts
File metadata and controls
103 lines (84 loc) · 3.32 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
103
import axios, { AxiosInstance } from 'axios';
import { Block, Transaction, RpcRequest, RpcResponse } from '../types/blockchain.types';
export class EthClient {
private client: AxiosInstance;
private requestId: number = 1;
constructor(rpcUrl: string) {
this.client = axios.create({
baseURL: rpcUrl,
headers: {
'Content-Type': 'application/json',
},
timeout: 30000,
});
}
private async call<T>(method: string, params: any[] = []): Promise<T> {
const request: RpcRequest = {
jsonrpc: '2.0',
method: method,
params: params,
id: this.requestId++,
};
const response = await this.client.post<RpcResponse<T>>('', request);
if (response.data.error) {
throw new Error(`RPC Error: ${response.data.error.message} (code: ${response.data.error.code})`);
}
return response.data.result as T;
}
async getBlockNumber(): Promise<number> {
const result = await this.call<string>('eth_blockNumber');
return parseInt(result, 16);
}
async getBlockByNumber(blockNumber: number, includeTransactions: boolean = true): Promise<Block | null> {
const hexBlockNumber = '0x' + blockNumber.toString(16);
const result = await this.call<any>('eth_getBlockByNumber', [hexBlockNumber, includeTransactions]);
if (!result) return null;
return {
number: parseInt(result.number, 16),
hash: result.hash,
parentHash: result.parentHash,
timestamp: parseInt(result.timestamp, 16),
transactions: result.transactions,
gasUsed: result.gasUsed,
gasLimit: result.gasLimit,
miner: result.miner,
};
}
async getLatestBlock(includeTransactions: boolean = true): Promise<Block | null> {
const result = await this.call<any>('eth_getBlockByNumber', ['latest', includeTransactions]);
if (!result) return null;
return {
number: parseInt(result.number, 16),
hash: result.hash,
parentHash: result.parentHash,
timestamp: parseInt(result.timestamp, 16),
transactions: result.transactions,
gasUsed: result.gasUsed,
gasLimit: result.gasLimit,
miner: result.miner,
};
}
async getTransaction(txHash: string): Promise<Transaction | null> {
const result = await this.call<any>('eth_getTransactionByHash', [txHash]);
if (!result) return null;
return {
hash: result.hash,
from: result.from,
to: result.to,
value: result.value,
gasPrice: result.gasPrice,
gas: result.gas,
blockNumber: parseInt(result.blockNumber, 16),
};
}
async getBalance(address: string, block: string = 'latest'): Promise<string> {
const result = await this.call<string>('eth_getBalance', [address, block]);
return result;
}
async getGasPrice(): Promise<string> {
return await this.call<string>('eth_gasPrice');
}
async sendRawTransaction(signedTx: string): Promise<string> {
return await this.call<string>('eth_sendRawTransaction', [signedTx]);
}
}