Giter VIP home page Giter VIP logo

iwan-js-sdk's Introduction

iwan-js-sdk

GitHub License

JavaScript SDK for iWan RPC Server

Install

Use NPM or Yarn to install the library:

npm install --save iwan-sdk

Config

After installation, the iWan SDK can be used to connect to the iWan RPC server to call a method such as getBalance. The default config can be used or custom config parameters can be passed using the option object.

const iWanClient = require('iwan-sdk');

By default the SDK will connect to "api.wanchain.org:8443"

let apiClient = new iWanClient(YourApiKey, YourSecretKey);

A different URL can be specified in the option object which is subject to iWan.

//Subject to https://iwan.wanchain.org
let option = {
    url:"apitest.wanchain.org",
    port:8443,
    flag:"ws",
    version:"v3"
};
apiClient = new iWanClient(YourApiKey, YourSecretKey, option);

Th client should be closed after all operations.

apiClient.close();

Instead of using the iWan SDK for connecting to the iWan RPC server, a raw WebSocket API can also be used, for more information, please see the documentation iWan RPC API. However, we strongly recommend using the iWan SDK.

Details about option

The SDK object can accept an option object. See below for examples of usage.

  • option {Object}
    • url {String} The RPC server URL, default is 'api.wanchain.org'.
    • port {Number} The RPC server port, default is 8443.
    • flag {String} The flag to connect the iWan RPC server, default is 'ws'.
    • version {String} The RPC method version, default is 'v3'.

ApiKey and SecretKey

In order to get an ApiKey, sign up at iWan. Then create a new project to get a new ApiKey and SecretKey key pair.

Basic Usage

Both Promise and callback are supported for each method.

  • callback {Function}
    • err {String} in case of error, error details will be stored in err, err will contain null otherwise.
    • result {Object} if successful (in other words err is null), the result object will contain the result of the method called, such as getBalance.

The method getBalance is used as an example below to show the use of callback and Promise in the iWan SDK :

Callback

callback can be used for asynchronous mode:

apiClient.getBalance('WAN', '0x0cc79fa3b80c5b9b02051facd02478ea88a78e2c', (err, balance) => {
  if (err) {
    console.log(err);
  } else {
    console.log("Balance result is ", balance);
  }
});

Promise

Promise can be used for synchronous mode:

try {
  let balance = await apiClient.getBalance('WAN', '0x0cc79fa3b80c5b9b02051facd02478ea88a78e2c');
  console.log("Balance result is ", balance);
} catch (err) {
  console.log(err);
}

Examples

Development

  1. git clone https://github.com/wanchain/iWan-js-sdk.git
  2. npm install
  3. npm test

Documentation

iWan SDK API : API details about iWan SDK

iwan-js-sdk's People

Contributors

gabriel-2010 avatar gao623 avatar lansehuiyi6 avatar noahmaizels 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.