Giter VIP home page Giter VIP logo

eosjs's Introduction

⚠️ Important! We have recently released a major breaking rewrite for eosjs. Be sure to lock your dependencies. ⚠️

If you are looking for the the previous version of eosjs you can find it here.

eosjs

Javascript API for integration with EOSIO-based blockchains using EOSIO RPC API.

Documentation can be found here

Installation

NodeJS

npm install eosjs@beta

Basic Usage

NodeJS

const eosjs = require('eosjs');
const fetch = require('node-fetch');                            // node only; not needed in browsers
const { TextDecoder, TextEncoder } = require('text-encoding');  // node, IE11 and IE Edge Browsers

SignatureProvider

SignatureProvider holds private keys and is responsible for signing transactions

const defaultPrivateKey = "5JtUScZK2XEp3g9gh7F8bwtPTRAkASmNrrftmx4AxDKD5K4zDnr"; // useraaaaaaaa
const signatureProvider = new eosjs.SignatureProvider([defaultPrivateKey]);

JSON-RPC

Open a connection to JSON-RPC, include fetch when on NodeJS

const rpc = new eosjs.Rpc.JsonRpc('http://127.0.0.1:8000', { fetch });

API Constructor

Include textDecoder and textEncoder when using in browser.

const api = new eosjs.Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() });

Sending a transaction

(async () => {
  const result = await api.transact({
    actions: [{
      account: 'eosio.token',
      name: 'transfer',
      authorization: [{
        actor: 'useraaaaaaaa',
        permission: 'active',
      }],
      data: {
        from: 'useraaaaaaaa',
        to: 'useraaaaaaab',
        quantity: '0.0001 SYS',
        memo: '',
      },
    }]
  }, {
    blocksBehind: 3,
    expireSeconds: 30,
  });
  console.dir(result);
})();

Error handling

use eosjs_jsonrpc.RpcError for handling JSON-RPC Errors

...
try {
  const result = await api.transact({
  ...
} catch (e) {
  console.log('\nCaught exception: ' + e);
  if (e instanceof eosjs_jsonrpc.RpcError)
    console.log(JSON.stringify(e.json, null, 2);
}
...

Browsers

After running npm run build-web or yarn build-web, the browser distribution will be located in dist. For full browser usage examples, see the documentation.

How it works

transact() is used to sign and push transactions onto the blockchain with an optional configuration object parameter. This parameter can override the default value of broadcast: true, and can be used to fill TAPOS fields given blocksBehind and expireSeconds. Given no configuration options, transactions are expected to be unpacked with TAPOS fields (expiration, ref_block_num, ref_block_prefix) and will automatically be broadcast onto the chain.

Running Tests

Automated Test Suite

npm run test or yarn test

Integration Tests

  1. npm run build-web or yarn build-web
  2. Open test.html in your browser of choice

The integration tests assume that you have a local node for EOS set up at localhost:8000. The test.html file should run through 5 test cases with the final showing an exception on the screen for missing required TAPOS.

eosjs's People

Contributors

tbfleming avatar jeffreyssmith2nd avatar brandonfancher avatar randytorres avatar chris-allnutt avatar esheffield avatar dskvr avatar sergmetelin avatar c0d3ster avatar flux627 avatar bhazzard avatar christiandunst avatar altingfest avatar nsjames avatar rickykung avatar

Watchers

Ilia Groshev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.