Skip to content

Commit

Permalink
finish integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Dec 9, 2024
1 parent 373c878 commit 30d0cf4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 33 deletions.
24 changes: 21 additions & 3 deletions bridges/snowbridge/primitives/router/src/inbound/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ pub enum Asset {
/// Reason why a message conversion failed.
#[derive(Copy, Clone, TypeInfo, PalletError, Encode, Decode, RuntimeDebug)]
pub enum ConvertMessageError {
/// Invalid foreign ERC20 token ID
/// Invalid foreign ERC-20 token ID
InvalidAsset,
/// Cannot reachor a foreign ERC-20 asset location.
CannotReanchor,
}

pub trait ConvertMessage {
Expand All @@ -69,19 +71,25 @@ pub struct MessageToXcm<
ConvertAssetId,
WethAddress,
GatewayProxyAddress,
EthereumUniversalLocation,
GlobalAssetHubLocation,
> where
EthereumNetwork: Get<NetworkId>,
InboundQueuePalletInstance: Get<u8>,
ConvertAssetId: MaybeEquivalence<TokenId, Location>,
WethAddress: Get<H160>,
GatewayProxyAddress: Get<H160>,
EthereumUniversalLocation: Get<InteriorLocation>,
GlobalAssetHubLocation: Get<Location>,
{
_phantom: PhantomData<(
EthereumNetwork,
InboundQueuePalletInstance,
ConvertAssetId,
WethAddress,
GatewayProxyAddress,
EthereumUniversalLocation,
GlobalAssetHubLocation,
)>,
}

Expand All @@ -91,20 +99,26 @@ impl<
ConvertAssetId,
WethAddress,
GatewayProxyAddress,
EthereumUniversalLocation,
GlobalAssetHubLocation,
> ConvertMessage
for MessageToXcm<
EthereumNetwork,
InboundQueuePalletInstance,
ConvertAssetId,
WethAddress,
GatewayProxyAddress,
EthereumUniversalLocation,
GlobalAssetHubLocation,
>
where
EthereumNetwork: Get<NetworkId>,
InboundQueuePalletInstance: Get<u8>,
ConvertAssetId: MaybeEquivalence<TokenId, Location>,
WethAddress: Get<H160>,
GatewayProxyAddress: Get<H160>,
EthereumUniversalLocation: Get<InteriorLocation>,
GlobalAssetHubLocation: Get<Location>,
{
fn convert(
message: Message,
Expand Down Expand Up @@ -172,9 +186,13 @@ where
reserve_assets.push(asset);
},
Asset::ForeignTokenERC20 { token_id, value } => {
let asset_id = ConvertAssetId::convert(&token_id)
let asset_loc = ConvertAssetId::convert(&token_id)
.ok_or(ConvertMessageError::InvalidAsset)?;
let asset: XcmAsset = (asset_id, *value).into();
let mut reanchored_asset_loc = asset_loc.clone();
reanchored_asset_loc
.reanchor(&GlobalAssetHubLocation::get(), &EthereumUniversalLocation::get())
.map_err(|_| ConvertMessageError::CannotReanchor)?;
let asset: XcmAsset = (reanchored_asset_loc, *value).into();
withdraw_assets.push(asset);
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use codec::{Decode, Encode};
use emulated_integration_tests_common::RESERVABLE_ASSET_ID;
use frame_support::pallet_prelude::TypeInfo;
use hex_literal::hex;
use penpal_emulated_chain::PARA_ID_B;
use rococo_westend_system_emulated_network::asset_hub_westend_emulated_chain::genesis::AssetHubWestendAssetOwner;
use snowbridge_core::{outbound::OperatingMode, AssetMetadata, TokenIdOf};
use snowbridge_router_primitives::inbound::{
Expand All @@ -28,8 +29,6 @@ use snowbridge_router_primitives::inbound::{
use sp_core::H256;
use testnet_parachains_constants::westend::snowbridge::EthereumNetwork;
use xcm_executor::traits::ConvertLocation;
use penpal_emulated_chain::PARA_ID_B;
use penpal_emulated_chain::penpal_runtime;

const INITIAL_FUND: u128 = 5_000_000_000_000;
pub const CHAIN_ID: u64 = 11155111;
Expand All @@ -38,7 +37,7 @@ const ETHEREUM_DESTINATION_ADDRESS: [u8; 20] = hex!("44a57ee2f2FCcb85FDa2B0B18EB
const XCM_FEE: u128 = 100_000_000_000;
const TOKEN_AMOUNT: u128 = 100_000_000_000;

#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeIfmtnfo)]
pub enum ControlCall {
#[codec(index = 3)]
CreateAgent,
Expand Down Expand Up @@ -289,7 +288,6 @@ fn send_weth_asset_from_asset_hub_to_ethereum() {
});
}


/// Tests sending a token to a 3rd party parachain, called PenPal. The token reserve is
/// still located on AssetHub.
#[test]
Expand Down Expand Up @@ -336,7 +334,9 @@ fn send_token_from_ethereum_to_penpal() {
1000,
));

assert!(<PenpalB as PenpalBPallet>::ForeignAssets::asset_exists(weth_asset_location.clone()));
assert!(<PenpalB as PenpalBPallet>::ForeignAssets::asset_exists(
weth_asset_location.clone()
));
});

AssetHubWestend::execute_with(|| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@ use bridge_hub_westend_runtime::{
EthereumInboundQueueV2,
};
use codec::Encode;
use emulated_integration_tests_common::RESERVABLE_ASSET_ID;
use hex_literal::hex;
use penpal_emulated_chain::PARA_ID_B;
use snowbridge_core::{AssetMetadata, TokenIdOf};
use snowbridge_router_primitives::inbound::{
v2::{Asset::NativeTokenERC20, Message},
v2::{
Asset::{ForeignTokenERC20, NativeTokenERC20},
Message,
},
EthereumLocationsConverterFor,
};
use sp_core::{H160, H256};
use sp_runtime::MultiAddress;
use emulated_integration_tests_common::RESERVABLE_ASSET_ID;
use penpal_emulated_chain::PARA_ID_B;
use snowbridge_core::AssetMetadata;
use xcm_executor::traits::ConvertLocation;
use snowbridge_core::TokenIdOf;
use snowbridge_router_primitives::inbound::v2::Asset::ForeignTokenERC20;
const TOKEN_AMOUNT: u128 = 100_000_000_000;

/// Calculates the XCM prologue fee for sending an XCM to AH.
Expand Down Expand Up @@ -537,14 +538,16 @@ fn send_token_to_penpal_v2() {
// Pay fees on Penpal.
PayFees { asset: weth_fee_penpal },
// Deposit assets to beneficiary.
DepositAsset { assets: Wild(AllOf {
id: AssetId(token_location.clone()),
fun: WildFungibility::Fungible,
}),
beneficiary: beneficiary.clone(), },
DepositAsset {
assets: Wild(AllOf {
id: AssetId(token_location.clone()),
fun: WildFungibility::Fungible,
}),
beneficiary: beneficiary.clone(),
},
SetTopic(H256::random().into()),
]
.into(),
.into(),
},
];
let xcm: Xcm<()> = instructions.into();
Expand Down Expand Up @@ -611,6 +614,8 @@ fn send_foreign_erc20_token_back_to_polkadot() {
let asset_id: Location =
[PalletInstance(ASSETS_PALLET_ID), GeneralIndex(RESERVABLE_ASSET_ID.into())].into();

register_foreign_asset(weth_location());

let asset_id_in_bh: Location = Location::new(
1,
[
Expand All @@ -627,8 +632,8 @@ fn send_foreign_erc20_token_back_to_polkadot() {
Parachain(AssetHubWestend::para_id().into()),
],
)
.appended_with(asset_id.clone().interior)
.unwrap();
.appended_with(asset_id.clone().interior)
.unwrap();

let ethereum_destination = Location::new(2, [GlobalConsensus(Ethereum { chain_id: CHAIN_ID })]);

Expand All @@ -653,36 +658,33 @@ fn send_foreign_erc20_token_back_to_polkadot() {
.into();
AssetHubWestend::fund_accounts(vec![(ethereum_sovereign.clone(), INITIAL_FUND)]);

// Mint the asset into the bridge sovereign account, to mimic locked funds
AssetHubWestend::mint_asset(
<AssetHubWestend as Chain>::RuntimeOrigin::signed(AssetHubWestendAssetOwner::get()),
RESERVABLE_ASSET_ID,
AssetHubWestendSender::get(),
ethereum_sovereign.clone(),
TOKEN_AMOUNT,
);

let token_id = TokenIdOf::convert_location(&asset_id_after_reanchored).unwrap();
let asset: Asset = (asset_id_after_reanchored, TOKEN_AMOUNT).into();

let assets = vec![
// to pay fees
NativeTokenERC20 { token_id: WETH.into(), value: 2_000_000_000_000u128 },
NativeTokenERC20 { token_id: WETH.into(), value: 3_000_000_000_000u128 },
// the token being transferred
ForeignTokenERC20 { token_id: token_id.into(), value: TOKEN_AMOUNT },
];

BridgeHubWestend::execute_with(|| {
type RuntimeEvent = <BridgeHubWestend as Chain>::RuntimeEvent;
let instructions = vec![
WithdrawAsset(asset.clone().into()),
DepositAsset { assets: Wild(AllCounted(2)), beneficiary },
];
let instructions = vec![DepositAsset { assets: Wild(AllCounted(2)), beneficiary }];
let xcm: Xcm<()> = instructions.into();
let versioned_message_xcm = VersionedXcm::V5(xcm);
let origin = EthereumGatewayAddress::get();

let message = Message {
origin,
fee: 1_500_000_000_000u128,
fee: 3_500_000_000_000u128,
assets,
xcm: versioned_message_xcm.encode(),
claimer: Some(claimer_bytes),
Expand Down Expand Up @@ -943,7 +945,6 @@ pub(crate) fn set_up_weth_and_dot_pool(asset: v5::Location) {
});
}


pub(crate) fn set_up_weth_and_dot_pool_on_penpal(asset: v5::Location) {
let wnd: v5::Location = v5::Parent.into();
let penpal_location = BridgeHubWestend::sibling_location_of(PenpalB::para_id());
Expand All @@ -956,8 +957,7 @@ pub(crate) fn set_up_weth_and_dot_pool_on_penpal(asset: v5::Location) {
type RuntimeEvent = <PenpalB as Chain>::RuntimeEvent;

let signed_owner = <PenpalB as Chain>::RuntimeOrigin::signed(owner.clone());
let signed_bh_sovereign =
<PenpalB as Chain>::RuntimeOrigin::signed(bh_sovereign.clone());
let signed_bh_sovereign = <PenpalB as Chain>::RuntimeOrigin::signed(bh_sovereign.clone());

assert_ok!(<PenpalB as PenpalBPallet>::ForeignAssets::mint(
signed_bh_sovereign.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ impl snowbridge_pallet_inbound_queue_v2::Config for Runtime {
EthereumSystem,
WethAddress,
EthereumGatewayAddress,
EthereumUniversalLocation,
AssetHubFromEthereum,
>;
}

Expand Down

0 comments on commit 30d0cf4

Please sign in to comment.