Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 57 additions & 39 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import scalecodec
from async_substrate_interface import AsyncSubstrateInterface
from async_substrate_interface.substrate_addons import RetryAsyncSubstrate
from async_substrate_interface.utils.storage import StorageKey
from bittensor_drand import get_encrypted_commitment
from bittensor_wallet.utils import SS58_FORMAT
from numpy.typing import NDArray
Expand Down Expand Up @@ -768,7 +769,8 @@ async def state_call(
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
return await self.substrate.rpc_request(
method="state_call",
params=[method, data, block_hash] if block_hash else [method, data],
params=[method, data],
block_hash=block_hash,
reuse_block_hash=reuse_block,
)

Expand Down Expand Up @@ -2288,29 +2290,35 @@ async def get_liquidity_list(
block=block, block_hash=block_hash, reuse_block=reuse_block
)

query = self.substrate.query
# Fetch global fees and current price
fee_global_tao_query_sk = await self.substrate.create_storage_key(
pallet="Swap",
storage_function="FeeGlobalTao",
params=[netuid],
block_hash=block_hash,
)
fee_global_alpha_query_sk = await self.substrate.create_storage_key(
pallet="Swap",
storage_function="FeeGlobalAlpha",
params=[netuid],
block_hash=block_hash,
)
sqrt_price_query_sk = await self.substrate.create_storage_key(
pallet="Swap",
storage_function="AlphaSqrtPrice",
params=[netuid],
block_hash=block_hash,
)
(
fee_global_tao,
fee_global_alpha,
sqrt_price,
(fee_global_tao_query, fee_global_alpha_query, sqrt_price_query),
positions_response,
) = await asyncio.gather(
query(
module="Swap",
storage_function="FeeGlobalTao",
params=[netuid],
block_hash=block_hash,
),
query(
module="Swap",
storage_function="FeeGlobalAlpha",
params=[netuid],
block_hash=block_hash,
),
query(
module="Swap",
storage_function="AlphaSqrtPrice",
params=[netuid],
self.substrate.query_multi(
[
fee_global_tao_query_sk,
fee_global_alpha_query_sk,
sqrt_price_query_sk,
],
block_hash=block_hash,
),
self.query_map(
Expand All @@ -2321,36 +2329,46 @@ async def get_liquidity_list(
),
)
# convert to floats
fee_global_tao = fixed_to_float(fee_global_tao)
fee_global_alpha = fixed_to_float(fee_global_alpha)
sqrt_price = fixed_to_float(sqrt_price)
fee_global_tao = fixed_to_float(fee_global_tao_query[1])
fee_global_alpha = fixed_to_float(fee_global_alpha_query[1])
sqrt_price = fixed_to_float(sqrt_price_query[1])

# Fetch global fees and current price
current_tick = price_to_tick(sqrt_price**2)

# Fetch positions
positions = []
positions_values: list[tuple[dict, int, int]] = []
positions_storage_keys: list[StorageKey] = []
async for _, p in positions_response:
position = p.value

tick_low_idx = position.get("tick_low")[0]
tick_high_idx = position.get("tick_high")[0]

tick_low, tick_high = await asyncio.gather(
query(
module="Swap",
storage_function="Ticks",
params=[netuid, tick_low_idx],
block_hash=block_hash,
),
query(
module="Swap",
storage_function="Ticks",
params=[netuid, tick_high_idx],
block_hash=block_hash,
),
positions_values.append((position, tick_low_idx, tick_high_idx))
tick_low_sk = await self.substrate.create_storage_key(
pallet="Swap",
storage_function="Ticks",
params=[netuid, tick_low_idx],
block_hash=block_hash,
)
tick_high_sk = await self.substrate.create_storage_key(
pallet="Swap",
storage_function="Ticks",
params=[netuid, tick_high_idx],
block_hash=block_hash,
)
positions_storage_keys.extend([tick_low_sk, tick_high_sk])

# query all our ticks at once
ticks_query = await self.substrate.query_multi(
positions_storage_keys, block_hash=block_hash
)
# iterator with just the values
ticks = iter([x[1] for x in ticks_query])
positions = []
for position, tick_low_idx, tick_high_idx in positions_values:
tick_low = next(ticks)
tick_high = next(ticks)
# Calculate fees above/below range for both tokens
tao_below = get_fees(
current_tick=current_tick,
Expand Down
61 changes: 42 additions & 19 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
from async_substrate_interface.substrate_addons import RetrySyncSubstrate
from async_substrate_interface.sync_substrate import SubstrateInterface
from async_substrate_interface.types import ScaleObj
from async_substrate_interface.utils.storage import StorageKey
from bittensor_drand import get_encrypted_commitment
from numpy.typing import NDArray
from bittensor.utils import deprecated_message

from bittensor.core.async_subtensor import ProposalVoteData
from bittensor.core.axon import Axon
from bittensor.core.chain_data import (
Expand Down Expand Up @@ -105,6 +106,7 @@
torch,
u16_normalized_float,
u64_normalized_float,
deprecated_message,
)
from bittensor.utils.balance import (
Balance,
Expand Down Expand Up @@ -428,8 +430,7 @@ def state_call(
"""
block_hash = self.determine_block_hash(block)
return self.substrate.rpc_request(
method="state_call",
params=[method, data, block_hash] if block_hash else [method, data],
method="state_call", params=[method, data], block_hash=block_hash
)

# Common subtensor calls ===========================================================================================
Expand Down Expand Up @@ -1530,31 +1531,41 @@ def get_liquidity_list(
logging.debug(f"Subnet {netuid} is not active.")
return None

query = self.substrate.query
block_hash = self.determine_block_hash(block)

# Fetch global fees and current price
fee_global_tao_query = query(
module="Swap",
fee_global_tao_query_sk = self.substrate.create_storage_key(
pallet="Swap",
storage_function="FeeGlobalTao",
params=[netuid],
block_hash=block_hash,
)
fee_global_alpha_query = query(
module="Swap",
fee_global_alpha_query_sk = self.substrate.create_storage_key(
pallet="Swap",
storage_function="FeeGlobalAlpha",
params=[netuid],
block_hash=block_hash,
)
sqrt_price_query = query(
module="Swap",
sqrt_price_query_sk = self.substrate.create_storage_key(
pallet="Swap",
storage_function="AlphaSqrtPrice",
params=[netuid],
block_hash=block_hash,
)
fee_global_tao = fixed_to_float(fee_global_tao_query)
fee_global_alpha = fixed_to_float(fee_global_alpha_query)
sqrt_price = fixed_to_float(sqrt_price_query)
fee_global_tao_query, fee_global_alpha_query, sqrt_price_query = (
self.substrate.query_multi(
[
fee_global_tao_query_sk,
fee_global_alpha_query_sk,
sqrt_price_query_sk,
],
block_hash=block_hash,
)
)

fee_global_tao = fixed_to_float(fee_global_tao_query[1])
fee_global_alpha = fixed_to_float(fee_global_alpha_query[1])
sqrt_price = fixed_to_float(sqrt_price_query[1])
current_tick = price_to_tick(sqrt_price**2)

# Fetch positions
Expand All @@ -1564,26 +1575,38 @@ def get_liquidity_list(
block=block,
params=[netuid, wallet.coldkeypub.ss58_address],
)

positions = []
positions_values: list[tuple[dict, int, int]] = []
positions_storage_keys: list[StorageKey] = []
for _, p in positions_response:
position = p.value

tick_low_idx = position["tick_low"][0]
tick_high_idx = position["tick_high"][0]

tick_low = query(
module="Swap",
tick_low_sk = self.substrate.create_storage_key(
pallet="Swap",
storage_function="Ticks",
params=[netuid, tick_low_idx],
block_hash=block_hash,
)
tick_high = query(
module="Swap",
tick_high_sk = self.substrate.create_storage_key(
pallet="Swap",
storage_function="Ticks",
params=[netuid, tick_high_idx],
block_hash=block_hash,
)
positions_values.append((position, tick_low_idx, tick_high_idx))
positions_storage_keys.extend([tick_low_sk, tick_high_sk])
# query all our ticks at once
ticks_query = self.substrate.query_multi(
positions_storage_keys, block_hash=block_hash
)
# iterator with just the values
ticks = iter([x[1] for x in ticks_query])
positions = []
for position, tick_low_idx, tick_high_idx in positions_values:
tick_low = next(ticks)
tick_high = next(ticks)

# Calculate fees above/below range for both tokens
tao_below = get_fees(
Expand Down
3 changes: 1 addition & 2 deletions bittensor/utils/balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,7 @@ def fixed_to_float(
) -> float:
# By default, this is a U64F64
# which is 64 bits of integer and 64 bits of fractional

data: int = fixed["bits"]
data: int = fb.value if isinstance((fb := fixed["bits"]), ScaleType) else fb

# Logical and to get the fractional part; remaining is the integer part
fractional_part = data & (2**frac_bits - 1)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies = [
"uvicorn",
"bittensor-drand>=0.5.0",
"bittensor-wallet>=3.1.0",
"async-substrate-interface>=1.3.1"
"async-substrate-interface>=1.4.2"
]

[project.optional-dependencies]
Expand Down
1 change: 0 additions & 1 deletion tests/unit_tests/extrinsics/asyncex/test_registration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest

from bittensor.core import async_subtensor
from bittensor.core.extrinsics.asyncex import registration as async_registration


Expand Down
3 changes: 1 addition & 2 deletions tests/unit_tests/extrinsics/test_set_weights.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from unittest.mock import MagicMock, patch

import pytest
import torch
# import torch

from bittensor.core import subtensor as subtensor_module
from bittensor.core.extrinsics.set_weights import (
_do_set_weights,
set_weights_extrinsic,
Expand Down
Loading
Loading