Skip to content

Commit

Permalink
add foreign erc20 token id lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Nov 18, 2024
1 parent bf500d2 commit cb9111f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
13 changes: 3 additions & 10 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ use snowbridge_core::{
inbound::{Log, Proof, VerificationError},
TokenId,
};
use snowbridge_router_primitives::inbound::{
dry_run::DryRunError,
v2::{Message, MessageToXcm},
};
use snowbridge_router_primitives::inbound::v2::MessageToXcm;
use sp_core::H160;
use sp_runtime::{
traits::{IdentifyAccount, IdentityLookup, MaybeEquivalence, Verify},
Expand Down Expand Up @@ -135,8 +132,6 @@ impl SendXcm for MockXcmSender {
}
}

pub const DOT: u128 = 10_000_000_000;

pub struct MockTokenIdConvert;
impl MaybeEquivalence<TokenId, Location> for MockTokenIdConvert {
fn convert(_id: &TokenId) -> Option<Location> {
Expand All @@ -161,7 +156,8 @@ impl inbound_queue::Config for Test {
type WeightInfo = ();
type GatewayAddress = GatewayAddress;
type AssetHubParaId = ConstU32<1000>;
type MessageConverter = MessageToXcm<EthereumNetwork, InboundQueuePalletInstance>;
type MessageConverter =
MessageToXcm<EthereumNetwork, InboundQueuePalletInstance, MockTokenIdConvert>;
#[cfg(feature = "runtime-benchmarks")]
type Helper = Test;
}
Expand Down Expand Up @@ -266,6 +262,3 @@ pub fn mock_execution_proof() -> ExecutionProof {
execution_branch: vec![],
}
}

pub const ASSET_HUB_PARAID: u32 = 1000u32;
pub const TEMPLATE_PARAID: u32 = 1001u32;
26 changes: 14 additions & 12 deletions bridges/snowbridge/primitives/router/src/inbound/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use codec::{Decode, DecodeLimit, Encode};
use core::marker::PhantomData;
use frame_support::PalletError;
use scale_info::TypeInfo;
use snowbridge_core::TokenId;
use sp_core::{Get, RuntimeDebug, H160, H256};
use sp_runtime::traits::MaybeEquivalence;
use sp_std::prelude::*;
use xcm::{
prelude::{Junction::AccountKey20, *},
Expand Down Expand Up @@ -62,25 +64,29 @@ pub enum ConvertMessageError {
InvalidVersionedXCM,
/// Invalid claimer MultiAddress provided in payload.
InvalidClaimer,
/// Invalid foreign ERC20 token ID
InvalidAsset,
}

pub trait ConvertMessage {
fn convert(message: Message) -> Result<Xcm<()>, ConvertMessageError>;
}

pub struct MessageToXcm<EthereumNetwork, InboundQueuePalletInstance>
pub struct MessageToXcm<EthereumNetwork, InboundQueuePalletInstance, ConvertAssetId>
where
EthereumNetwork: Get<NetworkId>,
InboundQueuePalletInstance: Get<u8>,
ConvertAssetId: MaybeEquivalence<TokenId, Location>,
{
_phantom: PhantomData<(EthereumNetwork, InboundQueuePalletInstance)>,
_phantom: PhantomData<(EthereumNetwork, InboundQueuePalletInstance, ConvertAssetId)>,
}

impl<EthereumNetwork, InboundQueuePalletInstance> ConvertMessage
for MessageToXcm<EthereumNetwork, InboundQueuePalletInstance>
impl<EthereumNetwork, InboundQueuePalletInstance, ConvertAssetId> ConvertMessage
for MessageToXcm<EthereumNetwork, InboundQueuePalletInstance, ConvertAssetId>
where
EthereumNetwork: Get<NetworkId>,
InboundQueuePalletInstance: Get<u8>,
ConvertAssetId: MaybeEquivalence<TokenId, Location>,
{
fn convert(message: Message) -> Result<Xcm<()>, ConvertMessageError> {
let mut message_xcm: Xcm<()> = Xcm::new();
Expand All @@ -105,8 +111,7 @@ where
let network = EthereumNetwork::get();

let fee_asset = Location::new(1, Here);
let fee_value = 1_000_000_000u128; // TODO needs to be dry-run to get the fee but also
// need to add a fee here for the dry run... Chicken/egg problem?
let fee_value = 1_000_000_000u128; // TODO get from command
let fee: Asset = (fee_asset, fee_value).into();
let mut instructions = vec![
ReceiveTeleportedAsset(fee.clone().into()),
Expand All @@ -128,12 +133,9 @@ where
instructions.push(ReserveAssetDeposited((token_location, *value).into()));
},
InboundAsset::ForeignTokenERC20 { token_id, value } => {
// TODO check how token is represented as H256 on AH, assets pallet?
let token_location: Location =
Location::new(0, [AccountId32 { network: None, id: (*token_id).into() }]);
// TODO Is this token always on AH? Would probably need to distinguish between
// tokens on other parachains eventually
instructions.push(WithdrawAsset((token_location, *value).into()));
let asset_id = ConvertAssetId::convert(&token_id)
.ok_or(ConvertMessageError::InvalidAsset)?;
instructions.push(WithdrawAsset((asset_id, *value).into()));
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl snowbridge_pallet_inbound_queue_v2::Config for Runtime {
type Helper = Runtime;
type WeightInfo = crate::weights::snowbridge_pallet_inbound_queue_v2::WeightInfo<Runtime>;
type AssetHubParaId = ConstU32<1000>;
type MessageConverter = snowbridge_router_primitives::inbound::v2::MessageToXcm<EthereumNetwork, ConstU8<INBOUND_QUEUE_PALLET_INDEX>>;
type MessageConverter = snowbridge_router_primitives::inbound::v2::MessageToXcm<EthereumNetwork, ConstU8<INBOUND_QUEUE_PALLET_INDEX>, EthereumSystem>;
}

impl snowbridge_pallet_outbound_queue::Config for Runtime {
Expand Down

0 comments on commit cb9111f

Please sign in to comment.