From 5fd88d277cffa50f0ca21dc71ad09ccb09a3467e Mon Sep 17 00:00:00 2001 From: Web3 Philosopher Date: Tue, 5 Sep 2023 13:17:50 +0100 Subject: [PATCH] EVM demo (#90) --- ismp-demo/src/lib.rs | 70 ++++++++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 19 deletions(-) diff --git a/ismp-demo/src/lib.rs b/ismp-demo/src/lib.rs index 9ecad25..ebd3e3e 100644 --- a/ismp-demo/src/lib.rs +++ b/ismp-demo/src/lib.rs @@ -20,10 +20,14 @@ extern crate alloc; -use alloc::string::ToString; +use alloc::{ + format, + string::{String, ToString}, +}; use frame_support::{traits::fungible::Mutate, PalletId}; use ismp::{ error::Error as IsmpError, + host::StateMachine, module::IsmpModule, router::{Post, Request, Response}, }; @@ -47,7 +51,7 @@ pub mod pallet { }; use frame_system::pallet_prelude::*; use ismp::{ - host::StateMachine, + host::{Ethereum, StateMachine}, router::{DispatchGet, DispatchPost, DispatchRequest, IsmpDispatcher}, }; @@ -94,6 +98,14 @@ pub mod pallet { source_chain: StateMachine, }, + /// Request data receieved + Request { + /// Source of the request + source: StateMachine, + /// utf-8 decoded data + data: String, + }, + /// Get response recieved GetResponse(Vec>>), } @@ -198,7 +210,7 @@ pub mod pallet { ensure_signed(origin)?; let post = DispatchPost { - dest: params.destination, + dest: StateMachine::Ethereum(params.destination), from: PALLET_ID.to_bytes(), to: params.module.0.to_vec(), timeout_timestamp: params.timeout, @@ -271,8 +283,8 @@ pub mod pallet { /// Destination module pub module: H160, - /// Destination parachain - pub destination: StateMachine, + /// Destination EVM host + pub destination: Ethereum, /// Timeout timestamp on destination chain in seconds pub timeout: u64, @@ -292,20 +304,40 @@ impl IsmpModule for IsmpModuleCallback { fn on_accept(&self, request: Post) -> Result<(), IsmpError> { let source_chain = request.source; - let payload = ::Balance> as codec::Decode>::decode( - &mut &*request.data, - ) - .map_err(|_| { - IsmpError::ImplementationSpecific("Failed to decode request data".to_string()) - })?; - >::mint_into(&payload.to, payload.amount.into()) - .map_err(|_| IsmpError::ImplementationSpecific("Failed to mint funds".to_string()))?; - Pallet::::deposit_event(Event::::BalanceReceived { - from: payload.from, - to: payload.to, - amount: payload.amount, - source_chain, - }); + match source_chain { + StateMachine::Ethereum(_) => Pallet::::deposit_event(Event::Request { + source: source_chain, + data: unsafe { String::from_utf8_unchecked(request.data) }, + }), + StateMachine::Polkadot(_) | StateMachine::Kusama(_) => { + let payload = + ::Balance> as codec::Decode>::decode( + &mut &*request.data, + ) + .map_err(|_| { + IsmpError::ImplementationSpecific( + "Failed to decode request data".to_string(), + ) + })?; + >::mint_into( + &payload.to, + payload.amount.into(), + ) + .map_err(|_| { + IsmpError::ImplementationSpecific("Failed to mint funds".to_string()) + })?; + Pallet::::deposit_event(Event::::BalanceReceived { + from: payload.from, + to: payload.to, + amount: payload.amount, + source_chain, + }); + } + source => { + Err(IsmpError::ImplementationSpecific(format!("Unsupported source {source:?}")))? + } + } + Ok(()) }