Skip to content

Commit 8338a37

Browse files
authored
Merge pull request #2628 from opentensor/async/thewhaleking/fix-subtensor-methods
2 parents b5893f2 + b937ca5 commit 8338a37

File tree

4 files changed

+50
-489
lines changed

4 files changed

+50
-489
lines changed

bittensor/core/async_subtensor.py

Lines changed: 19 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,14 +1272,9 @@ async def get_hotkey_owner(
12721272
reuse_block_hash=reuse_block,
12731273
)
12741274
exists = False
1275-
val = None
1276-
if hasattr(hk_owner_query, "value"):
1277-
val = decode_account_id(hk_owner_query.value[0])
1278-
if val:
1279-
exists = await self.does_hotkey_exist(
1280-
hotkey_ss58, block_hash=block_hash
1281-
)
1282-
hotkey_owner = val if exists else None
1275+
if hk_owner_query:
1276+
exists = await self.does_hotkey_exist(hotkey_ss58, block_hash=block_hash)
1277+
hotkey_owner = hk_owner_query if exists else None
12831278
return hotkey_owner
12841279

12851280
async def get_minimum_required_stake(self):
@@ -1632,7 +1627,7 @@ async def get_subnet_burn_cost(
16321627
block: Optional[int] = None,
16331628
block_hash: Optional[str] = None,
16341629
reuse_block: bool = False,
1635-
) -> Optional[str]:
1630+
) -> Optional[int]:
16361631
"""
16371632
Retrieves the burn cost for registering a new subnet within the Bittensor network. This cost represents the
16381633
amount of Tao that needs to be locked or burned to establish a new subnet.
@@ -1739,139 +1734,6 @@ async def get_subnets(
17391734
subnets.append(netuid)
17401735
return subnets
17411736

1742-
async def get_total_stake_for_coldkey(
1743-
self,
1744-
ss58_address: str,
1745-
block: Optional[int] = None,
1746-
block_hash: Optional[str] = None,
1747-
reuse_block: bool = False,
1748-
) -> Balance:
1749-
"""
1750-
Returns the total stake held on a coldkey.
1751-
1752-
Arguments:
1753-
ss58_address (str): The SS58 address of the coldkey
1754-
block (Optional[int]): The blockchain block number for the query.
1755-
block_hash (str): The hash of the block number to retrieve the stake from.
1756-
reuse_block (bool): Whether to reuse the last-used block hash.
1757-
1758-
Returns:
1759-
Balance of the stake held on the coldkey.
1760-
"""
1761-
block_hash = await self.determine_block_hash(
1762-
block=block, block_hash=block_hash, reuse_block=reuse_block
1763-
)
1764-
result = await self.substrate.query(
1765-
module="SubtensorModule",
1766-
storage_function="TotalColdkeyStake",
1767-
params=[ss58_address],
1768-
block_hash=block_hash,
1769-
reuse_block_hash=reuse_block,
1770-
)
1771-
return Balance.from_rao(getattr(result, "value", 0))
1772-
1773-
async def get_total_stake_for_coldkeys(
1774-
self,
1775-
*ss58_addresses: str,
1776-
block: Optional[int] = None,
1777-
block_hash: Optional[str] = None,
1778-
reuse_block: bool = False,
1779-
) -> dict[str, Balance]:
1780-
"""
1781-
Returns the total stake held on multiple coldkeys.
1782-
1783-
Arguments:
1784-
ss58_addresses (tuple[str]): The SS58 address(es) of the coldkey(s)
1785-
block (Optional[int]): The blockchain block number for the query.
1786-
block_hash (str): The hash of the block number to retrieve the stake from.
1787-
reuse_block (bool): Whether to reuse the last-used block hash.
1788-
1789-
Returns:
1790-
Dict in view {address: Balance objects}.
1791-
"""
1792-
if reuse_block:
1793-
block_hash = self.substrate.last_block_hash
1794-
elif not block_hash:
1795-
block_hash = await self.substrate.get_chain_head()
1796-
else:
1797-
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
1798-
calls = [
1799-
(
1800-
await self.substrate.create_storage_key(
1801-
"SubtensorModule",
1802-
"TotalColdkeyStake",
1803-
[address],
1804-
block_hash=block_hash,
1805-
)
1806-
)
1807-
for address in ss58_addresses
1808-
]
1809-
batch_call = await self.substrate.query_multi(calls, block_hash=block_hash)
1810-
results = {}
1811-
for item in batch_call:
1812-
results.update({item[0].params[0]: Balance.from_rao(item[1] or 0)})
1813-
return results
1814-
1815-
async def get_total_stake_for_hotkey(
1816-
self,
1817-
ss58_address,
1818-
block: Optional[int] = None,
1819-
block_hash: Optional[str] = None,
1820-
reuse_block: bool = False,
1821-
) -> Balance:
1822-
"""
1823-
Returns the total stake held on a hotkey.
1824-
1825-
Arguments:
1826-
ss58_address (str): The SS58 address of the hotkey
1827-
block (Optional[int]): The blockchain block number for the query.
1828-
block_hash (str): The hash of the block number to retrieve the stake from.
1829-
reuse_block (bool): Whether to reuse the last-used block hash when retrieving info.
1830-
1831-
Returns:
1832-
Balance of the stake held on the hotkey.
1833-
"""
1834-
block_hash = await self.determine_block_hash(
1835-
block=block, block_hash=block_hash, reuse_block=reuse_block
1836-
)
1837-
result = await self.substrate.query(
1838-
module="SubtensorModule",
1839-
storage_function="TotalHotkeyStake",
1840-
params=[ss58_address],
1841-
block_hash=block_hash,
1842-
reuse_block_hash=reuse_block,
1843-
)
1844-
return Balance.from_rao(getattr(result, "value", 0))
1845-
1846-
async def get_total_stake_for_hotkeys(
1847-
self,
1848-
*ss58_addresses,
1849-
block: Optional[int] = None,
1850-
block_hash: Optional[str] = None,
1851-
reuse_block: bool = False,
1852-
) -> dict[str, Balance]:
1853-
"""
1854-
Returns the total stake held on hotkeys.
1855-
1856-
Arguments:
1857-
ss58_addresses (tuple[str]): The SS58 address(es) of the hotkey(s)
1858-
block (Optional[int]): The blockchain block number for the query.
1859-
block_hash (str): The hash of the block number to retrieve the stake from.
1860-
reuse_block (bool): Whether to reuse the last-used block hash when retrieving info.
1861-
1862-
Returns:
1863-
Dict {address: Balance objects}.
1864-
"""
1865-
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
1866-
results = await self.substrate.query_multiple(
1867-
params=[s for s in ss58_addresses],
1868-
module="SubtensorModule",
1869-
storage_function="TotalHotkeyStake",
1870-
block_hash=block_hash,
1871-
reuse_block_hash=reuse_block,
1872-
)
1873-
return {k: Balance.from_rao(r or 0) for (k, r) in results.items()}
1874-
18751737
async def get_total_subnets(
18761738
self,
18771739
block: Optional[int] = None,
@@ -1903,7 +1765,7 @@ async def get_total_subnets(
19031765
return getattr(result, "value", None)
19041766

19051767
async def get_transfer_fee(
1906-
self, wallet: "Wallet", dest: str, value: Union[Balance, float, int]
1768+
self, wallet: "Wallet", dest: str, value: Balance
19071769
) -> Balance:
19081770
"""
19091771
Calculates the transaction fee for transferring tokens from a wallet to a specified destination address. This
@@ -1924,38 +1786,23 @@ async def get_transfer_fee(
19241786
has sufficient funds to cover both the transfer amount and the associated costs. This function provides a
19251787
crucial tool for managing financial operations within the Bittensor network.
19261788
"""
1927-
if isinstance(value, float):
1928-
value = Balance.from_tao(value)
1929-
elif isinstance(value, int):
1930-
value = Balance.from_rao(value)
1931-
1932-
if isinstance(value, Balance):
1933-
call = await self.substrate.compose_call(
1934-
call_module="Balances",
1935-
call_function="transfer_allow_death",
1936-
call_params={"dest": dest, "value": str(value.rao)},
1937-
)
1789+
value = check_and_convert_to_balance(value)
19381790

1939-
try:
1940-
payment_info = await self.substrate.get_payment_info(
1941-
call=call, keypair=wallet.coldkeypub
1942-
)
1943-
except Exception as e:
1944-
logging.error(
1945-
f":cross_mark: [red]Failed to get payment info: [/red]{e}"
1946-
)
1947-
payment_info = {"partial_fee": int(2e7)} # assume 0.02 Tao
1791+
call = await self.substrate.compose_call(
1792+
call_module="Balances",
1793+
call_function="transfer_allow_death",
1794+
call_params={"dest": dest, "value": value.rao},
1795+
)
19481796

1949-
return Balance.from_rao(payment_info["partial_fee"])
1950-
else:
1951-
fee = Balance.from_rao(int(2e7))
1952-
logging.error(
1953-
"To calculate the transaction fee, the value must be Balance, float, or int. Received type: %s. Fee "
1954-
"is %s",
1955-
type(value),
1956-
2e7,
1797+
try:
1798+
payment_info = await self.substrate.get_payment_info(
1799+
call=call, keypair=wallet.coldkeypub
19571800
)
1958-
return fee
1801+
except Exception as e:
1802+
logging.error(f":cross_mark: [red]Failed to get payment info: [/red]{e}")
1803+
payment_info = {"partial_fee": int(2e7)} # assume 0.02 Tao
1804+
1805+
return Balance.from_rao(payment_info["partial_fee"])
19591806

19601807
async def get_vote_data(
19611808
self,

0 commit comments

Comments
 (0)