Try   HackMD

Firechain.JS - Getting Started

tags: Introduction Technical tutorials

Firechain.js is a JavaScript library for interacting with the Firechain network. It provides a set of simple APIs for things like sending transactions, querying the state of the blockchain, and deploying smart contracts, as well as a set of utility functions for encoding and decoding data. It also provides a set of networking modules for connecting to Firechain nodes.

Firechain.js is available as an ES6 module and can be used in both Node.js and the browser.

Installation

Firechain.js is available as an npm package. We recommend using yarn to install it.

yarn add @firechain/firechainjs yarn add @firechain/firechainjs-ws

Importing

Firechain.js is available as an ES6 module. You can import it as follows:

import { abi, error, keystore, utils, constant, accountBlock, FirechainAPI, wallet } from '@firechain/firechainjs'; // networking modules are optional and can be imported separately import ws from '@firechain/firechainjs-ws'; import http from '@firechain/firechainjs-http'; import ipc from '@firechain/firechainjs-ipc';

If you are using CommonJS, you can use the require syntax instead:

const { abi, error, keystore, utils, constant, accountBlock, FirechainAPI, wallet } = require('@firechain/firechainjs'); const { WS_RPC } = require('@firechain/firechainjs-ws'); const { HTTP_RPC } = require('@firechain/firechainjs-http'); const { IPC_RPC } = require('@firechain/firechainjs-ipc');

Usage Example

import { FirechainAPI, abi, wallet } from '@firechain/firechainjs'; import { WS_RPC } from '@firechain/firechainjs-ws'; const rpcProvider = new WS_RPC('ws://localhost:8483'); let provider = new FirechainAPI(rpcProvider, async () => { console.log('🔥 Connected to RPC Provider!'); // Once connected, you can make requests. For example: console.log( "Global Chain Height:", await provider.request('ledger_getGlobalChainHeight') ); });

Working with ABIs

The ABI is the interface between the contract and the outside world. It defines the methods that can be called, and the events that can be emitted. It is also used to generate the contract's interface. Firechain uses a modified version of the Solidity Contract ABI specification.

Parameter Types

Contract methods (including constructors, asynchronous calls, and queries) can contain arbitrary parameters, and they can also have multiple return values. Currently, the following parameter types are supported:

Type Name Example Value Encoded Value
uint<M> Unsigned integer, 0 < M <= 256, M % 8 == 0 uint256 '2345675643' '000000000000000000000000000000000000000000000000000000008bd02b7b'
int<M> Signed integer, 0 < M <= 256, M % 8 == 0 int8 '2' '0000000000000000000000000000000000000000000000000000000000000002'
uint Equivalent to uint256 uint '2345675643' '000000000000000000000000000000000000000000000000000000008bd02b7b'
int Equivalent to int256 int '2' '0000000000000000000000000000000000000000000000000000000000000002'
tokenId Token id tokenId 't5649544520544f4b454e6e40' '000000000000000000000000000000000000000000005649544520544f4b454e'
address Address of account address 'fire:010000000000000000000000000000000000000063bef3da00' '0000000000000000000000010000000000000000000000000000000000000000'
gid Consensus group id gid '01000000000000000000' '0000000000000000000000000000000000000000000001000000000000000000'
bool Boolean bool true '0000000000000000000000000000000000000000000000000000000000000001'
bytes<M> Fixed-length byte array, 0 < M <= 32 bytes32 '0x0100000000000000000000000000000000000000000000000000000000000000' '0100000000000000000000000000000000000000000000000000000000000000'
bytes Variable-length byte array bytes '0xdf3234' '00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003df32340000000000000000000000000000000000000000000000000000000000'
string Variable-length string string 'foobar' '0000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000'
<type>[M] Fixed-length array of type, M >= 0. Range of values:uint<M>, int<M>, uint, int, tokenId, address, gid, bool and string uint8[2] ['1','2'] '00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002'
<type>[] Variable-length array of type. Range of values: uint<M>, int<M>, uint, int, tokenId, address, gid, bool and string uint256[] ['1','2'] '000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002'
  • Example jsonInterface
{ "type": "event", "name": "eventName", "inputs": [{ "name": "foo", "type": "address" }] }

Methods

encodeLogSignature

  • Parameters

    • jsonInterface | Array<jsonInterface>
    • eventName? Name of method to encode, required if the first parameter is a jsonInterface array.
  • Return

    • hexString
  • Example

let encodedEvent = abi.encodeLogSignature( [ { type: 'event', name: 'someEvent', inputs: [ { name: 'foo', type: 'address' }, { name: 'bar', type: 'uint256' }, ], } ], 'someEvent', ) // 17c53855485cba60b5dea781a996394bb9d3b44bc8932b3adf79ac70e908b220

encodeFunctionSignature

  • Parameters

    • jsonInterface | Array<jsonInterface>
    • methodName? Name of method to encode, required if the first parameter is a jsonInterface array.
  • Return

    • hexString
  • Example

let encodedSignature = abi.encodeFunctionSignature({ type: 'function', name: 'someFunction', inputs: [{ name: 'addr', type: 'address' }], })

encodeFunctionCall

  • Parameters

    • jsonInterface | Array<jsonInterface>
    • params Input parameters
    • methodName? Name of method to encode, required if the first parameter is a jsonInterface array.
  • Return

    • hexString
  • Example

let encodedCall = abi.encodeFunctionCall(
  {
    name: 'someFunction',
    type: 'function',
    inputs: [
      {
        type: 'uint256',
        name: 'bar',
      },
      {
        type: 'string',
        name: 'foo',
      },
    ],
  },
  ['400000000000000000000', 'Firechain'],
)

encodeParameter

  • Parameters

    • type Parameter type
    • params Input parameters. If the parameter type is array, an parameter array should be passed in
  • Return

    • hexString
  • Example

let encodedParam = abi.encodeParameter('uint256', '1234567890') // => 00000000000000000000000000000000000000000000000000000000499602d2

decodeParameter

  • Parameters

    • type Parameter type
    • hexString Encoded hex string
  • Return

    • decodeResult
  • Example

let decodedParam = abi.decodeParameter( 'uint256', '00000000000000000000000000000000000000000000000000000000499602d2', ) // => 1234567890

encodeParameters

  • Parameters

    • jsonInterface | Array<type-string> | Array<jsonInterface>
    • params<Array | json-string> Input parameters
    • methodName? Name of method to encode, required if the first parameter is an array.
  • Return

    • hexString
  • Example

let encodedParams = abi.encodeParameters( { type: 'constructor', inputs: [{ type: 'uint64[]' }, { type: 'string' }] }, [['1', '2'], 'three'], // NB: You can use stringified JSON here, too. )

decodeParameters

  • Parameters

    • jsonInterface | Array<type-string> | Array<jsonInterface>
    • hexString Encoded hex string
    • methodName? Name of method to decode, required if the first parameter is an array.
  • Return

    • decodeResult
  • Example

let encoded = '00000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002' let decodedParams = abi.decodeParameters( [ { type: 'function', name: 'someFunction', inputs: [ { name: 'foo', type: 'address' }, { name: 'bar', type: 'uint8[]' }, ], }, ], encoded, 'someFunction', ) // ['fire:00010000000000000000000000000000000000005cce05dbde', [1,2]]

decodeLog

  • Parameters

    • jsonInterface.inputs | jsonInterface | Array<jsonInterface>
    • hexString Encoded hex string
    • Array<hexString>
    • eventName? Name of event to decode, required if the first parameter is a jsonInterface array.
  • Return

    • decodeResult
  • Example

let encoded = ['fire:00010000000000000000000000000000000000005cce05dbde', [1,2]] let decodedLog = abi.decodeLog( [ { name: 'foo', type: 'address' }, { name: 'bar', type: 'uint8[]' }, ], ...encoded, 'someEvent' )