RpcClient (Class)¶
WebSocket RPC client for communicating with Kaspa nodes.
Provides methods for querying blockchain state, submitting transactions, and subscribing to real-time notifications. Supports both Borsh and JSON encodings.
node_id
property
¶
The unique identifier of the connected node, or None if not connected via resolver.
resolver
property
¶
resolver: Optional[Resolver]
The resolver used for node discovery, or None if not set.
__new__
¶
__new__(resolver: Optional[Resolver] = None, url: Optional[str] = None, encoding: str | Encoding | None = Borsh, network_id: Optional[NetworkId] = None) -> RpcClient
Create a new RPC client.
| PARAMETER | DESCRIPTION |
|---|---|
resolver
|
Optional resolver for node discovery.
TYPE:
|
url
|
Optional direct node URL.
TYPE:
|
encoding
|
RPC encoding - either a string ("borsh" or "json") or an Encoding enum variant (default: "borsh"). |
network_id
|
Network identifier (default: "mainnet").
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RpcClient
|
A new RpcClient instance.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
Exception
|
If client creation fails. |
add_event_listener
¶
add_event_listener(event: NotificationEvent, callback: Any, *args: Any, **kwargs: Any) -> None
Register a callback for RPC events.
| PARAMETER | DESCRIPTION |
|---|---|
event
|
Event type as kebab string or NotificationEvent variant. See NotificationEvent for acceptable values.
TYPE:
|
callback
|
Function to call when event occurs.
TYPE:
|
*args
|
Additional arguments to pass to callback.
TYPE:
|
**kwargs
|
Additional keyword arguments to pass to callback.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
Exception
|
If the event type is invalid. |
connect
¶
connect(block_async_connect: Optional[bool] = None, strategy: Optional[str] = None, url: Optional[str] = None, timeout_duration: Optional[int] = None, retry_interval: Optional[int] = None) -> None
Connect to a Kaspa node (async).
| PARAMETER | DESCRIPTION |
|---|---|
block_async_connect
|
Block until connected (default: True).
TYPE:
|
strategy
|
Connection strategy ("retry" or "fallback", default: "retry").
TYPE:
|
url
|
Optional URL to connect to (overrides resolver).
TYPE:
|
timeout_duration
|
Connection timeout in milliseconds.
TYPE:
|
retry_interval
|
Retry interval in milliseconds.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
Exception
|
If connection fails. |
default_port
staticmethod
¶
default_port(encoding: Encoding, network: NetworkType) -> int
Get the default RPC port for a given encoding and network type.
| PARAMETER | DESCRIPTION |
|---|---|
encoding
|
RPC encoding format ("borsh" or "json").
TYPE:
|
network
|
Network type (e.g., "mainnet", "testnet-10", "testnet-11").
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The default port number for the specified configuration.
TYPE:
|
disconnect
¶
Disconnect from the node (async).
| RAISES | DESCRIPTION |
|---|---|
Exception
|
If disconnection fails. |
estimate_network_hashes_per_second
¶
estimate_network_hashes_per_second(request: EstimateNetworkHashesPerSecondRequest) -> EstimateNetworkHashesPerSecondResponse
get_balance_by_address
¶
get_balance_by_address(request: GetBalanceByAddressRequest) -> GetBalanceByAddressResponse
get_balances_by_addresses
¶
get_balances_by_addresses(request: GetBalancesByAddressesRequest) -> GetBalancesByAddressesResponse
get_block_count
¶
get_block_count(request: GetBlockCountRequest | None = None) -> GetBlockCountResponse
get_block_dag_info
¶
get_block_dag_info(request: GetBlockDagInfoRequest | None = None) -> GetBlockDagInfoResponse
get_block_template
¶
get_block_template(request: GetBlockTemplateRequest) -> GetBlockTemplateResponse
get_coin_supply
¶
get_coin_supply(request: GetCoinSupplyRequest | None = None) -> GetCoinSupplyResponse
get_connected_peer_info
¶
get_connected_peer_info(request: GetConnectedPeerInfoRequest | None = None) -> GetConnectedPeerInfoResponse
get_connections
¶
get_connections(request: GetConnectionsRequest | None = None) -> GetConnectionsResponse
get_current_block_color
¶
get_current_block_color(request: GetCurrentBlockColorRequest) -> GetCurrentBlockColorResponse
get_current_network
¶
get_current_network(request: GetCurrentNetworkRequest | None = None) -> GetCurrentNetworkResponse
get_daa_score_timestamp_estimate
¶
get_daa_score_timestamp_estimate(request: GetDaaScoreTimestampEstimateRequest) -> GetDaaScoreTimestampEstimateResponse
get_fee_estimate
¶
get_fee_estimate(request: GetFeeEstimateRequest | None = None) -> GetFeeEstimateResponse
get_fee_estimate_experimental
¶
get_fee_estimate_experimental(request: GetFeeEstimateExperimentalRequest) -> GetFeeEstimateExperimentalResponse
get_mempool_entries
¶
get_mempool_entries(request: GetMempoolEntriesRequest) -> GetMempoolEntriesResponse
get_mempool_entries_by_addresses
¶
get_mempool_entries_by_addresses(request: GetMempoolEntriesByAddressesRequest) -> GetMempoolEntriesByAddressesResponse
get_peer_addresses
¶
get_peer_addresses(request: GetPeerAddressesRequest | None = None) -> GetPeerAddressesResponse
get_server_info
¶
get_server_info(request: GetServerInfoRequest | None = None) -> GetServerInfoResponse
get_sink_blue_score
¶
get_sink_blue_score(request: GetSinkBlueScoreRequest | None = None) -> GetSinkBlueScoreResponse
get_sync_status
¶
get_sync_status(request: GetSyncStatusRequest | None = None) -> GetSyncStatusResponse
get_system_info
¶
get_system_info(request: GetSystemInfoRequest | None = None) -> GetSystemInfoResponse
get_utxo_return_address
¶
get_utxo_return_address(request: GetUtxoReturnAddressRequest) -> GetUtxoReturnAddressResponse
get_utxos_by_addresses
¶
get_utxos_by_addresses(request: GetUtxosByAddressesRequest) -> GetUtxosByAddressesResponse
get_virtual_chain_from_block
¶
get_virtual_chain_from_block(request: GetVirtualChainFromBlockRequest) -> GetVirtualChainFromBlockResponse
get_virtual_chain_from_block_v2
¶
get_virtual_chain_from_block_v2(request: GetVirtualChainFromBlockV2Request) -> GetVirtualChainFromBlockV2Response
remove_all_event_listeners
¶
Remove all registered event listeners.
remove_event_listener
¶
remove_event_listener(event: NotificationEvent, callback: Optional[Any] = None) -> None
Remove an event listener.
| PARAMETER | DESCRIPTION |
|---|---|
event
|
Event type as kebab string or NotificationEvent variant. See NotificationEvent for acceptable values.
TYPE:
|
callback
|
Specific callback to remove, or None to remove all.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
Exception
|
If the event type is invalid. |
resolve_finality_conflict
¶
resolve_finality_conflict(request: ResolveFinalityConflictRequest) -> ResolveFinalityConflictResponse
set_network_id
¶
set_network_id(network_id: NetworkId) -> None
Set the network ID for the client.
| PARAMETER | DESCRIPTION |
|---|---|
network_id
|
The network identifier.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
Exception
|
If setting the network ID fails. |
set_resolver
¶
set_resolver(resolver: Resolver) -> None
Set a new resolver for node discovery.
| PARAMETER | DESCRIPTION |
|---|---|
resolver
|
The resolver to use.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
Exception
|
If setting the resolver fails. |
start
¶
Start the RPC client (async).
| RAISES | DESCRIPTION |
|---|---|
Exception
|
If starting fails. |
stop
¶
Stop background RPC services (automatically stopped when invoking RpcClient.disconnect).
submit_transaction
¶
submit_transaction(request: SubmitTransactionRequest) -> SubmitTransactionResponse
submit_transaction_replacement
¶
submit_transaction_replacement(request: SubmitTransactionReplacementRequest) -> SubmitTransactionReplacementResponse
subscribe_utxos_changed
¶
subscribe_utxos_changed(addresses: Sequence[Address]) -> None
Subscribe to UTXO changes for specific addresses (async).
| PARAMETER | DESCRIPTION |
|---|---|
addresses
|
List of addresses to monitor.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
Exception
|
If not connected or subscription fails. |
subscribe_virtual_chain_changed
¶
Subscribe to virtual chain changes (async).
| PARAMETER | DESCRIPTION |
|---|---|
include_accepted_transaction_ids
|
Include transaction IDs in notifications.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
Exception
|
If not connected or subscription fails. |
trigger_abort
¶
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.
unsubscribe_utxos_changed
¶
unsubscribe_utxos_changed(addresses: Sequence[Address]) -> None
Unsubscribe from UTXO changes for specific addresses (async).
| PARAMETER | DESCRIPTION |
|---|---|
addresses
|
List of addresses to stop monitoring.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
Exception
|
If not connected or unsubscription fails. |
unsubscribe_virtual_chain_changed
¶
Unsubscribe from virtual chain changes (async).
| PARAMETER | DESCRIPTION |
|---|---|
include_accepted_transaction_ids
|
Must match the subscription parameter.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
Exception
|
If not connected or unsubscription fails. |