Darwinia Documentation
  • Home
  • Blog
  • Docs
  • Crab Network
  • Developers
  • Languages iconEnglish
    • 中文

›Tutorials

Get Started

  • Introduction
  • FAQ

    • General
    • Developers
    • Community Building
    • Token Holder FAQ
    • Mainnet Related
    • Lexion
  • Community
  • Roadmap
  • Tools
  • Sample Article
  • How to contribute

Understand Darwinia

  • Overview
  • Architecture

    • Architecture
    • Bridge Chain
    • Solo Mode
    • Parachain Mode
  • Appchain SDK
  • NFT Identifiability
  • Accounts

    • Account Keys
    • Account Generation
    • Account Address
    • Check Balance
    • Balance Types

    Economics

    • Native Tokens
    • Distribution
    • Transaction Fees

    Staking

    • Basic Staking
    • Advanced Staking
    • Staking Power
    • Slash Algorithm
  • Bridge Chain Mechanism
  • Cryptography
  • Governance
  • Glossary

Tutorials

    Darwinia Wallet

    • Create an Account
    • Become a nominator
  • Running a node
  • Become a validator
  • Become a relayer
  • Participate in governance
  • Recharge EVM address
  • Using Web3 for transaction
  • Using Web3 for contract
  • Wormhole

    • General
    • Ethereum -> Darwinia Bridge
    • Darwinia -> Ethereum Bridge

RFCs

  • RFCs
  • 0001 Darwinia Developement Structure
  • 0007 Dawinia Token Staking Model
  • 0009 Dawinia Liquid KTON Reward
  • 0010 Darwinia Cross Chain Nft Bridge Protocol
  • 0011 Using Harberger Tax To Find Price For Xclaim Vault Collaterals
  • 0012 Darwinia Bridge Core Interoperation In Chainrelay Enabled Blockchains
  • 0013 Darwinia Cross Chain Nft Standards
  • 0014 Darwinia Token Migration By Cross Chain Redeem Protocol
Edit

Using Web3 for transaction

This tutorial is only used in Pangolin test network.

Preparation

  1. Install Nodejs
$ sudo apt install -y nodejs
  1. install web3 package
$ mkdir transaction && cd transaction/
$ npm init --yes
$ npm install --save web3

The project layout as follows:

$ ls transaction/
balance.js  node_modules/  package.json  package-lock.json  transaction.js

Note: If you are working on Pangolin Network,change the default address http://localhost:9933 to http://t1.hkg.itering.com:9933。

Get Balance

// balance.js
const Web3 = require('web3');

// Variables definition
const addressFrom = '0x6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b';
const addressTo = '0xAa01a1bEF0557fa9625581a293F3AA7770192632';
const web3 = new Web3('http://localhost:9933');

// Balance call
const balances = async () => {
   const balanceFrom = web3.utils.fromWei(
      await web3.eth.getBalance(addressFrom),
      'ether'
   );
   const balanceTo = await web3.utils.fromWei(
      await web3.eth.getBalance(addressTo),
      'ether'
   );

   console.log(`The balance of ${addressFrom} is: ${balanceFrom} Pring.`);
   console.log(`The balance of ${addressTo} is: ${balanceTo} Pring.`);
};

balances();

The output:

$ node balance.js
The balance of 0x6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b is: 123.45678900000000009 Pring.
The balance of 0xAa01a1bEF0557fa9625581a293F3AA7770192632 is: 0 Pring.

Transfer Balance

Make a transaction to Transfer 50 PRING from 0x6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b to 0xAa01a1bEF0557fa9625581a293F3AA7770192632.

// transfer.js
const Web3 = require('web3');

// Variables definition
const privKey =
   '99B3C12287537E38C90A9219D4CB074A89A16E9CDB20BF85728EBD97C343E342';
const addressFrom = '0x6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b';
const addressTo = '0xAa01a1bEF0557fa9625581a293F3AA7770192632';
const web3 = new Web3('http://localhost:9933');

// Create transaction
const deploy = async () => {
   console.log(
      `Attempting to send transaction from ${addressFrom} to ${addressTo}`
   );

   const createTransaction = await web3.eth.accounts.signTransaction(
      {
         from: addressFrom,
         to: addressTo,
         value: web3.utils.toWei('50', 'ether'),
         gas: '5000000000',
      },
      privKey
   );

   const createReceipt = await web3.eth.sendSignedTransaction(
      createTransaction.rawTransaction
   );

   console.log(
      `Transaction successful with hash: ${createReceipt.transactionHash}`
   );

}

deploy();

The output:

$ node transaction.js 
Attempting to send transaction from 0x6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b to 0xAa01a1bEF0557fa9625581a293F3AA7770192632
Transaction successful with hash: 0xaccfb5438c6927c6c32adc640394600f5dda183ea82683dc5a9feddc64b5d438

Get balances again:

The balance of 0x6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b is: 73.45678900000000009 Pring.
The balance of 0xAa01a1bEF0557fa9625581a293F3AA7770192632 is: 50 Pring.
Last updated on 2/26/2021
← Recharge EVM addressUsing Web3 for contract →
  • Preparation
  • Get Balance
  • Transfer Balance
Links
BlogOfficial WebsiteDarwinia Wallet Apps
Darwinia Documentation
Follow @DarwiniaNetwork
Star
Copyright © 2021 Darwinia Network