Skip to content

Commit 0b5df3c

Browse files
committed
test wip -- set price
1 parent 7a9b662 commit 0b5df3c

File tree

1 file changed

+78
-1
lines changed

1 file changed

+78
-1
lines changed

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use approx::assert_abs_diff_eq;
88
use frame_support::assert_ok;
99
use pallet_subtensor_swap::position::PositionId;
1010
use sp_core::U256;
11-
use substrate_fixed::types::{I64F64, I96F32, U64F64, U96F32};
11+
use substrate_fixed::{
12+
transcendental::sqrt,
13+
types::{I64F64, I96F32, U64F64, U96F32},
14+
};
1215
use subtensor_runtime_common::{AlphaCurrency, NetUidStorageIndex};
1316
use subtensor_swap_interface::{SwapEngine, SwapHandler};
1417

@@ -3333,3 +3336,77 @@ fn test_coinbase_subnets_with_no_reg_get_no_emission() {
33333336
assert!(alpha_out_2[&netuid1] > zero);
33343337
});
33353338
}
3339+
3340+
#[test]
3341+
fn test_coinbase_alpha_in_more_than_alpha_emission() {
3342+
new_test_ext(1).execute_with(|| {
3343+
let zero = U96F32::saturating_from_num(0);
3344+
let netuid0 = add_dynamic_network(&U256::from(1), &U256::from(2));
3345+
mock::setup_reserves(
3346+
netuid0,
3347+
TaoCurrency::from(1_000_000_000_000_000),
3348+
AlphaCurrency::from(1_000_000_000_000_000),
3349+
);
3350+
3351+
// Set netuid0 to have price tao_emission / price > alpha_emission
3352+
let alpha_emission = U96F32::saturating_from_num(
3353+
SubtensorModule::get_block_emission_for_issuance(
3354+
SubtensorModule::get_alpha_issuance(netuid0).into(),
3355+
)
3356+
.unwrap_or(0),
3357+
);
3358+
let price_to_set: U64F64 = U64F64::saturating_from_num(0.2);
3359+
let price_to_set_fixed: U96F32 = U96F32::saturating_from_num(price_to_set);
3360+
let sqrt_price_to_set: U64F64 = sqrt(price_to_set).unwrap();
3361+
3362+
let tao_emission: U96F32 = U96F32::saturating_from_num(alpha_emission)
3363+
.saturating_mul(price_to_set_fixed)
3364+
.saturating_add(U96F32::saturating_from_num(0.01));
3365+
3366+
// Set the price
3367+
pallet_subtensor_swap::AlphaSqrtPrice::<Test>::insert(netuid0, sqrt_price_to_set);
3368+
// Check the price is set
3369+
assert_abs_diff_eq!(
3370+
pallet_subtensor_swap::Pallet::<Test>::current_alpha_price(netuid0).to_num::<f64>(),
3371+
price_to_set.to_num::<f64>(),
3372+
epsilon = 1.0
3373+
);
3374+
3375+
let subnet_emissions = BTreeMap::from([(netuid0, tao_emission)]);
3376+
3377+
let (tao_in, alpha_in, alpha_out, subsidy_amount) =
3378+
SubtensorModule::get_subnet_terms(&subnet_emissions);
3379+
3380+
// Check our condition is met
3381+
assert!(tao_emission / price_to_set_fixed > alpha_emission);
3382+
3383+
// alpha_out should be the alpha_emission, always
3384+
assert_abs_diff_eq!(
3385+
alpha_out[&netuid0].to_num::<f64>(),
3386+
alpha_emission.to_num::<f64>(),
3387+
epsilon = 1.0
3388+
);
3389+
3390+
// alpha_in should equal the alpha_emission
3391+
assert_abs_diff_eq!(
3392+
alpha_in[&netuid0].to_num::<f64>(),
3393+
alpha_emission.to_num::<f64>(),
3394+
epsilon = 1.0
3395+
);
3396+
// tao_in should be the alpha_in at the ratio of the price
3397+
assert_abs_diff_eq!(
3398+
tao_in[&netuid0].to_num::<f64>(),
3399+
alpha_in[&netuid0]
3400+
.saturating_mul(price_to_set_fixed)
3401+
.to_num::<f64>(),
3402+
epsilon = 1.0
3403+
);
3404+
3405+
// subsidy_amount should be the difference between the tao_emission and the tao_in
3406+
assert_abs_diff_eq!(
3407+
subsidy_amount[&netuid0].to_num::<f64>(),
3408+
tao_emission.to_num::<f64>() - tao_in[&netuid0].to_num::<f64>(),
3409+
epsilon = 1.0
3410+
);
3411+
});
3412+
}

0 commit comments

Comments
 (0)