Spectre RPC client uses (wRPC) interface to connect directly with Spectre Node. wRPC supports two types of encodings: borsh (binary, default) and json.

There are two ways to connect: Directly to any Spectre Node or to a community-maintained public node infrastructure using the Resolver class.

Connecting to a public node using a resolver

let rpc = new RpcClient({
resolver : new Resolver(),
networkId : "mainnet",
});

await rpc.connect();

Connecting to a Spectre Node directly

let rpc = new RpcClient({
// if port is not provided it will default
// to the default port for the networkId
url : "127.0.0.1",
networkId : "mainnet",
});

Example usage


// Create a new RPC client with a URL
let rpc = new RpcClient({ url : "wss://<node-wrpc-address>" });

// Create a new RPC client with a resolver
// (networkId is required when using a resolver)
let rpc = new RpcClient({
resolver : new Resolver(),
networkId : "mainnet",
});

rpc.addEventListener("connect", async (event) => {
console.log("Connected to", rpc.url);
await rpc.subscribeDaaScore();
});

rpc.addEventListener("disconnect", (event) => {
console.log("Disconnected from", rpc.url);
});

try {
await rpc.connect();
} catch(err) {
console.log("Error connecting:", err);
}

You can register event listeners to receive notifications from the RPC client using RpcClient.addEventListener and RpcClient.removeEventListener functions.

IMPORTANT: If RPC is disconnected, upon reconnection you do not need to re-register event listeners, but your have to re-subscribe for Spectre node notifications:

rpc.addEventListener("connect", async (event) => {
console.log("Connected to", rpc.url);
// re-subscribe each time we connect
await rpc.subscribeDaaScore();
// ... perform wallet address subscriptions
});

If using NodeJS, it is important that RpcClient.disconnect is called before the process exits to ensure that the WebSocket connection is properly closed. Failure to do this will prevent the process from exiting.

Constructors

Properties

encoding: string

The current protocol encoding.

isConnected: boolean

The current connection status of the RPC client.

nodeId: string

Optional: Resolver node id.

providerName: string

Optional: public node provider name.

providerUrl: string

Optional: public node provider URL.

resolver: Resolver

Current rpc resolver

url: string

The current URL of the RPC client.

Methods

  • Parameters

    Returns void

  • Type Parameters

    Parameters

    • event: M
    • Optional callback: ((eventData) => void)
        • (eventData): void
        • Parameters

          Returns void

    Returns any

  • Bans a peer from connecting to the Spectre node for a specified duration. Returned information: None.

    Parameters

    Returns Promise<IBanResponse>

    See

    IBanRequest, IBanResponse

    Throws

    string on an RPC error, a server-side error or when supplying incorrect arguments.

  • Unregister a single event listener callback from all events.

    Parameters

    Returns void

  • Connect to the Spectre RPC server. This function starts a background task that connects and reconnects to the server if the connection is terminated. Use disconnect() to terminate the connection.

    Parameters

    Returns Promise<void>

    See

    IConnectOptions interface for more details.

  • Disconnect from the Spectre RPC server.

    Returns Promise<void>

  • Returns void

  • Unregister all notification callbacks for all events.

    Returns void

  • Unregister an event listener. This function will remove the callback for the specified event. If the callback is not supplied, all callbacks will be removed for the specified event.

    Parameters

    Returns void

  • Set the network id for the RPC client. This setting will take effect on the next connection.

    Parameters

    Returns void

  • Set the resolver for the RPC client. This setting will take effect on the next connection.

    Parameters

    Returns void

  • Start background RPC services (automatically started when invoking RpcClient.connect).

    Returns Promise<void>

  • Stop background RPC services (automatically stopped when invoking RpcClient.disconnect).

    Returns Promise<void>

  • Manage subscription for a block added notification event. Block added notification event is produced when a new block is added to the Spectre BlockDAG.

    Returns Promise<void>

  • Manage subscription for a finality conflict notification event. Finality conflict notification event is produced when a finality conflict occurs in the Spectre BlockDAG.

    Returns Promise<void>

  • Manage subscription for a finality conflict resolved notification event. Finality conflict resolved notification event is produced when a finality conflict in the Spectre BlockDAG is resolved.

    Returns Promise<void>

  • Manage subscription for a new block template notification event. New block template notification event is produced when a new block template is generated for mining in the Spectre BlockDAG.

    Returns Promise<void>

  • Manage subscription for a pruning point UTXO set override notification event. Pruning point UTXO set override notification event is produced when the UTXO set override for the pruning point changes in the Spectre BlockDAG.

    Returns Promise<void>

  • Manage subscription for a sink blue score changed notification event. Sink blue score changed notification event is produced when the blue score of the sink block changes in the Spectre BlockDAG.

    Returns Promise<void>

  • Subscribe for a UTXOs changed notification event. UTXOs changed notification event is produced when the set of unspent transaction outputs (UTXOs) changes in the Spectre BlockDAG. The event notification will be scoped to the provided list of addresses.

    Parameters

    Returns Promise<void>

  • Manage subscription for a virtual chain changed notification event. Virtual chain changed notification event is produced when the virtual chain changes in the Spectre BlockDAG.

    Parameters

    • include_accepted_transaction_ids: boolean

    Returns Promise<void>

  • Manage subscription for a virtual DAA score changed notification event. Virtual DAA score changed notification event is produced when the virtual Difficulty Adjustment Algorithm (DAA) score changes in the Spectre BlockDAG.

    Returns Promise<void>

    • Return copy of self without private attributes.

    Returns Object

  • Return stringified version of self.

    Returns string

  • Triggers a disconnection on the underlying WebSocket if the WebSocket is in connected state. This is intended for debug purposes only. Can be used to test application reconnection logic.

    Returns void

  • Returns Promise<void>

  • Returns Promise<void>

  • Returns Promise<void>

  • Returns Promise<void>

  • Returns Promise<void>

  • Returns Promise<void>

  • Unsubscribe from UTXOs changed notification event for a specific set of addresses.

    Parameters

    Returns Promise<void>

  • Manage subscription for a virtual chain changed notification event. Virtual chain changed notification event is produced when the virtual chain changes in the Spectre BlockDAG.

    Parameters

    • include_accepted_transaction_ids: boolean

    Returns Promise<void>

  • Manage subscription for a virtual DAA score changed notification event. Virtual DAA score changed notification event is produced when the virtual Difficulty Adjustment Algorithm (DAA) score changes in the Spectre BlockDAG.

    Returns Promise<void>

  • Constructs an WebSocket RPC URL given the partial URL or an IP, RPC encoding and a network type.

    Arguments

    • url - Partial URL or an IP address
    • encoding - RPC encoding
    • network_type - Network type

    Parameters

    Returns string