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

Commit

Permalink
feat: migrate crab issuing to use pallet! procedural macro
Browse files Browse the repository at this point in the history
  • Loading branch information
songtianyi committed Mar 25, 2021
1 parent c48831e commit 046acba
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 56 deletions.
4 changes: 2 additions & 2 deletions bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ fn pangolin_build_spec_genesis() -> pangolin_runtime::GenesisConfig {
darwinia_claims: Default::default(),
darwinia_vesting: Default::default(),
pallet_sudo: pangolin_runtime::SudoConfig { key: root.clone() },
darwinia_crab_issuing: pangolin_runtime::CrabIssuingConfig { total_mapped_ring: BUNCH_OF_COINS },
darwinia_crab_issuing: pangolin_runtime::DarwiniaCrabIssuingConfig { total_mapped_ring: BUNCH_OF_COINS },
darwinia_crab_backing: pangolin_runtime::DarwiniaCrabBackingConfig { backed_ring: BUNCH_OF_COINS },
darwinia_ethereum_relay: pangolin_runtime::EthereumRelayConfig {
genesis_header_info: (
Expand Down Expand Up @@ -471,7 +471,7 @@ fn pangolin_development_genesis() -> pangolin_runtime::GenesisConfig {
},
darwinia_vesting: Default::default(),
pallet_sudo: pangolin_runtime::SudoConfig { key: root.clone() },
darwinia_crab_issuing: pangolin_runtime::CrabIssuingConfig { total_mapped_ring: BUNCH_OF_COINS },
darwinia_crab_issuing: pangolin_runtime::DarwiniaCrabIssuingConfig { total_mapped_ring: BUNCH_OF_COINS },
darwinia_crab_backing: pangolin_runtime::DarwiniaCrabBackingConfig { backed_ring: BUNCH_OF_COINS },
darwinia_ethereum_relay: pangolin_runtime::EthereumRelayConfig {
genesis_header_info: (
Expand Down
2 changes: 1 addition & 1 deletion bin/node/runtime/pangolin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ frame_support::construct_runtime! {
// Multisig module. Late addition.
Multisig: pallet_multisig::{Module, Call, Storage, Event<T>} = 32,

CrabIssuing: darwinia_crab_issuing::{Module, Call, Storage, Config, Event<T>} = 33,
DarwiniaCrabIssuing: darwinia_crab_issuing::{Module, Call, Storage, Config, Event<T>} = 33,
DarwiniaCrabBacking: darwinia_crab_backing::{Module, Storage, Config<T>} = 34,

EthereumRelay: darwinia_ethereum_relay::{Module, Call, Storage, Config<T>, Event<T>} = 35,
Expand Down
109 changes: 57 additions & 52 deletions frame/bridge/crab/issuing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;

pub mod weights;
// --- darwinia ---
pub use weights::WeightInfo;
// --- substrate ---
use frame_support::traits::Currency;

#[cfg(test)]
mod mock;
Expand All @@ -42,73 +46,74 @@ mod types {
type RingCurrency<T> = <T as Config>::RingCurrency;
}

// --- substrate ---
use frame_support::{
decl_error, decl_event, decl_module, decl_storage,
traits::{Currency, Get},
};
use sp_runtime::{traits::AccountIdConversion, ModuleId};
// --- darwinia ---
use types::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_support::traits::{Currency, Get};
use frame_system::pallet_prelude::*;
use sp_runtime::{traits::AccountIdConversion, ModuleId};
use types::*;

pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config {
#[pallet::constant]
type ModuleId: Get<ModuleId>;

type ModuleId: Get<ModuleId>;
type RingCurrency: Currency<AccountId<Self>>;

type RingCurrency: Currency<AccountId<Self>>;
type WeightInfo: WeightInfo;

type WeightInfo: WeightInfo;
}
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
}

decl_event! {
pub enum Event<T>
where
AccountId = AccountId<T>,
RingBalance = RingBalance<T>,
{
// Define the pallet struct placeholder, various pallet function are implemented on it.
#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::error]
pub enum Error<T> {}

#[pallet::event]
pub enum Event<T: Config> {
/// Dummy Event. [who, swapped *CRING*, burned Mapped *RING*]
DummyEvent(AccountId, RingBalance, MappedRing),
DummyEvent(AccountId<T>, RingBalance<T>, MappedRing),
}
}

decl_error! {
pub enum Error for Module<T: Config> {
#[pallet::storage]
#[pallet::getter(fn total_mapped_ring)]
pub(super) type TotalMappedRing<T: Config> = StorageValue<_, MappedRing>;

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

#[pallet::call]
impl<T: Config> Pallet<T> {}

#[pallet::genesis_config]
pub struct GenesisConfig {
pub total_mapped_ring: MappedRing,
}
}

decl_storage! {
trait Store for Module<T: Config> as DarwiniaCrabIssuing {
pub TotalMappedRing get(fn total_mapped_ring) config(): MappedRing;
#[cfg(feature = "std")]
impl Default for GenesisConfig {
fn default() -> Self {
Self {
total_mapped_ring: Default::default(),
}
}
}

add_extra_genesis {
build(|config| {
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {
let _ = T::RingCurrency::make_free_balance_be(
&<Module<T>>::account_id(),
&T::ModuleId::get().into_account(),
T::RingCurrency::minimum_balance(),
);

TotalMappedRing::put(config.total_mapped_ring);
});
}
}

decl_module! {
pub struct Module<T: Config> for enum Call
where
origin: T::Origin
{
type Error = Error<T>;

const ModuleId: ModuleId = T::ModuleId::get();

fn deposit_event() = default;
}
}

impl<T: Config> Module<T> {
pub fn account_id() -> T::AccountId {
T::ModuleId::get().into_account()
<TotalMappedRing<T>>::put(self.total_mapped_ring);
}
}
}
2 changes: 1 addition & 1 deletion frame/bridge/crab/issuing/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ frame_support::construct_runtime! {
{
System: frame_system::{Module, Call, Storage, Config, Event<T>},
Ring: darwinia_balances::<Instance0>::{Module, Call, Storage, Config<T>, Event<T>},
CrabIssuing: darwinia_crab_issuing::{Module, Call, Storage, Config, Event<T>},
DarwiniaCrabIssuing: darwinia_crab_issuing::{Module, Call, Storage, Config, Event<T>},
}
}

Expand Down

0 comments on commit 046acba

Please sign in to comment.