Skip to content

Commit

Permalink
added custom pallet for bridging
Browse files Browse the repository at this point in the history
  • Loading branch information
mitun567 committed Jun 11, 2024
1 parent 6fa3241 commit f1de263
Show file tree
Hide file tree
Showing 13 changed files with 366 additions and 95 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ ark-ec = { version = "0.4.0", default-features = false }
ark-ff = { version = "0.4.0", default-features = false }
ark-std = { version = "0.4.0", default-features = false }

gtrandom = { version = "0.2.10", features = ["js"] }

[profile.release]
panic = "unwind"
2 changes: 1 addition & 1 deletion customSpec.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions customSpecRaw.json

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions pallets/pallet-counter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "pallet-counter"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2021"
description = "A simple pallet to increment a counter and emit an event."
license = "MIT-0"

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["derive"] }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-runtime = { version = "24.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }

[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
]
53 changes: 53 additions & 0 deletions pallets/pallet-counter/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;


use super::*;
use frame_benchmarking::{benchmarks, whitelisted_caller, account};
use frame_system::RawOrigin;

benchmarks! {
mint {
let caller: T::AccountId = whitelisted_caller();
let amount: BalanceOf<T> = 100u32.into();
}: _(RawOrigin::Root, caller.clone(), amount)
verify {
assert_eq!(T::Currency::free_balance(&caller), amount);
}

burn {
let caller: T::AccountId = whitelisted_caller();
let amount: BalanceOf<T> = 100u32.into();
T::Currency::deposit_creating(&caller, amount);
}: _(RawOrigin::Root, caller.clone(), amount)
verify {
assert_eq!(T::Currency::free_balance(&caller), 0u32.into());
}

lock {
let caller: T::AccountId = whitelisted_caller();
let amount: BalanceOf<T> = 100u32.into();
T::Currency::deposit_creating(&caller, amount);
}: _(RawOrigin::Signed(caller.clone()), amount)
verify {
assert_eq!(T::Currency::reserved_balance(&caller), amount);
assert_eq!(LockedBalance::<T>::get(&caller), amount);
}

unlock {
let caller: T::AccountId = whitelisted_caller();
let amount: BalanceOf<T> = 100u32.into();
T::Currency::reserve(&caller, amount)?;
<LockedBalance<T>>::insert(&caller, amount);
}: _(RawOrigin::Signed(caller.clone()), amount)
verify {
assert_eq!(T::Currency::reserved_balance(&caller), 0u32.into());
assert_eq!(LockedBalance::<T>::get(&caller), 0u32.into());
}
}

impl_benchmark_test_suite!(
Pallet,
crate::mock::new_test_ext(),
crate::mock::Test,
);
94 changes: 94 additions & 0 deletions pallets/pallet-counter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
use frame_support::{
dispatch::DispatchResult,
pallet_prelude::*,
traits::{Currency, ReservableCurrency},
};
use frame_system::pallet_prelude::*;

type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type Currency: Currency<Self::AccountId> + ReservableCurrency<Self::AccountId>;
}

#[pallet::storage]
#[pallet::getter(fn locked_balance)]
pub type LockedBalance<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, BalanceOf<T>, ValueQuery>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Minted { who: T::AccountId, amount: BalanceOf<T> },
Burned { who: T::AccountId, amount: BalanceOf<T> },
Locked { who: T::AccountId, amount: BalanceOf<T> },
Unlocked { who: T::AccountId, amount: BalanceOf<T> },
}

#[pallet::error]
pub enum Error<T> {
InsufficientBalance,
LockNotFound,
UnlockNotPossible,
Unauthorized,
}

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(10_000)]
pub fn mint(origin: OriginFor<T>, account: T::AccountId, amount: BalanceOf<T>) -> DispatchResult {
ensure_root(origin)?;

T::Currency::deposit_creating(&account, amount);
Self::deposit_event(Event::Minted { who: account, amount });
Ok(())
}

#[pallet::weight(10_000)]
pub fn burn(origin: OriginFor<T>, account: T::AccountId, amount: BalanceOf<T>) -> DispatchResult {
ensure_root(origin)?;

T::Currency::withdraw(
&account,
amount,
frame_support::traits::WithdrawReasons::TRANSFER,
frame_support::traits::ExistenceRequirement::KeepAlive,
)?;
Self::deposit_event(Event::Burned { who: account, amount });
Ok(())
}

#[pallet::weight(10_000)]
pub fn lock(origin: OriginFor<T>, amount: BalanceOf<T>) -> DispatchResult {
let who = ensure_signed(origin)?;

T::Currency::reserve(&who, amount)?;
<LockedBalance<T>>::insert(&who, amount);
Self::deposit_event(Event::Locked { who: who.clone(), amount });
Ok(())
}

#[pallet::weight(10_000)]
pub fn unlock(origin: OriginFor<T>, amount: BalanceOf<T>) -> DispatchResult {
let who = ensure_signed(origin)?;

let locked_amount = <LockedBalance<T>>::get(&who);
ensure!(locked_amount >= amount, Error::<T>::UnlockNotPossible);

T::Currency::unreserve(&who, amount);
<LockedBalance<T>>::mutate(&who, |balance| *balance -= amount);
Self::deposit_event(Event::Unlocked { who: who.clone(), amount });
Ok(())
}
}
}
27 changes: 27 additions & 0 deletions pallets/pallet-counter/src/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// pallets/pallet-counter/src/weights.rs

use frame_support::weights::Weight;

pub trait WeightInfo {
fn mint(amount: u64) -> Weight;
fn burn(amount: u64) -> Weight;
fn lock(amount: u64) -> Weight;
fn unlock(amount: u64) -> Weight;
}

pub struct DefaultWeightInfo;

impl WeightInfo for DefaultWeightInfo {
fn mint(amount: u64) -> Weight {
Weight::from_parts(10_000 + 100 * amount, 0)
}
fn burn(amount: u64) -> Weight {
Weight::from_parts(10_000 + 100 * amount, 0)
}
fn lock(amount: u64) -> Weight {
Weight::from_parts(20_000 + 200 * amount, 0)
}
fn unlock(amount: u64) -> Weight {
Weight::from_parts(20_000 + 200 * amount, 0)
}
}
1 change: 1 addition & 0 deletions pallets/template/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ codec = { package = "parity-scale-codec", version = "3.6.1", default-features =
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }

frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }

[dev-dependencies]
Expand Down
Loading

0 comments on commit f1de263

Please sign in to comment.