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
14 changes: 14 additions & 0 deletions bittensor_cli/src/bittensor/subtensor_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import aiohttp
from async_substrate_interface.utils.storage import StorageKey
from bittensor_wallet import Wallet
from bittensor_wallet.bittensor_wallet import Keypair
from bittensor_wallet.utils import SS58_FORMAT
from scalecodec import GenericCall
from async_substrate_interface.errors import SubstrateRequestException
Expand Down Expand Up @@ -1488,6 +1489,19 @@ async def get_owned_hotkeys(

return [decode_account_id(hotkey[0]) for hotkey in owned_hotkeys or []]

async def get_extrinsic_fee(self, call: GenericCall, keypair: Keypair) -> Balance:
"""
Determines the fee for the extrinsic call.
Args:
call: Created extrinsic call
keypair: The keypair that would sign the extrinsic (usually you would just want to use the *pub for this)

Returns:
Balance object representing the fee for this extrinsic.
"""
fee_dict = await self.substrate.get_payment_info(call, keypair)
return Balance.from_rao(fee_dict["partial_fee"])

async def get_stake_fee(
self,
origin_hotkey_ss58: Optional[str],
Expand Down
109 changes: 82 additions & 27 deletions bittensor_cli/src/commands/stake/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,45 @@ async def stake_add(
bool: True if stake operation is successful, False otherwise
"""

async def get_stake_extrinsic_fee(
netuid_: int,
amount_: Balance,
staking_address_: str,
safe_staking_: bool,
price_limit: Optional[Balance] = None,
):
"""
Quick method to get the extrinsic fee for adding stake depending on the args supplied.
Args:
netuid_: The netuid where the stake will be added
amount_: the amount of stake to add
staking_address_: the hotkey ss58 to stake to
safe_staking_: whether to use safe staking
price_limit: rate with tolerance

Returns:
Balance object representing the extrinsic fee for adding stake.
"""
call_fn = "add_stake" if not safe_staking_ else "add_stake_limit"
call_params = {
"hotkey": staking_address_,
"netuid": netuid_,
"amount_staked": amount_.rao,
}
if safe_staking_:
call_params.update(
{
"limit_price": price_limit,
"allow_partial": allow_partial_stake,
}
)
call = await subtensor.substrate.compose_call(
call_module="SubtensorModule",
call_function=call_fn,
call_params=call_params,
)
return await subtensor.get_extrinsic_fee(call, wallet.coldkeypub)

async def safe_stake_extrinsic(
netuid_: int,
amount_: Balance,
Expand All @@ -87,7 +126,7 @@ async def safe_stake_extrinsic(
"hotkey": hotkey_ss58_,
"netuid": netuid_,
"amount_staked": amount_.rao,
"limit_price": price_limit,
"limit_price": price_limit.rao,
"allow_partial": allow_partial_stake,
},
),
Expand Down Expand Up @@ -332,19 +371,6 @@ async def stake_extrinsic(
# Temporary workaround - calculations without slippage
current_price_float = float(subnet_info.price.tao)
rate = 1.0 / current_price_float
received_amount = rate * amount_to_stake

# Add rows for the table
base_row = [
str(netuid), # netuid
f"{hotkey[1]}", # hotkey
str(amount_to_stake), # amount
str(rate)
+ f" {Balance.get_unit(netuid)}/{Balance.get_unit(0)} ", # rate
str(received_amount.set_unit(netuid)), # received
str(stake_fee), # fee
# str(slippage_pct), # slippage
]

# If we are staking safe, add price tolerance
if safe_staking:
Expand All @@ -356,21 +382,45 @@ async def stake_extrinsic(
rate_with_tolerance = f"{_rate_with_tolerance:.4f}"
price_with_tolerance = Balance.from_tao(
price_with_tolerance
).rao # Actual price to pass to extrinsic
) # Actual price to pass to extrinsic
else:
rate_with_tolerance = "1"
price_with_tolerance = Balance.from_rao(1)
extrinsic_fee = await get_stake_extrinsic_fee(
netuid_=netuid,
amount_=amount_to_stake,
staking_address_=hotkey[1],
safe_staking_=safe_staking,
price_limit=price_with_tolerance,
)
prices_with_tolerance.append(price_with_tolerance)

base_row.extend(
[
f"{rate_with_tolerance} {Balance.get_unit(netuid)}/{Balance.get_unit(0)} ",
f"[{'dark_sea_green3' if allow_partial_stake else 'red'}]"
# safe staking
f"{allow_partial_stake}[/{'dark_sea_green3' if allow_partial_stake else 'red'}]",
]
row_extension = [
f"{rate_with_tolerance} {Balance.get_unit(netuid)}/{Balance.get_unit(0)} ",
f"[{'dark_sea_green3' if allow_partial_stake else 'red'}]"
# safe staking
f"{allow_partial_stake}[/{'dark_sea_green3' if allow_partial_stake else 'red'}]",
]
else:
extrinsic_fee = await get_stake_extrinsic_fee(
netuid_=netuid,
amount_=amount_to_stake,
staking_address_=hotkey[1],
safe_staking_=safe_staking,
)

row_extension = []
received_amount = rate * (amount_to_stake - stake_fee - extrinsic_fee)
# Add rows for the table
base_row = [
str(netuid), # netuid
f"{hotkey[1]}", # hotkey
str(amount_to_stake), # amount
str(rate)
+ f" {Balance.get_unit(netuid)}/{Balance.get_unit(0)} ", # rate
str(received_amount.set_unit(netuid)), # received
str(stake_fee), # fee
str(extrinsic_fee),
# str(slippage_pct), # slippage
] + row_extension
rows.append(tuple(base_row))

# Define and print stake table + slippage warning
Expand Down Expand Up @@ -569,17 +619,17 @@ def _define_stake_table(
"Hotkey", justify="center", style=COLOR_PALETTE["GENERAL"]["HOTKEY"]
)
table.add_column(
f"Amount ({Balance.get_unit(0)})",
"Amount (τ)",
justify="center",
style=COLOR_PALETTE["POOLS"]["TAO"],
)
table.add_column(
f"Rate (per {Balance.get_unit(0)})",
"Rate (per τ)",
justify="center",
style=COLOR_PALETTE["POOLS"]["RATE"],
)
table.add_column(
"Received",
"Est. Received",
justify="center",
style=COLOR_PALETTE["POOLS"]["TAO_EQUIV"],
)
Expand All @@ -588,6 +638,11 @@ def _define_stake_table(
justify="center",
style=COLOR_PALETTE["STAKE"]["STAKE_AMOUNT"],
)
table.add_column(
"Extrinsic Fee (τ)",
justify="center",
style=COLOR_PALETTE.STAKE.TAO,
)
# TODO: Uncomment when slippage is reimplemented for v3
# table.add_column(
# "Slippage", justify="center", style=COLOR_PALETTE["STAKE"]["SLIPPAGE_PERCENT"]
Expand Down
Loading
Loading