Skip to content

Commit

Permalink
Add commission rate and the wait list
Browse files Browse the repository at this point in the history
  • Loading branch information
jaypan authored and Jay Pan committed Dec 18, 2024
1 parent a8ae463 commit 95a2ac9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
3 changes: 2 additions & 1 deletion pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,10 @@ pub mod pallet {
///
/// It maps from an account to its information.
/// Moreover, it counts the number of candidates.
/// Precompiles will call this structure to list all
#[pallet::storage]
#[pallet::getter(fn candidate_pool)]
pub(crate) type CandidatePool<T: Config> = CountedStorageMap<
pub type CandidatePool<T: Config> = CountedStorageMap<
_,
Twox64Concat,
T::AccountId,
Expand Down
5 changes: 5 additions & 0 deletions precompiles/parachain-staking/ParachainStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ interface ParachainStaking {
struct CollatorInfo {
bytes32 owner;
uint256 amount;
uint256 commission;
}

/// Get all collator informations
// selector: 0xaaacb283
function getCollatorList() external view returns (CollatorInfo[] memory);

/// Get all wait informations
// selector: 0x83d2afed
function getWaitList() external view returns (CollatorInfo[] memory);

/// Join the set of delegators by delegating to a collator candidate
/// selector: 0xd9f511cd
function joinDelegators(bytes32 collator, uint256 stake) external;
Expand Down
51 changes: 46 additions & 5 deletions precompiles/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub struct ParachainStakingPrecompile<Runtime>(PhantomData<Runtime>);
pub struct CollatorInfo {
owner: H256,
amount: U256,
commission: U256,
}

#[precompile_utils::precompile]
Expand All @@ -73,13 +74,53 @@ where

handle.record_db_read::<Runtime>(7200)?;

Ok(parachain_staking::Pallet::<Runtime>::top_candidates()
let all_collators = parachain_staking::CandidatePool::<Runtime>::iter()
.map(|(_id, stake_info)| {
CollatorInfo {
owner: H256::from(<AccountIdOf<Runtime> as Into<[u8; 32]>>::into(stake_info.id)),
amount: stake_info.stake.into(),
commission: U256::from(stake_info.commission.deconstruct() as u128),
}
}).collect::<Vec<CollatorInfo>>();
let top_candiate = parachain_staking::Pallet::<Runtime>::top_candidates()
.into_iter()
.map(|stake_info| CollatorInfo {
owner: H256::from(<AccountIdOf<Runtime> as Into<[u8; 32]>>::into(stake_info.owner)),
amount: stake_info.amount.into(),
.map(|stake_info| {
H256::from(<AccountIdOf<Runtime> as Into<[u8; 32]>>::into(stake_info.owner))
})
.collect::<Vec<CollatorInfo>>())
.collect::<Vec<H256>>();
let candidate_list = all_collators
.into_iter()
.filter(|x| top_candiate.contains(&x.owner));
Ok(candidate_list.collect::<Vec<CollatorInfo>>())
}

#[precompile::public("getWaitList()")]
#[precompile::public("get_wait_list()")]
#[precompile::view]
fn get_wait_list(handle: &mut impl PrecompileHandle) -> EvmResult<Vec<CollatorInfo>> {
// CandidatePool: UnBoundedVec(AccountId(32) + Balance(16))
// we account for a theoretical 150 pool.

handle.record_db_read::<Runtime>(7200)?;

let all_collators = parachain_staking::CandidatePool::<Runtime>::iter()
.map(|(_id, stake_info)| {
CollatorInfo {
owner: H256::from(<AccountIdOf<Runtime> as Into<[u8; 32]>>::into(stake_info.id)),
amount: stake_info.stake.into(),
commission: U256::from(stake_info.commission.deconstruct() as u128),
}
}).collect::<Vec<CollatorInfo>>();
let top_candiate = parachain_staking::Pallet::<Runtime>::top_candidates()
.into_iter()
.map(|stake_info| {
H256::from(<AccountIdOf<Runtime> as Into<[u8; 32]>>::into(stake_info.owner))
})
.collect::<Vec<H256>>();
let candidate_list = all_collators
.into_iter()
.filter(|x| !top_candiate.contains(&x.owner));
Ok(candidate_list.collect::<Vec<CollatorInfo>>())
}

#[precompile::public("joinDelegators(bytes32,uint256)")]
Expand Down

0 comments on commit 95a2ac9

Please sign in to comment.