Skip to content

Commit

Permalink
Add restake interface (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
boundless-forest authored Jan 9, 2023
1 parent f348232 commit 82b9a73
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
22 changes: 22 additions & 0 deletions precompile/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ where
Ok(true)
}

#[precompile::public("restake(uint256,uint256,uint8[])")]
fn restake(
handle: &mut impl PrecompileHandle,
ring_amount: U256,
kton_amount: U256,
deposits: Vec<u8>,
) -> EvmResult<bool> {
let origin = handle.context().caller.into();
let deposits = deposits.into_iter().map(|i| i.into()).collect();

RuntimeHelper::<Runtime>::try_dispatch(
handle,
Some(origin).into(),
darwinia_staking::Call::<Runtime>::restake {
ring_amount: ring_amount.as_u128(),
kton_amount: kton_amount.as_u128(),
deposits,
},
)?;
Ok(true)
}

#[precompile::public("claim()")]
fn claim(handle: &mut impl PrecompileHandle) -> EvmResult<bool> {
let origin = handle.context().caller.into();
Expand Down
19 changes: 18 additions & 1 deletion precompile/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ fn precompiles() -> TestPrecompiles<TestRuntime> {
fn selectors() {
assert!(PCall::stake_selectors().contains(&0x757f9b3b));
assert!(PCall::unstake_selectors().contains(&0xef20fcb3));
assert!(PCall::restake_selectors().contains(&0x17092fcb));
assert!(PCall::claim_selectors().contains(&0x4e71d92d));
assert!(PCall::nominate_selectors().contains(&0xb332180b));
assert!(PCall::collect_selectors().contains(&0x10a66536));
assert!(PCall::chill_selectors().contains(&0x2b8a3ae6));
}

#[test]
fn stake_and_unstake() {
fn stake_unstake_restake() {
let alice: H160 = Alice.into();
ExtBuilder::default().with_balances(vec![(alice, 300)]).build().execute_with(|| {
// stake
Expand Down Expand Up @@ -72,6 +73,22 @@ fn stake_and_unstake() {
)
.execute_returns(EvmDataWriter::new().write(true).build());
assert_eq!(Staking::ledger_of(alice).unwrap().staked_ring, 0);
assert_eq!(Staking::ledger_of(alice).unwrap().unstaking_ring.len(), 1);

// restake
precompiles()
.prepare_test(
alice,
Precompile,
PCall::restake {
ring_amount: 200.into(),
kton_amount: U256::zero(),
deposits: vec![],
},
)
.execute_returns(EvmDataWriter::new().write(true).build());
assert_eq!(Staking::ledger_of(alice).unwrap().staked_ring, 200);
assert_eq!(Staking::ledger_of(alice).unwrap().unstaking_ring.len(), 0);
});
}

Expand Down

0 comments on commit 82b9a73

Please sign in to comment.