@@ -646,13 +646,21 @@ async def all_subnets(
646646 )
647647 if not block_hash and reuse_block :
648648 block_hash = self .substrate .last_block_hash
649- query = await self .substrate .runtime_call (
650- "SubnetInfoRuntimeApi" ,
651- "get_all_dynamic_info" ,
652- block_hash = block_hash ,
649+
650+ query , subnet_prices = await asyncio .gather (
651+ self .substrate .runtime_call (
652+ "SubnetInfoRuntimeApi" ,
653+ "get_all_dynamic_info" ,
654+ block_hash = block_hash ,
655+ ),
656+ self .get_subnet_prices (),
653657 )
654- subnets = DynamicInfo .list_from_dicts (query .decode ())
655- return subnets
658+
659+ decoded = query .decode ()
660+
661+ for sn in decoded :
662+ sn .update ({"price" : subnet_prices .get (sn ["netuid" ], Balance .from_tao (0 ))})
663+ return DynamicInfo .list_from_dicts (decoded )
656664
657665 async def blocks_since_last_step (
658666 self ,
@@ -902,8 +910,13 @@ async def get_all_subnets_info(
902910 )
903911 if not result :
904912 return []
905- else :
906- return SubnetInfo .list_from_dicts (result )
913+
914+ subnets_prices = await self .get_subnet_prices ()
915+
916+ for subnet in result :
917+ subnet .update ({"price" : subnets_prices .get (subnet ["netuid" ], 0 )})
918+
919+ return SubnetInfo .list_from_dicts (result )
907920
908921 async def get_balance (
909922 self ,
@@ -2267,6 +2280,84 @@ async def get_subnet_info(
22672280 return None
22682281 return SubnetInfo .from_dict (result )
22692282
2283+ async def get_subnet_price (
2284+ self ,
2285+ netuid : int ,
2286+ block : Optional [int ] = None ,
2287+ block_hash : Optional [str ] = None ,
2288+ reuse_block : bool = False ,
2289+ ) -> Balance :
2290+ """Gets the current Alpha price in TAO for all subnets.
2291+
2292+ Arguments:
2293+ netuid: The unique identifier of the subnet.
2294+ block: The blockchain block number for the query.
2295+ block_hash (Optional[str]): The hash of the block to retrieve the stake from. Do not specify if using block
2296+ or reuse_block
2297+ reuse_block (bool): Whether to use the last-used block. Do not set if using block_hash or block.
2298+
2299+ Returns:
2300+ The current Alpha price in TAO units for the specified subnet.
2301+ """
2302+ # SN0 price is always 1 TAO
2303+ if netuid == 0 :
2304+ return Balance .from_tao (1 )
2305+
2306+ block_hash = await self .determine_block_hash (
2307+ block = block , block_hash = block_hash , reuse_block = reuse_block
2308+ )
2309+ current_sqrt_price = await self .substrate .query (
2310+ module = "Swap" ,
2311+ storage_function = "AlphaSqrtPrice" ,
2312+ params = [netuid ],
2313+ block_hash = block_hash ,
2314+ )
2315+
2316+ current_sqrt_price = fixed_to_float (current_sqrt_price )
2317+ current_price = current_sqrt_price * current_sqrt_price
2318+ return Balance .from_rao (int (current_price * 1e9 ))
2319+
2320+ async def get_subnet_prices (
2321+ self ,
2322+ block : Optional [int ] = None ,
2323+ block_hash : Optional [str ] = None ,
2324+ reuse_block : bool = False ,
2325+ ) -> dict [int , Balance ]:
2326+ """Gets the current Alpha price in TAO for a specified subnet.
2327+
2328+ Args:
2329+ block: The blockchain block number for the query.
2330+ block_hash (Optional[str]): The hash of the block to retrieve the stake from. Do not specify if using block
2331+ or reuse_block
2332+ reuse_block (bool): Whether to use the last-used block. Do not set if using block_hash or block.
2333+
2334+ Returns:
2335+ dict:
2336+ - subnet unique ID
2337+ - The current Alpha price in TAO units for the specified subnet.
2338+ """
2339+ block_hash = await self .determine_block_hash (
2340+ block = block , block_hash = block_hash , reuse_block = reuse_block
2341+ )
2342+
2343+ current_sqrt_prices = await self .substrate .query_map (
2344+ module = "Swap" ,
2345+ storage_function = "AlphaSqrtPrice" ,
2346+ block_hash = block_hash ,
2347+ page_size = 129 , # total number of subnets
2348+ )
2349+
2350+ prices = {}
2351+ async for id_ , current_sqrt_price in current_sqrt_prices :
2352+ current_sqrt_price = fixed_to_float (current_sqrt_price )
2353+ current_price = current_sqrt_price * current_sqrt_price
2354+ current_price_in_tao = Balance .from_rao (int (current_price * 1e9 ))
2355+ prices .update ({id_ : current_price_in_tao })
2356+
2357+ # SN0 price is always 1 TAO
2358+ prices .update ({0 : Balance .from_tao (1 )})
2359+ return prices
2360+
22702361 async def get_unstake_fee (
22712362 self ,
22722363 amount : Balance ,
@@ -3336,7 +3427,8 @@ async def subnet(
33363427 )
33373428
33383429 if isinstance (decoded := query .decode (), dict ):
3339- return DynamicInfo .from_dict (decoded )
3430+ price = self .get_subnet_price (netuid = netuid , block = block )
3431+ return DynamicInfo .from_dict ({** decoded , "price" : price })
33403432 return None
33413433
33423434 async def subnet_exists (
0 commit comments