Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion bridges/snowbridge/pallets/system-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use snowbridge_outbound_queue_primitives::{
use snowbridge_pallet_system::ForeignToNativeId;
use sp_core::{H160, H256};
use sp_io::hashing::blake2_256;
use sp_runtime::traits::MaybeConvert;
use sp_runtime::traits::{MaybeConvert, MaybeEquivalence};
use sp_std::prelude::*;
use xcm::prelude::*;
use xcm_executor::traits::ConvertLocation;
Expand Down Expand Up @@ -318,4 +318,13 @@ pub mod pallet {
snowbridge_pallet_system::Pallet::<T>::maybe_convert(foreign_id)
}
}

impl<T: Config> MaybeEquivalence<TokenId, Location> for Pallet<T> {
fn convert(foreign_id: &TokenId) -> Option<Location> {
snowbridge_pallet_system::Pallet::<T>::convert(foreign_id)
}
fn convert_back(location: &Location) -> Option<TokenId> {
snowbridge_pallet_system::Pallet::<T>::convert_back(location)
}
}
}
5 changes: 5 additions & 0 deletions bridges/snowbridge/pallets/system-v2/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ fn register_all_tokens_succeeds() {
let foreign_token_id =
EthereumSystemV2::location_to_message_origin(tc.native.clone()).unwrap();

assert_eq!(
NativeToForeignId::<Test>::get(reanchored_location.clone()),
Some(foreign_token_id)
);

assert_eq!(
ForeignToNativeId::<Test>::get(foreign_token_id),
Some(reanchored_location.clone())
Expand Down
17 changes: 16 additions & 1 deletion bridges/snowbridge/pallets/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use snowbridge_outbound_queue_primitives::{
};
use sp_core::{RuntimeDebug, H160, H256};
use sp_io::hashing::blake2_256;
use sp_runtime::{traits::MaybeConvert, DispatchError, SaturatedConversion};
use sp_runtime::{traits::{MaybeConvert, MaybeEquivalence}, DispatchError, SaturatedConversion};
use sp_std::prelude::*;
use xcm::prelude::*;
use xcm_executor::traits::ConvertLocation;
Expand Down Expand Up @@ -233,6 +233,11 @@ pub mod pallet {
pub type ForeignToNativeId<T: Config> =
StorageMap<_, Blake2_128Concat, TokenId, Location, OptionQuery>;

/// Lookup table for native location relative to ethereum to foreign token ID
#[pallet::storage]
pub type NativeToForeignId<T: Config> =
StorageMap<_, Blake2_128Concat, Location, TokenId, OptionQuery>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
Expand Down Expand Up @@ -489,6 +494,7 @@ pub mod pallet {
.ok_or(Error::<T>::LocationConversionFailed)?;

if !ForeignToNativeId::<T>::contains_key(token_id) {
NativeToForeignId::<T>::insert(location.clone(), token_id);
ForeignToNativeId::<T>::insert(token_id, location.clone());
}

Expand Down Expand Up @@ -534,4 +540,13 @@ pub mod pallet {
ForeignToNativeId::<T>::get(foreign_id)
}
}

impl<T: Config> MaybeEquivalence<TokenId, Location> for Pallet<T> {
fn convert(foreign_id: &TokenId) -> Option<Location> {
ForeignToNativeId::<T>::get(foreign_id)
}
fn convert_back(location: &Location) -> Option<TokenId> {
NativeToForeignId::<T>::get(location)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::message::{Command, Message, SendMessage};
use frame_support::{ensure, traits::Get};
use snowbridge_core::{AgentId, ChannelId, ParaId, TokenId, TokenIdOf};
use sp_core::{H160, H256};
use sp_runtime::traits::MaybeConvert;
use sp_runtime::traits::{MaybeConvert, MaybeEquivalence};
use sp_std::{iter::Peekable, marker::PhantomData, prelude::*};
use xcm::prelude::*;
use xcm_executor::traits::{ConvertLocation, ExportXcm};
Expand Down Expand Up @@ -48,7 +48,7 @@ where
EthereumNetwork: Get<NetworkId>,
OutboundQueue: SendMessage<Balance = u128>,
AgentHashedDescription: ConvertLocation<H256>,
ConvertAssetId: MaybeConvert<TokenId, Location>,
ConvertAssetId: MaybeEquivalence<TokenId, Location>,
{
type Ticket = (Vec<u8>, XcmHash);

Expand Down Expand Up @@ -194,7 +194,7 @@ struct XcmConverter<'a, ConvertAssetId, Call> {
}
impl<'a, ConvertAssetId, Call> XcmConverter<'a, ConvertAssetId, Call>
where
ConvertAssetId: MaybeConvert<TokenId, Location>,
ConvertAssetId: MaybeEquivalence<TokenId, Location>,
{
fn new(message: &'a Xcm<Call>, ethereum_network: NetworkId, agent_id: AgentId) -> Self {
Self {
Expand Down Expand Up @@ -411,9 +411,7 @@ where
// transfer amount must be greater than 0.
ensure!(amount > 0, ZeroAssetTransfer);

let token_id = TokenIdOf::convert_location(&asset_id).ok_or(InvalidAsset)?;

ConvertAssetId::maybe_convert(token_id).ok_or(InvalidAsset)?;
let token_id = ConvertAssetId::convert_back(&asset_id).ok_or(InvalidAsset)?;

// Check if there is a SetTopic and skip over it if found.
let topic_id = match_expression!(self.next()?, SetTopic(id), id).ok_or(SetTopicExpected)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ impl SendMessageFeeProvider for MockErrOutboundQueue {
}

pub struct MockTokenIdConvert;
impl MaybeConvert<TokenId, Location> for MockTokenIdConvert {
fn maybe_convert(_id: TokenId) -> Option<Location> {
impl MaybeEquivalence<TokenId, Location> for MockTokenIdConvert {
fn convert(_id: &TokenId) -> Option<Location> {
Some(Location::new(1, [GlobalConsensus(ByGenesis(WESTEND_GENESIS_HASH))]))
}
fn convert_back(_loc: &Location) -> Option<TokenId> {
Some(1)
}
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::v2::{

use crate::v2::convert::XcmConverterError::{AssetResolutionFailed, FilterDoesNotConsumeAllAssets};
use sp_core::H160;
use sp_runtime::traits::MaybeConvert;
use sp_runtime::traits::{MaybeConvert, MaybeEquivalence};
use sp_std::{iter::Peekable, marker::PhantomData, prelude::*};
use xcm::prelude::*;
use xcm_executor::traits::ConvertLocation;
Expand Down Expand Up @@ -64,7 +64,7 @@ pub struct XcmConverter<'a, ConvertAssetId, Call> {
}
impl<'a, ConvertAssetId, Call> XcmConverter<'a, ConvertAssetId, Call>
where
ConvertAssetId: MaybeConvert<TokenId, Location>,
ConvertAssetId: MaybeEquivalence<TokenId, Location>,
{
pub fn new(message: &'a Xcm<Call>, ethereum_network: NetworkId) -> Self {
Self {
Expand Down Expand Up @@ -173,8 +173,7 @@ where
ensure!(amount > 0, ZeroAssetTransfer);

// Ensure PNA already registered
let token_id = TokenIdOf::convert_location(&asset_id).ok_or(InvalidAsset)?;
ConvertAssetId::maybe_convert(token_id).ok_or(InvalidAsset)?;
let token_id = ConvertAssetId::convert_back(&asset_id).ok_or(InvalidAsset)?;

commands.push(Command::MintForeignToken { token_id, recipient, amount });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use frame_support::{
traits::{Contains, Get, ProcessMessageError},
};
use snowbridge_core::{ParaId, TokenId};
use sp_runtime::traits::MaybeConvert;
use sp_runtime::traits::{MaybeConvert, MaybeEquivalence};
use sp_std::{marker::PhantomData, ops::ControlFlow, prelude::*};
use xcm::prelude::*;
use xcm_builder::{CreateMatcher, ExporterFor, MatchXcm};
Expand Down Expand Up @@ -53,7 +53,7 @@ where
UniversalLocation: Get<InteriorLocation>,
EthereumNetwork: Get<NetworkId>,
OutboundQueue: SendMessage,
ConvertAssetId: MaybeConvert<TokenId, Location>,
ConvertAssetId: MaybeEquivalence<TokenId, Location>,
AssetHubParaId: Get<ParaId>,
{
type Ticket = (Vec<u8>, XcmHash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ impl SendMessageFeeProvider for MockErrOutboundQueue {
}

pub struct MockTokenIdConvert;
impl MaybeConvert<TokenId, Location> for MockTokenIdConvert {
fn maybe_convert(_id: TokenId) -> Option<Location> {
impl MaybeEquivalence<TokenId, Location> for MockTokenIdConvert {
fn convert(_id: &TokenId) -> Option<Location> {
Some(Location::new(1, [GlobalConsensus(ByGenesis(WESTEND_GENESIS_HASH))]))
}
fn convert_back(_loc: &Location) -> Option<TokenId> {
Some(1)
}
}

#[test]
Expand Down
Loading