Skip to content

Commit

Permalink
remove fee call (#1115)
Browse files Browse the repository at this point in the history
  • Loading branch information
camfairchild authored Mar 8, 2023
1 parent 44b2c73 commit 786c19a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 173 deletions.
53 changes: 4 additions & 49 deletions bittensor/_subtensor/extrinsics/delegation.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,34 +205,14 @@ def delegate_extrinsic(
else:
staking_balance = staking_balance

# Estimate transfer fee.
staking_fee = None # To be filled.
with bittensor.__console__.status(":satellite: Estimating Delegation Fees..."):
with subtensor.substrate as substrate:
call = substrate.compose_call(
call_module='SubtensorModule',
call_function='add_stake',
call_params={
'hotkey': delegate_ss58,
'amount_staked': staking_balance.rao
}
)
payment_info = substrate.get_payment_info(call = call, keypair = wallet.coldkey)
if payment_info:
staking_fee = bittensor.Balance.from_rao(payment_info['partialFee'])
bittensor.__console__.print("[green]Estimated Fee: {}[/green]".format( staking_fee ))
else:
staking_fee = bittensor.Balance.from_tao( 0.2 )
bittensor.__console__.print(":cross_mark: [red]Failed[/red]: could not estimate delegation fee, assuming base fee of 0.2")

# Check enough balance to stake.
if staking_balance + staking_fee > my_prev_coldkey_balance:
bittensor.__console__.print(":cross_mark: [red]Not enough balance[/red]:[bold white]\n balance:{}\n amount: {}\n fee: {}\n coldkey: {}[/bold white]".format(my_prev_coldkey_balance, staking_balance, staking_fee, wallet.name))
if staking_balance > my_prev_coldkey_balance:
bittensor.__console__.print(":cross_mark: [red]Not enough balance[/red]:[bold white]\n balance:{}\n amount: {}\n coldkey: {}[/bold white]".format(my_prev_coldkey_balance, staking_balance, wallet.name))
return False

# Ask before moving on.
if prompt:
if not Confirm.ask("Do you want to delegate:[bold white]\n amount: {}\n to: {}\n fee: {}\n take: {}\n owner: {}[/bold white]".format( staking_balance, delegate_ss58, staking_fee, delegate_take, delegate_owner) ):
if not Confirm.ask("Do you want to delegate:[bold white]\n amount: {}\n to: {}\n take: {}\n owner: {}[/bold white]".format( staking_balance, delegate_ss58, delegate_take, delegate_owner) ):
return False

try:
Expand Down Expand Up @@ -332,39 +312,14 @@ def undelegate_extrinsic(
else:
staking_balance = amount

# Estimate transfer fee.
staking_fee = None # To be filled.
with bittensor.__console__.status(":satellite: Estimating Delegation Fees..."):
with subtensor.substrate as substrate:
call = substrate.compose_call(
call_module='SubtensorModule',
call_function='remove_stake',
call_params={
'hotkey': delegate_ss58,
'amount_unstaked': staking_balance.rao
}
)
payment_info = substrate.get_payment_info(call = call, keypair = wallet.coldkey)
if payment_info:
staking_fee = bittensor.Balance.from_rao(payment_info['partialFee'])
bittensor.__console__.print("[green]Estimated Fee: {}[/green]".format( staking_fee ))
else:
staking_fee = bittensor.Balance.from_tao( 0.2 )
bittensor.__console__.print(":cross_mark: [red]Failed[/red]: could not estimate delegation fee, assuming base fee of 0.2")

# Check enough stake to unstake.
if staking_balance > my_prev_delegated_stake:
bittensor.__console__.print(":cross_mark: [red]Not enough stake[/red]:[bold white]\n stake:{}\n amount: {}\n coldkey: {}[/bold white]".format(my_prev_delegated_stake, staking_balance, wallet.name))
return False

# Check enough balance to unstake.
if my_prev_coldkey_balance < staking_fee:
bittensor.__console__.print(":cross_mark: [red]Not enough balance[/red]:[bold white]\n balance:{}\n amount: {}\n fee: {}\n coldkey: {}[/bold white]".format(my_prev_coldkey_balance, staking_balance, staking_fee, wallet.name))
return False

# Ask before moving on.
if prompt:
if not Confirm.ask("Do you want to un-delegate:[bold white]\n amount: {}\n to: {}\n fee: {}\n take: {}\n owner: {}[/bold white]".format( staking_balance, delegate_ss58, staking_fee, delegate_take, delegate_owner) ):
if not Confirm.ask("Do you want to un-delegate:[bold white]\n amount: {}\n to: {}\n take: {}\n owner: {}[/bold white]".format( staking_balance, delegate_ss58, delegate_take, delegate_owner) ):
return False

try:
Expand Down
58 changes: 7 additions & 51 deletions bittensor/_subtensor/extrinsics/staking.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,39 +101,19 @@ def add_stake_extrinsic(
else:
staking_balance = staking_balance

# Estimate transfer fee.
staking_fee = None # To be filled.
with bittensor.__console__.status(":satellite: Estimating Staking Fees..."):
with subtensor.substrate as substrate:
call = substrate.compose_call(
call_module='SubtensorModule',
call_function='add_stake',
call_params={
'hotkey': hotkey_ss58,
'amount_staked': staking_balance.rao
}
)
payment_info = substrate.get_payment_info(call = call, keypair = wallet.coldkeypub)
if payment_info:
staking_fee = bittensor.Balance.from_rao(payment_info['partialFee'])
bittensor.__console__.print("[green]Estimated Fee: {}[/green]".format( staking_fee ))
else:
staking_fee = bittensor.Balance.from_tao( 0.2 )
bittensor.__console__.print(":cross_mark: [red]Failed[/red]: could not estimate staking fee, assuming base fee of 0.2")

# Check enough to stake.
if staking_balance > old_balance + staking_fee:
bittensor.__console__.print(":cross_mark: [red]Not enough stake[/red]:[bold white]\n balance:{}\n amount: {}\n fee: {}\n coldkey: {}[/bold white]".format(old_balance, staking_balance, staking_fee, wallet.name))
if staking_balance > old_balance:
bittensor.__console__.print(":cross_mark: [red]Not enough stake[/red]:[bold white]\n balance:{}\n amount: {}\n coldkey: {}[/bold white]".format(old_balance, staking_balance, wallet.name))
return False

# Ask before moving on.
if prompt:
if not own_hotkey:
# We are delegating.
if not Confirm.ask("Do you want to delegate:[bold white]\n amount: {}\n to: {}\n fee: {}\n take: {}\n owner: {}[/bold white]".format( staking_balance, wallet.hotkey_str, staking_fee, hotkey_take, hotkey_owner) ):
if not Confirm.ask("Do you want to delegate:[bold white]\n amount: {}\n to: {}\n take: {}\n owner: {}[/bold white]".format( staking_balance, wallet.hotkey_str, hotkey_take, hotkey_owner) ):
return False
else:
if not Confirm.ask("Do you want to stake:[bold white]\n amount: {}\n to: {}\n fee: {}[/bold white]".format( staking_balance, wallet.hotkey_str, staking_fee) ):
if not Confirm.ask("Do you want to stake:[bold white]\n amount: {}\n to: {}[/bold white]".format( staking_balance, wallet.hotkey_str) ):
return False

try:
Expand Down Expand Up @@ -272,38 +252,14 @@ def add_stake_multiple_extrinsic (
assert isinstance(amount, bittensor.Balance)
staking_balance = amount

# Estimate staking fee.
stake_fee = None # To be filled.
with bittensor.__console__.status(":satellite: Estimating Staking Fees..."):
with subtensor.substrate as substrate:
call = substrate.compose_call(
call_module='SubtensorModule',
call_function='add_stake',
call_params={
'hotkey': hotkey_ss58,
'amount_staked': staking_balance.rao
}
)
payment_info = substrate.get_payment_info(call = call, keypair = wallet.coldkey)
if payment_info:
stake_fee = bittensor.Balance.from_rao(payment_info['partialFee'])
bittensor.__console__.print("[green]Estimated Fee: {}[/green]".format( stake_fee ))
else:
stake_fee = bittensor.Balance.from_tao( 0.2 )
bittensor.__console__.print(":cross_mark: [red]Failed[/red]: could not estimate staking fee, assuming base fee of 0.2")

# Check enough to stake
if staking_all:
staking_balance -= stake_fee
max(staking_balance, bittensor.Balance.from_tao(0))

if staking_balance > old_balance - stake_fee:
if staking_balance > old_balance:
bittensor.__console__.print(":cross_mark: [red]Not enough balance[/red]: [green]{}[/green] to stake: [blue]{}[/blue] from coldkey: [white]{}[/white]".format(old_balance, staking_balance, wallet.name))
continue

# Ask before moving on.
if prompt:
if not Confirm.ask("Do you want to stake:\n[bold white] amount: {}\n hotkey: {}\n fee: {}[/bold white ]?".format( staking_balance, wallet.hotkey_str, stake_fee) ):
if not Confirm.ask("Do you want to stake:\n[bold white] amount: {}\n hotkey: {}[/bold white ]?".format( staking_balance, wallet.hotkey_str) ):
continue

try:
Expand All @@ -320,7 +276,7 @@ def add_stake_multiple_extrinsic (
# We only wait here if we expect finalization.
if not wait_for_finalization and not wait_for_inclusion:
bittensor.__console__.print(":white_heavy_check_mark: [green]Sent[/green]")
old_balance -= staking_balance + stake_fee
old_balance -= staking_balance
successful_stakes += 1
if staking_all:
# If staked all, no need to continue
Expand Down
28 changes: 4 additions & 24 deletions bittensor/_subtensor/extrinsics/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,14 @@ def transfer_extrinsic(
# Check balance.
with bittensor.__console__.status(":satellite: Checking Balance..."):
account_balance = subtensor.get_balance( wallet.coldkey.ss58_address )

# Estimate transfer fee.
with bittensor.__console__.status(":satellite: Estimating Transfer Fees..."):
with subtensor.substrate as substrate:
call = substrate.compose_call(
call_module='Balances',
call_function='transfer',
call_params={
'dest': dest,
'value': transfer_balance.rao
}
)
payment_info = substrate.get_payment_info(call = call, keypair = wallet.coldkey)
transfer_fee = "N/A"
if payment_info:
transfer_fee = bittensor.Balance.from_rao(payment_info['partialFee'])
bittensor.__console__.print("[green]Estimated Fee: {}[/green]".format( transfer_fee ))
else:
bittensor.__console__.print(":cross_mark: [red]Failed[/red]: could not estimate transfer fee, assuming base fee of 0.2")
transfer_fee = bittensor.Balance.from_tao( 0.2 )

if account_balance < transfer_balance + transfer_fee:
bittensor.__console__.print(":cross_mark: [red]Not enough balance[/red]:[bold white]\n balance: {}\n amount: {} fee: {}[/bold white]".format( account_balance, transfer_balance, transfer_fee ))

if account_balance < transfer_balance:
bittensor.__console__.print(":cross_mark: [red]Not enough balance[/red]:[bold white]\n balance: {}\n amount: {}[/bold white]".format( account_balance, transfer_balance ))
return False

# Ask before moving on.
if prompt:
if not Confirm.ask("Do you want to transfer:[bold white]\n amount: {}\n from: {}:{}\n to: {}\n for fee: {}[/bold white]".format( transfer_balance, wallet.name, wallet.coldkey.ss58_address, dest, transfer_fee )):
if not Confirm.ask("Do you want to transfer:[bold white]\n amount: {}\n from: {}:{}\n to: {}[/bold white]".format( transfer_balance, wallet.name, wallet.coldkey.ss58_address, dest )):
return False

with bittensor.__console__.status(":satellite: Transferring..."):
Expand Down
48 changes: 4 additions & 44 deletions bittensor/_subtensor/extrinsics/unstaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,30 +138,10 @@ def unstake_extrinsic (
if unstaking_balance > stake_on_uid:
bittensor.__console__.print(":cross_mark: [red]Not enough stake[/red]: [green]{}[/green] to unstake: [blue]{}[/blue] from hotkey: [white]{}[/white]".format(stake_on_uid, unstaking_balance, wallet.hotkey_str))
return False

# Estimate unstaking fee.
unstake_fee = None # To be filled.
with bittensor.__console__.status(":satellite: Estimating Staking Fees..."):
with subtensor.substrate as substrate:
call = substrate.compose_call(
call_module='SubtensorModule',
call_function='remove_stake',
call_params={
'hotkey': hotkey_ss58,
'amount_unstaked': unstaking_balance.rao
}
)
payment_info = substrate.get_payment_info(call = call, keypair = wallet.coldkey)
if payment_info:
unstake_fee = bittensor.Balance.from_rao(payment_info['partialFee'])
bittensor.__console__.print("[green]Estimated Fee: {}[/green]".format( unstake_fee ))
else:
unstake_fee = bittensor.Balance.from_tao( 0.2 )
bittensor.__console__.print(":cross_mark: [red]Failed[/red]: could not estimate staking fee, assuming base fee of 0.2")


# Ask before moving on.
if prompt:
if not Confirm.ask("Do you want to unstake:\n[bold white] amount: {}\n hotkey: {}\n fee: {}[/bold white ]?".format( unstaking_balance, wallet.hotkey_str, unstake_fee) ):
if not Confirm.ask("Do you want to unstake:\n[bold white] amount: {}\n hotkey: {}[/bold white ]?".format( unstaking_balance, wallet.hotkey_str) ):
return False


Expand Down Expand Up @@ -280,30 +260,10 @@ def unstake_multiple_extrinsic (
if unstaking_balance > stake_on_uid:
bittensor.__console__.print(":cross_mark: [red]Not enough stake[/red]: [green]{}[/green] to unstake: [blue]{}[/blue] from hotkey: [white]{}[/white]".format(stake_on_uid, unstaking_balance, wallet.hotkey_str))
continue

# Estimate unstaking fee.
unstake_fee = None # To be filled.
with bittensor.__console__.status(":satellite: Estimating Staking Fees..."):
with subtensor.substrate as substrate:
call = substrate.compose_call(
call_module='SubtensorModule',
call_function='remove_stake',
call_params={
'hotkey': hotkey_ss58,
'amount_unstaked': unstaking_balance.rao
}
)
payment_info = substrate.get_payment_info(call = call, keypair = wallet.coldkey)
if payment_info:
unstake_fee = bittensor.Balance.from_rao(payment_info['partialFee'])
bittensor.__console__.print("[green]Estimated Fee: {}[/green]".format( unstake_fee ))
else:
unstake_fee = bittensor.Balance.from_tao( 0.2 )
bittensor.__console__.print(":cross_mark: [red]Failed[/red]: could not estimate staking fee, assuming base fee of 0.2")


# Ask before moving on.
if prompt:
if not Confirm.ask("Do you want to unstake:\n[bold white] amount: {}\n hotkey: {}\n fee: {}[/bold white ]?".format( unstaking_balance, wallet.hotkey_str, unstake_fee) ):
if not Confirm.ask("Do you want to unstake:\n[bold white] amount: {}\n hotkey: {}[/bold white ]?".format( unstaking_balance, wallet.hotkey_str) ):
continue

try:
Expand Down
5 changes: 0 additions & 5 deletions tests/unit_tests/bittensor_tests/test_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,6 @@ def test_stake_multiple(self):
substrate=MagicMock(
__enter__=MagicMock(
return_value=MagicMock(
get_payment_info=MagicMock(
return_value={
'partialFee': int(0.125 * 10**9) # 0.125 TAO
}
),
compose_call=mock_compose_call,
),
),
Expand Down

0 comments on commit 786c19a

Please sign in to comment.