From 7285a6c116c5f74e4509db560605368e93a8f279 Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 22 Sep 2025 17:06:23 -0700 Subject: [PATCH 1/3] update `get_subnet_price` --- bittensor/core/async_subtensor.py | 21 +++++++++------------ bittensor/core/subtensor.py | 17 +++++++---------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/bittensor/core/async_subtensor.py b/bittensor/core/async_subtensor.py index e40d8752c6..210e346387 100644 --- a/bittensor/core/async_subtensor.py +++ b/bittensor/core/async_subtensor.py @@ -2715,11 +2715,10 @@ async def get_subnet_price( ) -> Balance: """Gets the current Alpha price in TAO for all subnets. - Arguments: + Parameters: netuid: The unique identifier of the subnet. block: The blockchain block number for the query. - block_hash: The hash of the block to retrieve the stake from. Do not specify if using block - or reuse_block + block_hash: The hash of the block to retrieve the stake from. Do not specify if using block or reuse_block. reuse_block: Whether to use the last-used block. Do not set if using block_hash or block. Returns: @@ -2729,17 +2728,15 @@ async def get_subnet_price( if netuid == 0: return Balance.from_tao(1) - block_hash = await self.determine_block_hash(block=block) - current_sqrt_price = await self.substrate.query( - module="Swap", - storage_function="AlphaSqrtPrice", + block_hash = await self.determine_block_hash(block, block_hash, reuse_block) + call = await self.substrate.runtime_call( + api="SwapRuntimeApi", + method="current_alpha_price", params=[netuid], - block_hash=block_hash, + block_hash=block_hash ) - - current_sqrt_price = fixed_to_float(current_sqrt_price) - current_price = current_sqrt_price * current_sqrt_price - return Balance.from_rao(int(current_price * 1e9)) + price_rao = call.value + return Balance.from_rao(price_rao) async def get_subnet_prices( self, diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index f01f9cb790..fdc999f1f8 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -1910,7 +1910,7 @@ def get_subnet_price( ) -> Balance: """Gets the current Alpha price in TAO for all subnets. - Arguments: + Parameters: netuid: The unique identifier of the subnet. block: The blockchain block number for the query. @@ -1922,16 +1922,13 @@ def get_subnet_price( return Balance.from_tao(1) block_hash = self.determine_block_hash(block=block) - current_sqrt_price = self.substrate.query( - module="Swap", - storage_function="AlphaSqrtPrice", + price_rao = self.substrate.runtime_call( + api="SwapRuntimeApi", + method="current_alpha_price", params=[netuid], - block_hash=block_hash, - ) - - current_sqrt_price = fixed_to_float(current_sqrt_price) - current_price = current_sqrt_price * current_sqrt_price - return Balance.from_rao(int(current_price * 1e9)) + block_hash=block_hash + ).value + return Balance.from_rao(price_rao) def get_subnet_prices( self, From 84be03516cd8ac4178561585ef4a0bd52cbf69fe Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 22 Sep 2025 17:06:33 -0700 Subject: [PATCH 2/3] tests --- tests/unit_tests/test_async_subtensor.py | 10 +++++----- tests/unit_tests/test_subtensor.py | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/unit_tests/test_async_subtensor.py b/tests/unit_tests/test_async_subtensor.py index c6617427e9..8deef7ab47 100644 --- a/tests/unit_tests/test_async_subtensor.py +++ b/tests/unit_tests/test_async_subtensor.py @@ -3845,10 +3845,10 @@ async def test_get_subnet_price(subtensor, mocker): # preps netuid = 123 mocked_determine_block_hash = mocker.patch.object(subtensor, "determine_block_hash") - fake_price = {"bits": 3155343338053956962} + fake_price = 29258617 expected_price = Balance.from_tao(0.029258617) mocked_query = mocker.patch.object( - subtensor.substrate, "query", return_value=fake_price + subtensor.substrate, "runtime_call", return_value=mocker.Mock(value=fake_price) ) # Call @@ -3857,10 +3857,10 @@ async def test_get_subnet_price(subtensor, mocker): ) # Asserts - mocked_determine_block_hash.assert_awaited_once_with(block=None) + mocked_determine_block_hash.assert_awaited_once() mocked_query.assert_awaited_once_with( - module="Swap", - storage_function="AlphaSqrtPrice", + api="SwapRuntimeApi", + method="current_alpha_price", params=[netuid], block_hash=mocked_determine_block_hash.return_value, ) diff --git a/tests/unit_tests/test_subtensor.py b/tests/unit_tests/test_subtensor.py index 025b6a9b18..04ece6b373 100644 --- a/tests/unit_tests/test_subtensor.py +++ b/tests/unit_tests/test_subtensor.py @@ -4056,10 +4056,10 @@ def test_get_subnet_price(subtensor, mocker): # preps netuid = 123 mocked_determine_block_hash = mocker.patch.object(subtensor, "determine_block_hash") - fake_price = {"bits": 3155343338053956962} + fake_price = 29258617 expected_price = Balance.from_tao(0.029258617) mocked_query = mocker.patch.object( - subtensor.substrate, "query", return_value=fake_price + subtensor.substrate, "runtime_call", return_value=mocker.Mock(value=fake_price) ) # Call @@ -4070,8 +4070,8 @@ def test_get_subnet_price(subtensor, mocker): # Asserts mocked_determine_block_hash.assert_called_once_with(block=None) mocked_query.assert_called_once_with( - module="Swap", - storage_function="AlphaSqrtPrice", + api="SwapRuntimeApi", + method="current_alpha_price", params=[netuid], block_hash=mocked_determine_block_hash.return_value, ) From 2d747de83263fe284112082ffb8f84d30f0c688d Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 22 Sep 2025 17:08:41 -0700 Subject: [PATCH 3/3] ruff --- bittensor/core/async_subtensor.py | 2 +- bittensor/core/subtensor.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bittensor/core/async_subtensor.py b/bittensor/core/async_subtensor.py index 210e346387..6f8b367dd7 100644 --- a/bittensor/core/async_subtensor.py +++ b/bittensor/core/async_subtensor.py @@ -2733,7 +2733,7 @@ async def get_subnet_price( api="SwapRuntimeApi", method="current_alpha_price", params=[netuid], - block_hash=block_hash + block_hash=block_hash, ) price_rao = call.value return Balance.from_rao(price_rao) diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index fdc999f1f8..35979418c1 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -1926,7 +1926,7 @@ def get_subnet_price( api="SwapRuntimeApi", method="current_alpha_price", params=[netuid], - block_hash=block_hash + block_hash=block_hash, ).value return Balance.from_rao(price_rao)