To easily understand what EOSJS is, think of it as a library that was made to easily control the EOS blockchain with Javascript. To go a little bit deeper, it can be said that it was made to easily use EOS’s (nodeos) HTTP API. (web3.js in Ethereum, neon-js in Neo)

Because EOSJS also uses the HTTP API from EOS, BP selection is crucial. (Response time, availability, etc.)

Install

There are two ways to install.

Simple installation with NPM

> npm install eosjs

Use of CDN

EOS Connect

EOS Connect using EOSJS

If EOS is not running on localhost, you will get the following error if you run the above code. (Having the wrong httpEndpoint of EOS to connect will also have the same error as below.)



at ClientRequest.<anonymous> (/Users/mayajuni/Projects/eos-scan/node_modules/node-fetch/index.js:133:11)

at ClientRequest.emit (events.js:182:13)

at Socket.socketErrorListener (_http_client.js:382:9)

at Socket.emit (events.js:182:13)

at emitErrorNT (internal/streams/destroy.js:82:8)

at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)

at process._tickCallback (internal/process/next_tick.js:63:19)

name: 'FetchError',

message:

'request to

type: 'system',

errno: 'ECONNREFUSED',

code: 'ECONNREFUSED' } { FetchError: request to http://127.0.0.1:8888/v1/chain/get_info failed, reason: connect ECONNREFUSED 127.0.0.1:8888at ClientRequest. (/Users/mayajuni/Projects/eos-scan/node_modules/node-fetch/index.js:133:11)at ClientRequest.emit (events.js:182:13)at Socket.socketErrorListener (_http_client.js:382:9)at Socket.emit (events.js:182:13)at emitErrorNT (internal/streams/destroy.js:82:8)at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)at process._tickCallback (internal/process/next_tick.js:63:19)name: 'FetchError',message:'request to http://127.0.0.1:8888/v1/chain/get_info failed, reason: connect ECONNREFUSED 127.0.0.1:8888',type: 'system',errno: 'ECONNREFUSED',code: 'ECONNREFUSED' }

Mainnet Connect

If you try to connect like above, an error message as below will appear.

Even if an error appears, getInfo is possible, so let’s check and enter chainId using this.

In the case of Mainnet, aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906 is the common chainId, so enter this.

If you connect using the above method, the first get_info api is called once, as shown below. (At first, I didn’t know very well and gave a connect every time, which caused a tremendous amount of data usage on mobile L )

You can check the mainnet address at https://api.eosnewyork.io. Because every BP has a different amount of information they offer, response time, etc., it is critical to find and choose the BP that fits your needs. (There also is the method of directly getting the full node and connecting).

Connect Config

Detailed description of this part is on github. (github-configuration)

getInfo — api

You can get the basic network information of EOS. The code is as shown below.

Fun fact: Other APIs are all Promise-based, but only getInfo is callback method.

The return value is as shown below.

{

server_version: '36a043c5',

chain_id:

'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',

head_block_num: 5469047,

last_irreversible_block_num: 5468711,

last_irreversible_block_id:

'00537227a657d1f4fd74de877e9ad1a3839540ece45eedeaf177b20e51b2da1b',

head_block_id:

'005373770504c59e992214b3056c7bdabb07c53c5a9c4785909b90fe595a119f',

head_block_time: '2018-07-12T08:58:05.500',

head_block_producer: 'helloeoscnbp',

virtual_block_cpu_limit: 200000000,

virtual_block_net_limit: 1048576000,

block_cpu_limit: 199900,

block_net_limit: 1048576

}

If you do not know the chainId mentioned above (EOS Connect), you can get and use the chainId via getInfo.

This post was written lightly because it is just the first step of using EOSJS’s install, connect, and getInfo. We will look at EOSJS more deeply in the future.