|
| 1 | +from typing import Optional, Union, TYPE_CHECKING |
| 2 | + |
| 3 | +from bittensor.core.async_subtensor import AsyncSubtensor as _AsyncSubtensor |
| 4 | +from bittensor.core.subtensor import Subtensor as _Subtensor |
| 5 | +from .chain import Chain as _Chain |
| 6 | +from .commitments import Commitments as _Commitments |
| 7 | +from .delegates import Delegates as _Delegates |
| 8 | +from .extrinsics import Extrinsics as _Extrinsics |
| 9 | +from .metagraph import Metagraphs as _Metagraphs |
| 10 | +from .neurons import Neurons as _Neurons |
| 11 | +from .queries import Queries as _Queries |
| 12 | +from .stakes import Stakes as _Stakes |
| 13 | +from .subnet import Subnet as _Subnet |
| 14 | +from .wallet import Wallet as _Wallet |
| 15 | + |
| 16 | +if TYPE_CHECKING: |
| 17 | + from bittensor.core.config import Config |
| 18 | + |
| 19 | + |
| 20 | +class SubtensorApi: |
| 21 | + """Subtensor API class. |
| 22 | +
|
| 23 | + Arguments: |
| 24 | + network: The network to connect to. Defaults to `None` -> `finney`. |
| 25 | + config: Bittensor configuration object. Defaults to `None`. |
| 26 | + log_verbose: If true, sets the subtensor to log verbosely. Defaults to `False`. |
| 27 | + async_subtensor: If true, uses the async subtensor to create the connection. Defaults to `False`. |
| 28 | +
|
| 29 | + Example: |
| 30 | + # sync version |
| 31 | + import bittensor as bt |
| 32 | +
|
| 33 | + subtensor = bt.SubtensorApi() |
| 34 | + print(subtensor.block) |
| 35 | + print(subtensor.delegates.get_delegate_identities()) |
| 36 | + subtensor.chain.tx_rate_limit() |
| 37 | +
|
| 38 | + # async version |
| 39 | + import bittensor as bt |
| 40 | +
|
| 41 | + subtensor = bt.SubtensorApi(async_subtensor=True) |
| 42 | + async with subtensor: |
| 43 | + print(await subtensor.block) |
| 44 | + print(await subtensor.delegates.get_delegate_identities()) |
| 45 | + print(await subtensor.chain.tx_rate_limit()) |
| 46 | + """ |
| 47 | + |
| 48 | + def __init__( |
| 49 | + self, |
| 50 | + network: Optional[str] = None, |
| 51 | + config: Optional["Config"] = None, |
| 52 | + log_verbose: bool = False, |
| 53 | + async_subtensor: bool = False, |
| 54 | + _mock: bool = False, |
| 55 | + ): |
| 56 | + self.network = network |
| 57 | + self._mock = _mock |
| 58 | + self.log_verbose = log_verbose |
| 59 | + self.is_async = async_subtensor |
| 60 | + self._config = config |
| 61 | + self._subtensor = self._get_subtensor() |
| 62 | + |
| 63 | + # define empty fields |
| 64 | + self.substrate = self._subtensor.substrate |
| 65 | + self.add_args = self._subtensor.add_args |
| 66 | + self.chain_endpoint = self._subtensor.chain_endpoint |
| 67 | + self.close = self._subtensor.close |
| 68 | + self.config = self._subtensor.config |
| 69 | + self.setup_config = self._subtensor.setup_config |
| 70 | + self.help = self._subtensor.help |
| 71 | + |
| 72 | + self.determine_block_hash = self._subtensor.determine_block_hash |
| 73 | + self.encode_params = self._subtensor.encode_params |
| 74 | + self.sign_and_send_extrinsic = self._subtensor.sign_and_send_extrinsic |
| 75 | + self.start_call = self._subtensor.start_call |
| 76 | + self.wait_for_block = self._subtensor.wait_for_block |
| 77 | + |
| 78 | + def _get_subtensor(self) -> Union["_Subtensor", "_AsyncSubtensor"]: |
| 79 | + """Returns the subtensor instance based on the provided config and subtensor type flag.""" |
| 80 | + if self.is_async: |
| 81 | + return _AsyncSubtensor( |
| 82 | + network=self.network, |
| 83 | + config=self._config, |
| 84 | + _mock=self._mock, |
| 85 | + log_verbose=self.log_verbose, |
| 86 | + ) |
| 87 | + else: |
| 88 | + return _Subtensor( |
| 89 | + network=self.network, |
| 90 | + config=self._config, |
| 91 | + _mock=self._mock, |
| 92 | + log_verbose=self.log_verbose, |
| 93 | + ) |
| 94 | + |
| 95 | + def __str__(self): |
| 96 | + return f"<Network: {self.network}, Chain: {self.chain_endpoint}, {'Async version' if self.is_async else 'Sync version'}>" |
| 97 | + |
| 98 | + def __repr__(self): |
| 99 | + return self.__str__() |
| 100 | + |
| 101 | + def __enter__(self): |
| 102 | + return self |
| 103 | + |
| 104 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 105 | + self.close() |
| 106 | + |
| 107 | + async def __aenter__(self): |
| 108 | + return await self._subtensor.__aenter__() |
| 109 | + |
| 110 | + async def __aexit__(self, exc_type, exc_val, exc_tb): |
| 111 | + await self.substrate.close() |
| 112 | + |
| 113 | + @property |
| 114 | + def block(self): |
| 115 | + return self._subtensor.block |
| 116 | + |
| 117 | + @property |
| 118 | + def chain(self): |
| 119 | + return _Chain(self._subtensor) |
| 120 | + |
| 121 | + @property |
| 122 | + def commitments(self): |
| 123 | + return _Commitments(self._subtensor) |
| 124 | + |
| 125 | + @property |
| 126 | + def delegates(self): |
| 127 | + return _Delegates(self._subtensor) |
| 128 | + |
| 129 | + @property |
| 130 | + def extrinsics(self): |
| 131 | + return _Extrinsics(self._subtensor) |
| 132 | + |
| 133 | + @property |
| 134 | + def metagraphs(self): |
| 135 | + return _Metagraphs(self._subtensor) |
| 136 | + |
| 137 | + @property |
| 138 | + def neurons(self): |
| 139 | + return _Neurons(self._subtensor) |
| 140 | + |
| 141 | + @property |
| 142 | + def queries(self): |
| 143 | + return _Queries(self._subtensor) |
| 144 | + |
| 145 | + @property |
| 146 | + def stakes(self): |
| 147 | + return _Stakes(self._subtensor) |
| 148 | + |
| 149 | + @property |
| 150 | + def subnet(self): |
| 151 | + return _Subnet(self._subtensor) |
| 152 | + |
| 153 | + @property |
| 154 | + def wallet(self): |
| 155 | + return _Wallet(self._subtensor) |
0 commit comments