Install
openclaw skills install bittensor-sdkInteract with the Bittensor blockchain to manage wallets, stake TAO, register neurons, query subnets/metagraphs, track emissions, and set neuron weights.
openclaw skills install bittensor-sdkComprehensive Bittensor blockchain interaction skill for agents. Enables seamless interaction with the Bittensor decentralized AI network through the Python SDK.
Bittensor is a decentralized machine learning network where independent subnets compete for TAO token emissions. This skill provides agents with full access to:
pip install bittensor>=8.0.0
cp -r skills/bittensor-sdk ~/.opencode/skills/
The skill initializes the Subtensor interface for blockchain interaction:
import bittensor as bt
# Connect to mainnet
subtensor = bt.Subtensor(network="finney")
# Connect to testnet
subtensor = bt.Subtensor(network="test")
# Custom network with fallback endpoints
subtensor = bt.Subtensor(
network="finney",
fallback_endpoints=["wss://entrypoint-finney.opentensor.ai:443"],
retry_forever=True
)
from bittensor import wallet
# Load existing wallet
wallet = bt.Wallet()
# Create new wallet
wallet = bt.Wallet(name="my_wallet", hotkey="miner1")
# Check balances
coldkey_balance = wallet.coldkey_balance
print(f"Coldkey balance: {coldkey_balance}")
# Get all subnet netuids
netuids = subtensor.get_all_subnets_netuid()
print(f"Available subnets: {netuids}")
# Get detailed subnet info
subnet_info = subtensor.get_subnet_info(netuid=1)
print(f"Subnet 1 info: {subnet_info}")
from bittensor import Balance
# Stake TAO to a hotkey
amount = Balance.from_tao(10.0) # 10 TAO
result = subtensor.add_stake(
wallet=wallet,
netuid=1,
hotkey_ss58="5Hx...", # Hotkey SS58 address
amount=amount,
safe_staking=True, # Enable price protection
allow_partial_stake=True
)
print(f"Stake result: {result}")
# Burned registration (recycle TAO)
result = subtensor.burned_register(
wallet=wallet,
netuid=1
)
# POW registration (computational proof)
result = subtensor.register(
wallet=wallet,
netuid=1
)
# Get metagraph for a subnet
metagraph = subtensor.metagraph(netuid=1)
print(f"Number of neurons: {metagraph.n}")
print(f"Stake per neuron: {metagraph.S}")
print(f"Rewards: {metagraph.R}")
print(f"Hotkeys: {metagraph.hotkeys}")
import numpy as np
# Validator sets weights for miners
uids = np.array([0, 1, 2, 3, 4]) # Miner UIDs
weights = np.array([0.2, 0.3, 0.2, 0.15, 0.15]) # Normalized weights
result = subtensor.set_weights(
wallet=validator_wallet,
netuid=1,
uids=uids,
weights=weights,
wait_for_inclusion=True,
wait_for_finalization=True
)
import bittensor as bt
from bittensor import Balance
import numpy as np
# Initialize
subtensor = bt.Subtensor(network="finney")
wallet = bt.Wallet(name="miner_wallet", hotkey="miner1")
# Check balance
balance = subtensor.get_balance(wallet.coldkey.ss58_address)
print(f"Balance: {balance.tao} TAO")
# Register on subnet 1
print("Registering neuron...")
result = subtensor.register(
wallet=wallet,
netuid=1,
wait_for_inclusion=True
)
# Get metagraph info
metagraph = subtensor.metagraph(netuid=1)
print(f"Neurons on subnet 1: {metagraph.n}")
# Check my neuron
my_uid = metagraph.hotkeys.index(wallet.hotkey.ss58_address)
my_neuron = metagraph.neurons[my_uid]
print(f"My UID: {my_uid}")
print(f"My stake: {my_neuron.stake}")
print(f"My emission: {my_neuron.emission}")
import bittensor as bt
from bittensor import Balance
import numpy as np
# Initialize
subtensor = bt.Subtensor(network="finney")
validator_wallet = bt.Wallet(name="validator", hotkey="val1")
# Get metagraph
metagraph = subtensor.metagraph(netuid=1)
print(f"Total miners: {metagraph.n}")
# Calculate weights based on performance
weights = np.zeros(metagraph.n)
for i in range(metagraph.n):
weights[i] = metagraph.R[i] * 0.7 + metagraph.S[i] * 0.3
# Normalize weights
weights = weights / weights.sum()
# Set weights
result = subtensor.set_weights(
wallet=validator_wallet,
netuid=1,
uids=np.arange(metagraph.n),
weights=weights,
wait_for_inclusion=True,
wait_for_finalization=True
)
print(f"Weights set: {result.success}")
Problem: Unable to connect to network Solution:
# Use fallback endpoints
subtensor = bt.Subtensor(
network="finney",
fallback_endpoints=[
"wss://entrypoint-finney.opentensor.ai:443",
"wss://finney.opentensor.io:443"
],
retry_forever=True
)
Problem: Too many requests error Solution:
import time
time.sleep(1) # Rate limit delays
Problem: Registration fails repeatedly Solutions:
Problem: Wallet not found Solution:
# Create new wallet
wallet = bt.Wallet(name="new_wallet", hotkey="new_hotkey")
wallet.create_if_non_existing()
subtensor.close() when doneWhen presenting Bittensor SDK results to users:
Example output format:
=== Subnet 1 Status ===
Neurons: 256 registered
Total Stake: 125,450.5 TAO
Emission: 0.123 TAO/block
Registration Cost: 5.2 TAO
Validator Take: 18%
═══════════════════════════════════
For complete API reference, extended examples, and comprehensive troubleshooting, see: