diff --git a/bittensor/core/async_subtensor.py b/bittensor/core/async_subtensor.py index e40d8752c6..6f8b367dd7 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, ) - - 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..35979418c1 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)) + ).value + return Balance.from_rao(price_rao) def get_subnet_prices( self, 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, )