Skip to main content

Python library for the StackCoin API

Project description

stackcoin-python

Python library for the StackCoin API. Provides a typed async REST client and a WebSocket gateway for real-time events.

Install

pip install stackcoin

Requires Python 3.13+. Dependencies: httpx, pydantic>=2, websockets.

Quick start

import asyncio
import stackcoin

async def main():
    async with stackcoin.Client(token="...") as client:
        me = await client.get_me()
        print(f"{me.username}: {me.balance} STK")

        events = await client.get_events()
        for event in events:
            print(f"[{event.type}] {event.data}")

asyncio.run(main())

Gateway (real-time events)

import stackcoin

gateway = stackcoin.Gateway(token="...")

@gateway.on("transfer.completed")
async def on_transfer(event: stackcoin.TransferCompletedEvent):
    print(f"Transfer of {event.data.amount} STK from #{event.data.from_id} to #{event.data.to_id}")

@gateway.on("request.accepted")
async def on_accepted(event: stackcoin.RequestAcceptedEvent):
    print(f"Request #{event.data.request_id} accepted")

await gateway.connect()

Catching up on missed events

If your bot persists its cursor position and reconnects with a last_event_id, the server replays up to 100 missed events. If more than 100 were missed, the join is rejected — pass a client so the Gateway can automatically catch up via the REST API. Without a client, a TooManyMissedEventsError is raised.

async with stackcoin.Client(token="...") as client:
    gateway = stackcoin.Gateway(
        token="...",
        client=client,
        last_event_id=saved_cursor,
        on_event_id=lambda eid: save_cursor(eid),
    )

    @gateway.on("transfer.completed")
    async def on_transfer(event: stackcoin.TransferCompletedEvent):
        ...

    await gateway.connect()

Examples

  • examples/basic_usage.py -- REST client basics (balance, requests, transactions)
  • examples/simple_cli.py -- interactive REPL with live gateway events

Testing

Tests for this library live in the main StackCoin/StackCoin repository as end-to-end tests that boot a real StackCoin server:

cd /path/to/StackCoin/test/e2e/py
uv sync
uv run pytest

The E2E suite covers the REST client, WebSocket gateway, event pagination, and the LuckyPot bot integration.

Development

Models are generated from the StackCoin OpenAPI spec using datamodel-codegen:

STACKCOIN_ROOT=/path/to/StackCoin just generate

This regenerates src/stackcoin/models.py from openapi.json.

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

stackcoin-0.1.4.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

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

stackcoin-0.1.4-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file stackcoin-0.1.4.tar.gz.

File metadata

  • Download URL: stackcoin-0.1.4.tar.gz
  • Upload date:
  • Size: 22.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.15

File hashes

Hashes for stackcoin-0.1.4.tar.gz
Algorithm Hash digest
SHA256 55f1cb71e9f97766d0f12986f1ce4ae91e2c9205c398aa73d5ea49be135f3712
MD5 455676cfb29795e096e5471526bd758a
BLAKE2b-256 c6a8cdea745b031351140c3895556cea99e1532675a9172068fda2d0c8f77a2e

See more details on using hashes here.

File details

Details for the file stackcoin-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: stackcoin-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.15

File hashes

Hashes for stackcoin-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bc699fc173248cc8eeff9e98da0a31251f438cba081a2cec0f8beb969aaf4a0d
MD5 7c3b88286945f3bd62c2650c58a4a6cd
BLAKE2b-256 d5c014b404cc94b519ca047143e3a666d5abe9e31290e8d32ec513c81bd84f4a

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