Technical Guide | Web3.js is based on the Ethereum Javascript API
Web3.js is a collection of libraries that you can use to connect to a local or remote Ethernet node using HTTP or IPC. The web3 JavaScript library can interact with the Ethereum blockchain. It can retrieve user accounts, send transactions, interact with smart contracts, and more.
Version : 1.0.0-beta.36
Web3.js API type
- Some science about Ethereum 2.0
- How is practical programmability implemented in the securities pass?
- Decred technical white paper Chinese version
- Eth : Etherum blockchain related method;
- Net: the network status of the node;
- Personal: account function and sending;
- Db: Get/place the local LevelDB;
- Shh: use Whisper's P2P messaging;
Install Web3.js
Node
Instantiate web3
First, we check if the dapp browser or the updated version of MetaMask is used to inject the Ethereum provider into the window object. If so, we use it to create our web3 object, but we also need to explicitly request access to the account using ethereum.enable().
If the ethereum object does not exist, we will check the injected web3 instance. If it exists, it means that we are using an older dapp browser (such as Mist or an older version of MetaMask). If so, we can get its provider and use it to create our web3 object.
If no web3 instance is injected, we will create a web3 object based on the local provider. (This backup is suitable for development environments, but it is not safe and not suitable for production environments.)
PS: web3.version.getNode is not recommended.
Balance
Check the user's balance
- web3.eth.getAccounts: Obtain an account from the Ethereum node. Web3.eth.accounts is deprecated.
- web3.eth.getBalance: Get the balance of the account.
- web3.utils.fromWei: Convert wei to ether, web3.fromWei is not recommended.
Transfer Ethernet from "A" to "B"
txnObject (transaction object) is the first parameter of web3.eth.sendTransaction. txnObject consists of JSON.
From:String – The address of the sending account.
To:String – the target wallet address.
Value:Number | String | BigNumber – (Optional) The number of Ethernets you want to send to the destination address.
Gas:Number | String | BigNumber – (optional, default: to be determined) The amount of gas used for the transaction (unused gas will be refunded).
gasPrice:Number | String | BigNumber – (optional, default: pending) The price of the gas in this transaction in wei. The default is the average network gas price.
Data:String – (Optional) A byte string containing the associated data for the message, or the initialization code if the contract creates a transaction.
Nonce:Number – (Optional) An integer of nonce. This allows you to override your own pending transactions that use the same nonce.
I made a web page using the above method.
- Get Ethereum node information
- Get the user's balance
- Transfer of Ethereum
You will be using MetaMask or Ganache. I recommend that you use Ganache, a personal blockchain developed for Ethereum that can be used to deploy contracts, develop applications, and run tests. If you haven't already done so, download Ganache and double-click the icon to install the app. This will generate a blockchain that runs locally on port 7545 while changing the port to 8545.
(Source: Public Number: Blockchain Research Laboratory)
We will continue to update Blocking; if you have any questions or suggestions, please contact us!
Was this article helpful?
93 out of 132 found this helpful
Related articles
- Elliptic Curve Cryptography & Workload Proof
- Technical Guide | Ethereum Wallet Development: Generate Account Address, Private Key, Keystore, mnemonic
- Restoring the entire process of asset transfer: $6.02 million stolen from the DragonEX exchange
- Popular Science | Hash Function Brief
- The first long-term supported version, Hyperledger launches Fabric v1.4 LTS
- In-depth understanding of IPFS (1): Beginner to Advanced Guide
- Technical Guide | Ethereum Wallet Development: Wallet Address Generation Process