# Tools

# C-Chain interaction tools

# Geth client

Interact with your Avalanche nodes using Geth (opens new window).

  1. Install Geth (opens new window).

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

geth attach ENDPOINT

where

  • ENDPOINT — your node HTTPS or WSS endpoint.

See also View node access and credentials.

Example:

  • Key-protected
  • Password-protected
geth attach https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d/ext/bc/C/rpc
  1. Invoke any methods from Web3 JavaScript API (opens new window).

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("0x68c307d5155f7f5e55c5150238ef7051ecf8fd25"))
0.51

# MetaMask

On the node access page, click Add to MetaMask.

# C-Chain development tools

# Truffle

Configure Truffle Suite (opens new window) to deploy contracts on C-Chain through your Avalanche nodes.

  1. Install Truffle Suite (opens new window), HD Wallet-enabled Web3 provider (opens new window), and create a project.

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

  • Key-protected
  • Password-protected
const HDWalletProvider = require("@truffle/hdwallet-provider");
const mnemonic = 'pattern enroll upgrade ...';
...
module.exports = {
 networks: {
    chainstack: {
        provider: () => new HDWalletProvider(mnemonic, "https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d/ext/bc/C/rpc"),
        network_id: "*"
    },
   }
  }
};

# Hardhat

Configure Hardhat (opens new window) to deploy contracts and interact on C-Chain through your Avalanche nodes.

  1. Install Hardhat (opens new window) 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: "ENDPOINT",
        accounts: ["PRIVATE_KEY"]
    },
   }
};

where

  • ENDPOINT — your node HTTPS or WSS endpoint.
  • PRIVATE_KEY — the private key of the account that you use to deploy the contract.

See also View node access and credentials.

Example:

  • Key-protected
  • Password-protected
require("@nomiclabs/hardhat-waffle");
...
module.exports = {
  solidity: "0.7.3",
  networks: {
    chainstack: {
        url: "https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d/ext/bc/C/rpc",
        accounts: ["ee5dda7d38d194783d32adcfc961401108b8fdde27e8fee115553959d434e68b"]
    },
   }
};
  1. Run npx hardhat run scripts/deploy.js --network chainstack and Hardhat will deploy using Chainstack.

# web3.js

Build DApps on C-Chain using web3.js (opens new window) and Avalanche nodes deployed with Chainstack.

# HTTP

  1. Install web3.js (opens new window).
  2. Use the HttpProvider object to connect to your node HTTPS endpoint.
const Web3 = require('web3');

const web3 = new Web3(new Web3.providers.HttpProvider('ENDPOINT'));

where

  • ENDPOINT — your node HTTPS endpoint.

Example to get the latest block number:

  • Key-protected
  • Password-protected
const Web3 = require('web3');

const web3 = new Web3(new Web3.providers.HttpProvider('https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d/ext/bc/C/rpc'));

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

# WebSocket

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

const web3 = new Web3(new Web3.providers.WebsocketProvider('ENDPOINT'));

where

  • ENDPOINT — your node WSS endpoint.

Example to get the latest block number:

  • Key-protected
  • Password-protected
const Web3 = require('web3');

const web3 = new Web3(new Web3.providers.WebsocketProvider('wss://ws-nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d/ext/bc/C/ws'));

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

# web3.py

Build DApps on C-Chain using web3.py (opens new window) and Avalanche nodes deployed with Chainstack.

  1. Install web3.py (opens new window).
  2. Connect over HTTP or WebSocket. See also EVM node connection: HTTP vs WebSocket.

# HTTP

Use the HTTPProvider to connect to your node HTTPS endpoint.

  • Key-protected
  • Password-protected
from web3 import Web3

web3 = Web3(Web3.HTTPProvider('ENDPOINT'))

where

  • ENDPOINT — your node HTTPS endpoint.
  • HOSTNAME — your node HTTPS endpoint hostname.
  • USERNAME — your node access username.
  • PASSWORD — your node access password.

Example to get the latest block number:

  • Key-protected
  • Password-protected
from web3 import Web3

web3 = Web3(Web3.HTTPProvider('https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d/ext/bc/C/rpc'))
print(web3.eth.blockNumber)

# WebSocket

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

  • Key-protected
  • Password-protected
from web3 import Web3

web3 = Web3(Web3.WebsocketProvider('ENDPOINT'))

where

  • ENDPOINT — your node WSS endpoint.
  • HOSTNAME — your node WSS endpoint hostname.
  • USERNAME — your node access username.
  • PASSWORD — your node access password.

Example to get the latest block number:

  • Key-protected
  • Password-protected
from web3 import Web3

web3 = Web3(Web3.WebsocketProvider('wss://ws-nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d/ext/bc/C/ws'))
print(web3.eth.blockNumber)

# web3.php

Build DApps on C-Chain using web3.php (opens new window) and Avalanche nodes deployed with Chainstack.

  1. Install web3.php (opens new window).
  2. Connect over HTTP:
<?php

require_once "vendor/autoload.php";

use Web3\Web3;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\HttpRequestManager;

$web3 = new Web3(new HttpProvider(new HttpRequestManager("ENDPOINT", 5)));
?>

where ENDPOINT is your node HTTPS endpoint.

  1. Use JSON-RPC methods (opens new window) to interact with the node.

Example to get the latest block number:

  • Key-protected
  • Password-protected
<?php

require_once "vendor/autoload.php";

use Web3\Web3;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\HttpRequestManager;

$web3 = new Web3(new HttpProvider(new HttpRequestManager("https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d/ext/bc/C/rpc", 5)));

$eth = $web3->eth;

$eth->blockNumber(function ($err, $data) {
        print "$data \n";
});
?>

# ethers.js

Build DApps on C-Chain using ethers.js (opens new window) and Avalanche nodes deployed with Chainstack.

  1. Install ethers.js (opens new window).
  2. Connect over HTTP or WebSocket.

# HTTP

Use the JsonRpcProvider object to connect to your node HTTPS endpoint.

  • Key-protected
  • Password-protected
const { ethers } = require("ethers");

var urlInfo = {
    url: 'ENDPOINT'
};
var provider = new ethers.providers.JsonRpcProvider(urlInfo, NETWORK_ID);

where

  • ENDPOINT — your node HTTPS endpoint.
  • USERNAME — your node access username.
  • PASSWORD — your node access password.
  • NETWORK_ID — Avalanche C-Chain network ID:
    • Mainnet: 43114
    • Fuji testnet: 43113

Example to get the latest block number on mainnet:

  • Key-protected
  • Password-protected
const { ethers } = require("ethers");

var urlInfo = {
    url: 'https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d/ext/bc/C/rpc'
};
var provider = new ethers.providers.JsonRpcProvider(urlInfo, 43114);

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

# WebSocket

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

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

const provider = new ethers.providers.WebSocketProvider('ENDPOINT', NETWORK_ID);

where

  • ENDPOINT — your node WSS endpoint.
  • NETWORK_ID — Avalanche C-Chain network ID:
    • Mainnet: 43114
    • Fuji testnet: 43113

Example to get the latest block number on mainnet:

  • Key-protected
  • Password-protected
const { ethers } = require("ethers");

const provider = new ethers.providers.WebSocketProvider('wss://ws-nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d/ext/bc/C/ws', 43114);

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

# Brownie

  1. Install Brownie (opens new window).
  2. Use the brownie networks add command with the node endpoint:
brownie networks add Avalanche ID name="NETWORK_NAME" host=ENDPOINT chainid=NETWORK_ID

where

  • ID — any name that you will use as the network tag to run a deployment. For example, chainstack-mainnet.
  • NETWORK_NAME — any name that you want to identify the network by in the list of networks. For example, avalanche-mainnet.
  • ENDPOINT — your node HTTPS or WSS endpoint.
  • NETWORK_ID — Avalanche C-Chain network ID:
    • Mainnet: 43114
    • Fuji testnet: 43113

Example to add an Avalanche C-Chain testnet node to the list of Brownie networks:

  • Key-protected
  • Password-protected
brownie networks add Avalanche avalanche-testnet name="Avalanche Fuji testnet" host=https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d/ext/bc/C/rpc chainid=43113

Example to run the deployment script:

brownie run deploy.py --network avalanche-testnet

# Foundry

  1. Install Foundry (opens new window).
  2. Use --rpc-url to run the operation through your Chainstack node.

# Forge

Use forge (opens new window) to develop, test, and deploy your smart contracts on C-Chain.

To deploy a contract:

forge create CONTRACT_NAME --contracts CONTRACT_PATH --private-key PRIVATE_KEY --rpc-url ENDPOINT

where

  • CONTRACT_NAME — name of the contract in the Solidity source code.
  • CONTRACT_PATH — path to your smart contract.
  • PRIVATE_KEY — the private key to your funded account that you will use to deploy the contract.
  • ENDPOINT — your node HTTPS endpoint.

Example to deploy the simple storage (opens new window) contract on C-Chain:

  • Key-protected
  • Password-protected
forge create SimpleStorage --contracts /root/foundry/contracts/simplestorage.sol --private-key 9c4b7f4ad48f977dbcdb2323249fd738cc9ff283a7514f3350d344e22c5b923d --rpc-url https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d/ext/bc/C/rpc

# Cast

Use cast (opens new window) to interact with the network and the deployed contracts on C-Chain.

To get the latest block number:

cast block-number --rpc-url ENDPOINT

where ENDPOINT is your node HTTPS endpoint.

Example:

  • Key-protected
  • Password-protected
cast block-number --rpc-url https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d/ext/bc/C/rpc

# X-Chain interaction tools

Interact with the X-Chain through your Avalanche nodes using JSON-RPC API (opens new window).

Use curl (opens new window) or Postman (opens new window) to invoke Avalanche X-Chain API methods (opens new window).

Example below demonstrates how to get AVAX balance of an address through your Avalanche node HTTPS endpoint on the X-Chain mainnet:

  • Key-protected
  • Password-protected
curl -X POST --data '{
  "jsonrpc":"2.0",
  "id"     : 1,
  "method" :"avm.getBalance",
  "params" :{
      "address":"X-avax1slt2dhfu6a6qezcn5sgtagumq8ag8we75f84sw",
      "assetID": "FvwEAhmxKfeiG8SnEvq42hc6whRyY3EFYAvebMqDNDGCgxN5Z"
  }
}' -H 'content-type:application/json;' https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d/ext/bc/X

# X-Chain development tools

# AvalancheJS

  1. Install AvalancheJS (opens new window).
  2. Use AvalancheJS AVM examples (opens new window) to interact with the X-Chain through your Avalanche node with the following settings:
const ip: string = "BASE_ENDPOINT"
// const port: number = 9650
const protocol: string = "https"
const networkID: number = CHAIN_ID
const avalanche: Avalanche = new Avalanche(ip, null, protocol, networkID)

where

  • BASE_ENDPOINT — your node key-protected endpoint without the https prefix and the ext postfix. For example, nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d.
  • CHAIN_ID — the chain ID of the network you are connecting to:
    • Mainnet: 1
    • Fuji testnet: 5

Make sure you remove const port and change port to null in the default example.

Example to get AVAX balance of an address through your Avalanche node HTTPS endpoint on the X-Chain mainnet:

import { Avalanche } from "../../dist"
import { AVMAPI } from "../../dist/apis/avm"

const ip: string = "nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d"
// const port: number = 9650
const protocol: string = "https"
const networkID: number = 1
const avalanche: Avalanche = new Avalanche(ip, null, protocol, networkID)
const xchain: AVMAPI = avalanche.XChain()

const main = async (): Promise<any> => {
  const address: string = "X-avax1k30tskunzxr2tmapy8p4y0ujn2802yr3743679"
  const balance: object = await xchain.getBalance(address, "AVAX")
  console.log(balance)
}

main()

# AvalancheGo

Subscribe to events over WebSocket to an X-Chain address.

  1. Install AvalancheGo (opens new window).
  2. Use the Go example (opens new window) to listen to events on an X-Chain address through your Avalanche node with the following settings:
...
 httpHeader := http.Header{}
    conn, _, err := dialer.Dial("EVENTS_ENDPOINT", httpHeader)
    if err != nil {
        panic(err)
    }
...

where EVENTS_ENDPOINT is your Avalanche node X-Chain events WebSocket endpoint. See also View node access and credentials.

Example to get all transactions to the Binance hot wallet (opens new window) on the X-Chain mainnet:

package main

import (
    "encoding/json"
    "log"
    "net"
    "net/http"
    "sync"

    "github.com/ava-labs/avalanchego/api"
    "github.com/ava-labs/avalanchego/pubsub"
    "github.com/gorilla/websocket"
)

func main() {
    dialer := websocket.Dialer{
        NetDial: func(netw, addr string) (net.Conn, error) {
            return net.Dial(netw, addr)
        },
    }

    httpHeader := http.Header{}
    conn, _, err := dialer.Dial("wss://ws-nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d/ext/bc/X/events", httpHeader)
    if err != nil {
        panic(err)
    }

    waitGroup := &sync.WaitGroup{}
    waitGroup.Add(1)

    readMsg := func() {
        defer waitGroup.Done()

        for {
            mt, msg, err := conn.ReadMessage()
            if err != nil {
                log.Println(err)
                return
            }
            switch mt {
            case websocket.TextMessage:
                log.Println(string(msg))
            default:
                log.Println(mt, string(msg))
            }
        }
    }

    go readMsg()

    cmd := &pubsub.Command{NewSet: &pubsub.NewSet{}}
    cmdmsg, err := json.Marshal(cmd)
    if err != nil {
        panic(err)
    }
    err = conn.WriteMessage(websocket.TextMessage, cmdmsg)
    if err != nil {
        panic(err)
    }

    var addresses []string
    addresses = append(addresses, "X-avax1slt2dhfu6a6qezcn5sgtagumq8ag8we75f84sw")
    cmd = &pubsub.Command{AddAddresses: &pubsub.AddAddresses{JSONAddresses: api.JSONAddresses{Addresses: addresses}}}
    cmdmsg, err = json.Marshal(cmd)
    if err != nil {
        panic(err)
    }

    err = conn.WriteMessage(websocket.TextMessage, cmdmsg)
    if err != nil {
        panic(err)
    }

    waitGroup.Wait()
}