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
19 changes: 8 additions & 11 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand Down
15 changes: 6 additions & 9 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions tests/unit_tests/test_async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)
Expand Down
8 changes: 4 additions & 4 deletions tests/unit_tests/test_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)
Expand Down
Loading