Note

This documentation is under construction and the chain3.js 1.0 stable version isn’t released. If you’re using a version v0.1.x of chain3.js then please have a look at chain3js 0.1.x or https://github.com/MOACChain/moac-core/wiki/Chain3.

Chain3

The Chain3 class is a wrapper to house all MOAC related modules.

Initiating of Chain3

Parameters

  1. provider - string|object: A URL or one of the Chain3 provider classes.
  2. net - net.Socket (optional): The net NodeJS package.
  3. options - object (optional) The Chain3 options

Example

import Chain3 from 'chain3';

// "Chain3.givenProvider" will be set in a MOAC supported browser.
const chain3 = new Chain3(Chain3.givenProvider || 'ws://some.local-or-remote.node:8546', net, options);

> chain3.mc
> chain3.utils
> chain3.version

Chain3.modules

This Static property will return an object with the classes of all major sub modules, to be able to instantiate them manually.

Returns

Object: A list of modules:
  • Mc - Function: the Mc module for interacting with the MOAC network see chain3.mc for more.
  • Net - Function: the Net module for interacting with network properties see chain3.mc.net for more.
  • Personal - Function: the Personal module for interacting with the Ethereum accounts see chain3.mc.personal for more.

Example

Chain3.modules
> {
    Mc(provider, net?, options?),
    Net(provider, net?, options?),
    Personal(provider, net?, options?),
}

options

An Chain3 module does provide several options for configuring the transaction confirmation worklfow or for defining default values. These are the currently available option properties on a Web3 module:

Example

import Chain3 from 'chain3';

const options = {
    defaultAccount: '0x0',
    defaultBlock: 'latest',
    defaultGas: 1,
    defaultGasPrice: 0,
    transactionBlockTimeout: 50,
    transactionConfirmationBlocks: 24,
    transactionPollingTimeout: 480,
    transactionSigner: new CustomTransactionSigner()
}

const chain3 = new Chain3('http://localhost:8545', null, options);

defaultBlock

chain3.defaultBlock
chain3.mc.defaultBlock
...

The default block is used for all methods which have a block parameter. You can override it by passing the block parameter if a block is required.

Example:

Returns

The defaultBlock property can return the following values:

  • Number: A block number
  • "genesis" - String: The genesis block
  • "latest" - String: The latest block (current head of the blockchain)
  • "pending" - String: The currently mined block (including pending transactions)

Default is "latest"


defaultAccount

chain3.defaultAccount
chain3.eth.defaultAccount
chain3.shh.defaultAccount
...

This default address is used as the default "from" property, if no "from" property is specified.

Returns

String - 20 Bytes: Any Ethereum address. You need to have the private key for that address in your node or keystore. (Default is undefined)


defaultGasPrice

chain3.defaultGasPrice
chain3.eth.defaultGasPrice
chain3.shh.defaultGasPrice
...

The default gas price which will be used for a request.

Returns

string|number: The current value of the defaultGasPrice property.


defaultGas

chain3.defaultGas
chain3.eth.defaultGas
chain3.shh.defaultGas
...

The default gas which will be used for a request.

Returns

string|number: The current value of the defaultGas property.


transactionBlockTimeout

chain3.transactionBlockTimeout
chain3.eth.transactionBlockTimeout
chain3.shh.transactionBlockTimeout
...

The transactionBlockTimeout will be used over a socket based connection. This option does define the amount of new blocks it should wait until the first confirmation happens. This means the PromiEvent rejects with a timeout error when the timeout got exceeded.

Returns

number: The current value of transactionBlockTimeout


transactionConfirmationBlocks

chain3.transactionConfirmationBlocks
chain3.eth.transactionConfirmationBlocks
chain3.shh.transactionConfirmationBlocks
...

This defines the number of blocks it requires until a transaction will be handled as confirmed.

Returns

number: The current value of transactionConfirmationBlocks


transactionPollingTimeout

chain3.transactionPollingTimeout
chain3.eth.transactionPollingTimeout
chain3.shh.transactionPollingTimeout
...

The transactionPollingTimeout will be used over a HTTP connection. This option does define the amount of polls (each second) it should wait until the first confirmation happens.

Returns

number: The current value of transactionPollingTimeout


transactionSigner

chain3.eth.transactionSigner
...

The transactionSigner property does provide us the possibility to customize the signing process of the Eth module and the related sub-modules.

The interface of a TransactionSigner:

interface TransactionSigner {
    sign(txObject: Transaction): Promise<SignedTransaction>
}

interface SignedTransaction {
    messageHash: string,
    v: string,
    r: string,
    s: string,
    rawTransaction: string
}

Returns

TransactionSigner: A JavaScript class of type TransactionSigner.


setProvider

chain3.setProvider(myProvider)
chain3.eth.setProvider(myProvider)
chain3.shh.setProvider(myProvider)
...

Will change the provider for its module.

Note

When called on the umbrella package chain3 it will also set the provider for all sub modules chain3.eth, chain3.shh, etc.

Parameters

  1. Object|String - provider: a valid provider
  2. Net - net: (optional) the node.js Net package. This is only required for the IPC provider.

Returns

Boolean

Example

import Web3 from 'chain3';

const chain3 = new Web3('http://localhost:8545');

// or
const chain3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

// change provider
chain3.setProvider('ws://localhost:8546');
// or
chain3.setProvider(new Web3.providers.WebsocketProvider('ws://localhost:8546'));

// Using the IPC provider in node.js
const net = require('net');
const chain3 = new Web3('/Users/myuser/Library/Ethereum/geth.ipc', net); // mac os path

// or
const chain3 = new Web3(new Web3.providers.IpcProvider('/Users/myuser/Library/Ethereum/geth.ipc', net)); // mac os path
// on windows the path is: '\\\\.\\pipe\\geth.ipc'
// on linux the path is: '/users/myuser/.ethereum/geth.ipc'

providers

Web3.providers
Eth.providers
...

Contains the current available providers.

Value

Object with the following providers:

  • Object - HttpProvider: The HTTP provider is deprecated, as it won’t work for subscriptions.
  • Object - WebsocketProvider: The Websocket provider is the standard for usage in legacy browsers.
  • Object - IpcProvider: The IPC provider is used node.js dapps when running a local node. Gives the most secure connection.

Example

const Web3 = require('chain3');
// use the given Provider, e.g in Mist, or instantiate a new websocket provider
const chain3 = new Web3(Web3.givenProvider || 'ws://localhost:8546');
// or
const chain3 = new Web3(Web3.givenProvider || new Web3.providers.WebsocketProvider('ws://localhost:8546'));

// Using the IPC provider in node.js
const net = require('net');

const chain3 = new Web3('/Users/myuser/Library/Ethereum/geth.ipc', net); // mac os path
// or
const chain3 = new Web3(new Web3.providers.IpcProvider('/Users/myuser/Library/Ethereum/geth.ipc', net)); // mac os path
// on windows the path is: '\\\\.\\pipe\\geth.ipc'
// on linux the path is: '/users/myuser/.ethereum/geth.ipc'

givenProvider

Web3.givenProvider
chain3.eth.givenProvider
chain3.shh.givenProvider
...

When using chain3.js in an Ethereum compatible browser, it will set with the current native provider by that browser. Will return the given provider by the (browser) environment, otherwise null.

Returns

Object: The given provider set or false.

Example

chain3.setProvider(Web3.givenProvider || 'ws://localhost:8546');

currentProvider

chain3.currentProvider
chain3.eth.currentProvider
chain3.shh.currentProvider
...

Will return the current provider.

Returns

Object: The current provider set.

Example

if (!chain3.currentProvider) {
    chain3.setProvider('http://localhost:8545');
}

BatchRequest

new chain3.BatchRequest()
new chain3.eth.BatchRequest()
new chain3.shh.BatchRequest()
...

Class to create and execute batch requests.

Parameters

none

Returns

Object: With the following methods:

  • add(request): To add a request object to the batch call.
  • execute(): Will execute the batch request.

Example

const contract = new chain3.eth.Contract(abi, address);

const batch = new chain3.BatchRequest();
batch.add(chain3.eth.getBalance.request('0x0000000000000000000000000000000000000000', 'latest'));
batch.add(contract.methods.balance(address).call.request({from: '0x0000000000000000000000000000000000000000'}));
batch.execute().then(...);

version

Property of the Chain3 class.
chain3.version

Contains the version of the chain3 wrapper class.

Returns

String: The current version.

Example

chain3.version;
> "1.0.0"