Skip to content

createPublicClient

createPublicClient<transport, chain, accountOrAddress, rpcSchema>(parameters): object

Defined in: src/clients/createPublicClient.ts:44

Creates a Public Client with a given Transport configured for a Chain.

A Public Client is an interface to “public” Ethereum JSON-RPC API, Arkiv JSON-RPC API, and Braga JSON-RPC API methods such as retrieving block numbers, transactions, reading from smart contracts, etc through Public Actions.

transport extends Transport

chain extends Chain | undefined = undefined

accountOrAddress extends `0x${string}` | Account | undefined = undefined

rpcSchema extends RpcSchema | undefined = ArkivRpcSchema

Configuration object for the public client (chain, transport, etc.)

A Arkiv Public Client. PublicArkivClient

buildQuery: () => QueryBuilder

Returns a QueryBuilder instance for building and executing queries. The QueryBuilder object follows the Builder pattern, allowing you to chain methods to build a query and then execute it.

QueryBuilder

A QueryBuilder instance for building and executing queries. QueryBuilder

import { createPublicClient, http } from 'arkiv'
import { braga } from 'arkiv/chains'
const client = createPublicClient({
chain: braga,
transport: http(),
})
const query = client.buildQuery()
const entities = await query.where("key", "=", "value").ownedBy("0x123").fetch()

getBlockTiming: () => Promise<{ blockDuration: number; currentBlock: bigint; currentBlockTime: number; }>

Returns the current block timing.

Promise<{ blockDuration: number; currentBlock: bigint; currentBlockTime: number; }>

The current block timing. GetBlockTimingReturnType

import { createPublicClient, http } from 'arkiv'
import { braga } from 'arkiv/chains'
const client = createPublicClient({
chain: braga,
transport: http(),
})
const blockTiming = await client.getBlockTiming()
// {
// currentBlock: 10n, // block number
// currentBlockTime: 1234567890, // block timestamp
// blockDuration: 2, // in seconds
// }

getEntity: (key) => Promise<Entity>

Returns the entity with the given key.

`0x${string}`

The entity key (hex string)

Promise<Entity>

The entity with the given key. Entity

import { createPublicClient, http } from 'arkiv'
import { braga } from 'arkiv/chains'
const client = createPublicClient({
chain: braga,
transport: http(),
})
const entity = await client.getEntity("0x123")
// {
// key: "0x123",
// value: "0x123",
// }

getEntityCount: () => Promise<number>

Returns the number of entities in the DBChain.

Promise<number>

The number of entities in the DBChain

import { createPublicClient, http } from 'arkiv'
import { braga } from 'arkiv/chains'
const client = createPublicClient({
chain: braga,
transport: http(),
})
const entityCount = await client.getEntityCount()
// entityCount = 0

query: (query, queryOptions?) => Promise<QueryReturnType>

Returns a QueryResult instance for fetching the results of a raw query. If no query options are provided, all payload is included, but no metadata (like owner, expiredAt, etc.) and attributes.

string

The raw query string

QueryOptions

The optional query options - QueryOptions

Promise<QueryReturnType>

A QueryReturnType instance - QueryReturnType

import { createPublicClient, http } from 'arkiv'
import { braga } from 'arkiv/chains'
const client = createPublicClient({
chain: braga,
transport: http(),
})
const queryResult = client.query('key = value && $owner = 0x123')
// queryResult = { entities: [{ key: "0x123", value: "0x123" }], cursor: undefined, blockNumber: undefined }
const queryResultWithOptions = client.query('key = value && $owner = 0x123', {
includeData: {
attributes: false,
payload: true,
metadata: true,
},
orderBy: [{ name: "key", type: "string", desc: "asc" }],
resultsPerPage: 10,
cursor: undefined,
atBlock: undefined,
})
// queryResultWithOptions = { entities: [{ key: "0x123", value: "0x123" }], cursor: "...", blockNumber: 32223n }

subscribeEntityEvents: (__namedParameters, pollingInterval?, fromBlock?) => Promise<() => void>

Subscribes to entity events. Takes an object with event handlers: {onError, onEntityCreated, onEntityUpdated, onEntityDeleted, onEntityExpiresInExtended}

(event) => void

(event) => void

(event) => void

(event) => void

(event) => void

(error) => void

number

The polling interval in milliseconds

bigint

The block number to start from

Promise<() => void>

A function to unsubscribe from the events

import { createPublicClient, http } from 'arkiv'
import { braga } from 'arkiv/chains'
const client = createPublicClient({
chain: braga,
transport: http(),
})
const unsubscribe = await client.subscribeEntityEvents({
onError: (error) => console.error("subscribeEntityEvents error", error),
})
unsubscribe() // unsubscribe from the events
import { createPublicClient, http } from 'arkiv'
import { braga } from 'arkiv/chains'
const client = createPublicClient({
chain: braga,
transport: http(),
})