Skip to content

Commit a4c49e3

Browse files
authored
Merge pull request #1189 from opentensor/feat/new-pool-init
change pool liq init
2 parents 2977bdf + f5df32a commit a4c49e3

File tree

4 files changed

+64
-30
lines changed

4 files changed

+64
-30
lines changed

pallets/subtensor/src/migrations/migrate_rao.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,26 @@ pub fn migrate_rao<T: Config>() -> Weight {
7171
}
7272
let owner: T::AccountId = SubnetOwner::<T>::get(netuid);
7373
let lock: u64 = SubnetLocked::<T>::get(netuid);
74-
let initial_liquidity: u64 = 100_000_000_000; // 100 TAO.
75-
let remaining_lock: u64 = lock.saturating_sub(initial_liquidity);
74+
75+
// Put initial TAO from lock into subnet TAO and produce numerically equal amount of Alpha
76+
// The initial TAO is the locked amount, with a minimum of 1 RAO and a cap of 100 TAO.
77+
let pool_initial_tao = 100_000_000_000.min(lock.max(1));
78+
79+
let remaining_lock = lock.saturating_sub(pool_initial_tao);
80+
// Refund the owner for the remaining lock.
7681
Pallet::<T>::add_balance_to_coldkey_account(&owner, remaining_lock);
77-
SubnetTAO::<T>::insert(netuid, initial_liquidity); // Set TAO to the lock.
78-
SubnetAlphaIn::<T>::insert(netuid, initial_liquidity); // Set AlphaIn to the initial alpha distribution.
82+
SubnetTAO::<T>::insert(netuid, pool_initial_tao); // Set TAO to the lock.
83+
84+
SubnetAlphaIn::<T>::insert(
85+
netuid,
86+
pool_initial_tao.saturating_mul(netuids.len() as u64),
87+
); // Set AlphaIn to the initial alpha distribution.
88+
7989
SubnetAlphaOut::<T>::insert(netuid, 0); // Set zero subnet alpha out.
8090
SubnetMechanism::<T>::insert(netuid, 1); // Convert to dynamic immediately with initialization.
8191
Tempo::<T>::insert(netuid, DefaultTempo::<T>::get());
8292
// Set the token symbol for this subnet using Self instead of Pallet::<T>
8393
TokenSymbol::<T>::insert(netuid, Pallet::<T>::get_symbol_for_subnet(*netuid));
84-
SubnetTAO::<T>::insert(netuid, initial_liquidity); // Set TAO to the lock.
8594
TotalStakeAtDynamic::<T>::insert(netuid, 0);
8695

8796
if let Ok(owner_coldkey) = SubnetOwner::<T>::try_get(netuid) {

pallets/subtensor/src/subnets/subnet.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,18 @@ impl<T: Config> Pallet<T> {
233233
Self::get_symbol_for_subnet(netuid_to_register),
234234
); // Set subnet token symbol.
235235

236-
// Put 100 TAO from lock into subnet TAO and produce numerically equal amount of Alpha
237-
let mut pool_initial_tao = 100_000_000_000;
238-
if pool_initial_tao > actual_tao_lock_amount {
239-
pool_initial_tao = actual_tao_lock_amount;
240-
}
241-
if pool_initial_tao < 1 {
242-
pool_initial_tao = 1;
243-
}
236+
// Put initial TAO from lock into subnet TAO and produce numerically equal amount of Alpha
237+
// The initial TAO is the locked amount, with a minimum of 1 RAO and a cap of 100 TAO.
238+
let pool_initial_tao = 100_000_000_000.min(actual_tao_lock_amount.max(1));
239+
244240
let actual_tao_lock_amount_less_pool_tao =
245241
actual_tao_lock_amount.saturating_sub(pool_initial_tao);
246242
SubnetTAO::<T>::insert(netuid_to_register, pool_initial_tao);
247-
SubnetAlphaIn::<T>::insert(netuid_to_register, pool_initial_tao);
243+
SubnetAlphaIn::<T>::insert(
244+
netuid_to_register,
245+
pool_initial_tao.saturating_mul(Self::get_all_subnet_netuids().len() as u64),
246+
); // Set AlphaIn to the initial alpha distribution.
247+
248248
SubnetOwner::<T>::insert(netuid_to_register, coldkey.clone());
249249
SubnetOwnerHotkey::<T>::insert(netuid_to_register, hotkey.clone());
250250
TotalStakeAtDynamic::<T>::insert(netuid_to_register, TotalStake::<T>::get());

pallets/subtensor/src/tests/migration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,9 @@ fn test_migrate_rao() {
598598

599599
// Verify root subnet (netuid 0) state after migration
600600
assert_eq!(SubnetTAO::<Test>::get(netuid_0), 4 * stake_amount); // Root has everything
601-
assert_eq!(SubnetTAO::<Test>::get(netuid_1), 100_000_000_000); // Initial Rao amount.
601+
assert_eq!(SubnetTAO::<Test>::get(netuid_1), lock_amount); // Initial Rao amount.
602602
assert_eq!(SubnetAlphaIn::<Test>::get(netuid_0), 1); // No Alpha in pool on root.
603-
assert_eq!(SubnetAlphaIn::<Test>::get(netuid_1), 100_000_000_000); // Initial Rao amount.
603+
assert_eq!(SubnetAlphaIn::<Test>::get(netuid_1), 2 * lock_amount); // Initial Rao amount == num_subnets * lock_amount
604604
assert_eq!(SubnetAlphaOut::<Test>::get(netuid_0), 4 * stake_amount); // All stake is outstanding.
605605
assert_eq!(SubnetAlphaOut::<Test>::get(netuid_1), 0); // No stake outstanding.
606606

pallets/subtensor/src/tests/move_stake.rs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::*;
33
use approx::assert_abs_diff_eq;
44
use frame_support::{assert_err, assert_noop, assert_ok};
55
use sp_core::{Get, U256};
6+
use substrate_fixed::types::I96F32;
67

78
// 1. test_do_move_success
89
// Description: Test a successful move of stake between two hotkeys in the same subnet
@@ -111,14 +112,19 @@ fn test_do_move_different_subnets() {
111112
),
112113
0
113114
);
115+
let alpha_fee: I96F32 =
116+
I96F32::from_num(fee) / SubtensorModule::get_alpha_price(destination_netuid);
117+
let expected_value = I96F32::from_num(alpha)
118+
* SubtensorModule::get_alpha_price(origin_netuid)
119+
/ SubtensorModule::get_alpha_price(destination_netuid);
114120
assert_abs_diff_eq!(
115121
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
116122
&destination_hotkey,
117123
&coldkey,
118124
destination_netuid
119125
),
120-
stake_amount - 2 * fee,
121-
epsilon = stake_amount / 1000
126+
(expected_value - alpha_fee).to_num::<u64>(),
127+
epsilon = (expected_value / 1000).to_num::<u64>()
122128
);
123129
});
124130
}
@@ -700,13 +706,17 @@ fn test_do_move_storage_updates() {
700706
),
701707
0
702708
);
709+
let alpha_fee =
710+
I96F32::from_num(fee) / SubtensorModule::get_alpha_price(destination_netuid);
711+
let alpha2 = I96F32::from_num(alpha) * SubtensorModule::get_alpha_price(origin_netuid)
712+
/ SubtensorModule::get_alpha_price(destination_netuid);
703713
assert_abs_diff_eq!(
704714
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
705715
&destination_hotkey,
706716
&coldkey,
707717
destination_netuid
708718
),
709-
alpha - fee,
719+
(alpha2 - alpha_fee).to_num::<u64>(),
710720
epsilon = alpha / 1000
711721
);
712722
});
@@ -1063,10 +1073,12 @@ fn test_do_transfer_different_subnets() {
10631073
&destination_coldkey,
10641074
destination_netuid,
10651075
);
1076+
let expected_value = I96F32::from_num(stake_amount - fee)
1077+
/ SubtensorModule::get_alpha_price(destination_netuid);
10661078
assert_abs_diff_eq!(
10671079
dest_stake,
1068-
stake_amount - fee,
1069-
epsilon = stake_amount / 1000
1080+
expected_value.to_num::<u64>(),
1081+
epsilon = (expected_value / 1000).to_num::<u64>()
10701082
);
10711083
});
10721084
}
@@ -1114,10 +1126,15 @@ fn test_do_swap_success() {
11141126
&coldkey,
11151127
destination_netuid,
11161128
);
1129+
let alpha_fee =
1130+
I96F32::from_num(fee) / SubtensorModule::get_alpha_price(destination_netuid);
1131+
let expected_value = I96F32::from_num(alpha_before)
1132+
* SubtensorModule::get_alpha_price(origin_netuid)
1133+
/ SubtensorModule::get_alpha_price(destination_netuid);
11171134
assert_abs_diff_eq!(
11181135
alpha_after,
1119-
stake_amount - fee,
1120-
epsilon = stake_amount / 1000
1136+
(expected_value - alpha_fee).to_num::<u64>(),
1137+
epsilon = (expected_value / 1000).to_num::<u64>()
11211138
);
11221139
});
11231140
}
@@ -1309,7 +1326,6 @@ fn test_do_swap_partial_stake() {
13091326
SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey);
13101327
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, origin_netuid, total_stake, 0);
13111328

1312-
let fee_as_alpha2 = SubtensorModule::swap_tao_for_alpha(destination_netuid, fee);
13131329
let swap_amount = total_stake / 2;
13141330
assert_ok!(SubtensorModule::do_swap_stake(
13151331
RuntimeOrigin::signed(coldkey),
@@ -1328,14 +1344,20 @@ fn test_do_swap_partial_stake() {
13281344
total_stake - swap_amount,
13291345
epsilon = total_stake / 1000
13301346
);
1347+
1348+
let alpha_fee =
1349+
I96F32::from_num(fee) / SubtensorModule::get_alpha_price(destination_netuid);
1350+
let expected_value = I96F32::from_num(swap_amount)
1351+
* SubtensorModule::get_alpha_price(origin_netuid)
1352+
/ SubtensorModule::get_alpha_price(destination_netuid);
13311353
assert_abs_diff_eq!(
13321354
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
13331355
&hotkey,
13341356
&coldkey,
13351357
destination_netuid
13361358
),
1337-
swap_amount - fee_as_alpha2,
1338-
epsilon = total_stake / 1000
1359+
(expected_value - alpha_fee).to_num::<u64>(),
1360+
epsilon = (expected_value / 1000).to_num::<u64>()
13391361
);
13401362
});
13411363
}
@@ -1378,16 +1400,19 @@ fn test_do_swap_storage_updates() {
13781400
0
13791401
);
13801402

1381-
let fee_as_alpha = SubtensorModule::swap_tao_for_alpha(destination_netuid, fee);
1382-
1403+
let alpha_fee =
1404+
I96F32::from_num(fee) / SubtensorModule::get_alpha_price(destination_netuid);
1405+
let expected_value = I96F32::from_num(alpha)
1406+
* SubtensorModule::get_alpha_price(origin_netuid)
1407+
/ SubtensorModule::get_alpha_price(destination_netuid);
13831408
assert_abs_diff_eq!(
13841409
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
13851410
&hotkey,
13861411
&coldkey,
13871412
destination_netuid
13881413
),
1389-
alpha - fee_as_alpha,
1390-
epsilon = 5
1414+
(expected_value - alpha_fee).to_num::<u64>(),
1415+
epsilon = (expected_value / 1000).to_num::<u64>()
13911416
);
13921417
});
13931418
}

0 commit comments

Comments
 (0)