Interacting via CLI

Introduction

For advanced developers, interacting with Studio Blockchain via the Command Line Interface (CLI) offers powerful tools to deploy contracts, query blockchain data, and manage transactions programmatically. This guide covers setting up the CLI, executing essential commands, and leveraging Web3 libraries for seamless interaction.


1. Setting Up CLI Tools

Prerequisites

Ensure the following are installed on your system:

  1. Node.js: For running JavaScript-based CLI tools (download here).

  2. npm or yarn: For managing packages and dependencies.

  3. web3.js or ethers.js: Libraries to interact with the blockchain.

Installing Web3.js or Ethers.js

Install the library of your choice globally:

bashCopy codenpm install -g web3 ethers

2. Configuring the RPC Endpoint

To interact with Studio Blockchain, you need to configure the RPC endpoint in your scripts or CLI environment.

Testnet Details:

  • HTTP RPC URL: https://rpc.studio-blockchain.com

  • WebSocket URL: wss://ws.studio-blockchain.com

  • Chain ID: [Insert Chain ID here]

Example Configuration in JavaScript

javascriptCopy codeconst Web3 = require('web3');
const web3 = new Web3('https://rpc.studio-blockchain.com');

// Check network ID
web3.eth.net.getId().then(console.log);

3. Common CLI Commands

Querying Blockchain Data

  1. Get Latest Block:

    bashCopy codecurl -X POST -H "Content-Type: application/json" \
    --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true],"id":1}' \
    https://rpc.studio-blockchain.com
  2. Get Block by Number: Replace 0x10 with the block number in hexadecimal.

    bashCopy codecurl -X POST -H "Content-Type: application/json" \
    --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x10", true],"id":1}' \
    https://rpc.studio-blockchain.com
  3. Check Wallet Balance: Replace 0xYourAddress with the wallet address.

    bashCopy codecurl -X POST -H "Content-Type: application/json" \
    --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0xYourAddress", "latest"],"id":1}' \
    https://rpc.studio-blockchain.com

Deploying a Contract via CLI

Use the web3 library to deploy smart contracts programmatically.

Example: Deploying an ERC-20 Token

javascriptCopy codeconst Web3 = require('web3');
const web3 = new Web3('https://rpc.studio-blockchain.com');

// Replace with your compiled contract ABI and bytecode
const abi = [ /* ABI array */ ];
const bytecode = '0x...'; // Contract bytecode

const deployContract = async () => {
    const account = web3.eth.accounts.privateKeyToAccount('0xYourPrivateKey');
    web3.eth.accounts.wallet.add(account);

    const contract = new web3.eth.Contract(abi);

    const deployTx = contract.deploy({ data: bytecode });
    const gas = await deployTx.estimateGas();
    const result = await deployTx.send({
        from: account.address,
        gas,
    });

    console.log('Contract deployed at:', result.options.address);
};

deployContract();

Monitoring Transactions

  1. Get Transaction by Hash: Replace 0xTxHash with the transaction hash.

    bashCopy codecurl -X POST -H "Content-Type: application/json" \
    --data '{"jsonrpc":"2.0","method":"eth_getTransactionByHash","params":["0xTxHash"],"id":1}' \
    https://rpc.studio-blockchain.com
  2. Get Transaction Receipt:

    bashCopy codecurl -X POST -H "Content-Type: application/json" \
    --data '{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0xTxHash"],"id":1}' \
    https://rpc.studio-blockchain.com

Subscribing to Events via WebSocket

Leverage the WebSocket endpoint for real-time updates.

Example: Subscribe to New Blocks

javascriptCopy codeconst Web3 = require('web3');
const web3 = new Web3('wss://ws.studio-blockchain.com');

web3.eth.subscribe('newBlockHeaders', (error, blockHeader) => {
    if (!error) {
        console.log('New block:', blockHeader);
    } else {
        console.error(error);
    }
});

4. Best Practices for CLI Usage

  1. Secure Private Keys:

    • Avoid hardcoding private keys in your scripts.

    • Use environment variables or secure key management tools.

  2. Monitor Gas Usage:

    • Use web3.eth.getGasPrice() to get the current gas price.

    • Monitor gas usage metrics via the API (/blocks, /transactions/latest).

  3. Use Testnet for Experimentation:

    • Always test your scripts on the Studio Blockchain Testnet before deploying to the mainnet.

  4. Automate Tasks:

    • Use scripts to automate repetitive tasks like deploying contracts or querying balances.

Last updated