Skip to content

Commit

Permalink
Merge pull request #322 from peaqnetwork/feat/1209008326914613_add_co…
Browse files Browse the repository at this point in the history
…mmission_rate_evm

Add commission rate and the wait list
  • Loading branch information
sfffaaa authored Dec 18, 2024
2 parents 96deaab + 40860fb commit 0e9be32
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
6 changes: 4 additions & 2 deletions pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,10 @@ pub mod pallet {
}

/// The maximum number of collator candidates selected at each round.
/// precompiles will call this
#[pallet::storage]
#[pallet::getter(fn max_selected_candidates)]
pub(crate) type MaxSelectedCandidates<T: Config> = StorageValue<_, u32, ValueQuery>;
pub type MaxSelectedCandidates<T: Config> = StorageValue<_, u32, ValueQuery>;

/// Current round number and next round scheduled transition.
#[pallet::storage]
Expand Down Expand Up @@ -576,9 +577,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
2 changes: 1 addition & 1 deletion precompiles/parachain-staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ authors = [ "Peaq" ]
precompile-utils = { path = "../utils", default-features = false }
parachain-staking = { path = "../../pallets/parachain-staking", default-features = false }
address-unification = { path = "../../pallets/address-unification", default-features = false }
pallet-session = { workspace = true, default-features = false }

# Substrate
parity-scale-codec = { workspace = true, features = ["max-encoded-len"] }
Expand All @@ -36,7 +37,6 @@ precompile-utils = { path = "../utils", features = [ "std", "testing" ] }
pallet-timestamp = { workspace = true, features = [ "std" ] }
scale-info = { workspace = true }
pallet-authorship = { workspace = true, default-features = false }
pallet-session = { workspace = true, default-features = false }

[features]
default = ["std", "experimental"]
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: 39 additions & 6 deletions precompiles/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ pub struct ParachainStakingPrecompile<Runtime>(PhantomData<Runtime>);
pub struct CollatorInfo {
owner: H256,
amount: U256,
commission: U256,
}

#[precompile_utils::precompile]
impl<Runtime> ParachainStakingPrecompile<Runtime>
where
Runtime: parachain_staking::Config + pallet_evm::Config,
Runtime: parachain_staking::Config + pallet_evm::Config + pallet_session::Config,
Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
<Runtime::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<Runtime::AccountId>>,
Runtime::RuntimeCall: From<parachain_staking::Call<Runtime>>,
Expand All @@ -73,13 +74,45 @@ 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.total.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.total.into(),
commission: U256::from(stake_info.commission.deconstruct() as u128),
})
.collect::<Vec<CollatorInfo>>();
let validators = pallet_session::Pallet::<Runtime>::validators()
.into_iter()
.map(|info| H256::from(<AccountIdOf<Runtime> as Into<[u8; 32]>>::into(info)))
.collect::<Vec<H256>>();
let candidate_list = all_collators.into_iter().filter(|x| !validators.contains(&x.owner));
Ok(candidate_list.collect::<Vec<CollatorInfo>>())
}

#[precompile::public("joinDelegators(bytes32,uint256)")]
Expand Down
2 changes: 2 additions & 0 deletions precompiles/parachain-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,12 @@ fn collator_list_test() {
CollatorInfo {
owner: convert_mock_account_by_u8_list(MockPeaqAccount::Alice),
amount: U256::from(110),
commission: U256::from(0),
},
CollatorInfo {
owner: convert_mock_account_by_u8_list(MockPeaqAccount::Charlie),
amount: U256::from(20),
commission: U256::from(0),
},
]);
});
Expand Down

0 comments on commit 0e9be32

Please sign in to comment.