Querying via JSON-RPC
For information about Polaris Ethereum's JSON-RPC specification please see the EVM JSON-RPC section.
Querying via JSON-RPC with ethers.js
Requirements
- Node.js (opens in a new tab) (v8.9.4 or higher)
- ethers.js (opens in a new tab)(v4.0.0 or higher)
Create a new project and install ethers.js
npm install ethers
To query the latest block number of an EVM chain over JSON-RPC using ethers.js
you can use the following code:
const provider = new ethers.providers.JsonRpcProvider("<JSON_RPC_ENDPOINT>");
const blockNumber = await provider.getBlockNumber();
blockNumber
will be a number
representing the latest block number.
Querying via JSON-RPC with geth
Requirements
- Golang (opens in a new tab) (v1.13 or higher)
- Geth (opens in a new tab) (v1.9.7 or higher)
Create a new project and install geth
go get -u github.com/ethereum/go-ethereum
To query the latest block number of an EVM chain over JSON-RPC using geth
you can use the following code:
package main
import (
"fmt"
"log"
"math/big"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
rpcEndpoint := "http://localhost:8545" // default local node RPC Endpoint
client, err := ethclient.Dial(rpcEndpoint)
if err != nil {
log.Fatal(err)
}
header, err := client.HeaderByNumber(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
blockNumber := header.Number.String()
fmt.Println(blockNumber) // 5671744
}
blockNumber
will be a string
representing the latest block number.