Network Provider

A network provider is an abstracts connection to the protocol, for issuing queries and sending signed state-changing transactions.

The JsonRpcProvider allows you to connect to protocol nodes that you control or have access to, including mainnet, testnets, or localnets.


Connecting to Blockchain

There are several methods to connect to the protocol network provider. If you are not running your own local protocol node, it is recommended that you use the getDefaultProvider() method.

manifold . getDefaultProvider( [ network = “localnet” ] )   => Provider

This creates a FallbackProvider backed by multiple backends.

This is the recommended method of connecting to the protocol network if you are not running your own protocol node.

get a standard network provider
let networkProvider = manifold.getDefaultProvider("localnet");

JsonRpcProvider ( inherits from Provider )

prototype . connection

An object describing the connection of the JSON-RPC endpoint with the following properties:

  • url: string url (the JSON-RPC URL)

  • timeout: int RPC request timeout in milliseconds (default: 120,000 ms)

  • user: string username (a username to be used for basic authentication, optional)

  • password: string password ( a password to use for basic authentication, optional)

  • allowInsecure: boolean allow basic authentication over an insecure HTTP network (default: false)

new manifold . providers . JsonRpcProvider( [ urlOrInfo = “http://localhost:26657” ] [ , network ] )

Connect to the JSON-RPC API URL urlOrInfo of a protocol node.

The urlOrInfo may also be specified as an object with the following properties:

  • url: string url (the JSON-RPC URL, required)

  • timeout: int RPC request timeout in milliseconds (default: 60,000 ms)

  • user: string username (a username to be used for basic authentication, optional)

  • password: string password (a password to use for basic authentication, optional)

  • allowInsecure: boolean allow basic authentication over an insecure HTTP network (default: false)

See also: JSON-RPC provider-specific Properties and Operations

connect to a default provider
// You can use any standard network name
//  - "homestead"
//  - "testnet"

let networkProvider = manifold.getDefaultProvider('localnet');
connect to private trusted node
let provider = new manifold.providers.JsonRpcProvider({
    url: "https://x.x.x.x",
    timeout: 60000
}, "manifold");
connect to private customized node
let provider = new manifold.providers.JsonRpcProvider({
    url: "https://x.x.x.x",
    timeout: 60000
}, {
    chainId: "awesome",
    name: "awesome"
});

Properties

Not all properties are mutable unless otherwise specified, and will reflect their default values if left unspecified.

Provider Variables

prototype . blockNumber

Returns the most recent block number (block height) this provider has seen and has triggered events for. If no block has been seen, this is null.

data type: integer

prototype . polling

mutable

If the provider is currently polling because it is actively watching for events. This may be set to enable/disable polling temporarily or disabled permanently to allow a node process to exit.

data type: boolean

prototype . pollingInterval

mutable

The frequency (in milliseconds) that the provider is polling. The default interval is 4 seconds.

This may make sense to lower for polling a local node. When polling external nodes, setting this too low may result in the service blocking your IP address or otherwise throttling your API calls.

data type: integer

Network

A network represents various properties of a network, such as mainnet, testnet, or private networks.

prototype . getNetwork ( )   => Promise<Network>

A Promise that resolves to a Network object describing the connected network and chain. A network has the following properties:

  • chainId — the chain ID (network ID) of the connected network

  • name — the name of the network (e.g., “testnet”)

get a standard network
let network = manifold.providers.getNetwork('localnet');
// {
//    chainId: "manifold",
//    name: "localnet"
// }
a custom development network
let network = {
    chainId: "localnet",
    name: "local"
}

Account

A ‘dummy’ wallet is used below, there is not real user behind it.

prototype . getBalance ( AddressOrName )   => Promise<BigNumber>

Returns a Promise with the balance (as a BigNumber) of AddressOrName.

get the balance of an account
let address = "manifold1x7tp9tt7mu0jm6qdmljgntvzzp53lrtndr7h8x";

provider.getBalance(address).then((balance) => {

    // balance is a BigNumber (in gwei); format is as a string (in manifold)
    let manifoldString = manifold.utils.formatMan(balance);

    console.log("Balance: " + manifoldString);
});

//expected result:
//Balance: 0.0
prototype . getTransactionCount ( AddressOrName )   => Promise<BigNumber>

Returns a Promise with the number of sent transactions (as a BigNumber) from AddressOrName. This is also the nonce required to send a new transaction.

get the transaction count of an account
let address = "manifold1x7tp9tt7mu0jm6qdmljgntvzzp53lrtndr7h8x";

provider.getTransactionCount(address).then((nonce) => {
    console.log("Total Transactions Ever Sent: " + nonce.toString());
});

//expected result:
//Total Transactions Ever Sent: 0
prototype . getAccountNumber ( AddressOrName )   => Promise<BigNumber>

Returns a Promise with the account number of wallet (as a BigNumber) from AddressOrName.

get the account number
let address = "manifold1x7tp9tt7mu0jm6qdmljgntvzzp53lrtndr7h8x";

provider.getAccountNumber(address).then((accountNumber) => {
    console.log("Account number: " + accountNumber.toString());
});

//expected result:
//Account number:0

Blockchain Status

prototype . getBlockNumber ( )   => Promise<number>

Returns a Promise with the latest block number (as a Number).

get latest block number
provider.getBlockNumber().then((blockNumber) => {
    console.log("Latest block number: " + blockNumber);
});
// expected result:
// Latest block number: "*integer* latest block number"
prototype . getBlock ( blockHashOrBlockNumber )   => Promise<Block>

Returns a Promise with the block at blockHashOrBlockNumber. (See: Block Responses)

blocks
// Block Number
provider.getBlock(12345).then((block) => {
    console.log(block);
});
//expected result:
//block response, click on the link above for more information
prototype . getTransactionReceipt ( transactionHash )   => Promise<TransactionReceipt>

Returns a Promise with the transaction receipt with transactionHash. (See: Transaction Receipts)

query transaction receipt
let transactionHash = "0x434c7fe4c7c7068289f0d369e428b7a3bf3882c3253f2b7f9529c0985a1cb500"

provider.getTransactionReceipt(transactionHash).then((receipt) => {
    console.log(receipt);
});
//expected result:
//transaction receipt, click on the link above for more information
prototype . getTransactionFee ( route, transactionType, overrides, … )   => Promise<TransactionFee>

Returns a Promise that resolves to the estimated transaction fee structure.

The valid routes and transaction types are:
  • kyc — the route for kyc module
    • kyc-whitelist — the whitelist transaction type

    • kyc-revokeWhitelist — the revoke whitelist transaction type

  • bank — the route for bank module
    • bank-send — the transfer ERC transaction type

  • token — the route for token module
    • token-mintFungibleToken — the mint transaction type

    • token-burnFungibleToken — the burn transaction type

    • token-freeze — the freeze transaction type

    • token-unfreeze — the unfreeze transaction type

    • token-createFungibleToken — the create transaction type

    • token-setFungibleTokenStatus — the set status transaction type

  • nameservice — the route for name service module
    • nameservice-createAlias — the create transaction type

    • nameservice-setAliasStatus — the set status transaction type

the transaction fee structure
{
    amount: [
        {
            // The denomination should be in gwei
            denom: string,

            // The fee amount in gwei
            amount: BigNumberish
        }
    ],
    // Reserved for future
    gas: BigNumberish
}
query the transaction fee
let value = utils.parseMan("10").toString();
provider.getTransactionFee("bank", "bank-send").then((fee) => {
    console.log("Fee:", fee);
});

Waiting for Transactions

prototype . waitForTransaction ( transactionHash )   => Promise<TransactionReceipt>

Returns a Promise which resolves to the Transaction Receipt once transactionHash is validated.

transaction validated
provider.waitForTransaction(transactionHash).then((receipt) => {
    console.log('Transaction validated: ' + receipt.hash);
    console.log(receipt);
});

//expected result:
//transaction receipt, click on the link above for more information

Objects and Types

There are several common objects and types that are commonly used as input parameters or return types for various provider calls.


Block Tag

A block tag is used to uniquely identify a block’s position in the protocol:

a Number or hex string:

Each block has a block number (e.g., 1202 or "0x4b2").

“latest”:

The most recently validated block.

“pending”:

The block that is currently being validated.


Provider-Specific Extra API Calls

JsonRpcProvider

prototype . send ( method , params )   => Promise<any>

Sends the JSON-RPC method with params. This is useful for calling non-standard or less common JSON-RPC methods. A Promise is returned which will resolve to the parsed JSON result.

send vendor-specific JSON-RPC API
//method parameter is based on vendor RPC API
jsonRpcProvider.send('status', [ ]).then((result) => {
    console.log(result);
});
// expected result:
// "status of the provider for this case"