Ethernet Square Intelligent Contract Token application Development (4)-WEB3 client interacts with geth node __ block chain

Source: Internet
Author: User
I. Overview

If there are many ways to develop dapp interaction with the ether, use WEB3 to interact with Geth, WEB3 is an official JS client interaction tool provided by the ether square. You can also introduce web3.js in HTML to interact with Nodejs project references, but this method is less secure and is not recommended for use. Web3.js most commonly used scenarios or server-side programming in Nodejs, can be combined with express and other frameworks to write various forms of application and web interface to the traditional app use

Ii. Environmental Preparedness 1, the use of NPM global installation web3

sudo npm install web3-g
2, a new foundation of the NPM project
mkdir web3
cd web3
NPM init
npm Install
3. Edit Entry File

When you execute the NPM init command, you need to make a version of the project, a portal file, and so on, set up a new entry file by default is Index.js. You can use the following command to perform Index.js

Nodejs Index.js

can also execute JS in interactive mode, directly execute Nodejs can

third, interactive practice 1, the introduction of WEB3 settings provider

WEB3 = require ("web3")
var web3 = new Web3 (web3.givenprovider| | ' http://127.0.0.1:8545 ');
Web3.setprovider (' http://127.0.0.1:8545 ');
2. Query version number
Console.log (' list web3 version: ');
Console.log (web3.version)
3, query support module
Console.log (' list all the modules: ' +web3.modules ');
Getdetail (Web3.modules)
4. Query auxiliary function
Console.log (' list utils: ' +web3.utils);
Getdetail (Web3.utils)
5, query default account
Console.log (' Default account: ')
Console.log (Web3.eth.defaultAccount)

6. Inquire Gas cost

Web3.eth.getGasPrice (). Then (function (r) {
	Console.log (' Display the Gasprice base on the last Block: ');
	Console.log (R)
})
7. Inquiry Account List
Web3.eth.getAccounts (). Then (function (r) {
	Console.log (' Display all accounts: ');
	Console.log (R)
})
8. New Account
Web3.eth.personal.newAccount (' Sunbaolong '). Then (function (r) {
	Console.log (' Create a new account ')
	Console.log (R)
})
9, unlock the account
Web3.eth.personal.unlockAccount (' 0xa5d4725d9dc3f7e73818936abe151602ad6d26fa ', "Sunbaolong"). Then (function (r) {
	console.log (' Unlock account ')
	Console.log (R)
})
10, unlock the account
Web3.eth.sendTransaction ({
    from: ' 0xa5d4725d9dc3f7e73818936abe151602ad6d26fa ') to
    : ' 0x09f90ceff015e0610a9b9b7d0e3f7c498e0b0f06 ',
    value: ' ('
) '. Then (function (r) {
   Console.log (' Transaction is send ');
   Console.log (R)
});
Other features please practice yourself


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.