1010import scalecodec
1111from async_substrate_interface import AsyncSubstrateInterface
1212from async_substrate_interface .substrate_addons import RetryAsyncSubstrate
13+ from async_substrate_interface .utils .storage import StorageKey
1314from bittensor_drand import get_encrypted_commitment
1415from bittensor_wallet .utils import SS58_FORMAT
1516from numpy .typing import NDArray
@@ -768,7 +769,8 @@ async def state_call(
768769 block_hash = await self .determine_block_hash (block , block_hash , reuse_block )
769770 return await self .substrate .rpc_request (
770771 method = "state_call" ,
771- params = [method , data , block_hash ] if block_hash else [method , data ],
772+ params = [method , data ],
773+ block_hash = block_hash ,
772774 reuse_block_hash = reuse_block ,
773775 )
774776
@@ -2288,29 +2290,35 @@ async def get_liquidity_list(
22882290 block = block , block_hash = block_hash , reuse_block = reuse_block
22892291 )
22902292
2291- query = self .substrate .query
2293+ # Fetch global fees and current price
2294+ fee_global_tao_query_sk = await self .substrate .create_storage_key (
2295+ pallet = "Swap" ,
2296+ storage_function = "FeeGlobalTao" ,
2297+ params = [netuid ],
2298+ block_hash = block_hash ,
2299+ )
2300+ fee_global_alpha_query_sk = await self .substrate .create_storage_key (
2301+ pallet = "Swap" ,
2302+ storage_function = "FeeGlobalAlpha" ,
2303+ params = [netuid ],
2304+ block_hash = block_hash ,
2305+ )
2306+ sqrt_price_query_sk = await self .substrate .create_storage_key (
2307+ pallet = "Swap" ,
2308+ storage_function = "AlphaSqrtPrice" ,
2309+ params = [netuid ],
2310+ block_hash = block_hash ,
2311+ )
22922312 (
2293- fee_global_tao ,
2294- fee_global_alpha ,
2295- sqrt_price ,
2313+ (fee_global_tao_query , fee_global_alpha_query , sqrt_price_query ),
22962314 positions_response ,
22972315 ) = await asyncio .gather (
2298- query (
2299- module = "Swap" ,
2300- storage_function = "FeeGlobalTao" ,
2301- params = [netuid ],
2302- block_hash = block_hash ,
2303- ),
2304- query (
2305- module = "Swap" ,
2306- storage_function = "FeeGlobalAlpha" ,
2307- params = [netuid ],
2308- block_hash = block_hash ,
2309- ),
2310- query (
2311- module = "Swap" ,
2312- storage_function = "AlphaSqrtPrice" ,
2313- params = [netuid ],
2316+ self .substrate .query_multi (
2317+ [
2318+ fee_global_tao_query_sk ,
2319+ fee_global_alpha_query_sk ,
2320+ sqrt_price_query_sk ,
2321+ ],
23142322 block_hash = block_hash ,
23152323 ),
23162324 self .query_map (
@@ -2321,36 +2329,46 @@ async def get_liquidity_list(
23212329 ),
23222330 )
23232331 # convert to floats
2324- fee_global_tao = fixed_to_float (fee_global_tao )
2325- fee_global_alpha = fixed_to_float (fee_global_alpha )
2326- sqrt_price = fixed_to_float (sqrt_price )
2332+ fee_global_tao = fixed_to_float (fee_global_tao_query [ 1 ] )
2333+ fee_global_alpha = fixed_to_float (fee_global_alpha_query [ 1 ] )
2334+ sqrt_price = fixed_to_float (sqrt_price_query [ 1 ] )
23272335
23282336 # Fetch global fees and current price
23292337 current_tick = price_to_tick (sqrt_price ** 2 )
23302338
23312339 # Fetch positions
2332- positions = []
2340+ positions_values : list [tuple [dict , int , int ]] = []
2341+ positions_storage_keys : list [StorageKey ] = []
23332342 async for _ , p in positions_response :
23342343 position = p .value
23352344
23362345 tick_low_idx = position .get ("tick_low" )[0 ]
23372346 tick_high_idx = position .get ("tick_high" )[0 ]
2338-
2339- tick_low , tick_high = await asyncio .gather (
2340- query (
2341- module = "Swap" ,
2342- storage_function = "Ticks" ,
2343- params = [netuid , tick_low_idx ],
2344- block_hash = block_hash ,
2345- ),
2346- query (
2347- module = "Swap" ,
2348- storage_function = "Ticks" ,
2349- params = [netuid , tick_high_idx ],
2350- block_hash = block_hash ,
2351- ),
2347+ positions_values .append ((position , tick_low_idx , tick_high_idx ))
2348+ tick_low_sk = await self .substrate .create_storage_key (
2349+ pallet = "Swap" ,
2350+ storage_function = "Ticks" ,
2351+ params = [netuid , tick_low_idx ],
2352+ block_hash = block_hash ,
23522353 )
2354+ tick_high_sk = await self .substrate .create_storage_key (
2355+ pallet = "Swap" ,
2356+ storage_function = "Ticks" ,
2357+ params = [netuid , tick_high_idx ],
2358+ block_hash = block_hash ,
2359+ )
2360+ positions_storage_keys .extend ([tick_low_sk , tick_high_sk ])
23532361
2362+ # query all our ticks at once
2363+ ticks_query = await self .substrate .query_multi (
2364+ positions_storage_keys , block_hash = block_hash
2365+ )
2366+ # iterator with just the values
2367+ ticks = iter ([x [1 ] for x in ticks_query ])
2368+ positions = []
2369+ for position , tick_low_idx , tick_high_idx in positions_values :
2370+ tick_low = next (ticks )
2371+ tick_high = next (ticks )
23542372 # Calculate fees above/below range for both tokens
23552373 tao_below = get_fees (
23562374 current_tick = current_tick ,
0 commit comments