Ethereum RPC - Hướng dẫn nhanh | ERPC
Hướng dẫn nhanh Ethereum RPC
Hướng dẫn này đưa bạn từ con số không đến response Ethereum JSON-RPC đầu tiên với ERPC.
1. Lấy API Key của bạn
Bạn có thể lấy và kiểm tra API key trên ERPC Web Dashboard: ERPC Web Dashboard
Đã dùng ERPC cho Solana? Bạn có thể dùng cùng một RPC key như với Solana.
2. Gửi Request đầu tiên của bạn
Gửi một request JSON-RPC 2.0 dưới dạng HTTP POST, với API key của bạn trong tham số
api-key:bash
curl https://edge.erpc.global/eth?api-key=<YOUR_API_KEY> --header 'Content-Type: application/json' --data '{
"jsonrpc":"2.0","id":1,
"method":"eth_blockNumber"
}'curl https://edge.erpc.global/eth?api-key=<YOUR_API_KEY> --header 'Content-Type: application/json' --data '{
"jsonrpc":"2.0","id":1,
"method":"eth_blockNumber"
}'Response trả về số block mới nhất ở dạng thập lục phân:
json
{ "jsonrpc": "2.0", "id": 1, "result": "0x..." }{ "jsonrpc": "2.0", "id": 1, "result": "0x..." }Bạn có thể kiểm tra endpoint đang phục vụ chain nào bằng
eth_chainId:bash
curl https://edge.erpc.global/eth?api-key=<YOUR_API_KEY> --header 'Content-Type: application/json' --data '{
"jsonrpc":"2.0","id":1,
"method":"eth_chainId"
}'curl https://edge.erpc.global/eth?api-key=<YOUR_API_KEY> --header 'Content-Type: application/json' --data '{
"jsonrpc":"2.0","id":1,
"method":"eth_chainId"
}'3. Theo dõi qua WebSocket
Endpoint WebSocket chấp nhận các subscription
eth_subscribe chuẩn:bash
wscat -c "wss://edge.erpc.global/eth?api-key=<YOUR_API_KEY>"
# After connected, send:
{"jsonrpc":"2.0","id":1,"method":"eth_subscribe","params":["newHeads"]}wscat -c "wss://edge.erpc.global/eth?api-key=<YOUR_API_KEY>"
# After connected, send:
{"jsonrpc":"2.0","id":1,"method":"eth_subscribe","params":["newHeads"]}Để biết các loại subscription khả dụng, hãy tham khảo tài liệu Ethereum JSON-RPC chính thức được liên kết bên dưới.
4. Batch Request
Bạn có thể gửi tối đa 256 lệnh gọi JSON-RPC trong một HTTP request. Mỗi lệnh gọi trong một batch được tính phí như một lệnh gọi riêng.
bash
curl https://edge.erpc.global/eth?api-key=<YOUR_API_KEY> --header 'Content-Type: application/json' --data '[
{"jsonrpc":"2.0","id":1,"method":"eth_blockNumber"},
{"jsonrpc":"2.0","id":2,"method":"eth_gasPrice"}
]'curl https://edge.erpc.global/eth?api-key=<YOUR_API_KEY> --header 'Content-Type: application/json' --data '[
{"jsonrpc":"2.0","id":1,"method":"eth_blockNumber"},
{"jsonrpc":"2.0","id":2,"method":"eth_gasPrice"}
]'Bước tiếp theo
- Endpoint, đo lường mức sử dụng, và mã trạng thái: Tài liệu Ethereum RPC Endpoint
- Danh sách method đầy đủ với cấu trúc request và response: Tài liệu API Ethereum RPC
- Tài liệu Ethereum JSON-RPC chính thức: https://ethereum.org/en/developers/docs/apis/json-rpc/






