A JSON-RPC provider which is backed by a WebSocket.

WebSockets are often preferred because they retain a live connection to a server, which permits more instant access to events.

However, this incurs higher server infrastructure costs, so additional resources may be required to host your own WebSocket nodes and many third-party services charge additional fees for WebSocket endpoints.

Extends

Constructors

new WebSocketProvider()

new WebSocketProvider(
   url, 
   network?, 
   options?): WebSocketProvider

Create a new WebSocketProvider.

Parameters

ParameterTypeDescription
urlstring | string[] | WebSocketLike | WebSocketCreatorThe URL(s) or WebSocket object or creator.
network?NetworkishThe network to connect to.
options?JsonRpcApiProviderOptionsThe options for the JSON-RPC API provider.

Returns

WebSocketProvider

Overrides

SocketProvider.constructor

Source

providers/provider-websocket.ts:78

Properties

PropertyTypeDefault valueDescription
readyMapMap<Shard, boolean>...A map to track the readiness of each shard.

Accessors

connect

get connect(): FetchRequest[]

Get the list of connected FetchRequests.

Returns

FetchRequest[]

The list of connected FetchRequests.

Source

providers/abstract-provider.ts:839


destroyed

get destroyed(): boolean

If this provider has been destroyed using the destroy method.

Once destroyed, all resources are reclaimed, internal event loops and timers are cleaned up and no further requests may be sent to the provider.

Returns

boolean

Source

providers/abstract-provider.ts:2158


paused

get paused(): boolean

Whether the provider is currently paused.

A paused provider will not emit any events, and generally should not make any requests to the network, but that is up to sub-classes to manage.

Setting paused = true is identical to calling .pause(false), which will buffer any events that occur while paused until the provider is unpaused.

Returns

boolean

Whether the provider is paused.

Source

providers/abstract-provider.ts:2190


pollingInterval

get pollingInterval(): number

Get the polling interval.

Returns

number

The polling interval.

Source

providers/abstract-provider.ts:996


provider

get provider(): this

Returns this, to allow an AbstractProvider to implement the Contract Runner interface.

Returns

this

The provider instance.

Source

providers/abstract-provider.ts:1006


ready

get ready(): boolean

Returns true only if the JsonRpcApiProvider._start | _start has been called.

Returns

boolean

True if the provider is ready.

Source

providers/provider-jsonrpc.ts:1079


websocket

get websocket(): WebSocketLike[]

Get the array of WebSocketLike objects.

Throws

If the websocket is closed.

Returns

WebSocketLike[]

The array of WebSocketLike objects.

Source

providers/provider-websocket.ts:64

Methods

_clearTimeout()

_clearTimeout(timerId): void

Clear a timer created using the AbstractProvider._setTimeout | _setTimeout method.

Parameters

ParameterTypeDescription
timerIdnumberThe ID of the timer to clear.

Returns

void

Inherited from

SocketProvider._clearTimeout

Source

providers/abstract-provider.ts:1861


destroy()

destroy(): Promise<void>

Destroy the WebSocket connections and clean up resources.

Returns

Promise<void>

A promise that resolves when the WebSocket connections are closed.

Overrides

SocketProvider.destroy

Source

providers/provider-websocket.ts:274


getActiveRegions()

getActiveRegions(): Promise<Shard[]>

Get the active region shards based on the protocol expansion number.

Returns

Promise<Shard[]>

A promise that resolves to the active shards.

Inherited from

SocketProvider.getActiveRegions

Source

providers/abstract-provider.ts:923


getActiveZones()

getActiveZones(): Promise<Zone[]>

Get the active zones for a shard based on the protocol expansion number.

Returns

Promise<Zone[]>

A promise that resolves to the active zones.

Inherited from

SocketProvider.getActiveZones

Source

providers/abstract-provider.ts:940


getLatestQiRate()

getLatestQiRate(zone, amt?): Promise<bigint>

Get the latest Qi rate for a zone.

Parameters

ParameterTypeDescription
zoneZoneThe zone to get the rate for.
amt?bigintThe amount to get the rate for. Default is 1

Returns

Promise<bigint>

A promise that resolves to the latest Qi rate.

Inherited from

SocketProvider.getLatestQiRate

Source

providers/abstract-provider.ts:962


getLatestQuaiRate()

getLatestQuaiRate(zone, amt?): Promise<bigint>

Get the latest Quai rate for a zone.

Parameters

ParameterTypeDescription
zoneZoneThe zone to get the rate for.
amt?bigintThe amount in quais to get the rate for. Default is 1

Returns

Promise<bigint>

A promise that resolves to the latest Quai -> Qi rate for the given amount.

Inherited from

SocketProvider.getLatestQuaiRate

Source

providers/abstract-provider.ts:881


getProtocolExpansionNumber()

getProtocolExpansionNumber(): Promise<number>

Get the protocol expansion number.

Returns

Promise<number>

A promise that resolves to the protocol expansion number.

Inherited from

SocketProvider.getProtocolExpansionNumber

Source

providers/abstract-provider.ts:914


getQiRateAtBlock()

getQiRateAtBlock(
   zone, 
   blockTag, 
amt?): Promise<bigint>

Get the Qi rate at a specific block.

Parameters

ParameterTypeDescription
zoneZoneThe zone to get the rate for.
blockTagBlockTagThe block tag to get the rate at.
amt?bigintThe amount to get the rate for. Default is 1

Returns

Promise<bigint>

A promise that resolves to the Qi rate at the specified block.

Inherited from

SocketProvider.getQiRateAtBlock

Source

providers/abstract-provider.ts:975


getQuaiRateAtBlock()

getQuaiRateAtBlock(
   zone, 
   blockTag, 
amt?): Promise<bigint>

Get the Quai rate at a specific block.

Parameters

ParameterTypeDescription
zoneZoneThe zone to get the rate for.
blockTagBlockTagThe block tag to get the rate at.
amt?bigintThe amount to get the rate for. Default is 1

Returns

Promise<bigint>

A promise that resolves to the Quai rate at the specified block.

Inherited from

SocketProvider.getQuaiRateAtBlock

Source

providers/abstract-provider.ts:894


getSigner()

getSigner(address?): Promise<JsonRpcSigner>

Returns a JsonRpcSigner for the given address.

Parameters

ParameterTypeDescription
address?string | numberThe address or index of the account.

Returns

Promise<JsonRpcSigner>

A promise that resolves to the JsonRpcSigner.

Inherited from

SocketProvider.getSigner

Throws

If the account is invalid.

Source

providers/provider-jsonrpc.ts:1489


listAccounts()

listAccounts(): Promise<JsonRpcSigner[]>

Returns a list of JsonRpcSigners for all accounts.

Returns

Promise<JsonRpcSigner[]>

A promise that resolves to an array of JsonRpcSigners.

Inherited from

SocketProvider.listAccounts

Source

providers/provider-jsonrpc.ts:1526


pause()

pause(dropWhilePaused?): void

Pause the provider. If dropWhilePaused, any events that occur while paused are dropped, otherwise all events will be emitted once the provider is unpaused.

Parameters

ParameterTypeDescription
dropWhilePaused?booleanWhether to drop events while paused.

Returns

void

Inherited from

SocketProvider.pause

Source

providers/abstract-provider.ts:2211


resume()

resume(): void

Resume the provider.

Returns

void

Inherited from

SocketProvider.resume

Source

providers/abstract-provider.ts:2240


send()

send(
   method, 
   params, 
   shard?, 
now?): Promise<any>

Requests the method with params via the JSON-RPC protocol over the underlying channel. This can be used to call methods on the backend that do not have a high-level API within the Provider API.

This method queues requests according to the batch constraints in the options, assigns the request a unique ID.

Do NOT override this method in sub-classes; instead override JsonRpcApiProvider._send | _send or force the options values in the call to the constructor to modify this method’s behavior.

Parameters

ParameterTypeDescription
methodstringThe method to call.
paramsany[] | Record<string, any>The parameters to pass to the method.
shard?ShardThe shard to send the request to.
now?booleanIf true, the request will be sent immediately.

Returns

Promise<any>

A promise that resolves to the result of the method call.

Inherited from

SocketProvider.send

Source

providers/provider-jsonrpc.ts:1447


shardFromHash()

shardFromHash(hash): Shard

Get the shard from a hash.

Parameters

ParameterTypeDescription
hashstringThe hash to get the shard from.

Returns

Shard

The shard.

Inherited from

SocketProvider.shardFromHash

Source

providers/abstract-provider.ts:860


waitShardReady()

waitShardReady(shard): Promise<void>

Wait until the shard is ready. Max wait time is ~8 seconds.

Parameters

ParameterTypeDescription
shardShardThe shard identifier.

Returns

Promise<void>

A promise that resolves when the shard is ready.

Throws

If the shard is not ready within the timeout period.

Source

providers/provider-websocket.ts:150


zoneFromAddress()

zoneFromAddress(_address): Promise<Zone>

Get the zone from an address.

Parameters

ParameterTypeDescription
_addressAddressLikeThe address to get the zone from.

Returns

Promise<Zone>

A promise that resolves to the zone.

Inherited from

SocketProvider.zoneFromAddress

Source

providers/abstract-provider.ts:849


zoneFromHash()

zoneFromHash(hash): Zone

Get the zone from a hash.

Parameters

ParameterTypeDescription
hashstringThe hash to get the zone from.

Returns

Zone

The zone.

Inherited from

SocketProvider.zoneFromHash

Source

providers/abstract-provider.ts:870