Skip to content
222 changes: 111 additions & 111 deletions pallets/proxy/src/weights.rs

Large diffs are not rendered by default.

101 changes: 100 additions & 1 deletion pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use frame_system::{RawOrigin, pallet_prelude::BlockNumberFor};
pub use pallet::*;
use sp_core::H256;
use sp_runtime::{
BoundedVec, Percent,
BoundedVec, Percent, Permill,
traits::{BlakeTwo256, Hash},
};
use sp_std::collections::btree_set::BTreeSet;
Expand Down Expand Up @@ -193,6 +193,44 @@ mod pallet_benchmarks {
);
}

#[benchmark]
fn add_stake_payable() {
let netuid = NetUid::from(1);
let tempo: u16 = 1;

Subtensor::<T>::init_new_network(netuid, tempo);
SubtokenEnabled::<T>::insert(netuid, true);
Subtensor::<T>::set_burn(netuid, benchmark_registration_burn());
Subtensor::<T>::set_network_registration_allowed(netuid, true);
Subtensor::<T>::set_max_allowed_uids(netuid, 4096);

let seed: u32 = 1;
let coldkey: T::AccountId = account("Test", 0, seed);
let app_coldkey: T::AccountId = account("cold", 0, seed);
let hotkey: T::AccountId = account("Alice", 0, seed);
let total_stake = TaoBalance::from(1_000_000_000);
let amount = TaoBalance::from(60_000_000);
let fee_percentage = Permill::from_percent(1);

seed_swap_reserves::<T>(netuid);
add_balance_to_coldkey_account::<T>(&coldkey, total_stake.into());
assert_ok!(Subtensor::<T>::burned_register(
RawOrigin::Signed(coldkey.clone()).into(),
netuid,
hotkey.clone()
));

#[extrinsic_call]
_(
RawOrigin::Signed(coldkey.clone()),
hotkey.clone(),
netuid,
amount,
app_coldkey,
fee_percentage,
);
}

#[benchmark]
fn serve_axon() {
let netuid = NetUid::from(1);
Expand Down Expand Up @@ -1039,6 +1077,67 @@ mod pallet_benchmarks {
);
}

#[benchmark]
fn remove_stake_payable() {
let netuid = NetUid::from(1);
let tempo: u16 = 1;
let seed: u32 = 1;

// Set our total stake to 1000 TAO
Subtensor::<T>::increase_total_stake(1_000_000_000_000_u64.into());

Subtensor::<T>::init_new_network(netuid, tempo);
Subtensor::<T>::set_network_registration_allowed(netuid, true);
SubtokenEnabled::<T>::insert(netuid, true);

Subtensor::<T>::set_max_allowed_uids(netuid, 4096);
assert_eq!(Subtensor::<T>::get_max_allowed_uids(netuid), 4096);

let coldkey: T::AccountId = account("Test", 0, seed);
let hotkey: T::AccountId = account("Alice", 0, seed);
Subtensor::<T>::set_burn(netuid, benchmark_registration_burn());

let app_coldkey: T::AccountId = account("cold", 0, seed);
let fee_percentage = Permill::from_percent(1);
let tao_reserve = TaoBalance::from(150_000_000_000_u64);
let alpha_in = AlphaBalance::from(100_000_000_000_u64);
set_reserves::<T>(netuid, tao_reserve, alpha_in);

let wallet_bal = 1_000_000_000_000u64.into();
add_balance_to_coldkey_account::<T>(&coldkey.clone(), wallet_bal);

assert_ok!(Subtensor::<T>::burned_register(
RawOrigin::Signed(coldkey.clone()).into(),
netuid,
hotkey.clone()
));

let u64_staked_amt = TaoBalance::from(1_000_000_000_000u64);
add_balance_to_coldkey_account::<T>(&coldkey.clone(), u64_staked_amt);

assert_ok!(Subtensor::<T>::add_stake(
RawOrigin::Signed(coldkey.clone()).into(),
hotkey.clone(),
netuid,
u64_staked_amt.into()
));

let amount_unstaked = AlphaBalance::from(30_000_000_000_u64);

// Remove stake limit for benchmark
StakingOperationRateLimiter::<T>::remove((hotkey.clone(), coldkey.clone(), netuid));

#[extrinsic_call]
_(
RawOrigin::Signed(coldkey.clone()),
hotkey.clone(),
netuid,
amount_unstaked,
app_coldkey,
fee_percentage,
);
}

#[benchmark]
fn swap_stake_limit() {
let coldkey: T::AccountId = whitelisted_caller::<AccountIdOf<T>>();
Expand Down
44 changes: 44 additions & 0 deletions pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,28 @@ mod dispatches {
Self::do_remove_stake(origin, hotkey, netuid, amount_unstaked)
}

/// --- The extrinsic is a combination of remove_stake and fees token transfer
#[pallet::call_index(137)]
#[pallet::weight(<T as crate::pallet::Config>::WeightInfo::remove_stake_payable())]
pub fn remove_stake_payable(
origin: OriginFor<T>,
hotkey: T::AccountId,
netuid: NetUid,
amount_unstaked: AlphaBalance,
coldkey_fees_tank: T::AccountId,
fee_percentage: sp_runtime::Permill,
) -> DispatchResult {
Self::do_remove_stake_payable(
origin,
hotkey,
netuid,
amount_unstaked,
coldkey_fees_tank,
fee_percentage,
)
.map(|_| ())
}

/// Serves or updates axon /prometheus information for the neuron associated with the caller. If the caller is
/// already registered the metadata is updated. If the caller is not registered this call throws NotRegistered.
///
Expand Down Expand Up @@ -2531,5 +2553,27 @@ mod dispatches {
Self::deposit_event(Event::AutoParentDelegationEnabledSet { hotkey, enabled });
Ok(())
}

/// --- The extrinsic is a combination of add_stake and fees token transfer
#[pallet::call_index(136)]
#[pallet::weight(<T as crate::pallet::Config>::WeightInfo::add_stake_payable())]
pub fn add_stake_payable(
origin: OriginFor<T>,
hotkey: T::AccountId,
netuid: NetUid,
amount_staked: TaoBalance,
coldkey_fees_tank: T::AccountId,
fee_percentage: sp_runtime::Permill,
) -> DispatchResult {
Self::do_add_stake_payable(
origin,
hotkey,
netuid,
amount_staked,
coldkey_fees_tank,
fee_percentage,
)
.map(|_| ())
}
}
}
5 changes: 5 additions & 0 deletions pallets/subtensor/src/macros/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ mod events {
TaoBalance,
),

/// Fees for the stake has been transferred from actor cold key to the app coldkey.
/// Parameters:
/// (origin_coldkey, destination_coldkey, amount)
FeesTransferred(T::AccountId, T::AccountId, TaoBalance),

/// Stake has been swapped from one subnet to another for the same coldkey-hotkey pair.
///
/// Parameters:
Expand Down
25 changes: 25 additions & 0 deletions pallets/subtensor/src/staking/add_stake.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use sp_runtime::Permill;
use subtensor_runtime_common::{NetUid, TaoBalance};
use subtensor_swap_interface::{Order, SwapHandler};

Expand Down Expand Up @@ -71,6 +72,30 @@ impl<T: Config> Pallet<T> {
)
}

pub fn do_add_stake_payable(
origin: OriginFor<T>,
hotkey: T::AccountId,
netuid: NetUid,
stake_to_be_added: TaoBalance,
coldkey_fees_tank: T::AccountId,
fee_percentage: Permill,
) -> Result<AlphaBalance, DispatchError> {
let coldkey = ensure_signed(origin.clone())?;

ensure!(
Self::can_remove_balance_from_coldkey_account(&coldkey, stake_to_be_added.into()),
Error::<T>::NotEnoughBalanceToStake
);

let amount_fees = TaoBalance::from(fee_percentage.mul_floor(stake_to_be_added.to_u64()));
let stake_after_fees = stake_to_be_added.saturating_sub(amount_fees);

if !amount_fees.is_zero() {
Self::do_transfer_fees(&coldkey, coldkey_fees_tank, amount_fees)?;
}
Self::do_add_stake(origin, hotkey, netuid, stake_after_fees)
}

/// ---- The implementation for the extrinsic add_stake_limit: Adds stake to a hotkey
/// account on a subnet with price limit.
///
Expand Down
23 changes: 23 additions & 0 deletions pallets/subtensor/src/staking/remove_stake.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use sp_runtime::Permill;
use substrate_fixed::types::U96F32;
use subtensor_runtime_common::{AlphaBalance, NetUid, TaoBalance, Token};
use subtensor_swap_interface::{Order, SwapHandler};
Expand Down Expand Up @@ -612,4 +613,26 @@ impl<T: Config> Pallet<T> {

Ok(())
}

pub fn do_remove_stake_payable(
origin: OriginFor<T>,
hotkey: T::AccountId,
netuid: NetUid,
amount_unstaked: AlphaBalance,
coldkey_fees_tank: T::AccountId,
fee_percentage: Permill,
) -> dispatch::DispatchResult {
let coldkey = ensure_signed(origin.clone())?;

let balance_before = Self::get_coldkey_balance(&coldkey);
Self::do_remove_stake(origin, hotkey, netuid, amount_unstaked)?;
let balance_after = Self::get_coldkey_balance(&coldkey);
let tao_received = balance_after.saturating_sub(balance_before);

let amount_fees = TaoBalance::from(fee_percentage.mul_floor(tao_received.to_u64()));
if !amount_fees.is_zero() {
Self::do_transfer_fees(&coldkey, coldkey_fees_tank, amount_fees)?;
}
Ok(())
}
}
11 changes: 11 additions & 0 deletions pallets/subtensor/src/staking/stake_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,17 @@ impl<T: Config> Pallet<T> {
Ok(tao_equivalent)
}

pub fn do_transfer_fees(
from: &<T as frame_system::Config>::AccountId,
to: <T as frame_system::Config>::AccountId,
amount: TaoBalance,
) -> Result<(), DispatchError> {
Self::transfer_tao(from, &to, amount)?;
Self::deposit_event(Event::FeesTransferred(from.clone(), to.clone(), amount));

Ok(())
}

pub fn get_alpha_share_pool(
hotkey: <T as frame_system::Config>::AccountId,
netuid: NetUid,
Expand Down
Loading
Loading