Skip to content
This repository has been archived by the owner on Mar 30, 2023. It is now read-only.

Commit

Permalink
Fix type & code optimization (#154)
Browse files Browse the repository at this point in the history
* Fix type & code optimization

* Use `u16` to bypass the polkadot-js/apps#8591
  • Loading branch information
aurexav authored Dec 22, 2022
1 parent 8b46c6c commit 5138fd0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
14 changes: 8 additions & 6 deletions pallet/deposit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ use frame_support::{
use frame_system::pallet_prelude::*;
use sp_runtime::traits::AccountIdConversion;

/// Deposit identifier.
///
/// It's not a global-unique identifier.
/// It's only used for distinguishing the deposits under a specific account.
pub type DepositId = u8;

/// Milliseconds per month.
pub const MILLISECS_PER_MONTH: Moment = MILLISECS_PER_YEAR / 12;

Expand All @@ -70,6 +64,14 @@ pub trait Minting {
fn mint(beneficiary: &Self::AccountId, amount: Balance) -> DispatchResult;
}

/// Deposit identifier.
///
/// It's not a global-unique identifier.
/// It's only used for distinguishing the deposits under a specific account.
// /~https://github.com/polkadot-js/apps/issues/8591
// pub type DepositId = u8;
pub type DepositId = u16;

/// Deposit.
#[derive(PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo, RuntimeDebug)]
pub struct Deposit {
Expand Down
7 changes: 4 additions & 3 deletions precompile/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ fp-evm = { default-features = false, git = "/~https://github.com/paritytech/fr
pallet-evm = { default-features = false, git = "/~https://github.com/paritytech/frontier", branch = "polkadot-v0.9.30" }

# darwinia
darwinia-staking = { default-features = false, path = "../../pallet/staking"}
darwinia-deposit = { default-features = false, path = "../../pallet/deposit"}
darwinia-staking = { default-features = false, path = "../../pallet/staking" }

# moonbeam
precompile-utils = { default-features = false, git = "/~https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30" }
Expand All @@ -33,6 +32,9 @@ sp-std = { default-features = false, git = "/~https://github.com/paritytech
codec = { package = "parity-scale-codec", version = "3.2" }
scale-info = { version = "2.3", features = ["derive"] }

# darwinia
darwinia-deposit = { path = "../../pallet/deposit" }

# moonbeam
precompile-utils = { git = "/~https://github.com/darwinia-network/moonbeam.git", branch = "polkadot-v0.9.30", features = ["testing"] }

Expand All @@ -52,7 +54,6 @@ std = [

# darwinia
"darwinia-staking/std",
"darwinia-deposit/std",

# moonbeam
"precompile-utils/std",
Expand Down
40 changes: 17 additions & 23 deletions precompile/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ mod mock;
#[cfg(test)]
mod tests;

// std
// core
use core::marker::PhantomData;
// darwinia
use darwinia_deposit::DepositId;
use darwinia_staking::Stake;
// moonbeam
use precompile_utils::prelude::*;
Expand All @@ -39,34 +38,29 @@ use sp_core::{H160, U256};
use sp_runtime::Perbill;
use sp_std::vec::Vec;

/// AccountId of the runtime.
type AccountIdOf<R> = <R as frame_system::pallet::Config>::AccountId;

/// DepositId of the runtime.
type DepositIdOf<R> = <<R as darwinia_staking::Config>::Deposit as Stake>::Item;

pub struct Staking<Runtime>(PhantomData<Runtime>);

#[precompile_utils::precompile]
impl<Runtime> Staking<Runtime>
where
Runtime: darwinia_staking::Config + pallet_evm::Config,
Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
Runtime::RuntimeCall: From<darwinia_staking::Call<Runtime>>,
<<Runtime as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin: OriginTrait,
Runtime::RuntimeCall: GetDispatchInfo
+ Dispatchable<PostInfo = PostDispatchInfo>
+ From<darwinia_staking::Call<Runtime>>,
<Runtime::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<Runtime::AccountId>>,
AccountIdOf<Runtime>: From<H160>,
DepositIdOf<Runtime>: From<u8>,
<Runtime as frame_system::Config>::AccountId: From<H160>,
<<Runtime as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin: OriginTrait,
<<Runtime as darwinia_staking::Config>::Deposit as Stake>::Item: From<u16>,
{
#[precompile::public("stake(uint256,uint256,uint8[])")]
fn stake(
handle: &mut impl PrecompileHandle,
ring_amount: U256,
kton_amount: U256,
deposits: Vec<DepositId>,
deposits: Vec<u16>,
) -> EvmResult<bool> {
let origin: AccountIdOf<Runtime> = handle.context().caller.into();
let deposits: Vec<DepositIdOf<Runtime>> = deposits.into_iter().map(|i| i.into()).collect();
let origin = handle.context().caller.into();
let deposits = deposits.into_iter().map(|i| i.into()).collect();

RuntimeHelper::<Runtime>::try_dispatch(
handle,
Expand All @@ -85,10 +79,10 @@ where
handle: &mut impl PrecompileHandle,
ring_amount: U256,
kton_amount: U256,
deposits: Vec<DepositId>,
deposits: Vec<u16>,
) -> EvmResult<bool> {
let origin: AccountIdOf<Runtime> = handle.context().caller.into();
let deposits: Vec<DepositIdOf<Runtime>> = deposits.into_iter().map(|i| i.into()).collect();
let origin = handle.context().caller.into();
let deposits = deposits.into_iter().map(|i| i.into()).collect();

RuntimeHelper::<Runtime>::try_dispatch(
handle,
Expand All @@ -104,7 +98,7 @@ where

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

RuntimeHelper::<Runtime>::try_dispatch(
handle,
Expand All @@ -116,7 +110,7 @@ where

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

RuntimeHelper::<Runtime>::try_dispatch(
handle,
Expand All @@ -131,7 +125,7 @@ where
#[precompile::public("nominate(address)")]
fn nominate(handle: &mut impl PrecompileHandle, target: Address) -> EvmResult<bool> {
let target: H160 = target.into();
let origin: AccountIdOf<Runtime> = handle.context().caller.into();
let origin = handle.context().caller.into();

RuntimeHelper::<Runtime>::try_dispatch(
handle,
Expand All @@ -143,7 +137,7 @@ where

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

RuntimeHelper::<Runtime>::try_dispatch(
handle,
Expand Down

0 comments on commit 5138fd0

Please sign in to comment.