|
| 1 | +use super::*; |
| 2 | +use frame_support::pallet_prelude::{Decode, Encode}; |
| 3 | +extern crate alloc; |
| 4 | +use alloc::vec::Vec; |
| 5 | +use codec::Compact; |
| 6 | +use sp_core::hexdisplay::AsBytesRef; |
| 7 | + |
| 8 | +#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] |
| 9 | +pub struct StakeInfo<T: Config> { |
| 10 | + hotkey: T::AccountId, |
| 11 | + coldkey: T::AccountId, |
| 12 | + stake: Compact<u64>, |
| 13 | +} |
| 14 | + |
| 15 | +impl<T: Config> Pallet<T> { |
| 16 | + fn _get_stake_info_for_coldkeys(coldkeys: Vec<T::AccountId>) -> Vec<(T::AccountId, Vec<StakeInfo<T>>)> { |
| 17 | + if coldkeys.len() == 0 { |
| 18 | + return Vec::new(); // No coldkeys to check |
| 19 | + } |
| 20 | + |
| 21 | + let mut stake_info: Vec<(T::AccountId, Vec<StakeInfo<T>>)> = Vec::new(); |
| 22 | + for coldkey_ in coldkeys { |
| 23 | + let mut stake_info_for_coldkey: Vec<StakeInfo<T>> = Vec::new(); |
| 24 | + |
| 25 | + for (hotkey, coldkey, stake) in <Stake<T>>::iter() { |
| 26 | + if coldkey == coldkey_ { |
| 27 | + stake_info_for_coldkey.push(StakeInfo { |
| 28 | + hotkey, |
| 29 | + coldkey, |
| 30 | + stake: stake.into(), |
| 31 | + }); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + stake_info.push((coldkey_, stake_info_for_coldkey)); |
| 36 | + } |
| 37 | + |
| 38 | + return stake_info; |
| 39 | + } |
| 40 | + |
| 41 | + pub fn get_stake_info_for_coldkeys(coldkey_account_vecs: Vec<Vec<u8>>) -> Vec<(T::AccountId, Vec<StakeInfo<T>>)> { |
| 42 | + let mut coldkeys: Vec<T::AccountId> = Vec::new(); |
| 43 | + for coldkey_account_vec in coldkey_account_vecs { |
| 44 | + if coldkey_account_vec.len() != 32 { |
| 45 | + continue; // Invalid coldkey |
| 46 | + } |
| 47 | + let coldkey: AccountIdOf<T> = T::AccountId::decode( &mut coldkey_account_vec.as_bytes_ref() ).unwrap(); |
| 48 | + coldkeys.push(coldkey); |
| 49 | + } |
| 50 | + |
| 51 | + if coldkeys.len() == 0 { |
| 52 | + return Vec::new(); // Invalid coldkey |
| 53 | + } |
| 54 | + |
| 55 | + let stake_info = Self::_get_stake_info_for_coldkeys(coldkeys); |
| 56 | + |
| 57 | + return stake_info; |
| 58 | + } |
| 59 | + |
| 60 | + pub fn get_stake_info_for_coldkey(coldkey_account_vec: Vec<u8>) -> Vec<StakeInfo<T>> { |
| 61 | + if coldkey_account_vec.len() != 32 { |
| 62 | + return Vec::new(); // Invalid coldkey |
| 63 | + } |
| 64 | + |
| 65 | + let coldkey: AccountIdOf<T> = T::AccountId::decode( &mut coldkey_account_vec.as_bytes_ref() ).unwrap(); |
| 66 | + let stake_info = Self::_get_stake_info_for_coldkeys(vec![coldkey]); |
| 67 | + |
| 68 | + if stake_info.len() == 0 { |
| 69 | + return Vec::new(); // Invalid coldkey |
| 70 | + } else { |
| 71 | + return stake_info.get(0).unwrap().1.clone(); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
0 commit comments