How to Use?

Use curl, Web3.js, ethers, NodeJS, Java, Go, Python to request the Ethereum network

curl

SolarPath supports HTTPs access to the Ethereum network, the following example uses curl to interact with Ethereum:

curl https://eth-mainnet.solarpath.io/v1/YOUR-API-KEY \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{"jsonrpc": "2.0", "id": 1, "method": "eth_blockNumber", "params": []}'

Web3.js

var Web3 = require('web3');
var url = 'https://eth-mainnet.solarpath.io/v1/YOUR-API-KEY';
var httpProvider = new Web3.providers.HttpProvider(url);
var web3 = new Web3(httpProvider);
web3.eth.getBlockNumber().then((result) => {
    console.log("Latest ETH block: " + result);
});

Ethers

var ethers = require('ethers');
var url = 'https://eth-mainnet.solarpath.io/v1/YOUR-API-KEY';
var etherProvider = new ethers.providers.JsonRpcProvider(url);
etherProvider.getBlockNumber().then((result) => {
    console.log("Latest ETH block: " + result);
});

Node JS

const https = require('https');
const myKey = 'YOUR-API-KEY';
const data = JSON.stringify({
    "jsonrpc":"2.0","method":"eth_blockNumber","params": [],"id":1
  });
  
const options = {
    host: 'eth-mainnet.solarpath.io',
    port: 443,
    path: '/v1/' + myKey,
    method: 'POST',
    headers: {'Content-Type': 'application/json'}
};

const req = https.request(options, res => {
    console.log(`status code: ${res.statusCode}`);

    res.on('data', d => {
        process.stdout.write(d);
    });
  });

  req.on('error', error => {
        console.error(error);
  });

  req.write(data);
  req.end();

Java

Import web3j dependencies, see: https://github.com/web3j/web3j

Web3j web3j = 
        Web3j.build(new HttpService("https://eth-mainnet.solarpath.io/v1/YOUR-API-KEY"));

EthBlock ethBlock = 
                web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false)
                .send();

System.out.println("Latest ETH block: " + ethBlock.getBlock().getNumber());

It is recommended to modify the instance object of Web3j with static final. When your service stops, call: web3j.shutdown();

Go

  1. Initialize a new go module: go mod init solarpath

  2. Install go-ethereum dependencies: go get github.com/ethereum/go-ethereum/rpc

package main

import (
    "fmt"
    "log"

    "github.com/ethereum/go-ethereum/rpc"
)

type Block struct {
    Number string
}

func main() {
    client, err := rpc.Dial("https://eth-mainnet.solarpath.io/v1/YOUR-API-KEY")
    if err != nil {
        log.Fatalf("Could not connect to SolarPath: %v", err)
    }

    var lastBlock Block
    err = client.Call(&lastBlock, "eth_getBlockByNumber", "latest", true)
    if err != nil {
        fmt.Println("Cannot get the ETH latest block:", err)
        return
    }

    fmt.Printf("Latest ETH block: %v\n", lastBlock.Number)
}

Python

from web3 import Web3, HTTPProvider
web3Provider = Web3(HTTPProvider('https://eth-mainnet.solarpath.io/v1/YOUR-API-KEY'))
print ("Latest ETH block ", web3Provider.eth.blockNumber)

Last updated