Skip to main content

A robust, type-safe Python client for interacting with Nano cryptocurrency nodes via WebSockets

Project description

NanoWS V2

A robust, type-safe Python client for interacting with Nano cryptocurrency nodes via WebSockets.

Features

  • Clean Domain-Driven Design Architecture: Clear separation of concerns with domain, application, and infrastructure layers.
  • Type Safety: Strong typing for all APIs using Python type hints.
  • Automatic Reconnection: Handles connection drops and automatically reconnects to the server.
  • Subscription Restoration: Automatically restores all subscriptions after reconnection.
  • Comprehensive Topic Support: Supports all Nano WebSocket API topics and filters.
  • Typed Messages: Parses WebSocket messages into typed objects for easier handling.

Installation

pip install nanows

Quick Start

import asyncio
from nanows.api import NanoWebSocketClient

async def main():
    # Create the client
    client = NanoWebSocketClient(url="ws://localhost:7078")
    
    try:
        # Connect to the WebSocket server
        await client.connect()
        
        # Subscribe to telemetry messages
        await client.subscribe_telemetry()
        
        # Subscribe to confirmation messages for specific accounts
        await client.subscribe_confirmation(
            accounts=["nano_1ipx847tk8o46pwxt5qjdbncjqcbwcc1rrmqnkztrfjy5k7z4imsrata9est"],
            include_election_info=True
        )
        
        # Receive and process messages
        async for message in client.receive_messages():
            if message.topic == "telemetry":
                print(f"Telemetry update - Block count: {message.block_count}")
            
            elif message.topic == "confirmation":
                print(f"Confirmation - Account: {message.account}, Amount: {message.amount}")
    
    finally:
        # Disconnect from the WebSocket server
        await client.disconnect()

# Run the example
asyncio.run(main())

Using the Context Manager

import asyncio
from nanows.api import NanoWebSocketClient

async def main():
    async with NanoWebSocketClient(url="ws://localhost:7078") as client:
        # Subscribe to telemetry messages
        await client.subscribe_telemetry()
        
        # Receive and process messages
        async for message in client.receive_messages():
            if message.topic == "telemetry":
                print(f"Telemetry update - Block count: {message.block_count}")

# Run the example
asyncio.run(main())

Automatic Reconnection

The client automatically handles connection drops and reconnects to the server. All subscriptions are restored after reconnection.

client = NanoWebSocketClient(
    url="ws://localhost:7078",
    reconnect_attempts=10,  # Maximum number of reconnection attempts
    reconnect_delay=2.0,    # Initial delay between reconnection attempts (seconds)
    reconnect_backoff=1.2,  # Backoff factor for reconnection delay
    keepalive_interval=60   # Interval between keepalive messages (seconds)
)

Supported Topics

  • confirmation: Block confirmations
  • vote: Representative votes
  • telemetry: Node telemetry
  • started_election: Started elections
  • stopped_election: Stopped elections
  • new_unconfirmed_block: New unconfirmed blocks
  • bootstrap: Bootstrap events
  • active_difficulty: Active difficulty updates
  • work: Proof of work generation

Typed Messages

The client parses WebSocket messages into typed objects for easier handling:

async for message in client.receive_messages():
    if isinstance(message, ConfirmationMessage):
        # Handle confirmation message
        print(f"Confirmation - Account: {message.account}, Amount: {message.amount}")
        
    elif isinstance(message, TelemetryMessage):
        # Handle telemetry message
        print(f"Telemetry - Block count: {message.block_count}, Peer count: {message.peer_count}")

Architecture

The client is built using a clean Domain-Driven Design architecture:

  • Domain Layer: Contains the core domain models and interfaces
  • Application Layer: Contains the high-level services that orchestrate the domain
  • Infrastructure Layer: Contains the concrete implementations of the interfaces

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nanows-2.0.1.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nanows-2.0.1-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file nanows-2.0.1.tar.gz.

File metadata

  • Download URL: nanows-2.0.1.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for nanows-2.0.1.tar.gz
Algorithm Hash digest
SHA256 7ba2f069933c832e72c880eeece1ad977b4a9bfb429e8bea76529bc19326c496
MD5 7842c159ae8e2754da9603c1f7c0c4ee
BLAKE2b-256 00c54ceab4f5abb0b8a5d9bf7802af0bfe35432b8a8e34eaaa56390566de3424

See more details on using hashes here.

File details

Details for the file nanows-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: nanows-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for nanows-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0c64232b38bf64d83598398fa27c99ae01d218d76832c78325a446f9bdfb6364
MD5 40e890a297fad92481c847144d005f18
BLAKE2b-256 882fb716b2c6571988c1c4da9c62805bdc4c9a2311d600930f76d8c5d3726774

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page