CLV Documentations
Portal
Portal
  • Welcome to Clover
  • Useful Links
  • CLV Token
  • Quick Start
    • Clover Networks
    • Sakura Networks
    • Using Local Node
    • Using Testnet
      • Create an account
      • Faucet
      • Run a Testnet Node
      • Connect to Testnet
  • Development Guide
    • Introduction
      • Prerequisites
      • Setup environment
    • Using MetaMask
    • Using Remix
    • Using Web3.js
      • Query Balance
      • Send Transaction
    • Counter Tutorial
      • Setup dapp project
      • Setup truffle
      • The Counter Contract
      • Deploy Contract
      • Counter Webapp
  • Clover Wallets & Dapps
    • Clover Extension Wallet
      • Getting Started
      • Switch Networks
      • Add Tokens
      • Send Tokens
      • Cross-Chain Transfer
      • View Seed Phrase
      • Import Account
      • dApp Integration
      • Substrate dApp Integration
      • Solana-dApp-Integration
      • dApp Interaction Protocol
      • Wallet Integration QA
    • Clover Mobile Wallet
    • Clover Web Wallet
      • dApp Integration
    • Clover Assets Explorer
    • Clover Cross-Chain Explorer
  • Maintain
    • Running a validator on Clover Network
    • Running a RPC node
    • Staking on Clover
      • Staking via Apps
      • Staking via Clover Wallet
  • Technical Documentation
    • 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
    • Clover Test Cases
    • Clover EVM
    • Clover Accounts Binding
    • Query Balance
    • Transaction Finality
  • Clover Eco Incentive Program
    • Introduction
    • Clover Developer Incentive Program
    • Virtual Ethereum address binding
    • Clover User Incentive Program
  • Parachain Auction
    • About Polkadot Parachain Auction
Powered by GitBook
On this page
  1. Development Guide
  2. Using Web3.js

Send Transaction

Use Web3.js to Send Transaction on Clover

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 Clover 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

PreviousQuery BalanceNextCounter Tutorial

Last updated 4 years ago