CLV Documentations
CLV DOC
CLV DOC
  • Intro to CLV
    • About CLV πŸ€
    • Clover Finance Rebrands to CLV
    • What is CLV Chain?
    • What is CLV Wallet?
    • What is $CLV Token?
    • πŸ“£ CLV Official Channels
    • 🏘 CLV Community Channels
  • Use CLV Chain
    • πŸ“š Beginner’s Guide
      • Setup CLV Wallet
      • Setup Metamask Wallet
      • How to Get $CLV
      • Bridge Other Assets to CLV P-Chain (Parachain)
    • 🌐 Network Details
    • 🏦 Economics
      • Tokenomics
      • Inflation Design
    • πŸ—³οΈGovernance
    • πŸ™‹πŸ» CLV Chain FAQ
    • CLV P-Chain EVM Explorer
    • $CLV Cross-Chain Explorer
    • $CLV Cross-Chain Transfer
  • CLV Validator & Staking
    • What are Nominator & Validator?
    • Stake as a Nominator
    • Running a Validator or RPC Node
    • Staking FAQ
  • Use CLV Wallet
    • πŸ’° Download CLV Wallet
      • Browser Extension (Google Chrome & Brave)
      • Apple iOS (Mobile)
      • Android (Mobile)
      • Web Wallet (Universal)
    • πŸ“± CLV Mobile Wallet
    • πŸ–₯ CLV Extension Wallet
    • πŸ•ΈοΈ CLV Web Wallet
  • CLV Chain Developer Guide
    • Getting Started
    • Using Local Node
      • Using MetaMask
      • Using Remix
      • Using Web3.js
    • Using Testnet
      • Create an account
      • Faucet
      • Run a Testnet Node
      • Connect to Testnet
    • dApp Example
      • Setup dApp project
      • Setup truffle
      • The Counter Contract
      • Deploy Contract
      • Counter Webapp
    • Test Cases
    • Technical Documentations
      • CLV EVM
      • Developers Incentive
      • CLV Accounts Binding
      • Virtual Ethereum Address Binding
      • Query Balance
      • Transaction Finality
      • Web3 Compatibility
        • eth_protocolVersion
        • eth_syncing
        • eth_hashrate
        • eth_coinbase
        • eth_mining
        • eth_chainId
        • eth_gasPrice
        • eth_accounts
        • eth_blockNumber
        • eth_getBalance
        • eth_getStorageAt
        • eth_getBlock
        • eth_getTransactionCount
        • eth_getBlockTransactionCount
        • eth_getBlockUncleCount
        • eth_getCode
        • eth_sendTransaction
        • eth_sendSignedTransaction
        • eth_call
        • eth_estimateGas
        • eth_getTransaction
        • eth_getTransactionByBlockHashAndIndex
        • eth_getTransactionByBlockNumberAndIndex
        • eth_getTransactionReceipt
        • eth_getUncle
        • eth_getLogs
        • eth_getWork
        • eth_submitWork
        • eth_submitHashrate
        • eth_subscribe
        • eth_unsubscribe
        • net_version
        • net_peerCount
        • net_listening
        • web3_clientVersion
        • web3_sha3
      • CLV P-Chain Integration
  • CLV Wallet Developer Guide
    • EVM dApp Integration
    • Substrate dApp Integration
    • Solana dApp Integration
    • Kadena dApp Integration
    • Aptos dApp Integration
    • Web Wallet dApp Integration
    • dApp Interaction Protocol
    • Wallet Integration QA
  • CLV Ecosystem
    • 🏞️ Ecosystem Partners
      • CLV Chain
      • CLV Wallet
    • 🌱 Incentive Programs
      • CLV Ecosystem Grant
      • CLV Wallet Integration Grant
      • CLV Bug Bounty Program
    • Whitelist Assets on Clover P-Chain
    • Bridge assets into CLV P-Chain
  • Assets
    • πŸ”€ Glossary
      • Blockchain (in General)
      • Polkadot
      • Wallet
    • 🎟️ CLV - Polkadot Parachain Auction 2021
      • Parachain Auction Rule Announcement
      • Parachain Winning Reward Announcement
    • πŸ›οΈ Brand Pack
    • πŸ“„ Whitepaper
Powered by GitBook
On this page
  • Introduction
  • Prerequisites
  • Query Balance
  • Send Transaction
  1. CLV Chain Developer Guide
  2. Using Local Node

Using Web3.js

Using Web3.js to interact with CLV

PreviousUsing RemixNextUsing Testnet

Last updated 2 years ago

Introduction

This guide walks through the process of using to manually sign and send a transaction to a CLV standalone node. For this example, we will use Node.js and straightforward JavaScript code.

The guide assumes that you have a local CLV node running in --dev mode. You can find instructions to set up a local CLV node .

Prerequisites

1. Start CLV node

Start your standalone CLV node, which can successfully produce blocks in your local environment.

2. Install Node.js and NPM

To install Node.js (we'll go for v12.x, you can choose other versions, such as the latest version v15.x) and NPM package manager. You can do this by running the following commands in your terminal:

For Mac users:

brew update
brew install node

For Debian users:

curl -sL https://deb.nodesource.com/setup_12.x | sudo bash -
sudo apt-get install -y nodejs

For Centos/Redhat users:

curl -sL https://deb.nodesource.com/setup_12.x | sudo bash -
sudo yum install nodejs

We can verify that everything installed correctly by querying the version for each package:

node -v
npm -v

3. Create your Node.js project

You can create a repository on your local environment by running:

mkdir clover-web3 && cd clover-web3/
npm init --yes
npm install web3 --save

Query Balance

We can easily do this by leveraging the Ethereum compatibility features of CLV.

First let's create a file balance.js under the project we've created. Here we just query the balance of the genesis account, and the genesis account is endowed with 10,000,000 ETH by your local CLV node under the development mode. To get the balances of the account, we need to make an asynchronous function that uses the web3.eth.getBalance(address) command. We can take advantage of the web3.utils.fromWei() function to transform the balance into a more readable number in ETH.

The file looks like this:

const Web3 = require('web3')

const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:9933'));
const GENESIS_ACCOUNT = '0xe6206C7f064c7d77C6d8e3eD8601c9AA435419cE';

async function getBalance(account) {
  const balance = await web3.eth.getBalance(account);
  const balanceOfEther = web3.utils.fromWei(balance, "ether");
  console.log(`${account} has balance: ${balanceOfEther} ETH`);
}

getBalance(GENESIS_ACCOUNT);

Running the Script

You can run the above script using command :

node balance.js

The output of the execution is as following:

Send Transaction

For our example, we only need a single JavaScript file (arbitrarily named transaction.js) to create and send the transaction, which we will run using the node command in the terminal. The script will transfer 100 CLV from the genesis account to another address. For simplicity, the file is divided into three sections: variable definition, create transaction, and send transaction.

We need to set a couple of values in the variable definitions, then construct and sign the transaction:

  1. Create your Web3 constructor (Web3).

  2. Specify the received address, CLV amount, gas price and gas limit.

  3. Sign the transaction and broadcast it the CLV chain

The code looks like:

const Web3 = require('web3')

const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:9933'));
const GENESIS_ACCOUNT = '0xe6206C7f064c7d77C6d8e3eD8601c9AA435419cE';

async function webTransfer(account, etherValue) {
  try {
    const before = await web3.eth.getBalance(account);
    console.log(`before transfer: ${account}, has balance ${web3.utils.fromWei(before, "ether")}`);

    const nonce = await web3.eth.getTransactionCount(GENESIS_ACCOUNT);
    const signedTransaction = await web3.eth.accounts.signTransaction({
      from: GENESIS_ACCOUNT,
      to: account,
      value: web3.utils.toWei(etherValue.toString(), "ether"),
      gasPrice: web3.utils.toWei("100", "gwei"),
      gas: "0x5208",
      nonce: nonce
    }, 'your private key configured somewhere');
    await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);
    const after = await web3.eth.getBalance(account);
    console.log(`after transfer: ${account}, has balance ${web3.utils.fromWei(after, "ether")}`);
    return { success: true };
  } catch (e) {
    return { success: false, message: e.toString() };
  }
}

webTransfer('0x1874FC5f915aa9Fd24C379fE7F9ec40607CEf78A', 100).then(console.log)

Running the Script

You can run the above script using command :

node transaction.js

web3.js
here