Ethernet Square Development Document 09-JAVASCRIPTAPI

Source: Internet
Author: User
Web3 Javascriptðappapi

In order for your application to work on the etheric square, you can use the objects provided by the Web3.js library WEB3. At the bottom, it communicates with the local node through RPC calls. The web3.js works with any of the etheric square nodes that expose RPC layers.

WEB3 contains ETH objects-Web3.eth (dedicated to the Ethernet block chain interaction) and Shh objects-web3.shh (for whisper interaction). Over time, we'll introduce other objects for other WEB3 protocols. A working example can be found here.

If you want to use Web3.js to view some of the more complex examples, check out these useful application patterns. using callbacks

Because this API is intended to work with the local RPC node, all of its features use the synchronous HTTP Requests.con by default

If you want to do an asynchronous request, you can pass an optional callback function as the last argument. All callback functions use the wrong first callback style:

Web3.eth.getBlock (the function (error, result) {
    if (!error)
        console.log (Result)
    else
        Console.error (error);
})
Bulk Request

Bulk requests allow queued requests and process them at once.

var batch = Web3.createbatch ();
Batch.add (web3.eth.getBalance.request (' 0x0000000000000000000000000000000000000000 ', ' latest ', callback));
Batch.add (Web3.eth.contract (ABI). at (address)-balance.request (address, Callback2));
Batch.execute ();
a description of large numbers in Web3.js

You will always get a BigNumber object with a balanced value, because JavaScript cannot handle large numbers correctly. Look at the following example:

"101010100324325345346456456456456456456"
//"101010100324325345346456456456456456456"
101010100324325345346456456456456456456
//1.0101010032432535e+38

Web3.js relies on the BigNumber library and automatically adds it.

var balance = new BigNumber (' 131242344353464564564574574567456 ');
or var balance = web3.eth.getBalance (someaddress);

Balance.plus. toString (10); ToString (converts) it to a number string
//"131242344353464564564574574567477"

The next example does not work because we have more than 20 floating points, so it is recommended that you in total Balance Wei, just convert it to other units when presented to the user:

var balance = new BigNumber (' 13124.234435346456466666457455567456 ');

Balance.plus. toString (10); ToString (converts) it to a number string, but can only show Max floating points 
//"13145.2344353464564666664 6 "//You number would is cut on the floating point
Web3 JAVASCRIPTÐAPPAPI Reference WEB3 version API Customer network revenge whisper connected () Setprovider (supplier) Currentprovider reboot () () () () Tohex (StringOrNumber) Toascii will (hexadecimal string) fromascii (Textstring,[padding]) todecimal (hexadecimal string) fromdecimal (number) Fromwei (numberstringorbignumber,unit) Towei (numberstringorbignumber, Unit) tobignumber (numberorhexstring) isaddress (hexadecimal string) Net listen/getlistening PeerCount/ Getpeercount ETH defaultaccount defaultblock sync/getsyncing issyncing coinbase/getcoinbase hashrate/gethashrate Ce/getgasprice account/getaccounts Mining/getmining blocknumber/getblocknumber Register (hexstring) (not yet implemented) unregister (hexString) (not yet Implementation) is the GetBalance (address) getstorageat (address, location) reference code (address) Getblock (hash/number) Getblocktransactioncount (hash/number) getuncle (hash/number) Getblockunclecount (hash/number) gettransaction (scatter) gettransactionfromblock (Hashornumber,indexnumber) Gettransactionreceipt (hash) gettransactioncount (address) Sendtransaction (object) Call (object) Estimategas (object) filter (array (, options)) Watch (callback) stopwatching (callback) get () contract (Abiarray) Contract.mymethod () contract.myevent () contract.allevents () GetcompiLers () compile.lll (String) compile.solidity (String) compile.serpent (string) namereg sendibantransaction IBAN fromaddress FromBban CreateIndirect verified Isdirect isindirect check mechanism customer address D b putstring (name, key, value) getString (name, key) Puthex (name, key) Gethex (name, keys) after Hush (PO Stobject) newidentity () hasidentity (hexadecimal string) newgroup (_id,_who) addtogroup (_id,_who) Filter (object/String) watch (callback) stopwatching (callback) Get (a callback) usage WEB3

The Web3 object provides all of the methods. cases

var Web3 = require (' web3 ');
Create an instance of WEB3 using the HTTP provider.
Note In mist web3 are already available, so check the ' it available before instantiating
var web3 = new Web3 (NE W Web3.providers.HttpProvider ("http://localhost:8545"));
Web3.version.api
Web3.version.api
//or Async
Web3.version.getApi (Callback (error, result) {...})
return

String-Ethernet Square JS API version. cases

var version = Web3.version.api;
Console.log (version); "0.2.0"
web3.version.client
Web3.version.client
//or Async
web3.version.getClient (Callback (error, result) {...})
return

String-Client/node version. cases

var version = Web3.version.client;
Console.log (version); "mist/v0.9.3/darwin/go1.4.1"
web3.version.network
Web3.version.network
//or Async
Web3.version.getNetwork (Callback (error, result) {...})
return

String-Network protocol version. cases

var version = Web3.version.network;
Console.log (version); 54
Web3.version.ethereum
Web3.version.ethereum
//or Async
Web3.version.getEthereum (Callback (error, result) {...})
return

String-Ethernet Square protocol version. cases

var version = Web3.version.ethereum;
Console.log (version); 60
Web3.version.whisper
Web3.version.whisper
//or Async
Web3.version.getWhisper (Callback (error, result) {...})
return

String-Whisper protocol version. cases

var version = Web3.version.whisper;
Console.log (version); 20
web3.isconnected
Web3.isconnected ()

Should be called to check whether a node's connection has parameters

Did not return

Boolean Example

if (!web3.isconnected ()) {

   //Show some dialog to ask the user to start a node

} else {

   //start WEB3 Filters , calls, etc

}
Web3.setprovider
Web3.setprovider (provider)

should be referred to as the provisioning provider. Parameters

Did not return

Undefined case

Web3.setprovider (New Web3.providers.HttpProvider (' http://localhost:8545 ')); 8080 for Cpp/az, 8545 for Go/mist
Web3.currentprovider
Web3.currentprovider

Will contain the current provider, if any. This can be used to check whether the fog has been provided by providers. return

object-provider setting or null; cases

Check if mist etc. already set a provider
if (!web3.currentprovider)
    Web3.setprovider (new Web3.providers.HttpProvider ("http://localhost:8545"));
Web3.reset
Web3.reset (keepissyncing)

The state of the reset web3 should be called. Resets everything except the manager to uninstall all filters. Stop polling. Parameters boolean-If true it will unload all the filters but will keep the web3.eth.isSyncing () poll returned

Undefined case

Web3.reset ();
WEB3.SHA3
WEB3.SHA3 (string [, Callback])
ParametersString-strings Function hash using the SHA3 algorithm-(optional) If a callback is passed, the HTTP request becomes asynchronous. Please refer to this note for details. return

String-The SHA3 of the given data. cases

var str = WEB3.SHA3 ("Some ASCII string to be hashed");
Console.log (str); "0x536f6d6520415343494920737472696e6720746f20626520686173686564"

var hash = web3.sha3 (str);
Console.log (hash); "0xb21dbc7a5eb6042d91f8f584af266f1a512ac89520f43562c6c1e37eab6eb0c4"
Web3.tohex
Web3.tohex (mixed);

Converts any value to hex. Parameters String| number| Object| array| BigNumber-resolves to a hex value. If it is an object or an array, it will be the first json.stringify. If it is a bignumber, it will make it a hex value for a number. <

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.