Interaction of Ethernet square with nodes JSON RPC API and Web3.js

Source: Internet
Author: User
Tags curl

The Go-ethereum client is built, and after it is done, we can interact with the nodes in various ways (JavaScript Console, JSON-RPC, WEB3, etc.). More than just the etheric square, the nodes in the block chain typically provide interfaces, and are the interface of the JSON RPC, most of which are interacting through JSON RPC and nodes. Use JSON RPC to tune functionality and pass data.
Actually went to call some of its ways. By exposing some of the JSON RPC protocols and then going to call some interfaces, this is essentially how the data is read and accessed. JSON RPC is just a transport channel, and the Ethernet and IPC interfaces. By running the nodes of the etheric Square, a pipeline of interprocess communication will be established to do things through pipelines. Will not be coupled, just replacing the HTTP pipe with a UNIX pipeline.
In some application scenarios, in order to access the Ethernet block chain, the business system needs to be tuned to the API of the Tai Fong client, sending the user's transaction data to the Tai Fong platform. Ethernet Square The client has provided RPC and IPC Two kinds of API calls for external systems.

in the case of RPC, the format requested using the Curl command is as follows:
To invoke the client command:
Let's say we want to invoke the client command eth.getbalance () to query the balance of the account with the 0x407 address, as follows:

Curl--data ' {"JSONRPC": "2.0", "Method": "Eth_getbalance", "params": ["0x407", "latest"], "id": 1} ' localhost:8123

Where: The Jsonrpc field specifies the JSON-RPC version number, the Method field specifies the API name to invoke, the params field is the passed parameter, and the ID is the Message identity field;

Invoke Contract Method:
Assuming that there is currently a deployed smart contract with address 0x6ff93, the contract method we are calling is signed as multiply (uint256) and the parameter value passed in is 6, then the format of the invocation command is as follows:

Curl--data
{
"JSONRPC": "2.0",
"method": "Eth_sendtransaction",
"params" [{
    "from": "0xeb85a5" ,
    "to": "0x6ff93",
    "Data": "0XCDDDDD"
}]
"id": 8}
localhost:8123

Where from is the account address for deducting gas, the address to be deployed for the smart contract, the data is the signature of the calling method and the incoming parameter, encoded by:

"0x" +sha3 ("Multiply (uint256)"). Substring (0,8) +to_32bit_hex_str (6)

The implementation of the TO_32BIT_HEX_STR () method differs depending on the variable type, and the specific rules refer to the Ethereum Contract ABI document (here).

As you can see from the example above, the call to the smart contract from the outside requires complex coding. Fortunately, at present, the ether Square provides the official use of JavaScript language implementation of the Web3.js module, RPC and IPC two kinds of calls are encapsulated, external provide a concise interface, very convenient to use.

The following article uses the Web3.js module to implement a call to an intelligent contract in a NODEJS environment and to provide an external restful API. Details are as follows:
System environment:
Ubuntu14.04.4+node V5.1.1;
node modules that need to be introduced
Express, WEB3, NET, you can use NPM command to install dependencies;
Main code Fragment

Introducing Module Dependencies

var Express =require (' Express ');
var web3=require ('  Web3 ');
var net=require (' net ');

Setting up RPC and IPC connection objects
See here to write link content
Invoke the Ethernet square client method to query the balance of the specified account

New Ethernet account, this method must use the way the IPC API is invoked

New contract object, where Contractdata is compiled for contract code in 16-string

Invoking the Smart Contract method

Open HTTP Server

Finally, using node to start the service, the "Example app listening at http://:::9090" service was started successfully. This allows the user to interact with the Ethernet Square client via restful HTTP requests and invoke the smart contract.

The

calculates programming. The calculation of the application is actually divided into two kinds, 1. The calculations that run on the EVM are also the core objectives of the etheric square. These high-level languages use what you have just said, such as solidity, written out to be a contract or a procedure. A contract is equivalent to the concept of a class, in which there are many function equivalent to many of the method in class. After the contract is written, you can invoke the JSON RPC interface, compile it into the opcodes on the EVM, and then deploy it to the Ethernet network so that the etheric square can carry out the contract and run the program. 2. Use JS or traditional methods to write things, because a program is not all need to put on the block chain, perhaps only your most concerned about the business logic such as transfer needs to be placed in the block chain to ensure its open and transparent executable. Others, such as showing the amount in the account, can send a request to the node via the other interface of the JSON RPC, telling me how much the balance is in the account on this address. The received response will contain this information and will be displayed on the interface. If you write some logic on a block chain, analogy to a database is a bit like Procedure,trigger, actually call the stored procedure, it does some operations, and then put the results on the database. There are two types of calculations, not all of which are assigned to the block chain. So suppose I have an ordinary Web site with a server behind it, a database and a blockchain. Some things exist in databases, some exist blockchain, and there is no problem, because they meet different needs. So you just need to prove something important that can't be changed on the blockchain, something that is relevant to the internal business, something less important but often read can be put in the database. So it's a weapon for the developers.
External calls to smart contracts

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.