Develop ethereum blockchain with web3 and infura

 

web3

Github:  https://github.com/ethereum/web3.js/ 
web3.js is a Javascript library provided by Ethereum. It encapsulates the RPC communication API of Ethereum and provides a series of methods for interacting with the blockchain, enabling js to interact with Ethereum made simple.

infura

Official website:  https://infura.io/ 
The method of installing geth locally requires a lot of time and space to synchronize blocks. It is much simpler to use infura. Infura provides public Ethereum and test nodes, which can be accessed using the API provided by infura. Ethereum and IPFS. To go to the official website, you only need to provide the email registration link.

Developed with web3 and infura

The most common operation is to check the ether balance of an ethereum address (similar to etherscan).

Introduce web3 through npm or other means, and use infura to provide mainnet/testnet for initialization.

// xxxx为你在infura申请的地址
web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/xxxxxxxx"));
  • 1
  • 2
  • 3

Next, you can call the web3 interface, such as getting the number of ethers of an address

// wei是以太坊上的的最小单位,ether小数点后18位为一个wei
var balanceWei = web3.eth.getBalance("0xC257274276a4E539741Ca11b590B9447B26A8051").toNumber();
// 从wei转换成ether
var balance = web3.fromWei(balanceWei, 'ether');
  • 1
  • 2
  • 3
  • 4

At this point, you can perform operations from the Ethereum mainnet, such as viewing block information, deploying smart contracts, etc. 
For specific development, please refer to the Ethereum JS API:  https://github.com/ethereum/wiki/wiki/JavaScript-API 
demo Simple etherscan 
block height
View ETH address
View TX

Demo Github: https://github.com/wlchn/ethersee

Guess you like

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