Avalanche RPC - Hướng dẫn nhanh | ERPC
Hướng dẫn nhanh Avalanche RPC
Hướng dẫn này đưa bạn từ con số không đến response Avalanche 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 hoặc Ethereum? Bạn có thể dùng cùng một RPC key.
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/ava?api-key=<YOUR_API_KEY> --header 'Content-Type: application/json' --data '{
"jsonrpc":"2.0","id":1,
"method":"eth_blockNumber"
}'curl https://edge.erpc.global/ava?api-key=<YOUR_API_KEY> --header 'Content-Type: application/json' --data '{
"jsonrpc":"2.0","id":1,
"method":"eth_blockNumber"
}'Response trả về số block C-Chain 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. Kết quả là 0xa86a — 43114, chain ID mainnet của Avalanche C-Chain:bash
curl https://edge.erpc.global/ava?api-key=<YOUR_API_KEY> --header 'Content-Type: application/json' --data '{
"jsonrpc":"2.0","id":1,
"method":"eth_chainId"
}'curl https://edge.erpc.global/ava?api-key=<YOUR_API_KEY> --header 'Content-Type: application/json' --data '{
"jsonrpc":"2.0","id":1,
"method":"eth_chainId"
}'Cùng một endpoint này cũng phục vụ các API gốc của Avalanche. Upstream API được chọn theo đúng tên method, do đó các lệnh gọi X-Chain (
avm.*) và P-Chain (platform.*) không cần path hoặc host bổ sung:bash
curl https://edge.erpc.global/ava?api-key=<YOUR_API_KEY> --header 'Content-Type: application/json' --data '{
"jsonrpc":"2.0","id":1,
"method":"platform.getHeight",
"params":{}
}'curl https://edge.erpc.global/ava?api-key=<YOUR_API_KEY> --header 'Content-Type: application/json' --data '{
"jsonrpc":"2.0","id":1,
"method":"platform.getHeight",
"params":{}
}'3. Theo dõi qua WebSocket
Endpoint WebSocket chấp nhận các subscription
eth_subscribe chuẩn của C-Chain:bash
wscat -c "wss://edge.erpc.global/ava?api-key=<YOUR_API_KEY>"
# After connected, send:
{"jsonrpc":"2.0","id":1,"method":"eth_subscribe","params":["newHeads"]}wscat -c "wss://edge.erpc.global/ava?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 Avalanche C-Chain 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 cho các method EVM của C-Chain. 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/ava?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/ava?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 Avalanche RPC Endpoint
- Danh sách method đầy đủ với cấu trúc request và response: Tài liệu API Avalanche RPC
- Tài liệu Avalanche C-Chain chính thức: https://build.avax.network/docs/rpcs/c-chain






