Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 286 lines (226 sloc) 11.3 KB

Zilliqa-JavaScript-Library API

Build Status codecov Gitter chat lerna


zilliqa-js is structured as a Lerna monorepo. Each package roughly corresponds to a discrete ZilliqaModule, most of which can be used independently.

The only required package is @zilliqa-js/core, which contains the default HTTPProvider, and other core abstractions that are necessary for other modules to function.

The following table provides a description of each module and what you may want to use it for.

package description dependencies
@zilliqa-js/core Core abstractions and base classes, such as HTTPProvider and network logic for interfacing with the Zilliqa JSON-RPC. none
@zilliqa-js/account Wallet, Account and Transaction abstractions live in this package. @zilliqa-js/core, @zilliqa-js/crypto, @zilliqa-js/util, @zilliqa-js/proto
@zilliqa-js/blockchain Main interface to the Zilliqa JSON-RPC. none
@zilliqa-js/contract Exposes a Contracts module that takes care of smart contract deployment and interaction. @zilliqa-js/account, @zilliqa-js/blockchain, @zilliqa-js/core, @zilliqa-js/util
@zilliqa-js/crypto Exposes several loosely-coupled cryptographic convenience functions for working with the Zilliqa blockchain and its cryptographic primitives, such as Schnorr signatures. @zilliqa-js/util
@zilliqa-js/proto Protobuf source files and corresponding generated JS modules. none
@zilliqa-js/util Miscellaneous functions that take care of serialisation/deserialisation and validation. none


It is recommended that developers install the JavaScript client by making use of the umbrella package @zilliqa-js/zilliqa. This takes care of bootstrapping the various modules, which are then accessible as members of the Zilliqa class.

yarn add @zilliqa-js/zilliqa
# bn.js should be added with the above package. if it is not, install it manually.
yarn add bn.js

As this library is still in a relatively early stage of development, we recommend using the next tag to install it in NPM. This will ensure that you receive the latest published version, as it may not be ideal to wait for a semver bump before being able to take advantage of new features/bug fixes.

Quick Start

const { Transaction } = require('@zilliqa-js/account');
const { BN, Long, bytes } = require('@zilliqa-js/util');
const { Zilliqa } = require('@zilliqa-js/zilliqa');
const CP = require ('@zilliqa-js/crypto');

const zilliqa = new Zilliqa('');

// These are set by the core protocol, and may vary per-chain.
// These numbers are JUST AN EXAMPLE. They will NOT WORK on the public testnet
// or mainnet. Please check what they are before proceeding, or your
// transactions will simply be rejected.
const CHAIN_ID = 88;
const MSG_VERSION = 8;
const VERSION = bytes.pack(88, 8);

// Populate the wallet with an account
privkey = '3375F915F3F9AE35E6B301B7670F53AD1A5BE15D8221EC7FD5E503F21D3450C8';


add = CP.getAddressFromPrivateKey(privkey);
console.log('Your account address is:');

async function testBlockchain() {
  try {
    // GetBalance
    const balance = await zilliqa.blockchain.getBalance(add);
    console.log('Your account balance is:');

    // Send a transaction to the network
    const tx = await zilliqa.blockchain.createTransaction({
        version: VERSION,
        toAddr: '573EC96638C8BB1C386394602E1460634F02ADDA',
        amount: new BN(888888),
        gasPrice: new BN(100),
        gasLimit: Long.fromNumber(1),
    console.log("The transaction status is:");

    // Deploy a contract
    const code = `scilla_version 0

    (* HelloWorld contract *)
    import ListUtils
    (*               Associated library                *)
    library HelloWorld
    let one_msg = 
      fun (msg : Message) => 
      let nil_msg = Nil {Message} in
      Cons {Message} msg nil_msg
    let not_owner_code = Int32 1
    let set_hello_code = Int32 2
    (*             The contract definition             *)
    contract HelloWorld
    (owner: ByStr20)
    field welcome_msg : String = ""
    transition setHello (msg : String)
      is_owner = builtin eq owner _sender;
      match is_owner with
      | False =>
        msg = {_tag : "Main"; _recipient : _sender; _amount : Uint128 0; code : not_owner_code};
        msgs = one_msg msg;
        send msgs
      | True =>
        welcome_msg := msg;
        msg = {_tag : "Main"; _recipient : _sender; _amount : Uint128 0; code : set_hello_code};
        msgs = one_msg msg;
        send msgs
    transition getHello ()
        r <- welcome_msg;
        e = {_eventname: "getHello()"; msg: r};
        event e
    const init = [
      // this parameter is mandatory for all init arrays
        vname : '_scilla_version',
        type : 'Uint32',
        value : '0',
        vname: 'owner',
        type: 'ByStr20',
        // NOTE: all byte strings passed to Scilla contracts _must_ be
        // prefixed with 0x. Failure to do so will result in the network
        // rejecting the transaction while consuming gas!
        value: `0x${add}`,

    // Instance of class Contract
    const contract =, init);

    // Deploy the contract
    const [deployTx, hello] = await contract.deploy({
      version: VERSION,
      gasPrice: new BN(100),
      gasLimit: Long.fromNumber(2500),

    // Introspect the state of the underlying transaction
    console.log('Deployment Transaction ID: ',;
    console.log('Deployment Transaction Receipt: ', deployTx.txParams.receipt);

    // Get the deployed contract address
    console.log('The contract address is:');
    const callTx = await'setHello', [
        vname: 'msg',
        type: 'String',
        value: 'Hello World'
        // amount, gasPrice and gasLimit must be explicitly provided
        version: VERSION,
        amount: new BN(0),
        gasPrice: new BN(100),
        gasLimit: Long.fromNumber(2500),

    //Get the contract state
    const state = await hello.getState();
    console.log('The state of the contract is:');
  } catch (err) {


API Documentation and examples

Each package contains its own examples and API documentation. For convenience, these are links to the respective README documents.


This repository makes use of several technologies to provide a better and faster development experience for contributors, and has to be bootstrapped before one can do productive work.


zilliqa-js leverages Project References, which is available in TypeScript from version 3.x. As such, the build process is slightly different.

# install all dependencies and shared devDependencies
yarn install

# symlink packages, compile TS source files, and generate protobuf files.
yarn bootstrap

# watch TS source files and recompile on change
yarn build:ts -w


Tests for each package reside in packages/src/*/tests, and are run using jest. Files containing unit tests always have the prefix *.spec.ts, while integration/e2e tests have the prefix *.ispect.ts.

In order to run any tests, you must first make sure the source files are compiled and all dependencies are installed by running yarn bootstrap.

If you wish to run integration tests, you may do so agains a local or remote Zilliqa testnet. However, note that the public testnet may not always be caught up to the state-of-the-art, and, therefore, can cause zilliqa-js to behave in unexpected ways.

# run all unit tests
yarn test

# configure environment variables
# $GENESIS_PRIV_KEY - the private key used to run transaction-related tests
# $GENESIS_ADDRESS - the address for the private key
# HTTP_PROVIDER - the URL of the Zilliqa JSON-RPC server
vim .env

# run all integration tests
yarn test:integration


zilliqa-js is bundled using rollup. Unfortunately, because elliptic is a major dependency and contains a circular dependency that causes rollup to choke, we also make use of webpack to pre-process and thereby eliminate the problem.

To build the distributable bundles, simple run yarn bundle. This will output two bundles, *.umd.js and *.esm.js, to packages/*/dist. Node.js clients are pointed to the umd bundle, and bundlers are pointed to esm.

NOTE: these bundles are not minified.


You can view our licence here.