Skip to content

Commit

Permalink
added xcm bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
freddyli7 committed Nov 7, 2023
1 parent 1697156 commit 077a55d
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 22 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ members = [
"substrate-node/node",
"substrate-node/runtime",
"parachain-info",
"bridge-forwarder"
"bridge-forwarder",
"xcm-bridge"
]

exclude = [
Expand Down
16 changes: 7 additions & 9 deletions bridge-forwarder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod pallet {
use frame_system::pallet_prelude::*;
use frame_support::transactional;
use frame_support::traits::StorageVersion;
use sygma_traits::{TransactorForwarder, OtherWorldBridge, InnerWorldBridge};
use sygma_traits::{TransactorForwarder, Bridge};
use xcm::latest::{prelude::*, MultiAsset, MultiLocation};

const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);
Expand All @@ -20,14 +20,13 @@ pub mod pallet {
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type SygmaBridge: OtherWorldBridge;
type XCMBridge: InnerWorldBridge;
type SygmaBridge: Bridge;
type XCMBridge: Bridge;
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Assets being withdrawn from somewhere.
XCMTransferForward {},
OtherWorldTransferForward {},
}
Expand All @@ -37,16 +36,15 @@ pub mod pallet {
#[pallet::call_index(0)]
#[pallet::weight(Weight::from_parts(195_000_000, 0))]
#[transactional]
pub fn xcm_transactor_forwarder(origin: OriginFor<T>, what: MultiAsset, who: MultiLocation) -> DispatchResult {
T::XCMBridge::create_message()?;
T::XCMBridge::execute_message()?
fn xcm_transactor_forwarder(origin: OriginFor<T>, what: MultiAsset, who: MultiLocation) -> DispatchResult {
T::XCMBridge::transfer(origin.into(), what, who)?;
}

#[pallet::call_index(1)]
#[pallet::weight(Weight::from_parts(195_000_000, 0))]
#[transactional]
pub fn other_world_transactor_forwarder(origin: OriginFor<T>, what: MultiAsset, who: MultiLocation) -> DispatchResult {
T::SygmaBridge::deposit(origin.into(), what, who)?
fn other_world_transactor_forwarder(origin: OriginFor<T>, what: MultiAsset, who: MultiLocation) -> DispatchResult {
T::SygmaBridge::transfer(origin.into(), what, who)?
}
}
}
10 changes: 5 additions & 5 deletions bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub mod pallet {
use sp_std::collections::btree_map::BTreeMap;
use sygma_traits::{
ChainID, DecimalConverter, DepositNonce, DomainID, ExtractDestinationData, FeeHandler,
MpcAddress, ResourceId, TransferType, VerifyingContractAddress, OtherWorldBridge,
MpcAddress, ResourceId, TransferType, VerifyingContractAddress, Bridge,
};

#[allow(dead_code)]
Expand Down Expand Up @@ -683,10 +683,10 @@ pub mod pallet {
}
}

impl<T: Config> OtherWorldBridge for Pallet<T> {
fn do_deposit(sender: [u8; 32],
asset: MultiAsset,
dest: MultiLocation) -> DispatchResult {
impl<T: Config> Bridge for Pallet<T> {
fn transfer(sender: [u8; 32],
asset: MultiAsset,
dest: MultiLocation) -> DispatchResult {
&Self::deposit(sender, asset, dest)?
}
}
Expand Down
9 changes: 2 additions & 7 deletions traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,10 @@ pub trait TransactorForwarder {
fn other_world_transactor_forwarder(sender: [u8; 32], what: MultiAsset, who: MultiLocation) -> DispatchResult;
}

pub trait OtherWorldBridge {
fn do_deposit(
pub trait Bridge {
fn transfer(
sender: [u8; 32],
asset: MultiAsset,
dest: MultiLocation,
) -> DispatchResult;
}

pub trait InnerWorldBridge {
fn create_message() -> DispatchResult;
fn execute_message() -> DispatchResult;
}
52 changes: 52 additions & 0 deletions xcm-bridge/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[package]
name = "sygma-xcm-bridge"
version = "0.3.0"
edition = "2021"
license = "LGPL-3.0"

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["derive", "max-encoded-len"] }
scale-info = { version = "2.5.0", default-features = false, features = ["derive", "serde", "decode"] }

# Substrate
frame-support = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false }
frame-system = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false }
sp-std = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false }

# Polkadot
xcm = { package = "staging-xcm", git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false }
xcm-builder = { package = "staging-xcm-builder", git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false }
xcm-executor = { package = "staging-xcm-executor", git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0", default-features = false }

# Local
sygma-traits = { path = "../traits", default-features = false }

[dev-dependencies]
# Substrate
sp-runtime = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0" }
sp-io = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0" }
frame-support = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0" }
frame-system = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0" }
pallet-balances = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0" }
pallet-assets = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.1.0" }

sygma-traits = { path = "../traits" }

[features]
default = ["std"]
std = [
"codec/std",
"scale-info/std",
"frame-support/std",
"frame-system/std",
"sp-std/std",
"xcm/std",
"xcm-builder/std",
"xcm-executor/std",
"sygma-traits/std",
]
runtime-benchmarks = [
'frame-support/runtime-benchmarks',
'frame-system/runtime-benchmarks',
]
try-runtime = ["frame-support/try-runtime"]
38 changes: 38 additions & 0 deletions xcm-bridge/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub use self::pallet::*;

#[frame_support::pallet]
pub mod pallet {
use sygma_traits::{Bridge};

const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);

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

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

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
XCMTransferSend {},
}

#[pallet::call]
impl<T: Config> Bridge for Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(Weight::from_parts(195_000_000, 0))]
#[transactional]
fn transfer(sender: [u8; 32],
asset: MultiAsset,
dest: MultiLocation) -> DispatchResult {
// TODO: create xcm message
// TODO: execute xcm message
}
}
}

0 comments on commit 077a55d

Please sign in to comment.