Skip to content

Commit

Permalink
add new syntax for erc20 and erc721
Browse files Browse the repository at this point in the history
  • Loading branch information
Raid5594 committed Dec 21, 2023
1 parent 65806be commit 3c36941
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 141 deletions.
106 changes: 55 additions & 51 deletions pallets/erc20/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,70 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::{
decl_error, decl_event, decl_module, decl_storage,
pallet_prelude::*,
dispatch::DispatchResult,
ensure,
traits::{Currency, EnsureOrigin, ExistenceRequirement::AllowDeath, Get},
};
use frame_system::{self as system, ensure_signed};
use frame_system::{ensure_signed, pallet_prelude::*};
use pallet_chainbridge as bridge;
use pallet_erc721 as erc721;
pub use pallet::*;
use sp_arithmetic::traits::SaturatedConversion;
use sp_core::U256;
use sp_std::prelude::*;

type ResourceId = bridge::ResourceId;

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

pub trait Config: system::Config + bridge::Config + erc721::Config {
type RuntimeEvent: From<Event<Self>> + Into<<Self as frame_system::Config>::RuntimeEvent>;
/// Specifies the origin check provided by the bridge for calls that can only be called by the
/// bridge pallet
type BridgeOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = Self::AccountId>;

/// The currency mechanism.
type Currency: Currency<Self::AccountId>;

/// Ids can be defined by the runtime and passed in, perhaps from blake2b_128 hashes.
type HashId: Get<ResourceId>;
type NativeTokenId: Get<ResourceId>;
type Erc721Id: Get<ResourceId>;
}
#[frame_support::pallet]
pub mod pallet {
use super::*;

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

#[pallet::config]
pub trait Config: frame_system::Config + bridge::Config + erc721::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Specifies the origin check provided by the bridge for calls that can only be called by the
/// bridge pallet
type BridgeOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = Self::AccountId>;

/// The currency mechanism.
type Currency: Currency<Self::AccountId>;

/// Ids can be defined by the runtime and passed in, perhaps from blake2b_128 hashes.
type HashId: Get<ResourceId>;
type NativeTokenId: Get<ResourceId>;
type Erc721Id: Get<ResourceId>;
}

decl_error! {
pub enum Error for Module<T: Config>{
#[pallet::error]
pub enum Error<T> {
InvalidTransfer,
}
}

decl_storage! {
trait Store for Module<T: Config> as Test {}
}

decl_event!(
pub enum Event<T> where
<T as frame_system::Config>::Hash,
{
Remark(Hash),
#[pallet::event]
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
pub enum Event<T: Config> {
Remark(<T as frame_system::Config>::Hash),
}
);

decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin {
const HashId: ResourceId = T::HashId::get();
const NativeTokenId: ResourceId = T::NativeTokenId::get();
const Erc721Id: ResourceId = T::Erc721Id::get();

fn deposit_event() = default;

#[pallet::call]
impl<T: Config> Pallet<T> {
//
// Initiation calls. These start a bridge transfer.
//

/// Transfers an arbitrary hash to a (whitelisted) destination chain.
#[weight = 195_000_000]
pub fn transfer_hash(origin, hash: T::Hash, dest_id: bridge::ChainId) -> DispatchResult {
#[pallet::call_index(0)]
#[pallet::weight(195_000_000)]
pub fn transfer_hash(origin: OriginFor<T>, hash: T::Hash, dest_id: bridge::ChainId) -> DispatchResult {
ensure_signed(origin)?;

let resource_id = T::HashId::get();
Expand All @@ -76,8 +75,9 @@ decl_module! {
}

/// Transfers some amount of the native token to some recipient on a (whitelisted) destination chain.
#[weight = 195_000_000]
pub fn transfer_native(origin, amount: BalanceOf<T>, recipient: Vec<u8>, dest_id: bridge::ChainId) -> DispatchResult {
#[pallet::call_index(1)]
#[pallet::weight(195_000_000)]
pub fn transfer_native(origin: OriginFor<T>, amount: BalanceOf<T>, recipient: Vec<u8>, dest_id: bridge::ChainId) -> DispatchResult {
let source = ensure_signed(origin)?;
ensure!(<bridge::Module<T>>::chain_whitelisted(dest_id), Error::<T>::InvalidTransfer);
let bridge_id = <bridge::Module<T>>::account_id();
Expand All @@ -91,8 +91,9 @@ decl_module! {


/// Transfer a non-fungible token (erc721) to a (whitelisted) destination chain.
#[weight = 195_000_000]
pub fn transfer_erc721(origin, recipient: Vec<u8>, token_id: U256, dest_id: bridge::ChainId) -> DispatchResult {
#[pallet::call_index(2)]
#[pallet::weight(195_000_000)]
pub fn transfer_erc721(origin: OriginFor<T>, recipient: Vec<u8>, token_id: U256, dest_id: bridge::ChainId) -> DispatchResult {
let source = ensure_signed(origin)?;
ensure!(<bridge::Module<T>>::chain_whitelisted(dest_id), Error::<T>::InvalidTransfer);
match <erc721::Module<T>>::tokens(token_id) {
Expand All @@ -112,24 +113,27 @@ decl_module! {
//

/// Executes a simple currency transfer using the bridge account as the source
#[weight = 195_000_000]
pub fn transfer(origin, to: T::AccountId, amount: BalanceOf<T>) -> DispatchResult {
#[pallet::call_index(3)]
#[pallet::weight(195_000_000)]
pub fn transfer(origin: OriginFor<T>, to: T::AccountId, amount: BalanceOf<T>) -> DispatchResult {
let source = T::BridgeOrigin::ensure_origin(origin)?;
<T as Config>::Currency::transfer(&source, &to, amount, AllowDeath)?;
Ok(())
}

/// This can be called by the bridge to demonstrate an arbitrary call from a proposal.
#[weight = 195_000_000]
pub fn remark(origin, hash: T::Hash) -> DispatchResult {
#[pallet::call_index(4)]
#[pallet::weight(195_000_000)]
pub fn remark(origin: OriginFor<T>, hash: T::Hash) -> DispatchResult {
T::BridgeOrigin::ensure_origin(origin)?;
Self::deposit_event(RawEvent::Remark(hash));
Self::deposit_event(Event::<T>::Remark(hash));
Ok(())
}

/// Allows the bridge to issue new erc721 tokens
#[weight = 195_000_000]
pub fn mint_erc721(origin, recipient: T::AccountId, id: U256, metadata: Vec<u8>) -> DispatchResult {
#[pallet::call_index(5)]
#[pallet::weight(195_000_000)]
pub fn mint_erc721(origin: OriginFor<T>, recipient: T::AccountId, id: U256, metadata: Vec<u8>) -> DispatchResult {
T::BridgeOrigin::ensure_origin(origin)?;
<erc721::Module<T>>::mint_token(recipient, id, metadata)?;
Ok(())
Expand Down
Loading

0 comments on commit 3c36941

Please sign in to comment.