Skip to content
Merged
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
9 changes: 3 additions & 6 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use snowbridge_core::TokenId;
use snowbridge_inbound_queue_primitives::{v2::MessageToXcm, Log, Proof, VerificationError};
use sp_core::H160;
use sp_runtime::{
traits::{IdentityLookup, MaybeEquivalence},
traits::{IdentityLookup, MaybeConvert},
BuildStorage,
};
use sp_std::{convert::From, default::Default, marker::PhantomData};
Expand Down Expand Up @@ -76,13 +76,10 @@ impl<T: Config> BenchmarkHelper<T> for Test {
}

pub struct MockTokenIdConvert;
impl MaybeEquivalence<TokenId, Location> for MockTokenIdConvert {
fn convert(_id: &TokenId) -> Option<Location> {
impl MaybeConvert<TokenId, Location> for MockTokenIdConvert {
fn maybe_convert(_id: TokenId) -> Option<Location> {
Some(Location::parent())
}
fn convert_back(_loc: &Location) -> Option<TokenId> {
None
}
}

pub struct MockAccountLocationConverter<AccountId>(PhantomData<AccountId>);
Expand Down
9 changes: 3 additions & 6 deletions bridges/snowbridge/pallets/inbound-queue/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use snowbridge_core::{
use snowbridge_inbound_queue_primitives::{v1::MessageToXcm, Log, Proof, VerificationError};
use sp_core::{H160, H256};
use sp_runtime::{
traits::{IdentifyAccount, IdentityLookup, MaybeEquivalence, Verify},
traits::{IdentifyAccount, IdentityLookup, MaybeConvert, Verify},
BuildStorage, FixedU128, MultiSignature,
};
use sp_std::{convert::From, default::Default};
Expand Down Expand Up @@ -214,13 +214,10 @@ impl TransactAsset for SuccessfulTransactor {
}

pub struct MockTokenIdConvert;
impl MaybeEquivalence<TokenId, Location> for MockTokenIdConvert {
fn convert(_id: &TokenId) -> Option<Location> {
impl MaybeConvert<TokenId, Location> for MockTokenIdConvert {
fn maybe_convert(_id: TokenId) -> Option<Location> {
Some(Location::parent())
}
fn convert_back(_loc: &Location) -> Option<TokenId> {
None
}
}

impl inbound_queue::Config for Test {
Expand Down
14 changes: 5 additions & 9 deletions bridges/snowbridge/pallets/system-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ use snowbridge_outbound_queue_primitives::{
v2::{Command, Initializer, Message, SendMessage},
OperatingMode, SendError,
};
use snowbridge_pallet_system::{ForeignToNativeId, NativeToForeignId};
use snowbridge_pallet_system::ForeignToNativeId;
use sp_core::{H160, H256};
use sp_io::hashing::blake2_256;
use sp_runtime::traits::MaybeEquivalence;
use sp_runtime::traits::MaybeConvert;
use sp_std::prelude::*;
use xcm::prelude::*;
use xcm_executor::traits::ConvertLocation;
Expand Down Expand Up @@ -200,7 +200,6 @@ 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 @@ -255,12 +254,9 @@ pub mod pallet {
}
}

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)
impl<T: Config> MaybeConvert<TokenId, Location> for Pallet<T> {
fn maybe_convert(foreign_id: TokenId) -> Option<Location> {
snowbridge_pallet_system::Pallet::<T>::maybe_convert(foreign_id)
}
}
}
4 changes: 0 additions & 4 deletions bridges/snowbridge/pallets/system-v2/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ 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: 4 additions & 13 deletions 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::MaybeEquivalence, DispatchError, SaturatedConversion};
use sp_runtime::{traits::MaybeConvert, DispatchError, SaturatedConversion};
use sp_std::prelude::*;
use xcm::prelude::*;
use xcm_executor::traits::ConvertLocation;
Expand Down Expand Up @@ -231,12 +231,7 @@ pub mod pallet {
/// Lookup table for foreign token ID to native location relative to ethereum
#[pallet::storage]
pub type ForeignToNativeId<T: Config> =
StorageMap<_, Blake2_128Concat, TokenId, xcm::v5::Location, OptionQuery>;

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

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
Expand Down Expand Up @@ -494,7 +489,6 @@ 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 @@ -535,12 +529,9 @@ pub mod pallet {
}
}

impl<T: Config> MaybeEquivalence<TokenId, Location> for Pallet<T> {
fn convert(foreign_id: &TokenId) -> Option<Location> {
impl<T: Config> MaybeConvert<TokenId, Location> for Pallet<T> {
fn maybe_convert(foreign_id: TokenId) -> Option<Location> {
ForeignToNativeId::<T>::get(foreign_id)
}
fn convert_back(location: &Location) -> Option<TokenId> {
NativeToForeignId::<T>::get(location)
}
}
}
1 change: 0 additions & 1 deletion bridges/snowbridge/pallets/system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ fn register_all_tokens_succeeds() {
Default::default()
));

assert_eq!(NativeToForeignId::<Test>::get(tc.reanchored.clone()), Some(tc.foreign));
assert_eq!(ForeignToNativeId::<Test>::get(tc.foreign), Some(tc.reanchored.clone()));

System::assert_last_event(RuntimeEvent::EthereumSystem(Event::<Test>::RegisterToken {
Expand Down
10 changes: 5 additions & 5 deletions bridges/snowbridge/primitives/inbound-queue/src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use frame_support::{traits::tokens::Balance as BalanceT, PalletError};
use scale_info::TypeInfo;
use snowbridge_core::TokenId;
use sp_core::{Get, RuntimeDebug, H160, H256};
use sp_runtime::{traits::MaybeEquivalence, MultiAddress};
use sp_runtime::{traits::MaybeConvert, MultiAddress};
use sp_std::prelude::*;
use xcm::prelude::{Junction::AccountKey20, *};

Expand Down Expand Up @@ -104,7 +104,7 @@ pub struct MessageToXcm<
CreateAssetCall: Get<CallIndex>,
CreateAssetDeposit: Get<u128>,
Balance: BalanceT,
ConvertAssetId: MaybeEquivalence<TokenId, Location>,
ConvertAssetId: MaybeConvert<TokenId, Location>,
EthereumUniversalLocation: Get<InteriorLocation>,
GlobalAssetHubLocation: Get<Location>,
{
Expand Down Expand Up @@ -171,7 +171,7 @@ where
InboundQueuePalletInstance: Get<u8>,
Balance: BalanceT + From<u128>,
AccountId: Into<[u8; 32]>,
ConvertAssetId: MaybeEquivalence<TokenId, Location>,
ConvertAssetId: MaybeConvert<TokenId, Location>,
EthereumUniversalLocation: Get<InteriorLocation>,
GlobalAssetHubLocation: Get<Location>,
{
Expand Down Expand Up @@ -230,7 +230,7 @@ where
InboundQueuePalletInstance: Get<u8>,
Balance: BalanceT + From<u128>,
AccountId: Into<[u8; 32]>,
ConvertAssetId: MaybeEquivalence<TokenId, Location>,
ConvertAssetId: MaybeConvert<TokenId, Location>,
EthereumUniversalLocation: Get<InteriorLocation>,
GlobalAssetHubLocation: Get<Location>,
{
Expand Down Expand Up @@ -426,7 +426,7 @@ where
let total_fee_asset: Asset = (Location::parent(), asset_hub_fee).into();

let asset_loc =
ConvertAssetId::convert(&token_id).ok_or(ConvertMessageError::InvalidToken)?;
ConvertAssetId::maybe_convert(token_id).ok_or(ConvertMessageError::InvalidToken)?;

let mut reanchored_asset_loc = asset_loc.clone();
reanchored_asset_loc
Expand Down
16 changes: 6 additions & 10 deletions bridges/snowbridge/primitives/inbound-queue/src/v2/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use frame_support::ensure;
use snowbridge_core::TokenId;
use sp_core::{Get, RuntimeDebug, H160};
use sp_io::hashing::blake2_256;
use sp_runtime::{traits::MaybeEquivalence, MultiAddress};
use sp_runtime::{traits::MaybeConvert, MultiAddress};
use sp_std::prelude::*;
use xcm::{
prelude::{Junction::*, *},
Expand Down Expand Up @@ -103,7 +103,7 @@ where
CreateAssetDeposit: Get<u128>,
EthereumNetwork: Get<NetworkId>,
InboundQueueLocation: Get<InteriorLocation>,
ConvertAssetId: MaybeEquivalence<TokenId, Location>,
ConvertAssetId: MaybeConvert<TokenId, Location>,
GatewayProxyAddress: Get<H160>,
EthereumUniversalLocation: Get<InteriorLocation>,
AssetHubFromEthereum: Get<Location>,
Expand Down Expand Up @@ -163,7 +163,7 @@ where
assets.push(AssetTransfer::ReserveDeposit(asset));
},
EthereumAsset::ForeignTokenERC20 { token_id, value } => {
let asset_loc = ConvertAssetId::convert(&token_id)
let asset_loc = ConvertAssetId::maybe_convert(*token_id)
.ok_or(ConvertMessageError::InvalidAsset)?;
let reanchored_asset_loc = asset_loc
.reanchored(&AssetHubFromEthereum::get(), &EthereumUniversalLocation::get())
Expand Down Expand Up @@ -329,7 +329,7 @@ where
CreateAssetDeposit: Get<u128>,
EthereumNetwork: Get<NetworkId>,
InboundQueueLocation: Get<InteriorLocation>,
ConvertAssetId: MaybeEquivalence<TokenId, Location>,
ConvertAssetId: MaybeConvert<TokenId, Location>,
GatewayProxyAddress: Get<H160>,
EthereumUniversalLocation: Get<InteriorLocation>,
AssetHubFromEthereum: Get<Location>,
Expand Down Expand Up @@ -402,7 +402,6 @@ mod tests {
add_location_override, reanchor_to_ethereum, LocationIdConvert,
};
use sp_core::{H160, H256};
use sp_runtime::traits::MaybeEquivalence;
const GATEWAY_ADDRESS: [u8; 20] = hex!["eda338e4dc46038493b885327842fd3e301cab39"];

parameter_types! {
Expand All @@ -420,11 +419,8 @@ mod tests {
}

pub struct MockFailedTokenConvert;
impl MaybeEquivalence<TokenId, Location> for MockFailedTokenConvert {
fn convert(_id: &TokenId) -> Option<Location> {
None
}
fn convert_back(_loc: &Location) -> Option<TokenId> {
impl MaybeConvert<TokenId, Location> for MockFailedTokenConvert {
fn maybe_convert(_id: TokenId) -> Option<Location> {
None
}
}
Expand Down
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::MaybeEquivalence;
use sp_runtime::traits::MaybeConvert;
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: MaybeEquivalence<TokenId, Location>,
ConvertAssetId: MaybeConvert<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: MaybeEquivalence<TokenId, Location>,
ConvertAssetId: MaybeConvert<TokenId, Location>,
{
fn new(message: &'a Xcm<Call>, ethereum_network: NetworkId, agent_id: AgentId) -> Self {
Self {
Expand Down Expand Up @@ -413,7 +413,7 @@ where

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

ConvertAssetId::convert(&token_id).ok_or(InvalidAsset)?;
ConvertAssetId::maybe_convert(token_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,13 +62,10 @@ impl SendMessageFeeProvider for MockErrOutboundQueue {
}

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

#[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::MaybeEquivalence;
use sp_runtime::traits::MaybeConvert;
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: MaybeEquivalence<TokenId, Location>,
ConvertAssetId: MaybeConvert<TokenId, Location>,
{
pub fn new(message: &'a Xcm<Call>, ethereum_network: NetworkId) -> Self {
Self {
Expand Down Expand Up @@ -174,7 +174,7 @@ where

// Ensure PNA already registered
let token_id = TokenIdOf::convert_location(&asset_id).ok_or(InvalidAsset)?;
ConvertAssetId::convert(&token_id).ok_or(InvalidAsset)?;
ConvertAssetId::maybe_convert(token_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::MaybeEquivalence;
use sp_runtime::traits::MaybeConvert;
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: MaybeEquivalence<TokenId, Location>,
ConvertAssetId: MaybeConvert<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,13 +62,10 @@ impl SendMessageFeeProvider for MockErrOutboundQueue {
}

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

#[test]
Expand Down
12 changes: 4 additions & 8 deletions bridges/snowbridge/test-utils/src/mock_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>

use codec::Encode;
use frame_support::sp_runtime::traits::MaybeEquivalence;
use frame_support::sp_runtime::traits::MaybeConvert;
use snowbridge_core::TokenIdOf;
use sp_core::H256;
use std::{cell::RefCell, collections::HashMap};
Expand Down Expand Up @@ -35,12 +35,8 @@ pub fn reanchor_to_ethereum(
}

pub struct LocationIdConvert;
impl MaybeEquivalence<H256, Location> for LocationIdConvert {
fn convert(id: &H256) -> Option<Location> {
IDENTIFIER_TO_LOCATION.with(|b| b.borrow().get(id).and_then(|l| Option::from(l.clone())))
}
fn convert_back(lol: &Location) -> Option<H256> {
LOCATION_TO_IDENTIFIER
.with(|b| b.borrow().get(&lol.encode()).and_then(|id| Option::from(*id)))
impl MaybeConvert<H256, Location> for LocationIdConvert {
fn maybe_convert(id: H256) -> Option<Location> {
IDENTIFIER_TO_LOCATION.with(|b| b.borrow().get(&id).and_then(|l| Option::from(l.clone())))
}
}
Loading
Loading