Skip to content

Commit

Permalink
Add claim_with_penalty interface (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
boundless-forest authored Dec 29, 2022
1 parent 85b70e2 commit 77c6788
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
5 changes: 1 addition & 4 deletions pallet/deposit/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ impl darwinia_deposit::SimpleAsset for KtonAsset {
Assets::mint(RuntimeOrigin::signed(0), 0, *beneficiary, amount)
}

fn burn(
who: &Self::AccountId,
amount: Balance,
) -> sp_runtime::DispatchResult {
fn burn(who: &Self::AccountId, amount: Balance) -> sp_runtime::DispatchResult {
if Assets::balance(0, who) < amount {
Err(<pallet_assets::Error<Runtime>>::BalanceLow)?;
}
Expand Down
7 changes: 1 addition & 6 deletions pallet/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,7 @@ pub mod pallet {
DispatchResult::Ok(())
})?;

Self::deposit_event(Event::Staked {
staker: who,
ring_amount,
kton_amount,
deposits,
});
Self::deposit_event(Event::Staked { staker: who, ring_amount, kton_amount, deposits });

Ok(())
}
Expand Down
5 changes: 1 addition & 4 deletions pallet/staking/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ impl darwinia_deposit::SimpleAsset for KtonAsset {
Assets::mint(RuntimeOrigin::signed(0), 0, *beneficiary, amount)
}

fn burn(
who: &Self::AccountId,
amount: Balance,
) -> sp_runtime::DispatchResult {
fn burn(who: &Self::AccountId, amount: Balance) -> sp_runtime::DispatchResult {
if Assets::balance(0, who) < amount {
Err(<pallet_assets::Error<Runtime>>::BalanceLow)?;
}
Expand Down
13 changes: 13 additions & 0 deletions precompile/deposit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,17 @@ where

Ok(true)
}

#[precompile::public("claim_with_penalty(uint8)")]
fn claim_with_penalty(handle: &mut impl PrecompileHandle, id: u8) -> EvmResult<bool> {
let origin: AccountIdOf<Runtime> = handle.context().caller.into();

RuntimeHelper::<Runtime>::try_dispatch(
handle,
Some(origin).into(),
darwinia_deposit::Call::<Runtime>::claim_with_penalty { id: id.into() },
)?;

Ok(true)
}
}
19 changes: 19 additions & 0 deletions precompile/deposit/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fn precompiles() -> TestPrecompiles<TestRuntime> {
fn selectors() {
assert!(PCall::lock_selectors().contains(&0x998e4242));
assert!(PCall::claim_selectors().contains(&0x4e71d92d));
assert!(PCall::claim_with_penalty_selectors().contains(&0xfa04a9bf));
}

#[test]
Expand All @@ -56,3 +57,21 @@ fn lock_and_claim() {
assert!(Deposit::deposit_of(&alice).is_none());
});
}

#[test]
fn claim_with_penalty() {
let alice: H160 = Alice.into();
ExtBuilder::default().with_balances(vec![(alice, 300)]).build().execute_with(|| {
// lock
precompiles()
.prepare_test(alice, Precompile, PCall::lock { amount: 200.into(), months: 1 })
.execute_returns(EvmDataWriter::new().write(true).build());
assert!(Deposit::deposit_of(&alice).is_some());

// claim with penalty
precompiles()
.prepare_test(alice, Precompile, PCall::claim_with_penalty { id: 0 })
.execute_returns(EvmDataWriter::new().write(true).build());
assert!(Deposit::deposit_of(&alice).is_none());
});
}

0 comments on commit 77c6788

Please sign in to comment.