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
Parameter | Type | Description |
---|---|---|
| The config to be used in the client. See IPublicClientConfig. |
Returns
Overrides
Defined
@nilfoundation/niljs/src/clients/PublicClient.ts:43
Properties
shardId?
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
Defined
@nilfoundation/niljs/src/clients/BaseClient.ts:26
transport
readonly transport: ITransport;
The ITransport to be used in the client. See ITransport.
Inherited from
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
Parameter | Type | Description |
---|---|---|
|
| The arguments for the call. |
|
| The number/hash of the block. |
|
| 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
Parameter | Type | Description |
---|---|---|
|
| The arguments for the call. |
|
| 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
Parameter | Type | Description |
---|---|---|
|
| The address of the account or contract. |
|
| 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
Parameter | Type | Default value | Description |
---|---|---|---|
|
|
| The hash of the block whose information is requested. |
|
|
| 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
Parameter | Type | Default value | Description |
---|---|---|---|
|
|
| The number of the block whose information is requested. |
|
|
| The flag that determines whether full transaction information is returned in the output. |
|
|
| The ID of the shard where the block was generated. |
Returns
Promise
<Block
<boolean
>>
Returns information about a block with the given number.