Version : 1.0.0-beta.36
- TokenGazer: Ethereum May monthly comment
- Market analysis: the rise has just begun? How much will ETH rise
- The financial structure of the Ethereum Ecology: How does it work?
- Which Ethereum successor is strong? ——A picture to understand the blockchain
- Large-scale innovation of open financial applications on Ethereum, how does each application build this ecological space?
- Seeing this is equal to the vitality and chaos of Devon5: ETH2.0
Web3.js API type
- 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;
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.
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)