#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(test)]
mod tests;
pub mod inbound;
pub mod location;
pub mod operating_mode;
pub mod outbound;
pub mod pricing;
pub mod ringbuffer;
pub use location::{AgentId, AgentIdOf, TokenId, TokenIdOf};
pub use polkadot_parachain_primitives::primitives::{
Id as ParaId, IsSystem, Sibling as SiblingParaId,
};
pub use ringbuffer::{RingBufferMap, RingBufferMapImpl};
pub use sp_core::U256;
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{traits::Contains, BoundedVec};
use hex_literal::hex;
use scale_info::TypeInfo;
use sp_core::{ConstU32, H256};
use sp_io::hashing::keccak_256;
use sp_runtime::{traits::AccountIdConversion, RuntimeDebug};
use sp_std::prelude::*;
use xcm::prelude::{Junction::Parachain, Location};
pub use operating_mode::BasicOperatingMode;
pub use pricing::{PricingParameters, Rewards};
pub fn sibling_sovereign_account<T>(para_id: ParaId) -> T::AccountId
where
T: frame_system::Config,
{
SiblingParaId::from(para_id).into_account_truncating()
}
pub struct AllowSiblingsOnly;
impl Contains<Location> for AllowSiblingsOnly {
fn contains(location: &Location) -> bool {
matches!(location.unpack(), (1, [Parachain(_)]))
}
}
pub fn gwei(x: u128) -> U256 {
U256::from(1_000_000_000u128).saturating_mul(x.into())
}
pub fn meth(x: u128) -> U256 {
U256::from(1_000_000_000_000_000u128).saturating_mul(x.into())
}
pub fn eth(x: u128) -> U256 {
U256::from(1_000_000_000_000_000_000u128).saturating_mul(x.into())
}
pub const ROC: u128 = 1_000_000_000_000;
#[derive(
Clone, Copy, Encode, Decode, PartialEq, Eq, Default, RuntimeDebug, MaxEncodedLen, TypeInfo,
)]
pub struct ChannelId([u8; 32]);
fn derive_channel_id_for_sibling(para_id: ParaId) -> ChannelId {
let para_id: u32 = para_id.into();
let para_id_bytes: [u8; 4] = para_id.to_be_bytes();
let prefix: [u8; 4] = *b"para";
let preimage: Vec<u8> = prefix.into_iter().chain(para_id_bytes).collect();
keccak_256(&preimage).into()
}
impl ChannelId {
pub const fn new(id: [u8; 32]) -> Self {
ChannelId(id)
}
}
impl From<ParaId> for ChannelId {
fn from(value: ParaId) -> Self {
derive_channel_id_for_sibling(value)
}
}
impl From<[u8; 32]> for ChannelId {
fn from(value: [u8; 32]) -> Self {
ChannelId(value)
}
}
impl From<ChannelId> for [u8; 32] {
fn from(value: ChannelId) -> Self {
value.0
}
}
impl<'a> From<&'a [u8; 32]> for ChannelId {
fn from(value: &'a [u8; 32]) -> Self {
ChannelId(*value)
}
}
impl From<H256> for ChannelId {
fn from(value: H256) -> Self {
ChannelId(value.into())
}
}
impl AsRef<[u8]> for ChannelId {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
#[derive(Clone, Encode, Decode, RuntimeDebug, MaxEncodedLen, TypeInfo)]
pub struct Channel {
pub agent_id: AgentId,
pub para_id: ParaId,
}
pub trait StaticLookup {
type Source;
type Target;
fn lookup(s: Self::Source) -> Option<Self::Target>;
}
pub const PRIMARY_GOVERNANCE_CHANNEL: ChannelId =
ChannelId::new(hex!("0000000000000000000000000000000000000000000000000000000000000001"));
pub const SECONDARY_GOVERNANCE_CHANNEL: ChannelId =
ChannelId::new(hex!("0000000000000000000000000000000000000000000000000000000000000002"));
#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)]
pub struct AssetMetadata {
pub name: BoundedVec<u8, ConstU32<METADATA_FIELD_MAX_LEN>>,
pub symbol: BoundedVec<u8, ConstU32<METADATA_FIELD_MAX_LEN>>,
pub decimals: u8,
}
#[cfg(any(test, feature = "std", feature = "runtime-benchmarks"))]
impl Default for AssetMetadata {
fn default() -> Self {
AssetMetadata {
name: BoundedVec::truncate_from(vec![]),
symbol: BoundedVec::truncate_from(vec![]),
decimals: 0,
}
}
}
const METADATA_FIELD_MAX_LEN: u32 = 32;