Skip to main content

Python SDK for MegaTAO on Bittensor

Project description

MegaTAO Python SDK

Python client for the MegaTAO perpetual futures API on Bittensor.

Installation

pip install megatao-sdk

To enable on-chain write operations (deposit, withdraw, open/close positions), install the chain extra:

pip install megatao-sdk[chain]

Overview

The SDK exposes two sub-clients on the MegaTAO object:

Sub-client Access Purpose
Info client.info Read-only REST API — markets, prices, positions, orders, account data
Chain client.chain On-chain writes — deposit, withdraw, open/close positions, place/cancel orders

client.chain is only available when private_key and rpc_url are provided at construction.


Info Client

Read market data and account state via the REST API. No private key required.

from megatao_sdk import MegaTAO

with MegaTAO() as client:
    markets = client.info.markets()
    for m in markets:
        print(f"{m.symbol}: {m.price}")

Methods

Method Description
markets() List all available markets
market(address) Detailed info for a single market
prices() Current prices for all markets
orderbook(market, levels=10) Order book depth for a market
positions(address) Open positions for a trader
orders(address) Open limit orders for a trader
margin(address) Account summary and margin balances
trades(address) Trade history for a trader
vault() Vault state (reserves, insurance fund)
funding() Current funding rates
platform_stats() Aggregate platform statistics
leaderboard(limit=50, sort_by="pnl") Trader leaderboard
candles(market, resolution="1h", from_ts=None, to_ts=None) OHLCV candle data

All responses are typed Pydantic models. Numeric values are strings to preserve precision.

market = client.info.markets()[0]
market.symbol       # "BTC-USD"
market.price        # "67000.00"
market.max_leverage # 50

Async

import asyncio
from megatao_sdk import AsyncMegaTAO

async def main():
    async with AsyncMegaTAO() as client:
        markets = await client.info.markets()
        positions = await client.info.positions("0xYourAddress")

asyncio.run(main())

Chain Client

Submit on-chain transactions. Requires pip install megatao-sdk[chain] and a funded wallet.

from megatao_sdk import MegaTAO
from megatao_sdk.chain import TAO

client = MegaTAO(
    private_key="0xYourPrivateKey",
    rpc_url="https://your-bittensor-rpc-url",
)

# Deposit collateral
tx_hash = client.chain.deposit(1 * TAO)

# Open a 10x long
tx_hash = client.chain.open_market_position(
    market="0xMarketAddress",
    is_long=True,
    margin_wei=1 * TAO,
    leverage_wei=10 * TAO,
    max_slippage_bps=50,
)

# Close a position (size_to_close_wei=0 closes fully)
tx_hash = client.chain.close_position(position_id=b"...", size_to_close_wei=0)

# Withdraw collateral
tx_hash = client.chain.withdraw(1 * TAO)

TAO = 10**18 — all amounts are in wei (18 decimals).

Pass wait_for_receipt=True to any method to block until mined and return a TxReceipt instead of a tx hash.

Methods

Method Description
deposit(amount_wei) Deposit TAO as collateral
withdraw(amount_wei) Withdraw collateral
open_market_position(market, is_long, margin_wei, leverage_wei, max_slippage_bps) Open a market order position
close_position(position_id, size_to_close_wei=0) Close a position (0 = full close)
place_limit_order(market, is_buy, margin_wei, leverage_wei, price_wei, reduce_only_type=0) Place a limit order
place_take_profit(market, is_buy, margin_wei, leverage_wei, price_wei) Place a take-profit order (reduce-only, triggers on favorable price)
place_stop_loss(market, is_buy, margin_wei, leverage_wei, price_wei) Place a stop-loss order (reduce-only, triggers on adverse price)
cancel_order(market, order_id) Cancel a specific limit order
cancel_all_orders(market) Cancel all open orders for a market

Async

from megatao_sdk import AsyncMegaTAO
from megatao_sdk.chain import TAO

client = AsyncMegaTAO(
    private_key="0xYourPrivateKey",
    rpc_url="https://your-bittensor-rpc-url",
)

tx_hash = await client.chain.deposit(1 * TAO)

Error Handling

from megatao_sdk import MegaTAO, APIError, MegaTAOError

with MegaTAO() as client:
    try:
        info = client.info.market("0xInvalidAddress")
    except APIError as e:
        print(f"HTTP {e.status_code}: {e.message}")
    except MegaTAOError as e:
        print(f"SDK error: {e}")

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

megatao_sdk-0.2.0.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

megatao_sdk-0.2.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file megatao_sdk-0.2.0.tar.gz.

File metadata

  • Download URL: megatao_sdk-0.2.0.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for megatao_sdk-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b69176cc033956431949e57ee0276c8fc538c3ecfe204d3ec5ba9ec3a114acf1
MD5 6edb1792a4002cd8849d85bc58f23b11
BLAKE2b-256 00c52f7bffe65143513efff1ad054049953e7cb2c3188f4f77ad400912157adf

See more details on using hashes here.

File details

Details for the file megatao_sdk-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: megatao_sdk-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for megatao_sdk-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c53c3d845e1a2c1d46660baab5604e7d6b0ce64de671b29204366872f7f06f71
MD5 f666a7360823767af064e33ac91368eb
BLAKE2b-256 7e6b4fecfbae8821f384a8e5239b02c08ecf0d25f2dfd5f94429fb28162e07f2

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