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 Staking precompile getters: getDelegatorTotalStaked and getCandidateTotalBacked #2033

Merged
merged 4 commits into from
Jan 11, 2023
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
19 changes: 19 additions & 0 deletions precompiles/parachain-staking/StakingInterface.sol
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,23 @@ interface ParachainStaking {
uint256 candidateAutoCompoundingDelegationCount,
uint256 delegatorDelegationCount
) external;

/// @dev Fetch the total staked amount of a delegator, regardless of the
/// candidate.
/// @custom:selector e6861713
/// @param delegator Address of the delegator.
/// @return Total amount of stake.
function getDelegatorTotalStaked(address delegator)
external
view
returns (uint256);

/// @dev Fetch the total staked towards a candidate.
/// @custom:selector 670c59e9
/// @param candidate Address of the candidate.
/// @return Total amount of stake.
function getCandidateTotalBacked(address candidate)
4meta5 marked this conversation as resolved.
Show resolved Hide resolved
external
view
returns (uint256);
}
34 changes: 34 additions & 0 deletions precompiles/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,40 @@ where
Ok(())
}

#[precompile::public("getDelegatorTotalStaked(address)")]
#[precompile::view]
fn get_delegator_total_staked(
handle: &mut impl PrecompileHandle,
delegator: Address,
) -> EvmResult<U256> {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;

let delegator = Runtime::AddressMapping::into_account_id(delegator.0);

let amount = <pallet_parachain_staking::Pallet<Runtime>>::delegator_state(&delegator)
.map(|state| state.total)
.unwrap_or_default();

Ok(amount.into())
}

#[precompile::public("getCandidateTotalBacked(address)")]
#[precompile::view]
fn get_candidate_total_backed(
handle: &mut impl PrecompileHandle,
candidate: Address,
) -> EvmResult<U256> {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;

let candidate = Runtime::AddressMapping::into_account_id(candidate.0);

let amount = <pallet_parachain_staking::Pallet<Runtime>>::candidate_info(&candidate)
.map(|state| state.total_counted)
.unwrap_or_default();

Ok(amount.into())
}

fn u256_to_amount(value: U256) -> MayRevert<BalanceOf<Runtime>> {
value
.try_into()
Expand Down
81 changes: 81 additions & 0 deletions precompiles/parachain-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ fn selectors() {
assert!(PCall::schedule_delegator_bond_less_selectors().contains(&0xc172fd2b));
assert!(PCall::execute_delegation_request_selectors().contains(&0xe98c8abe));
assert!(PCall::cancel_delegation_request_selectors().contains(&0xc90eee83));
assert!(PCall::get_delegator_total_staked_selectors().contains(&0xe6861713));
assert!(PCall::get_candidate_total_backed_selectors().contains(&0x670c59e9));
}

#[test]
Expand Down Expand Up @@ -117,6 +119,8 @@ fn modifiers() {
tester.test_default_modifier(PCall::schedule_delegator_bond_less_selectors());
tester.test_default_modifier(PCall::execute_delegation_request_selectors());
tester.test_default_modifier(PCall::cancel_delegation_request_selectors());
tester.test_view_modifier(PCall::get_delegator_total_staked_selectors());
tester.test_view_modifier(PCall::get_candidate_total_backed_selectors());
});
}

Expand Down Expand Up @@ -1463,6 +1467,83 @@ fn set_auto_compound_fails_if_not_delegation() {
});
}

#[test]
fn get_delegator_total_staked_getter() {
ExtBuilder::default()
.with_balances(vec![
(Alice.into(), 1_000),
(Bob.into(), 1_000),
(Charlie.into(), 1_500),
])
.with_candidates(vec![(Alice.into(), 1_000), (Bob.into(), 1_000)])
.with_delegations(vec![
(Charlie.into(), Alice.into(), 1_000),
(Charlie.into(), Bob.into(), 499),
])
.build()
.execute_with(|| {
PrecompilesValue::get()
.prepare_test(
Alice,
Precompile1,
PCall::get_delegator_total_staked {
delegator: Address(Charlie.into()),
},
)
.execute_returns_encoded(U256::from(1_499));
});
}

#[test]
fn get_delegator_total_staked_getter_unknown() {
ExtBuilder::default()
.with_balances(vec![
(Alice.into(), 1_000),
(Bob.into(), 1_000),
(Charlie.into(), 1_500),
])
.with_candidates(vec![(Alice.into(), 1_000), (Bob.into(), 1_000)])
.build()
.execute_with(|| {
PrecompilesValue::get()
.prepare_test(
Alice,
Precompile1,
PCall::get_delegator_total_staked {
delegator: Address(Charlie.into()),
},
)
.execute_returns_encoded(U256::zero());
});
}

#[test]
fn get_candidate_total_backed_getter() {
ExtBuilder::default()
.with_balances(vec![
(Alice.into(), 1_000),
(Bob.into(), 1_000),
(Charlie.into(), 1_500),
])
.with_candidates(vec![(Alice.into(), 1_000), (Bob.into(), 1_000)])
.with_delegations(vec![
(Charlie.into(), Alice.into(), 1_000),
(Charlie.into(), Bob.into(), 499),
])
.build()
.execute_with(|| {
PrecompilesValue::get()
.prepare_test(
Alice,
Precompile1,
PCall::get_candidate_total_backed {
candidate: Address(Alice.into()),
},
)
.execute_returns_encoded(U256::from(2_000));
});
}

#[test]
fn test_solidity_interface_has_all_function_selectors_documented_and_implemented() {
for file in ["StakingInterface.sol"] {
Expand Down