Skip to content

Introduce SubtensorApi interface for SDK #2862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
May 7, 2025
Merged

Conversation

basfroman
Copy link
Collaborator

@basfroman basfroman commented May 6, 2025

Depends: opentensor/async-substrate-interface#100 (until this PR is merged, most tests fail)

SubtensorApi – Usage Guide

The SubtensorApi is a high-level, unified interface to interact with the Bittensor blockchain. It wraps both the synchronous and asynchronous Subtensor implementations, providing modular access to chain subsystems like wallets, delegates, neurons, and more.


1. Basic Synchronous Usage (default)

import bittensor as bt

sub = bt.SubtensorApi()

print(sub.block)  # Get current block number
print(sub.delegates.get_delegate_identities())
sub.chain.tx_rate_limit()

2. Asynchronous Usage

import bittensor as bt
import asyncio

async def main():
    sub = bt.SubtensorApi(async_subtensor=True)
    async with sub:
        print(await sub.block)
        print(await sub.delegates.get_delegate_identities())
        await sub.chain.tx_rate_limit()

asyncio.run(main())

3. Legacy Method Support

You can enable all legacy methods from the original Subtensor class directly on this interface:

import bittensor as bt

sub = bt.SubtensorApi(legacy_methods=True)
print(sub.bonds(0))  # Classic method usage

4. Retry and Fallback RPC Nodes

Enable redundancy and resilience with fallback chains and retry logic:

import bittensor as bt

sub = bt.SubtensorApi(
    "local",
    fallback_chains=[
        "wss://fallback1.taonetwork.com:9944",
        "wss://lite.sub.latent.to:443",
    ],
    retry_forever=True,
)
print(sub.block)

5. Mock Mode for Testing

Use mock=True to simulate the interface without connecting to the blockchain:

import bittensor as bt

sub = bt.SubtensorApi(mock=True)
print(sub)  # Output: "<Network: None, Chain: Mock, Sync version>"

6. Subsystem Overview

All methods are grouped into logical modules for better organization and readability. Some methods may belong to more than one group if they span multiple functional areas. This does not compromise the internal logic — rather, it enhances discoverability and cohesion. Method equivalence between SubtensorApi and the original Subtensor is automatically verified by test coverage on every pull request (PR).

The following properties are modular access points for specialized subsystems:

Property Description
chain Blockchain interaction methods
commitments Commitment and reveal logic
delegates Delegate management tools
extrinsics Transaction construction and signing
metagraphs Metagraph data and operations
neurons Neuron-level APIs
queries General query endpoints
stakes Staking operations
subnets Subnet access and management
wallets Wallet creation, import/export

7. Custom Configuration Support

You can pass a pre-built Config object:

import argparse
import bittensor as bt
parser = argparse.ArgumentParser('Miner')
bt.SubtensorApi.add_args(parser)
config = bt.config(parser)
sub = bt.SubtensorApi(config=config)

print(sub)

8. Using as a Context Manager (Synchronous)

import bittensor as bt

with bt.SubtensorApi() as sub:
    print(sub.block)

9. Using as a Context Manager (Asynchronous)

import bittensor as bt
import asyncio

async def main():
    async with bt.SubtensorApi(async_subtensor=True) as sub:
        print(await sub.block)

asyncio.run(main())

@basfroman basfroman requested a review from a team May 6, 2025 21:11
@basfroman basfroman self-assigned this May 6, 2025
@basfroman basfroman added the feature new feature added label May 6, 2025
Copy link
Contributor

@thewhaleking thewhaleking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small changes, otherwise g2g

basfroman and others added 2 commits May 7, 2025 13:31
Co-authored-by: BD Himes <37844818+thewhaleking@users.noreply.github.com>
Co-authored-by: BD Himes <37844818+thewhaleking@users.noreply.github.com>
Co-authored-by: BD Himes <37844818+thewhaleking@users.noreply.github.com>
@basfroman basfroman requested a review from thewhaleking May 7, 2025 20:43
@basfroman basfroman merged commit fd22870 into staging May 7, 2025
125 checks passed
@basfroman basfroman deleted the feat/roman/subtensor-api branch May 7, 2025 21:08
@ibraheem-abe ibraheem-abe mentioned this pull request May 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature new feature added
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants