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

Add commission rate and the wait list #322

Merged
merged 7 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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
45 changes: 40 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,47 @@ 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| {
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("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| 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("joinDelegators(bytes32,uint256)")]
Expand Down
Loading