Skip to content

wanchain/iwan-js-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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", // for mainnet use --> api.wanchain.org
    port:8443,
    flag:"ws",
    version:"v3",
    timeout:300000
};
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'.
    • timeout {Number} The RPC method timeout, default is 30000 (ms).

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