[learn from Lele web3 development] i Use the IDE tool webstorm to write web3js

preface

web3 can be written in java language through the web3j dependency of java, but web3j is not complete for abi applications and other support libraries, so it is still somewhat limited.
Therefore, in today's web3 field, we use javaScript based web3 JS library to write web3 applications is the mainstream.
In most textbooks on the market, Web3 JS is written based on vscode, an ide tool. Here I use webStorm.
Before using webstorm to write web3 applications, you need to ensure that nodeJs and npm are installed in your environment.
At the same time, readers need to have a certain foundation of javaScript, so as to make better use of api documents for learning.

My nodejs version: V9 four
npm version: 5.6.0

I Api understanding of web3

On the basis of js, we can realize the fastest interaction with contract and block scanning through the api document of web3.
api documents are recommended to refer to these two:
https://web3js.readthedocs.io/en/v1.7.3/web3-eth-contract.html
https://www.qikegu.com/docs/5132

II What is abi? (important)

According to some similar official statements, abi is explained as follows:

ABI stands for "Abstract Binary Interface / Abstract Binary Interface". It is a JSON array and an executable file for Ethereum virtual machine, which is similar to the binary executable file on windows platform. The ABI file is generated after the smart contract is compiled and can be executed on the Ethereum virtual machine.

Is it hard to understand?
Generally speaking, abi is a json that defines which functions you can call or which variables you can view when interacting with contract.
For example, most contract s in bsc have a function called 'decisions()'. If I want to call it, I need to declare the existence of this function in the form of json in abi before calling it.

This is similar to you reflecting a class in java. If you want to call its method, you need to accurately enter its method name.

III Build web3 project


Customize your project path

Created successfully

Install web3js dependencies
Enter command:

npm install web3


After execution, if there is no error, the installation is successful

IV Write a simple web3 script

Next, let's write a simple script to get its symbol according to the contract address.
Create a new directory called demo


Create a new demo script


The script is as follows:

const Web3 = require('web3'); // Load web3 Library
const web3Bsc = new Web3("https://bsc-dataseed1.binance.org:443 "); / / connect the nodes on the BSC chain through the web3 library, so as to instantiate the web3 node object on the BSC chain.

// Define abi, which can specify which functions or variables of the contract can interact with in the form of json, such as calling its get/set function.
const abi = [{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"constant":true,"inputs":[],"name":"_decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"_name","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"_symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"burn","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"renounceOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}];
// Get token symbol (name)
async function getTokenSymbol(){
    const contractAddress = new web3Bsc.eth.Contract(abi,"0x1d2f0da169ceb9fc7b3144628db156f3f6c60dbe");
    const symbol = await contractAddress.methods.symbol().call();//Execute the 'symbol()' function in the contract.
    console.log(symbol);
}
getTokenSymbol();

implement

The token symbol was successfully obtained and printed

Tags: Blockchain web3 solidity

Posted by cbj4074 on Sat, 16 Apr 2022 05:19:41 +0930