Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/per 1k estimate always zero #1152

Open
wants to merge 2 commits into
base: devnet-ready
Choose a base branch
from
Open
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
38 changes: 30 additions & 8 deletions pallets/subtensor/src/rpc_info/delegate_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,34 @@ pub struct DelegateInfo<T: Config> {
}

impl<T: Config> Pallet<T> {
fn return_per_1000_tao(
take: Compact<u16>,
total_stake: U64F64,
emissions_per_day: U64F64,
) -> U64F64 {
// Get the take as a percentage and subtract it from 1 for remainder.
let without_take: U64F64 = U64F64::from_num(1)
.saturating_sub(U64F64::from_num(take.0).saturating_div(u16::MAX.into()));

if total_stake > U64F64::from_num(0) {
emissions_per_day
.saturating_mul(without_take)
// Divide by 1000 TAO for return per 1k
.saturating_div(total_stake.saturating_div(U64F64::from_num(1000.0 * 1e9)))
} else {
U64F64::from_num(0)
}
}

#[cfg(test)]
pub fn return_per_1000_tao_test(
take: Compact<u16>,
total_stake: U64F64,
emissions_per_day: U64F64,
) -> U64F64 {
Self::return_per_1000_tao(take, total_stake, emissions_per_day)
}

fn get_delegate_by_existing_account(delegate: AccountIdOf<T>) -> DelegateInfo<T> {
let mut nominators = Vec::<(T::AccountId, Compact<u64>)>::new();

Expand Down Expand Up @@ -63,14 +91,8 @@ impl<T: Config> Pallet<T> {
let total_stake: U64F64 =
Self::get_stake_for_hotkey_on_subnet(&delegate.clone(), Self::get_root_netuid()).into();

let return_per_1000: U64F64 = if total_stake > U64F64::from_num(0) {
emissions_per_day
.saturating_mul(u16::MAX.saturating_sub(take.0).into())
.saturating_div(u16::MAX.into())
.saturating_div(total_stake.saturating_div(U64F64::from_num(1000)))
} else {
U64F64::from_num(0)
};
let return_per_1000: U64F64 =
Self::return_per_1000_tao(take, total_stake, emissions_per_day);

DelegateInfo {
delegate_ss58: delegate.clone(),
Expand Down
33 changes: 33 additions & 0 deletions pallets/subtensor/src/tests/delegate_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use codec::Compact;
use substrate_fixed::types::U64F64;

use super::mock::*;

#[test]
fn test_return_per_1000_tao() {
let take = // 18% take to the Validator
Compact::<u16>::from((U64F64::from_num(0.18 * u16::MAX as f64)).saturating_to_num::<u16>());

// 10_000 TAO total validator stake
let total_stake = U64F64::from_num(10_000.0 * 1e9);
// 1000 TAO emissions per day
let emissions_per_day = U64F64::from_num(1000.0 * 1e9);

let return_per_1000 =
SubtensorModule::return_per_1000_tao_test(take, total_stake, emissions_per_day);

// We expect 82 TAO per day with 10% of total_stake
let expected_return_per_1000 = U64F64::from_num(82.0);

let diff_from_expected: f64 = (return_per_1000 / U64F64::from_num(1e9))
.saturating_sub(expected_return_per_1000)
.to_num::<f64>();

let eps: f64 = 0.0005e9; // Precision within 0.0005 TAO
assert!(
diff_from_expected.abs() <= eps,
"Difference from expected: {} is greater than precision: {}",
diff_from_expected,
eps
);
}
1 change: 1 addition & 0 deletions pallets/subtensor/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod batch_tx;
mod children;
mod coinbase;
mod delegate_info;
mod difficulty;
mod emission;
mod epoch;
Expand Down
Loading