From 5138fd08b2ac43005dcd021d24c9b1ec34a74d16 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Thu, 22 Dec 2022 14:06:29 +0800 Subject: [PATCH] Fix type & code optimization (#154) * Fix type & code optimization * Use `u16` to bypass the https://github.com/polkadot-js/apps/issues/8591 --- pallet/deposit/src/lib.rs | 14 ++++++------ precompile/staking/Cargo.toml | 7 +++--- precompile/staking/src/lib.rs | 40 +++++++++++++++-------------------- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/pallet/deposit/src/lib.rs b/pallet/deposit/src/lib.rs index 40c572213..5d17a611f 100644 --- a/pallet/deposit/src/lib.rs +++ b/pallet/deposit/src/lib.rs @@ -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; @@ -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 { diff --git a/precompile/staking/Cargo.toml b/precompile/staking/Cargo.toml index bec082f49..a20f3d0db 100644 --- a/precompile/staking/Cargo.toml +++ b/precompile/staking/Cargo.toml @@ -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" } @@ -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"] } @@ -52,7 +54,6 @@ std = [ # darwinia "darwinia-staking/std", - "darwinia-deposit/std", # moonbeam "precompile-utils/std", diff --git a/precompile/staking/src/lib.rs b/precompile/staking/src/lib.rs index 6319054f9..8476f4ea3 100644 --- a/precompile/staking/src/lib.rs +++ b/precompile/staking/src/lib.rs @@ -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::*; @@ -39,34 +38,29 @@ use sp_core::{H160, U256}; use sp_runtime::Perbill; use sp_std::vec::Vec; -/// AccountId of the runtime. -type AccountIdOf = ::AccountId; - -/// DepositId of the runtime. -type DepositIdOf = <::Deposit as Stake>::Item; - pub struct Staking(PhantomData); #[precompile_utils::precompile] impl Staking where Runtime: darwinia_staking::Config + pallet_evm::Config, - Runtime::RuntimeCall: Dispatchable + GetDispatchInfo, - Runtime::RuntimeCall: From>, - <::RuntimeCall as Dispatchable>::RuntimeOrigin: OriginTrait, + Runtime::RuntimeCall: GetDispatchInfo + + Dispatchable + + From>, ::RuntimeOrigin: From>, - AccountIdOf: From, - DepositIdOf: From, + ::AccountId: From, + <::RuntimeCall as Dispatchable>::RuntimeOrigin: OriginTrait, + <::Deposit as Stake>::Item: From, { #[precompile::public("stake(uint256,uint256,uint8[])")] fn stake( handle: &mut impl PrecompileHandle, ring_amount: U256, kton_amount: U256, - deposits: Vec, + deposits: Vec, ) -> EvmResult { - let origin: AccountIdOf = handle.context().caller.into(); - let deposits: Vec> = 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::::try_dispatch( handle, @@ -85,10 +79,10 @@ where handle: &mut impl PrecompileHandle, ring_amount: U256, kton_amount: U256, - deposits: Vec, + deposits: Vec, ) -> EvmResult { - let origin: AccountIdOf = handle.context().caller.into(); - let deposits: Vec> = 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::::try_dispatch( handle, @@ -104,7 +98,7 @@ where #[precompile::public("claim()")] fn claim(handle: &mut impl PrecompileHandle) -> EvmResult { - let origin: AccountIdOf = handle.context().caller.into(); + let origin = handle.context().caller.into(); RuntimeHelper::::try_dispatch( handle, @@ -116,7 +110,7 @@ where #[precompile::public("collect(uint32)")] fn collect(handle: &mut impl PrecompileHandle, commission: u32) -> EvmResult { - let origin: AccountIdOf = handle.context().caller.into(); + let origin = handle.context().caller.into(); RuntimeHelper::::try_dispatch( handle, @@ -131,7 +125,7 @@ where #[precompile::public("nominate(address)")] fn nominate(handle: &mut impl PrecompileHandle, target: Address) -> EvmResult { let target: H160 = target.into(); - let origin: AccountIdOf = handle.context().caller.into(); + let origin = handle.context().caller.into(); RuntimeHelper::::try_dispatch( handle, @@ -143,7 +137,7 @@ where #[precompile::public("chill()")] fn chill(handle: &mut impl PrecompileHandle) -> EvmResult { - let origin: AccountIdOf = handle.context().caller.into(); + let origin = handle.context().caller.into(); RuntimeHelper::::try_dispatch( handle,