Skip to main content

Class: PublicClient

PublicClient is a class that allows for interacting with the network via the JSON-RPC API. It provides an abstraction of the connection to =nil;. PublicClient enables using API requests that do not require signing data (or otherwise using one's private key).

Example

import { PublicClient } from '@nilfoundation/niljs';

const client = new PublicClient({
transport: new HttpTransport({
endpoint: "http://127.0.0.1:8529",
}),
shardId: 1,
});

Extends

  • BaseClient

Constructors

new PublicClient()

new PublicClient(config): PublicClient

Creates an instance of PublicClient.

Parameters

ParameterTypeDescription

config

IClientBaseConfig

The config to be used in the client. See IPublicClientConfig.

Returns

PublicClient

Overrides

BaseClient.constructor

Defined in

nil/nil.js/src/clients/PublicClient.ts:38

Properties

shardId?

protected optional shardId: number;

The ID of the shard which the client needs to interact with. The shard with this ID will be used in every call made by the client.

Inherited from

BaseClient.shardId

Defined in

nil/nil.js/src/clients/BaseClient.ts:25


transport

protected transport: ITransport;

The ITransport to be used in the client. See ITransport.

Inherited from

BaseClient.transport

Defined in

nil/nil.js/src/clients/BaseClient.ts:18

Methods

call()

call(callArgs, blockNumberOrHash): Promise<`0x${string}`>

Returns the result of the call.

Parameters

ParameterTypeDescription

callArgs

CallArgs

The arguments for the call.

blockNumberOrHash

`0x${string}` | BlockTag

The number/hash of the block.

Returns

Promise<`0x${string}`>

Defined in

nil/nil.js/src/clients/PublicClient.ts:405


chainId()

chainId(): Promise<number>

Returns the chain ID.

Returns

Promise<number>

The chain ID.

Defined in

nil/nil.js/src/clients/PublicClient.ts:361


estimateGasLimit()

estimateGasLimit(): Promise<bigint>

Returns the gas limit.

Returns

Promise<bigint>

The gas limit.

Defined in

nil/nil.js/src/clients/PublicClient.ts:351


getBalance()

getBalance(address, blockNumberOrHash): Promise<bigint>

Returns the balance of the given address and at the given block.

Parameters

ParameterTypeDescription

address

`0x${string}`

The address of the account or contract.

blockNumberOrHash

`0x${string}` | BlockTag

The number/hash of the block.

Returns

Promise<bigint>

The balance of the address.

Example

import { PublicClient } from '@nilfoundation/niljs';

const client = new PublicClient({
endpoint: 'http://127.0.0.1:8529'
})

const balance = await client.getBalance(Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), 'latest');

Defined in

nil/nil.js/src/clients/PublicClient.ts:233


getBlockByHash()

getBlockByHash(
hash,
fullTx,
shardId): Promise<Block>

Returns the block with the given hash.

Parameters

ParameterTypeDefault valueDescription

hash

`0x${string}`

undefined

The hash of the block whose information is requested.

fullTx

boolean

false

The flag that determines whether full transaction information is returned in the output.

shardId

number

...

The ID of the shard where the block was generated.

Returns

Promise <Block>

Information about the block with the given hash.

Example

import { PublicClient } from '@nilfoundation/niljs';

const client = new PublicClient({
transport: new HttpTransport({
endpoint: "http://127.0.0.1:8529",
}),
shardId: 1,
});

const block = await client.getBlockByHash(0x9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08);

Defined in

nil/nil.js/src/clients/PublicClient.ts:60


getBlockByNumber()

getBlockByNumber(
blockNumber,
fullTx,
shardId): Promise<Block>

Returns the block with the given number.

Parameters

ParameterTypeDefault valueDescription

blockNumber

`0x${string}`

undefined

The number of the block whose information is requested.

fullTx

boolean

false

The flag that determines whether full transaction information is returned in the output.

shardId

number

...

The ID of the shard where the block was generated.

Returns

Promise <Block>

Returns information about a block with the given number.

Example

import { PublicClient } from '@nilfoundation/niljs';

const client = new PublicClient({
endpoint: 'http://127.0.0.1:8529'
})

const block = await client.getBlockByNumber(1);

Defined in

nil/nil.js/src/clients/PublicClient.ts:95


getBlockMessageCountByHash()

getBlockMessageCountByHash(hash, shardId): Promise<number>

Returns the total number of messages recorded in the block with the given hash.

Parameters

ParameterTypeDescription

hash

`0x${string}`

The hash of the block whose information is requested.

shardId

number

The ID of the shard where the block was generated.

Returns

Promise<number>

The number of messages contained within the block.

Example

import { PublicClient } from '@nilfoundation/niljs';

const client = new PublicClient({
endpoint: 'http://127.0.0.1:8529'
})

const count = await client.getBlockMessageCountByHash(Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));

Defined in

nil/nil.js/src/clients/PublicClient.ts:157


getBlockMessageCountByNumber()

getBlockMessageCountByNumber(blockNumber, shardId): Promise<number>

Returns the total number of messages recorded in the block with the given number.

Parameters

ParameterType

blockNumber

string

shardId

number

Returns

Promise<number>

The number of messages contained within the block.

Example

import { PublicClient } from '@nilfoundation/niljs';

const client = new PublicClient({
endpoint: 'http://127.0.0.1:8529'
})

const count = await client.getBlockMessageCountByNumber(1);

Defined in

nil/nil.js/src/clients/PublicClient.ts:129


getCode()

getCode(address, blockNumberOrHash): Promise<Uint8Array>

Returns the bytecode of the contract with the given address and at the given block.

Parameters

ParameterTypeDescription

address

`0x${string}`

The address of the account or contract.

blockNumberOrHash

`0x${string}` | BlockTag

The number/hash of the block.

Returns

Promise<Uint8Array>

The bytecode of the contract.

Example

import { PublicClient } from '@nilfoundation/niljs';

const client = new PublicClient({
endpoint: 'http://127.0.0.1:8529'
})

const code = await client.getCode(Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), 'latest');

Defined in

nil/nil.js/src/clients/PublicClient.ts:183


getCurrencies()

getCurrencies(address, blockNumberOrHash): Promise<Record<string, bigint>>

Returns all tokens by the given address.

Parameters

ParameterTypeDescription

address

`0x${string}`

The address of the account.

blockNumberOrHash

`0x${string}` | BlockTag

The number/hash of the block.

Returns

Promise<Record<string, bigint>>

The list of tokens.

Defined in

nil/nil.js/src/clients/PublicClient.ts:376


getGasPrice()

getGasPrice(): Promise<bigint>

Returns the gas price in wei.

Returns

Promise<bigint>

The gas price.

Defined in

nil/nil.js/src/clients/PublicClient.ts:341


getMessageByHash()

getMessageByHash(hash, shardId): Promise<ProcessedMessage>

Returns the structure of the internal message with the given hash.

Parameters

ParameterTypeDescription

hash

`0x${string}`

The hash of the message.

shardId

number

The ID of the shard where the message was recorded.

Returns

Promise<ProcessedMessage>

The message whose information is requested.

Example

import { PublicClient } from '@nilfoundation/niljs';

const client = new PublicClient({
endpoint: 'http://127.0.0.1:8529'
})

const message = await client.getMessageByHash(Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));

Defined in

nil/nil.js/src/clients/PublicClient.ts:259


getMessageCount()

getMessageCount(address, blockNumberOrHash): Promise<number>

Returns the transaction count of the account with the given address and at the given block.

Parameters

ParameterTypeDescription

address

`0x${string}`

The address of the account or contract.

blockNumberOrHash

`0x${string}` | BlockTag

The number/hash of the block.

Returns

Promise<number>

The number of transactions contained within the block.

Example

import { PublicClient } from '@nilfoundation/niljs';

const client = new PublicClient({
endpoint: 'http://127.0.0.1:8529'
})

const count = await client.getMessageCount(Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), 'latest');

Defined in

nil/nil.js/src/clients/PublicClient.ts:207


getMessageReceiptByHash()

getMessageReceiptByHash(hash, shardId): Promise<IReceipt>

Returns the receipt for the message with the given hash.

Parameters

ParameterTypeDescription

hash

`0x${string}`

The hash of the message.

shardId

number

The ID of the shard where the message was recorded.

Returns

Promise <IReceipt>

The receipt whose structure is requested.

Example

import { PublicClient } from '@nilfoundation/niljs';

const client = new PublicClient({
endpoint: 'http://127.0.0.1:8529'
})

const receipt = await client.getMessageReceiptByHash(1, Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));

Defined in

nil/nil.js/src/clients/PublicClient.ts:295


getShardId()

getShardId(): number

Returns the shard ID.

Returns

number

The shard ID.

Inherited from

BaseClient.getShardId

Defined in

nil/nil.js/src/clients/BaseClient.ts:50


request()

protected request<T>(requestObject): Promise<T>

Sends a request.

Type Parameters

Type Parameter

T

Parameters

ParameterTypeDescription

requestObject

Arguments

The request object. It contains the method and parameters.

Returns

Promise<T>

The response.

Inherited from

BaseClient.request

Defined in

nil/nil.js/src/clients/BaseClient.ts:42


sendRawMessage()

sendRawMessage(message): Promise<`0x${string}`>

Creates a new message or creates a contract for a previously signed message.

Parameters

ParameterTypeDescription

message

`0x${string}` | Uint8Array

The encoded bytecode of the message.

Returns

Promise<`0x${string}`>

The hash of the message.

Example

import { PublicClient } from '@nilfoundation/niljs';

const client = new PublicClient({
endpoint: 'http://127.0.0.1:8529'
})

const message = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);

Defined in

nil/nil.js/src/clients/PublicClient.ts:324


setShardId()

setShardId(shardId): void

Sets the shard ID.

Parameters

ParameterTypeDescription

shardId

number

The shard ID.

Returns

void

Inherited from

BaseClient.setShardId

Throws

Will throw an error if the provided shard ID is invalid.

Example

client.setShardId(1);

Defined in

nil/nil.js/src/clients/BaseClient.ts:61