Skip to content

Commit f7c2273

Browse files
committed
Improves all apis
1 parent a5d391d commit f7c2273

File tree

4 files changed

+341
-302
lines changed

4 files changed

+341
-302
lines changed

bittensor/core/async_subtensor.py

Lines changed: 90 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,71 +1575,116 @@ async def get_stake(
15751575

15761576
return Balance.from_rao(int(stake)).set_unit(netuid=netuid)
15771577

1578-
async def get_stake_fee(
1578+
async def get_stake_add_fee(
15791579
self,
1580-
origin_hotkey_ss58: Optional[str],
1581-
origin_netuid: Optional[int],
1582-
origin_coldkey_ss58: str,
1583-
destination_hotkey_ss58: Optional[str],
1584-
destination_netuid: Optional[int],
1585-
destination_coldkey_ss58: str,
1586-
amount: int,
1587-
block_hash: Optional[str] = None,
1580+
amount: Balance,
1581+
netuid: int,
1582+
coldkey_ss58: str,
1583+
hotkey_ss58: str,
1584+
block: Optional[int] = None,
15881585
) -> Balance:
15891586
"""
1590-
Calculates the fee for a staking operation.
1591-
1592-
:param origin_hotkey_ss58: SS58 address of source hotkey (None for new stake)
1593-
:param origin_netuid: Netuid of source subnet (None for new stake)
1594-
:param origin_coldkey_ss58: SS58 address of source coldkey
1595-
:param destination_hotkey_ss58: SS58 address of destination hotkey (None for removing stake)
1596-
:param destination_netuid: Netuid of destination subnet (None for removing stake)
1597-
:param destination_coldkey_ss58: SS58 address of destination coldkey
1598-
:param amount: Amount of stake to transfer in RAO
1599-
:param block_hash: Optional block hash at which to perform the calculation
1587+
Calculates the fee for adding new stake to a hotkey.
16001588
1601-
:return: The calculated stake fee as a Balance object
1589+
Args:
1590+
amount: Amount of stake to add in TAO
1591+
netuid: Netuid of subnet
1592+
coldkey_ss58: SS58 address of source coldkey
1593+
hotkey_ss58: SS58 address of destination hotkey
1594+
block: Block number at which to perform the calculation
16021595
1603-
When to use None:
1596+
Returns:
1597+
The calculated stake fee as a Balance object
1598+
"""
1599+
result = await self.query_runtime_api(
1600+
runtime_api="StakeInfoRuntimeApi",
1601+
method="get_stake_fee",
1602+
params=[
1603+
None,
1604+
coldkey_ss58,
1605+
(hotkey_ss58, netuid),
1606+
coldkey_ss58,
1607+
amount.rao,
1608+
],
1609+
block=block,
1610+
)
1611+
return Balance.from_rao(result)
16041612

1605-
1. Adding new stake (default fee):
1606-
- origin_hotkey_ss58 = None
1607-
- origin_netuid = None
1608-
- All other fields required
1613+
async def get_unstake_fee(
1614+
self,
1615+
amount: Balance,
1616+
netuid: int,
1617+
coldkey_ss58: str,
1618+
hotkey_ss58: str,
1619+
block: Optional[int] = None,
1620+
) -> Balance:
1621+
"""
1622+
Calculates the fee for unstaking from a hotkey.
16091623
1610-
2. Removing stake (default fee):
1611-
- destination_hotkey_ss58 = None
1612-
- destination_netuid = None
1613-
- All other fields required
1624+
Args:
1625+
amount: Amount of stake to unstake in TAO
1626+
netuid: Netuid of subnet
1627+
coldkey_ss58: SS58 address of source coldkey
1628+
hotkey_ss58: SS58 address of destination hotkey
1629+
block: Block number at which to perform the calculation
16141630
1615-
For all other operations, no None values - provide all parameters:
1616-
3. Moving between subnets
1617-
4. Moving between hotkeys
1618-
5. Moving between coldkeys
1631+
Returns:
1632+
The calculated stake fee as a Balance object
16191633
"""
1634+
result = await self.query_runtime_api(
1635+
runtime_api="StakeInfoRuntimeApi",
1636+
method="get_stake_fee",
1637+
params=[
1638+
None,
1639+
coldkey_ss58,
1640+
(hotkey_ss58, netuid),
1641+
coldkey_ss58,
1642+
amount.rao,
1643+
],
1644+
block=block,
1645+
)
1646+
return Balance.from_rao(result)
16201647

1621-
origin = None
1622-
if origin_hotkey_ss58 is not None and origin_netuid is not None:
1623-
origin = (origin_hotkey_ss58, origin_netuid)
1648+
async def get_stake_movement_fee(
1649+
self,
1650+
amount: Balance,
1651+
origin_netuid: int,
1652+
origin_hotkey_ss58: str,
1653+
origin_coldkey_ss58: str,
1654+
destination_netuid: int,
1655+
destination_hotkey_ss58: str,
1656+
destination_coldkey_ss58: str,
1657+
block: Optional[int] = None,
1658+
) -> Balance:
1659+
"""
1660+
Calculates the fee for moving stake between hotkeys/subnets/coldkeys.
16241661
1625-
destination = None
1626-
if destination_hotkey_ss58 is not None and destination_netuid is not None:
1627-
destination = (destination_hotkey_ss58, destination_netuid)
1662+
Args:
1663+
amount: Amount of stake to move in TAO
1664+
origin_netuid: Netuid of source subnet
1665+
origin_hotkey_ss58: SS58 address of source hotkey
1666+
origin_coldkey_ss58: SS58 address of source coldkey
1667+
destination_netuid: Netuid of destination subnet
1668+
destination_hotkey_ss58: SS58 address of destination hotkey
1669+
destination_coldkey_ss58: SS58 address of destination coldkey
1670+
block: Block number at which to perform the calculation
16281671
1672+
Returns:
1673+
The calculated stake fee as a Balance object
1674+
"""
16291675
result = await self.query_runtime_api(
16301676
runtime_api="StakeInfoRuntimeApi",
16311677
method="get_stake_fee",
16321678
params=[
1633-
origin,
1679+
(origin_hotkey_ss58, origin_netuid),
16341680
origin_coldkey_ss58,
1635-
destination,
1681+
(destination_hotkey_ss58, destination_netuid),
16361682
destination_coldkey_ss58,
1637-
amount,
1683+
amount.rao,
16381684
],
1639-
block_hash=block_hash,
1685+
block=block,
16401686
)
1641-
1642-
return Balance.from_rao(result if result is not None else 0)
1687+
return Balance.from_rao(result)
16431688

16441689
async def get_stake_for_coldkey_and_hotkey(
16451690
self,

bittensor/core/subtensor.py

Lines changed: 93 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,67 +1210,115 @@ def get_stake(
12101210

12111211
return Balance.from_rao(int(stake)).set_unit(netuid=netuid)
12121212

1213-
def get_stake_fee(
1213+
def get_stake_add_fee(
12141214
self,
1215-
origin_hotkey_ss58: Optional[str],
1216-
origin_netuid: Optional[int],
1215+
amount: Balance,
1216+
netuid: int,
1217+
coldkey_ss58: str,
1218+
hotkey_ss58: str,
1219+
block: Optional[int] = None,
1220+
) -> Balance:
1221+
"""
1222+
Calculates the fee for adding new stake to a hotkey.
1223+
1224+
Args:
1225+
amount: Amount of stake to add in TAO
1226+
netuid: Netuid of subnet
1227+
coldkey_ss58: SS58 address of coldkey
1228+
hotkey_ss58: SS58 address of hotkey
1229+
block: Block number at which to perform the calculation
1230+
1231+
Returns:
1232+
The calculated stake fee as a Balance object
1233+
"""
1234+
result = self.query_runtime_api(
1235+
runtime_api="StakeInfoRuntimeApi",
1236+
method="get_stake_fee",
1237+
params=[
1238+
None,
1239+
coldkey_ss58,
1240+
(hotkey_ss58, netuid),
1241+
coldkey_ss58,
1242+
amount.rao,
1243+
],
1244+
block=block,
1245+
)
1246+
return Balance.from_rao(result)
1247+
1248+
def get_unstake_fee(
1249+
self,
1250+
amount: Balance,
1251+
netuid: int,
1252+
coldkey_ss58: str,
1253+
hotkey_ss58: str,
1254+
block: Optional[int] = None,
1255+
) -> Balance:
1256+
"""
1257+
Calculates the fee for unstaking from a hotkey.
1258+
1259+
Args:
1260+
amount: Amount of stake to unstake in TAO
1261+
netuid: Netuid of subnet
1262+
coldkey_ss58: SS58 address of coldkey
1263+
hotkey_ss58: SS58 address of hotkey
1264+
block: Block number at which to perform the calculation
1265+
1266+
Returns:
1267+
The calculated stake fee as a Balance object
1268+
"""
1269+
result = self.query_runtime_api(
1270+
runtime_api="StakeInfoRuntimeApi",
1271+
method="get_stake_fee",
1272+
params=[
1273+
(hotkey_ss58, netuid),
1274+
coldkey_ss58,
1275+
None,
1276+
coldkey_ss58,
1277+
amount.rao,
1278+
],
1279+
block=block,
1280+
)
1281+
return Balance.from_rao(result)
1282+
1283+
def get_stake_movement_fee(
1284+
self,
1285+
amount: Balance,
1286+
origin_netuid: int,
1287+
origin_hotkey_ss58: str,
12171288
origin_coldkey_ss58: str,
1218-
destination_hotkey_ss58: Optional[str],
1219-
destination_netuid: Optional[int],
1289+
destination_netuid: int,
1290+
destination_hotkey_ss58: str,
12201291
destination_coldkey_ss58: str,
1221-
amount: Balance,
12221292
block: Optional[int] = None,
12231293
) -> Balance:
12241294
"""
1225-
Calculates the fee for a staking operation.
1226-
:param origin_hotkey_ss58: SS58 address of source hotkey (None for new stake)
1227-
:param origin_netuid: Netuid of source subnet (None for new stake)
1228-
:param origin_coldkey_ss58: SS58 address of source coldkey
1229-
:param destination_hotkey_ss58: SS58 address of destination hotkey (None for removing stake)
1230-
:param destination_netuid: Netuid of destination subnet (None for removing stake)
1231-
:param destination_coldkey_ss58: SS58 address of destination coldkey
1232-
:param amount: Amount of stake to transfer in RAO
1233-
:param block_hash: Optional block hash at which to perform the calculation
1234-
:return: The calculated stake fee as a Balance object
1235-
When to use None:
1236-
1. Adding new stake (default fee):
1237-
- origin_hotkey_ss58 = None
1238-
- origin_netuid = None
1239-
- All other fields required
1240-
2. Removing stake (default fee):
1241-
- destination_hotkey_ss58 = None
1242-
- destination_netuid = None
1243-
- All other fields required
1244-
For all other operations, no None values - provide all parameters:
1245-
3. Moving between subnets
1246-
4. Moving between hotkeys
1247-
5. Moving between coldkeys
1248-
"""
1249-
1250-
origin = None
1251-
if origin_hotkey_ss58 is not None and origin_netuid is not None:
1252-
origin = (origin_hotkey_ss58, origin_netuid)
1253-
1254-
destination = None
1255-
if destination_hotkey_ss58 is not None and destination_netuid is not None:
1256-
destination = (destination_hotkey_ss58, destination_netuid)
1295+
Calculates the fee for moving stake between hotkeys/subnets/coldkeys.
12571296
1297+
Args:
1298+
amount: Amount of stake to move in TAO
1299+
origin_netuid: Netuid of origin subnet
1300+
origin_hotkey_ss58: SS58 address of origin hotkey
1301+
origin_coldkey_ss58: SS58 address of origin coldkey
1302+
destination_netuid: Netuid of destination subnet
1303+
destination_hotkey_ss58: SS58 address of destination hotkey
1304+
destination_coldkey_ss58: SS58 address of destination coldkey
1305+
block: Block number at which to perform the calculation
1306+
1307+
Returns:
1308+
The calculated stake fee as a Balance object
1309+
"""
12581310
result = self.query_runtime_api(
12591311
runtime_api="StakeInfoRuntimeApi",
12601312
method="get_stake_fee",
12611313
params=[
1262-
origin,
1314+
(origin_hotkey_ss58, origin_netuid),
12631315
origin_coldkey_ss58,
1264-
destination,
1316+
(destination_hotkey_ss58, destination_netuid),
12651317
destination_coldkey_ss58,
1266-
amount,
1318+
amount.rao,
12671319
],
12681320
block=block,
12691321
)
1270-
1271-
if result is None:
1272-
raise Exception("Unable to retrieve stake fee.")
1273-
12741322
return Balance.from_rao(result)
12751323

12761324
def get_stake_for_coldkey_and_hotkey(

0 commit comments

Comments
 (0)