Skip to content

Commit

Permalink
Bump Zenlink Protocol v0.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
icodezjb committed Apr 3, 2021
1 parent a568bf6 commit 1616b70
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 157 deletions.
186 changes: 69 additions & 117 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
It should at least work until the following commits

- rustc 1.52.0-nightly (d1206f950 2021-02-15)
- polkadot.js.org (apps v0.85.2-7)
- Polkadot rococo-v1 @ `34e8d5061434c039f0e13c8319dedf54dbb4bc81`
- Cumulus rococo-v1 @ `90b1e1804897193b3911f49b45de51532e305733`
- Substrate rococo-v1 @ `1404f2af4cbe90d35b4c8a1405a9452feb789adc`
- polkadot.js.org (apps v0.86.3-28)
- Polkadot rococo-v1 @ `f0d5c3d578689109aecb2660a98432e7cd10f53d`
- Cumulus rococo-v1 @ `b9b9cbdbcce378d4e7ce724339fd92ffb759090f`
- Substrate rococo-v1 @ `127f108ad23a5d5b809b655a9aa6f05153a81cfe`


## Precondition
Expand Down
13 changes: 6 additions & 7 deletions example/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use sp_api::impl_runtime_apis;
use sp_core::OpaqueMetadata;
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
traits::{BlakeTwo256, Block as BlockT, Convert, IdentityLookup},
traits::{AccountIdLookup, BlakeTwo256, Block as BlockT, Convert},
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult, ModuleId, Perbill,
};
Expand Down Expand Up @@ -104,7 +104,7 @@ const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
const MAXIMUM_BLOCK_WEIGHT: Weight = 2 * WEIGHT_PER_SECOND;

parameter_types! {
pub const SS58Prefix: u8 = 42;
pub const SS58Prefix: u8 = 42;
pub const BlockHashCount: BlockNumber = 250;
pub const Version: RuntimeVersion = VERSION;
pub RuntimeBlockLength: BlockLength =
Expand Down Expand Up @@ -136,7 +136,7 @@ impl frame_system::Config for Runtime {
/// The aggregated dispatch type that is available for extrinsics.
type Call = Call;
/// The lookup mechanism to get account ID from whatever is passed in dispatchers.
type Lookup = IdentityLookup<AccountId>;
type Lookup = AccountIdLookup<AccountId, ()>;
/// The index type for storing how many extrinsics an account has signed.
type Index = Index;
/// The index type for blocks.
Expand Down Expand Up @@ -166,6 +166,7 @@ impl frame_system::Config for Runtime {
type BlockWeights = RuntimeBlockWeights;
type BlockLength = RuntimeBlockLength;
type SS58Prefix = SS58Prefix;
type OnSetCode = ParachainSystem;
}

parameter_types! {
Expand Down Expand Up @@ -215,7 +216,7 @@ impl cumulus_pallet_parachain_system::Config for Runtime {
type OnValidationData = ();
type SelfParaId = ParachainInfo;
type DownwardMessageHandlers = ZenlinkProtocol;
type HrmpMessageHandlers = ZenlinkProtocol;
type XcmpMessageHandlers = ZenlinkProtocol;
}

impl parachain_info::Config for Runtime {}
Expand All @@ -239,7 +240,7 @@ construct_runtime! {
}

/// The address format for describing accounts.
pub type Address = AccountId;
pub type Address = sp_runtime::MultiAddress<AccountId, ()>;
/// Block header type as expected by this runtime.
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
/// Block type as expected by this runtime.
Expand Down Expand Up @@ -370,15 +371,13 @@ impl_runtime_apis! {
ZenlinkProtocol::get_owner_pairs(&owner)
}

//buy amount token price
fn get_amount_in_price(
supply: TokenBalance,
path: Vec<AssetId>
) -> TokenBalance {
ZenlinkProtocol::desired_in_amount(supply, path)
}

//sell amount token price
fn get_amount_out_price(
supply: TokenBalance,
path: Vec<AssetId>
Expand Down
5 changes: 3 additions & 2 deletions example/runtime/src/zenlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ parameter_types! {
}.into();

pub SiblingParachains: Vec<MultiLocation> = vec![
// Pla local and live
// Phala local and live
MultiLocation::X2(Junction::Parent, Junction::Parachain { id: 30 }),
// Sherpax live
MultiLocation::X2(Junction::Parent, Junction::Parachain { id: 59 }),
Expand All @@ -38,6 +38,7 @@ parameter_types! {
}

pub struct AccountId32Converter;

impl Convert<AccountId, [u8; 32]> for AccountId32Converter {
fn convert(account_id: AccountId) -> [u8; 32] {
account_id.into()
Expand Down Expand Up @@ -127,7 +128,7 @@ impl zenlink_protocol::Config for Runtime {
type Event = Event;
type XcmExecutor = XcmExecutor<XcmConfig>;
type UpwardMessageSender = ParachainSystem;
type HrmpMessageSender = ParachainSystem;
type XcmpMessageSender = ParachainSystem;
type AccountIdConverter = LocationConverter;
type AccountId32Converter = AccountId32Converter;
type ParaId = ParachainInfo;
Expand Down
2 changes: 1 addition & 1 deletion zenlink-protocol/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zenlink-protocol"
version = "0.2.1"
version = "0.2.3"
authors = ["Zenlink Developers"]
edition = "2018"
license = "GPL-3.0-only"
Expand Down
2 changes: 1 addition & 1 deletion zenlink-protocol/rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zenlink-protocol-rpc"
version = "0.2.1"
version = "0.2.3"
authors = ["Zenlink Developers"]
edition = "2018"

Expand Down
8 changes: 4 additions & 4 deletions zenlink-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub use cumulus_primitives_core::{
relay_chain::Balance as RelayChainBalance, DownwardMessageHandler, HrmpMessageHandler,
HrmpMessageSender, InboundDownwardMessage, InboundHrmpMessage, OutboundHrmpMessage, ParaId,
UpwardMessage, UpwardMessageSender,
relay_chain::Balance as RelayChainBalance, DownwardMessageHandler, XcmpMessageHandler,
XcmpMessageSender, InboundDownwardMessage, InboundHrmpMessage, OutboundHrmpMessage, ParaId,
UpwardMessage, UpwardMessageSender, relay_chain, ServiceQuality,
};
use frame_support::{
decl_error, decl_event, decl_module, decl_storage,
Expand Down Expand Up @@ -68,7 +68,7 @@ pub trait Config: frame_system::Config {
/// Something to send an upward message.
type UpwardMessageSender: UpwardMessageSender;
/// Something to send an HRMP message.
type HrmpMessageSender: HrmpMessageSender;
type XcmpMessageSender: XcmpMessageSender;
/// The set of parachains which the xcm can reach
type TargetChains: Get<Vec<MultiLocation>>;
/// The Zenlink Protocol Module Id
Expand Down
41 changes: 20 additions & 21 deletions zenlink-protocol/src/xtransfer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ use codec::{Decode, Encode};
use sp_runtime::traits::Hash;
use sp_std::{
convert::{TryFrom, TryInto},
vec,
vec, prelude::Vec,
};

use crate::{
sp_api_hidden_includes_decl_storage::hidden_include::traits::PalletInfo,
AssetId, AssetProperty, Config, Convert, DownwardMessageHandler, ExecuteXcm, Get,
HrmpMessageHandler, HrmpMessageSender, InboundDownwardMessage, InboundHrmpMessage, Junction,
Module, MultiAsset, MultiLocation, NetworkId, Order, OutboundHrmpMessage, ParaId,
XcmpMessageHandler, XcmpMessageSender, InboundDownwardMessage, Junction,
Module, MultiAsset, MultiLocation, NetworkId, Order, ParaId,
RawEvent::{
HrmpMessageSent, UpwardMessageSent, XcmBadFormat, XcmBadVersion, XcmExecuteFail,
XcmExecuteSuccess,
},
SendXcm, TokenBalance, UpwardMessageSender, VersionedXcm, Xcm, XcmError, LOG_TARGET,
relay_chain, ServiceQuality,
};

/// Origin for the parachains module.
Expand Down Expand Up @@ -187,13 +188,13 @@ impl<T: Config> DownwardMessageHandler for Module<T> {
}
}

impl<T: Config> HrmpMessageHandler for Module<T> {
fn handle_hrmp_message(sender: ParaId, msg: InboundHrmpMessage) {
let hash = T::Hashing::hash(&msg.data);
impl<T: Config> XcmpMessageHandler for Module<T> {
fn handle_xcm_message(sender: ParaId, _sent_at: relay_chain::BlockNumber, xcm: VersionedXcm) {
let hash = xcm.using_encoded(T::Hashing::hash);
log::info!(target: LOG_TARGET, "Processing HRMP XCM: {:?}", &hash);
match VersionedXcm::decode(&mut &msg.data[..]).map(Xcm::try_from) {
Ok(Ok(xcm)) => {
log::info!(target: LOG_TARGET, "handle_hrmp_message: xcm = {:?}", xcm);
match Xcm::try_from(xcm) {
Ok(xcm) => {
log::info!(target: LOG_TARGET, "handle_xcm_message: xcm = {:?}", xcm);

let origin =
MultiLocation::X2(Junction::Parent, Junction::Parachain { id: sender.into() });
Expand All @@ -203,9 +204,12 @@ impl<T: Config> HrmpMessageHandler for Module<T> {
Err(e) => Self::deposit_event(XcmExecuteFail(hash, e)),
};
}
Ok(Err(..)) => Self::deposit_event(XcmBadVersion(hash)),
Err(..) => Self::deposit_event(XcmBadFormat(hash)),
}
Err(..) => Self::deposit_event(XcmBadVersion(hash)),
};
}

fn handle_blob_message(_sender: ParaId, _sent_at: relay_chain::BlockNumber, _blob: Vec<u8>) {
debug_assert!(false, "Blob messages not handled.")
}
}

Expand Down Expand Up @@ -242,11 +246,8 @@ impl<T: Config> SendXcm for Module<T> {
}
// An HRMP message for a sibling parachain.
Some(Junction::Parachain { id }) => {
let data = vmsg.encode();
let hash = T::Hashing::hash(&data);
let message = OutboundHrmpMessage { recipient: (*id).into(), data };

T::HrmpMessageSender::send_hrmp_message(message)
let hash = T::Hashing::hash_of(&vmsg);
T::XcmpMessageSender::send_xcm_message((*id).into(), vmsg, ServiceQuality::Ordered)
.map_err(|_| XcmError::CannotReachDestination)?;

Self::deposit_event(HrmpMessageSent(hash));
Expand All @@ -260,11 +261,9 @@ impl<T: Config> SendXcm for Module<T> {
let vmsg: VersionedXcm = msg.into();
match dest.at(1) {
Some(Junction::Parachain { id }) => {
let data = vmsg.encode();
let hash = T::Hashing::hash(&data);
let message = OutboundHrmpMessage { recipient: (*id).into(), data };
let hash = T::Hashing::hash_of(&vmsg);

T::HrmpMessageSender::send_hrmp_message(message)
T::XcmpMessageSender::send_xcm_message((*id).into(), vmsg, ServiceQuality::Ordered)
.map_err(|_| XcmError::CannotReachDestination)?;

Self::deposit_event(HrmpMessageSent(hash));
Expand Down

0 comments on commit 1616b70

Please sign in to comment.