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: RPC_ENDPOINT,
}),
shardId: 1,
});

Extends

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

@nilfoundation/niljs/src/clients/PublicClient.ts:43

Properties

shardId?

protected optional shardId: number;

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

Inherited from

BaseClient.shardId

Defined

@nilfoundation/niljs/src/clients/BaseClient.ts:26


transport

protected transport: ITransport;

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

Inherited from

BaseClient.transport

Defined

@nilfoundation/niljs/src/clients/BaseClient.ts:18

Methods

call()

call(
callArgs,
blockNumberOrHash,
overrides?): Promise<CallRes | object>

Performs a call to the specified address.

Parameters

ParameterTypeDescription

callArgs

XOR<DataCallArgs, AbiCallArgs>

The arguments for the call.

blockNumberOrHash

0x${string} | BlockTag

The number/hash of the block.

overrides?

Record<Address, ContractOverride>

The overrides of state for the chain call.

Returns

Promise<CallRes | object>

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:391


chainId()

chainId(): Promise<number>

Returns the chain ID.

Returns

Promise<number>

The chain ID.

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:340


clearChainIdCache()

clearChainIdCache(): void

Returns

void

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:354


estimateGas()

estimateGas(callArgs, blockNumberOrHash): Promise<EstimateFeeResult>

Performs a call to the specified address.

Parameters

ParameterTypeDescription

callArgs

XOR<DataCallArgs, AbiCallArgs>

The arguments for the call.

blockNumberOrHash

0x${string} | BlockTag

The number/hash of the block.

Returns

Promise<EstimateFeeResult>

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:450


estimateGasLimit()

estimateGasLimit(): Promise<bigint>

Returns the gas limit.

Returns

Promise<bigint>

The gas limit.

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:328


getBalance()

getBalance(address, blockNumberOrHash?): Promise<bigint>

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

Parameters

ParameterTypeDescription

address

Address

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: RPC_ENDPOINT
})

const balance = await client.getBalance(ADDRESS, 'latest');

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:221


getBlockByHash()

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

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.

Returns

Promise<Block<boolean>>

Information about the block with the given hash.

Example

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

const client = new PublicClient({
transport: new HttpTransport({
endpoint: RPC_ENDPOINT,
}),
shardId: 1,
});

const block = await client.getBlockByHash(HASH);

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:64


getBlockByNumber()

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

Returns the block with the given number.

Parameters

ParameterTypeDefault valueDescription

blockNumber

0x${string} | BlockTag

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

undefined | number

...

The ID of the shard where the block was generated.

Returns

Promise<Block<boolean>>

Returns information about a block with the given number.

Example

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

const client = new PublicClient({
endpoint: RPC_ENDPOINT
})

const block = await client.getBlockByNumber(1);

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:93


getBlockTransactionCountByHash()

getBlockTransactionCountByHash(hash): Promise<number>

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

Parameters

ParameterTypeDescription

hash

0x${string}

The hash of the block whose information is requested.

Returns

Promise<number>

The number of transactions contained within the block.

Example

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

const client = new PublicClient({
endpoint: RPC_ENDPOINT
})

const count = await client.getBlockTransactionCountByHash(HASH);

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:151


getBlockTransactionCountByNumber()

getBlockTransactionCountByNumber(blockNumber, shardId): Promise<number>

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

Parameters

ParameterType

blockNumber

string

shardId

undefined | number

Returns

Promise<number>

The number of transactions contained within the block.

Example

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

const client = new PublicClient({
endpoint: RPC_ENDPOINT
})

const count = await client.getBlockTransactionCountByNumber(1);

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:127


getCode()

getCode(address, blockNumberOrHash?): Promise<Uint8Array>

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

Parameters

ParameterTypeDescription

address

Address

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: RPC_ENDPOINT
})

const code = await client.getCode(ADDRESS, 'latest');

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:174


getGasPrice()

getGasPrice(shardId): Promise<bigint>

Returns the gas price in wei.

Parameters

ParameterType

shardId

number

Returns

Promise<bigint>

The gas price.

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:315


getShardId()

getShardId(): undefined | number

Returns the shard ID.

Returns

undefined | number

The shard ID.

Inherited from

BaseClient.getShardId

Defined

@nilfoundation/niljs/src/clients/BaseClient.ts:51


getTokens()

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

Returns all tokens at the given address.

Parameters

ParameterTypeDescription

address

Address

The address whose information is requested.

blockNumberOrHash

0x${string} | BlockTag

The number/hash of the block.

Returns

Promise<Record<string, bigint>>

The list of tokens.

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:364


getTransactionByHash()

getTransactionByHash(hash): Promise<ProcessedTransaction>

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

Parameters

ParameterTypeDescription

hash

0x${string}

The hash of the transaction.

Returns

Promise<ProcessedTransaction>

The transaction whose information is requested.

Example

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

const client = new PublicClient({
endpoint: RPC_ENDPOINT
})

const transaction = await client.getTransactionByHash(HASH);

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:243


getTransactionCount()

getTransactionCount(address, blockNumberOrHash?): Promise<number>

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

Parameters

ParameterTypeDescription

address

Address

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: RPC_ENDPOINT
})

const count = await client.getTransactionCount(ADDRESS, 'latest');

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:198


getTransactionReceiptByHash()

getTransactionReceiptByHash(hash): Promise<null | ProcessedReceipt>

Returns the receipt for the transaction with the given hash.

Parameters

ParameterTypeDescription

hash

0x${string}

The hash of the transaction.

Returns

Promise<null | ProcessedReceipt>

The receipt whose structure is requested.

Example

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

const client = new PublicClient({
endpoint: RPC_ENDPOINT
})

const receipt = await client.getTransactionReceiptByHash(HASH, 1);

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:274


request()

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

Sends a request.

Type Parameters

Type Parameter

T

Parameters

ParameterTypeDescription

requestObject

RequestArguments

The request object. It contains the request method and parameters.

Returns

Promise<T>

The response.

Inherited from

BaseClient.request

Defined

@nilfoundation/niljs/src/clients/BaseClient.ts:43


sendRawTransaction()

sendRawTransaction(transaction): Promise<0x${string}>

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

Parameters

ParameterTypeDescription

transaction

0x${string} | Uint8Array

The encoded bytecode of the transaction.

Returns

Promise<0x${string}>

The hash of the transaction.

Example

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

const client = new PublicClient({
endpoint: RPC_ENDPOINT
})

const transaction = Uint8Array.from(ARRAY);

Defined

@nilfoundation/niljs/src/clients/PublicClient.ts:300


setShardId()

setShardId(shardId): void

Sets the shard ID.

Parameters

ParameterTypeDescription

shardId

number

The shard ID.

Returns

void

Throws

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

Example

client.setShardId(1);

Inherited from

BaseClient.setShardId

Defined

@nilfoundation/niljs/src/clients/BaseClient.ts:62