120120 price_to_tick ,
121121 LiquidityPosition ,
122122)
123- from bittensor .utils .weight_utils import generate_weight_hash , convert_uids_and_weights
123+ from bittensor .utils .weight_utils import (
124+ generate_weight_hash ,
125+ convert_uids_and_weights ,
126+ U16_MAX ,
127+ )
124128
125129if TYPE_CHECKING :
126130 from bittensor_wallet import Wallet
@@ -445,15 +449,14 @@ def all_subnets(self, block: Optional[int] = None) -> Optional[list["DynamicInfo
445449 Optional[DynamicInfo]: A list of DynamicInfo objects, each containing detailed information about a subnet.
446450
447451 """
448- block_hash = self .determine_block_hash (block )
452+ block_hash = self .determine_block_hash (block = block )
449453 query = self .substrate .runtime_call (
450- "SubnetInfoRuntimeApi" ,
451- "get_all_dynamic_info" ,
454+ api = "SubnetInfoRuntimeApi" ,
455+ method = "get_all_dynamic_info" ,
452456 block_hash = block_hash ,
453457 )
454458 subnet_prices = self .get_subnet_prices ()
455459 decoded = query .decode ()
456-
457460 for sn in decoded :
458461 sn .update ({"price" : subnet_prices .get (sn ["netuid" ], Balance .from_tao (0 ))})
459462 return DynamicInfo .list_from_dicts (decoded )
@@ -1277,13 +1280,9 @@ def get_hotkey_owner(
12771280 def get_minimum_required_stake (self ) -> Balance :
12781281 """
12791282 Returns the minimum required stake for nominators in the Subtensor network.
1280- This method retries the substrate call up to three times with exponential backoff in case of failures.
12811283
12821284 Returns:
1283- Balance: The minimum required stake as a Balance object.
1284-
1285- Raises:
1286- Exception: If the substrate call fails after the maximum number of retries.
1285+ The minimum required stake as a Balance object in TAO.
12871286 """
12881287 result = self .substrate .query (
12891288 module = "SubtensorModule" , storage_function = "NominatorMinRequiredStake"
@@ -1761,6 +1760,7 @@ def get_stake(
17611760
17621761 return Balance .from_rao (int (stake )).set_unit (netuid = netuid )
17631762
1763+ # TODO: remove unused parameters in SDK.v10
17641764 def get_stake_add_fee (
17651765 self ,
17661766 amount : Balance ,
@@ -1782,19 +1782,7 @@ def get_stake_add_fee(
17821782 Returns:
17831783 The calculated stake fee as a Balance object
17841784 """
1785- result = self .query_runtime_api (
1786- runtime_api = "StakeInfoRuntimeApi" ,
1787- method = "get_stake_fee" ,
1788- params = [
1789- None ,
1790- coldkey_ss58 ,
1791- (hotkey_ss58 , netuid ),
1792- coldkey_ss58 ,
1793- amount .rao ,
1794- ],
1795- block = block ,
1796- )
1797- return Balance .from_rao (result )
1785+ return self .get_stake_operations_fee (amount = amount , netuid = netuid , block = block )
17981786
17991787 def get_subnet_info (
18001788 self , netuid : int , block : Optional [int ] = None
@@ -1887,6 +1875,7 @@ def get_subnet_prices(
18871875 prices .update ({0 : Balance .from_tao (1 )})
18881876 return prices
18891877
1878+ # TODO: remove unused parameters in SDK.v10
18901879 def get_unstake_fee (
18911880 self ,
18921881 amount : Balance ,
@@ -1908,20 +1897,9 @@ def get_unstake_fee(
19081897 Returns:
19091898 The calculated stake fee as a Balance object
19101899 """
1911- result = self .query_runtime_api (
1912- runtime_api = "StakeInfoRuntimeApi" ,
1913- method = "get_stake_fee" ,
1914- params = [
1915- (hotkey_ss58 , netuid ),
1916- coldkey_ss58 ,
1917- None ,
1918- coldkey_ss58 ,
1919- amount .rao ,
1920- ],
1921- block = block ,
1922- )
1923- return Balance .from_rao (result )
1900+ return self .get_stake_operations_fee (amount = amount , netuid = netuid , block = block )
19241901
1902+ # TODO: remove unused parameters in SDK.v10
19251903 def get_stake_movement_fee (
19261904 self ,
19271905 amount : Balance ,
@@ -1949,19 +1927,9 @@ def get_stake_movement_fee(
19491927 Returns:
19501928 The calculated stake fee as a Balance object
19511929 """
1952- result = self .query_runtime_api (
1953- runtime_api = "StakeInfoRuntimeApi" ,
1954- method = "get_stake_fee" ,
1955- params = [
1956- (origin_hotkey_ss58 , origin_netuid ),
1957- origin_coldkey_ss58 ,
1958- (destination_hotkey_ss58 , destination_netuid ),
1959- destination_coldkey_ss58 ,
1960- amount .rao ,
1961- ],
1962- block = block ,
1930+ return self .get_stake_operations_fee (
1931+ amount = amount , netuid = origin_netuid , block = block
19631932 )
1964- return Balance .from_rao (result )
19651933
19661934 def get_stake_for_coldkey_and_hotkey (
19671935 self ,
@@ -2049,6 +2017,31 @@ def get_stake_for_hotkey(
20492017
20502018 get_hotkey_stake = get_stake_for_hotkey
20512019
2020+ def get_stake_operations_fee (
2021+ self ,
2022+ amount : Balance ,
2023+ netuid : int ,
2024+ block : Optional [int ] = None ,
2025+ ):
2026+ """Returns fee for any stake operation in specified subnet.
2027+
2028+ Args:
2029+ amount: Amount of stake to add in Alpha/TAO.
2030+ netuid: Netuid of subnet.
2031+ block: Block number at which to perform the calculation.
2032+
2033+ Returns:
2034+ The calculated stake fee as a Balance object.
2035+ """
2036+ block_hash = self .determine_block_hash (block = block )
2037+ result = self .substrate .query (
2038+ module = "Swap" ,
2039+ storage_function = "FeeRate" ,
2040+ params = [netuid ],
2041+ block_hash = block_hash ,
2042+ )
2043+ return amount * (result .value / U16_MAX )
2044+
20522045 def get_subnet_burn_cost (self , block : Optional [int ] = None ) -> Optional [Balance ]:
20532046 """
20542047 Retrieves the burn cost for registering a new subnet within the Bittensor network. This cost represents the
@@ -2700,17 +2693,20 @@ def subnet(self, netuid: int, block: Optional[int] = None) -> Optional[DynamicIn
27002693 Optional[DynamicInfo]: A DynamicInfo object, containing detailed information about a subnet.
27012694
27022695 """
2703- block_hash = self .determine_block_hash (block )
2696+ block_hash = self .determine_block_hash (block = block )
27042697
27052698 query = self .substrate .runtime_call (
2706- "SubnetInfoRuntimeApi" ,
2707- "get_dynamic_info" ,
2699+ api = "SubnetInfoRuntimeApi" ,
2700+ method = "get_dynamic_info" ,
27082701 params = [netuid ],
27092702 block_hash = block_hash ,
27102703 )
27112704
27122705 if isinstance (decoded := query .decode (), dict ):
2713- price = self .get_subnet_price (netuid = netuid , block = block )
2706+ try :
2707+ price = self .get_subnet_price (netuid = netuid , block = block )
2708+ except SubstrateRequestException :
2709+ price = None
27142710 return DynamicInfo .from_dict ({** decoded , "price" : price })
27152711 return None
27162712
0 commit comments