Ethereum and Node Interaction JSON RPC API and Web3.js

The Go-ethereum client is built. After completion, we can interact with the node in various ways (JavaScript Console, JSON-RPC, web3, etc.). Not only Ethereum, but blockchain nodes generally provide some interfaces, and are JSON RPC interfaces, most of which interact with nodes through JSON RPC. Call functions and transfer data through JSON RPC.
Some ways to actually call it. By exposing some JSON RPC protocols, and then calling some interfaces, this is essentially the way to complete data reading and access. JSON RPC is just a transmission channel, and Ethereum also has an IPC interface. After running the Ethereum node, an inter-process communication pipeline will be established, and things will be done through the pipeline. No coupling, just replace the http pipe with the Unix pipe.
In some application scenarios, in order to access the Ethereum blockchain, the business system needs to call the api of the Ethereum client to send the user's transaction data to the Ethereum platform. The Ethereum client has provided RPC and IPC API calls for external systems.

Taking the RPC method as an example, the format of the curl command request is as follows:
Call client command:
Suppose we want to call the client command eth.getBalance() to query the balance of the account whose address is 0x407. The command is as follows:

curl --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x407", "latest"],"id":1}' localhost:8123

Among them: the jsonrpc field specifies the JSON-RPC version number, the method field specifies the name of the api method to be called, the params field is the transmitted parameter, and the id is the message identification field;

Calling the contract method:
Assuming that there is currently a deployed smart contract, the address is 0x6ff93, the signature of the contract method we want to call is multiply(uint256), and the incoming parameter value is 6, then the format of the calling command is as follows:

curl --data
{
"jsonrpc":“2.0”,
"method":“eth_sendtransaction”,
"params"[{
    "from":"0xeb85a5",
    "to":"0x6ff93",
    "data":"0xcddddd"
}]
"id":8}
localhost:8123

Among them, from is the account address that deducts GAS, to is the address of smart contract deployment, data is the signature and incoming parameters of the calling method, and the encoding method is:

“0x”+sha3(“multiply(uint256)”).substring(0,8)+to_32bit_Hex_str(6)

The implementation of the to_32bit_Hex_str() method will vary according to different variable types. For specific rules, please refer to the Ethereum Contract ABI document (HERE).

As you can see from the above example, calls to smart contracts from the outside require complex coding. Fortunately, at present, Ethereum officially provides the web3.js module implemented in javascript language, which encapsulates both RPC and IPC calling methods, and provides a concise interface to the outside world, which is very convenient to use.

The following article will use the web3.js module to implement calls to smart contracts in the nodejs environment and provide RESTful apis to the outside world . The details are as follows:
System environment:
Ubuntu14.04.4+Node V5.1.1;
Node modules
express, web3, and net that need to be introduced can be installed by using the npm command;
main code snippets

import module dependencies

var express =require('express');
var Web3=require('  Web3');
var net=require('net');

Set the RPC and IPC connection
objects . See the content of the link here.
Call the Ethereum client method to query the balance of the specified account.

Create a new Ethereum account, this method must use the calling method of IPC api

Create a new contract object, where contractData is the compiled hexadecimal string of the contract code

call smart contract method

open http server

Finally, use node to start the service, and see the "Example app listening at http://:::9090 " service starts successfully. In this way, users can interact with the Ethereum client through RESTful http requests and call smart contracts.

Computing programming? The calculation of the application is actually divided into two types. 1. The calculation running on the evm is also the core goal of Ethereum. These high-level languages ​​are written using what was just said, such as solidity, which is a contract or a program. A contract is equivalent to the concept of a class, and there can be many functions in it that are equivalent to many methods in the class. After the contract is written, you can call the JSON RPC interface, compile it into opcodes on evm, and then deploy it to the Ethereum network, so that Ethereum can execute the contract and run the program. 2. Things written in JS or traditional methods, because not everything in a program needs to be placed on the blockchain, maybe only the business logic you care about most, such as transfer, needs to be placed on the blockchain to ensure its openness, transparency and availability. implement. Others, such as displaying the amount in the account, can send a request to the node through another interface of JSON RPC to tell me the balance in the account at this address. In this way, the received response will contain this information and will be displayed on the interface. If you write some logic on the blockchain, and compare it to a database, it is a bit like procedure and trigger. In fact, you call the stored procedure, it performs some operations, and then puts the result on the database. There are two types of computations, and not all computations are handed over to the blockchain. So let's say I have a normal website with a server behind it, a database and a blockchain behind it. Some things exist in the database, some exist in the blockchain, and there is no problem because they meet different needs. So it only needs to prove that some important things that cannot be changed on the blockchain, some related to internal business, and some less important but frequently read can be placed in the database. So it is an extra weapon for developers.
External calls to smart contracts

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325405042&siteId=291194637