Skip to content

Class: Client

Nimiq Albatross client that runs in browsers via WASM and is exposed to Javascript.

Usage:

js
import init, * as Nimiq from "./pkg/nimiq_web_client.js";

init().then(async () => {
    const config = new Nimiq.ClientConfiguration();
    const client = await config.instantiateClient();
    // ...
});

Table of contents

Constructors

Methods

Constructors

constructor

new Client(): Client

Returns

Client

Methods

addConsensusChangedListener

addConsensusChangedListener(listener): Promise<number>

Adds an event listener for consensus-change events, such as when consensus is established or lost.

Parameters

NameType
listener(state: ConsensusState) => any

Returns

Promise<number>

Defined in

web.d.ts:779


addHeadChangedListener

addHeadChangedListener(listener): Promise<number>

Adds an event listener for new blocks added to the blockchain.

Parameters

NameType
listener(hash: string, reason: string, reverted_blocks: string[], adopted_blocks: string[]) => any

Returns

Promise<number>

Defined in

web.d.ts:785


addPeerChangedListener

addPeerChangedListener(listener): Promise<number>

Adds an event listener for peer-change events, such as when a new peer joins, or a peer leaves.

Parameters

NameType
listener(peer_id: string, reason: "left" | "joined", peer_count: number, peer_info?: PlainPeerInfo) => any

Returns

Promise<number>

Defined in

web.d.ts:791


addTransactionListener

addTransactionListener(listener, addresses): Promise<number>

Adds an event listener for transactions to and from the provided addresses.

The listener is called for transactions when they are included in the blockchain.

Parameters

NameType
listener(transaction: PlainTransactionDetails) => any
addresses(string | Address)[]

Returns

Promise<number>

Defined in

web.d.ts:800


free

free(): void

Returns

void

Defined in

web.d.ts:767


getAccount

getAccount(address): Promise<PlainAccount>

Fetches the account for the provided address from the network.

Throws if the address cannot be parsed and on network errors.

Parameters

NameType
addressstring | Address

Returns

Promise<PlainAccount>

Defined in

web.d.ts:865


getAccounts

getAccounts(addresses): Promise<PlainAccount[]>

Fetches the accounts for the provided addresses from the network.

Throws if an address cannot be parsed and on network errors.

Parameters

NameType
addresses(string | Address)[]

Returns

Promise<PlainAccount[]>

Defined in

web.d.ts:873


getBlock

getBlock(hash): Promise<PlainBlock>

Fetches a block by its hash.

Throws if the client does not have the block.

Fetching blocks from the network is not yet available.

Parameters

NameType
hashstring

Returns

Promise<PlainBlock>

Defined in

web.d.ts:847


getBlockAt

getBlockAt(height): Promise<PlainBlock>

Fetches a block by its height (block number).

Throws if the client does not have the block.

Fetching blocks from the network is not yet available.

Parameters

NameType
heightnumber

Returns

Promise<PlainBlock>

Defined in

web.d.ts:857


getHeadBlock

getHeadBlock(): Promise<PlainBlock>

Returns the current blockchain head block. Note that the web client is a light client and does not have block bodies, i.e. no transactions.

Returns

Promise<PlainBlock>

Defined in

web.d.ts:837


getHeadHash

getHeadHash(): Promise<string>

Returns the block hash of the current blockchain head.

Returns

Promise<string>

Defined in

web.d.ts:826


getHeadHeight

getHeadHeight(): Promise<number>

Returns the block number of the current blockchain head.

Returns

Promise<number>

Defined in

web.d.ts:831


getNetworkId

getNetworkId(): Promise<number>

Returns the network ID that the client is connecting to.

Returns

Promise<number>

Defined in

web.d.ts:811


getStaker

getStaker(address): Promise<PlainStaker>

Fetches the staker for the provided address from the network.

Throws if the address cannot be parsed and on network errors.

Parameters

NameType
addressstring | Address

Returns

Promise<PlainStaker>

Defined in

web.d.ts:881


getStakers

getStakers(addresses): Promise<PlainStaker[]>

Fetches the stakers for the provided addresses from the network.

Throws if an address cannot be parsed and on network errors.

Parameters

NameType
addresses(string | Address)[]

Returns

Promise<PlainStaker[]>

Defined in

web.d.ts:889


getTransaction

getTransaction(hash): Promise<PlainTransactionDetails>

Fetches the transaction details for the given transaction hash.

Parameters

NameType
hashstring

Returns

Promise<PlainTransactionDetails>

Defined in

web.d.ts:919


getTransactionReceiptsByAddress

getTransactionReceiptsByAddress(address, limit?, min_peers?): Promise<PlainTransactionReceipt[]>

This function is used to query the network for transaction receipts from and to a specific address, that have been included in the chain.

The obtained receipts are not verified before being returned.

Up to a limit number of transaction receipts are returned from newest to oldest. If the network does not have at least min_peers to query, then an error is returned.

Parameters

NameType
addressstring | Address
limit?number
min_peers?number

Returns

Promise<PlainTransactionReceipt[]>

Defined in

web.d.ts:933


getTransactionsByAddress

getTransactionsByAddress(address, since_block_height?, known_transaction_details?, limit?, min_peers?): Promise<PlainTransactionDetails[]>

This function is used to query the network for transactions from and to a specific address, that have been included in the chain.

The obtained transactions are verified before being returned.

Up to a limit number of transactions are returned from newest to oldest. If the network does not have at least min_peers to query, then an error is returned.

Parameters

NameType
addressstring | Address
since_block_height?number
known_transaction_details?PlainTransactionDetails[]
limit?number
min_peers?number

Returns

Promise<PlainTransactionDetails[]>

Defined in

web.d.ts:949


getValidator

getValidator(address): Promise<PlainValidator>

Fetches the validator for the provided address from the network.

Throws if the address cannot be parsed and on network errors.

Parameters

NameType
addressstring | Address

Returns

Promise<PlainValidator>

Defined in

web.d.ts:897


getValidators

getValidators(addresses): Promise<PlainValidator[]>

Fetches the validators for the provided addresses from the network.

Throws if an address cannot be parsed and on network errors.

Parameters

NameType
addresses(string | Address)[]

Returns

Promise<PlainValidator[]>

Defined in

web.d.ts:905


isConsensusEstablished

isConsensusEstablished(): Promise<boolean>

Returns if the client currently has consensus with the network.

Returns

Promise<boolean>

Defined in

web.d.ts:816


removeListener

removeListener(handle): Promise<void>

Removes an event listener by its handle.

Parameters

NameType
handlenumber

Returns

Promise<void>

Defined in

web.d.ts:806


sendTransaction

sendTransaction(transaction): Promise<PlainTransactionDetails>

Sends a transaction to the network and returns PlainTransactionDetails.

Throws in case of network errors.

Parameters

NameType
transactionstring | PlainTransaction | Transaction

Returns

Promise<PlainTransactionDetails>

Defined in

web.d.ts:913


waitForConsensusEstablished

waitForConsensusEstablished(): Promise<void>

Returns a promise that resolves when the client has established consensus with the network.

Returns

Promise<void>

Defined in

web.d.ts:821


create

create(config): Promise<Client>

Creates a new Client that automatically starts connecting to the network.

Parameters

NameType
configPlainClientConfiguration

Returns

Promise<Client>

Defined in

web.d.ts:773