# Tools

# Interaction tools

# Geth

Interact with your Ethereum node using Geth.

  1. Install Geth.

  2. Use geth attach command with the node's endpoint.

RPC:

geth attach https://USERNAME:PASSWORD@RPC_ENDPOINT

WSS:

geth attach wss://USERNAME:PASSWORD@WSS_ENDPOINT

where

  • USERNAME — your Ethereum node access username.
  • PASSWORD — your Ethereum node access password.
  • RPC_ENDPOINT — your Ethereum node RPC endpoint.
  • WSS_ENDPOINT — your Ethereum node WSS endpoint.

See View node access and credentials.

RPC example:

geth attach https://user-name:pass-word-pass-word-pass-word@nd-123-456-789.p2pify.com

WSS example:

geth attach wss://user-name:pass-word-pass-word-pass-word@ws-nd-123-456-789.p2pify.com
  1. Invoke any methods from Web3 JavaScript API.

Example below demonstrates how to get the balance of an address in wei value and convert it to ether value:

> web3.fromWei(web3.eth.getBalance("0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae"))
642538.078574759898951277

# GraphQL

You can use GraphQL on dedicated nodes on the Growth, Business, and Enterprise subscription plans.

# UI

You can query data using the graphical interface.

  1. On Chainstack, navigate to your dedicated Ethereum node. See View node access and credentials.
  2. Hover over GraphQL IDE URL and click Open.
  3. In the graphical interface that opens, run a GraphQL query.

Example to get the latest block number:

{ block { number } }

# Node.js

You can build a web app to query data using Node.js and axios:

const axios = require('axios');
const main = async () => {
  try {
    const result = await axios.post(
      'USERNAME:PASSWORD@RPC_ENDPOINT',
      {
        query: `
          QUERY
        `
      }
    );
    console.log(result.data);
  } catch(error) {
    console.error(error);
  }
}
main();

where

  • USERNAME — your Ethereum node access username.
  • PASSWORD — your Ethereum node access password.
  • GRAPHQL_ENDPOINT — the GraphQL endpoint of your dedicated Ethereum node.
  • QUERY — your GraphQL query.

See View node access and credentials.

Example to get the latest block number:

const axios = require('axios');
const main = async () => {
  try {
    const result = await axios.post(
      'https://user-name:pass-word-pass-word-pass-word@nd-123-456-789.p2pify.com/graphql',
      {
        query: `
          { block { number } }
        `
      }
    );
    console.log(result.data);
  } catch(error) {
    console.error(error);
  }
}
main();

# MetaMask

You can set your MetaMask to interact through your Ethereum nodes deployed with Chainstack.

  1. Open your MetaMask and click the network selector.

  2. In the network selector, click Custom RPC.

  3. In the New RPC URL field, enter the URL in the following format:

    https://USERNAME:PASSWORD@RPC_ENDPOINT

    where

    • USERNAME — your Ethereum node access username.
    • PASSWORD — your Ethereum node access password.
    • RPC_ENDPOINT — your Ethereum node RPC endpoint.
  4. In the Chain ID field, enter the ID of the network:

    • Mainnet: 1
    • Ropsten: 3
    • Rinkeby: 4
  5. Click Save.

See also View node access and credentials.

# Development tools

# Truffle

Configure Truffle Suite to deploy contracts to your Ethereum nodes.

  1. Install Truffle Suite, HD Wallet-enabled Web3 provider, and create a project.

  2. Create a new environment in truffle-config.js, add your mnemonic phrase generated by a wallet and the node RPC endpoint:

const HDWalletProvider = require("@truffle/hdwallet-provider");
const mnemonic = 'pattern enroll upgrade ...';
...
module.exports = {
 networks: {
    chainstack: {
        provider: () => new HDWalletProvider(mnemonic, "https://user-name:pass-word-pass-word-pass-word@nd-123-456-789.p2pify.com"),
        network_id: "*"
    },
   }
  }
};

# Hardhat

Configure Hardhat to deploy contracts and interact through your Ethereum nodes.

  1. Install Hardhat and create a project.

  2. Create a new environment in hardhat.config.js:

require("@nomiclabs/hardhat-waffle");
...
module.exports = {
  solidity: "0.7.3",
  networks: {
    chainstack: {
        url: "https://USERNAME:PASSWORD@RPC_ENDPOINT",
        accounts: ["PRIVATE_KEY"]
    },
   }
};

where

  • USERNAME — your Ethereum node access username.
  • PASSWORD — your Ethereum node access password.
  • RPC_ENDPOINT — your Ethereum node RPC endpoint.
  • PRIVATE_KEY — the private key of the account that you use to deploy the contract.
  1. Run npx hardhat run scripts/deploy.js --network chainstack and Hardhat will deploy using Chainstack.

See also Forking Ethereum mainnet with Hardhat.

# Embark

Configure Embark to deploy contracts to your Ethereum nodes.

  1. Install Embark and create a project.

  2. Create a new environment in config/contracts.js, add your mnemonic phrase generated by a wallet and the node RPC endpoint:

chainstack: {
  deployment:{
    accounts: [
      {
        mnemonic: 'pattern enroll upgrade ...'
      }
    ],
    host: "user-name:pass-word-pass-word-pass-word@nd-123-456-789.p2pify.com",
    port: false,
    protocol: "https",
    type: "rpc"
  }
}
  1. Run embark run chainstack and Embark will deploy using Chainstack.

# Remix IDE

To make Remix IDE interact with the network through a Chainstack node:

  1. Get MetaMask and set it to interact through a Chainstack node. See Interacting through MetaMask.
  2. In Remix IDE, navigate to the Deploy tab. Select Injected Web3 in Environment.

This will engage MetaMask and make Remix IDE interact with the network through a Chainstack node.

For a detailed tutorial with Remix IDE, see Trust Fund account on Ethereum.

# web3.js

Build DApps using web3.js and Ethereum nodes deployed with Chainstack.

  1. Install web3.js.
  2. Use the WebsocketProvider object to connect to your node's WSS endpoint.
const Web3 = require('web3');

const web3 = new Web3(new Web3.providers.WebsocketProvider('wss://USERNAME:PASSWORD@WSS_ENDPOINT'));

where

  • USERNAME — your Ethereum node access username.
  • PASSWORD — your Ethereum node access password.
  • WSS_ENDPOINT — your Ethereum node WSS endpoint.

Example to get the latest block number:

const Web3 = require('web3');

const web3 = new Web3(new Web3.providers.WebsocketProvider('wss://user-name:pass-word-pass-word-pass-word@ws-nd-123-456-789.p2pify.com'));

web3.eth.getBlockNumber().then(console.log);

# web3.py

Build DApps using web3.py and Ethereum nodes deployed with Chainstack.

  1. Install web3.py.
  2. Connect over HTTP or WebSocket. See also Ethereum node connection: HTTP vs WebSocket.

# HTTP

Use the HTTPProvider to connect to your node's RPC endpoint .

from web3 import Web3

web3 = Web3(Web3.HTTPProvider('https://%s:%s@%s'% ("USERNAME", "PASSWORD", "RPC_ENDPOINT")))
print(web3.eth.blockNumber)

where

  • USERNAME — your Ethereum node access username.
  • PASSWORD — your Ethereum node access password.
  • RPC_ENDPOINT — your Ethereum node RPC endpoint.

Example to get the latest block number:

from web3 import Web3

web3 = Web3(Web3.HTTPProvider('https://%s:%s@%s'% ("user-name", "pass-word-pass-word-pass-word", "nd-123-456-789.p2pify.com")))
print(web3.eth.blockNumber)

# WebSocket

Use the WebsocketProvider object to connect to your node's WSS endpoint.

from web3 import Web3

web3 = Web3(Web3.WebsocketProvider('wss://%s:%s@%s'% ("USERNAME", "PASSWORD", "WSS_ENDPOINT")))
print(web3.eth.blockNumber)

where

  • USERNAME — your Ethereum node access username.
  • PASSWORD — your Ethereum node access password.
  • WSS_ENDPOINT — your Ethereum node WSS endpoint.

Example to get the latest block number:

from web3 import Web3

web3 = Web3(Web3.WebsocketProvider('wss://%s:%s@%s'% ("user-name", "pass-word-pass-word-pass-word", "ws-nd-123-456-789.p2pify.com")))
print(web3.eth.blockNumber)

# web3j

Build DApps using web3j and Ethereum nodes deployed with Chainstack.

Use the HttpService object to connect to your node's RPC endpoint.

Example to get the latest block number:

package getLatestBlock;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthBlock;
import org.web3j.protocol.exceptions.ClientConnectionException;
import org.web3j.protocol.http.HttpService;

import okhttp3.Authenticator;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;

public final class App {

  private static final String USERNAME = "USERNAME";
  private static final String PASSWORD = "PASSWORD";
  private static final String RPC_ENDPOINT = "RPC_ENDPOINT";

  public static void main(String[] args) {
    try {

      OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
      clientBuilder.authenticator(new Authenticator() {
          @Override public Request authenticate(Route route, Response response) throws IOException {
              String credential = Credentials.basic(USERNAME, PASSWORD);
              return response.request().newBuilder().header("Authorization", credential).build();
          }
      });

      HttpService service = new HttpService(RPC_ENDPOINT, clientBuilder.build(), false);
      Web3j web3 = Web3j.build(service);


      EthBlock.Block latestBlock = web3.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false).send().getBlock();


      System.out.println("Latest Block: #" + latestBlock.getNumber());

    } catch (IOException | ClientConnectionException ex) {

      Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

}

where

  • USERNAME — your Ethereum node access username.
  • PASSWORD — your Ethereum node access password.
  • RPC_ENDPOINT — your Ethereum node RPC endpoint.

See also the full code on GitHub.

# ethers.js

Build DApps using ethers.js and Ethereum nodes deployed with Chainstack.

  1. Install ethers.js.
  2. Connect over HTTP or WebSocket. See also Ethereum node connection: HTTP vs WebSocket.

# HTTP

Use the JsonRpcProvider object to connect to your node's RPC endpoint.

const { ethers } = require("ethers");

var urlInfo = {
    url: 'RPC_ENDPOINT',
    user: 'USERNAME',
    password: 'PASSWORD'
};
var provider = new ethers.providers.JsonRpcProvider(urlInfo, NETWORK_ID);

where

  • RPC_ENDPOINT — your Ethereum node RPC endpoint.
  • USERNAME — your Ethereum node access username.
  • PASSWORD — your Ethereum node access password.
  • NETWORK_ID — Ethereum network ID:
    • Mainnet: 1
    • Ropsten: 3
    • Rinkeby: 4

Example to get the latest block number on mainnet:

const { ethers } = require("ethers");

var urlInfo = {
    url: 'https://nd-123-456-789.p2pify.com',
    user: 'user-name',
    password: 'pass-word-pass-word-pass-word'
};
var provider = new ethers.providers.JsonRpcProvider(urlInfo, 1);

provider.getBlockNumber().then(console.log);

# WebSocket

Use the WebSocketProvider object to connect to your node's WSS endpoint.

const { ethers } = require("ethers");

const provider = new ethers.providers.WebSocketProvider('wss://USERNAME:PASSWORD@WSS_ENDPOINT', NETWORK_ID);

where

  • USERNAME — your Ethereum node access username.
  • PASSWORD — your Ethereum node access password.
  • WSS_ENDPOINT — your Ethereum node WSS endpoint.
  • NETWORK_ID — Ethereum network ID:
    • Mainnet: 1
    • Ropsten: 3
    • Rinkeby: 4

Example to get the latest block number on mainnet:

const { ethers } = require("ethers");

const provider = new ethers.providers.WebSocketProvider('wss://user-name:pass-word-pass-word-pass-word@ws-nd-123-456-789.p2pify.com', 1);

provider.getBlockNumber().then(console.log);