Skip to content

Commit

Permalink
penpal test
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Dec 9, 2024
1 parent 8f11a76 commit 2484821
Show file tree
Hide file tree
Showing 7 changed files with 487 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions bridges/snowbridge/primitives/router/src/inbound/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ where
let mut reserve_assets = vec![];
let mut withdraw_assets = vec![];

let mut refund_surplus_to = origin_account_location;

if let Some(claimer) = message.claimer {
// If the claimer can be decoded, add it to the message. If the claimer decoding fails,
// do not add it to the message, because it will cause the xcm to fail.
Expand Down Expand Up @@ -185,8 +187,6 @@ where
instructions.push(WithdrawAsset(withdraw_assets.into()));
}

let mut refund_surplus_to = origin_account_location;

// If the message origin is not the gateway proxy contract, set the origin to
// the original sender on Ethereum. Important to be before the arbitrary XCM that is
// appended to the message on the next line.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ use snowbridge_pallet_inbound_queue_fixtures::{
};
use snowbridge_pallet_system;
use snowbridge_router_primitives::inbound::{
Command, Destination, EthereumLocationsConverterFor, MessageV1, VersionedMessage,
EthereumLocationsConverterFor
};
use snowbridge_router_primitives::inbound::v1::{
Command, Destination, MessageV1, VersionedMessage,
};
use sp_core::H256;
use sp_runtime::{DispatchError::Token, TokenError::FundsUnavailable};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ rococo-westend-system-emulated-network = { workspace = true }
testnet-parachains-constants = { features = ["rococo", "westend"], workspace = true, default-features = true }
asset-hub-westend-runtime = { workspace = true }
bridge-hub-westend-runtime = { workspace = true }
penpal-emulated-chain = { workspace = true }

# Snowbridge
snowbridge-core = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ 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 Down Expand Up @@ -441,6 +443,121 @@ fn transfer_relay_token() {
});
}

/// Tests sending a token to a 3rd party parachain, called PenPal. The token reserve is
/// still located on AssetHub.
#[test]
fn send_token_from_ethereum_to_penpal() {
let asset_hub_sovereign = BridgeHubWestend::sovereign_account_id_of(Location::new(
1,
[Parachain(AssetHubWestend::para_id().into())],
));
// Fund AssetHub sovereign account so it can pay execution fees for the asset transfer
BridgeHubWestend::fund_accounts(vec![(asset_hub_sovereign.clone(), INITIAL_FUND)]);
// Fund PenPal receiver (covering ED)
PenpalB::fund_accounts(vec![(PenpalBReceiver::get(), INITIAL_FUND)]);

PenpalB::execute_with(|| {
assert_ok!(<PenpalB as Chain>::System::set_storage(
<PenpalB as Chain>::RuntimeOrigin::root(),
vec![(
PenpalCustomizableAssetFromSystemAssetHub::key().to_vec(),
Location::new(2, [GlobalConsensus(Ethereum { chain_id: CHAIN_ID })]).encode(),
)],
));
});

let ethereum_network_v5: NetworkId = EthereumNetwork::get().into();

// The Weth asset location, identified by the contract address on Ethereum
let weth_asset_location: Location =
(Parent, Parent, ethereum_network_v5, AccountKey20 { network: None, key: WETH }).into();

let origin_location = (Parent, Parent, ethereum_network_v5).into();

// Fund ethereum sovereign on AssetHub
let ethereum_sovereign: AccountId =
EthereumLocationsConverterFor::<AccountId>::convert_location(&origin_location).unwrap();
AssetHubWestend::fund_accounts(vec![(ethereum_sovereign.clone(), INITIAL_FUND)]);

// Create asset on the Penpal parachain.
PenpalB::execute_with(|| {
assert_ok!(<PenpalB as PenpalBPallet>::ForeignAssets::force_create(
<PenpalB as Chain>::RuntimeOrigin::root(),
weth_asset_location.clone(),
asset_hub_sovereign.clone().into(),
false,
1000,
));

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

AssetHubWestend::execute_with(|| {
type RuntimeOrigin = <AssetHubWestend as Chain>::RuntimeOrigin;

assert_ok!(<AssetHubWestend as AssetHubWestendPallet>::ForeignAssets::force_create(
RuntimeOrigin::root(),
weth_asset_location.clone().try_into().unwrap(),
asset_hub_sovereign.into(),
false,
1,
));

assert!(<AssetHubWestend as AssetHubWestendPallet>::ForeignAssets::asset_exists(
weth_asset_location.clone().try_into().unwrap(),
));
});

BridgeHubWestend::execute_with(|| {
type RuntimeEvent = <BridgeHubWestend as Chain>::RuntimeEvent;

let message = VersionedMessage::V1(MessageV1 {
chain_id: CHAIN_ID,
command: Command::SendToken {
token: WETH.into(),
destination: Destination::ForeignAccountId32 {
para_id: PARA_ID_B,
id: PenpalBReceiver::get().into(),
fee: 100_000_000_000u128,
},
amount: TOKEN_AMOUNT,
fee: XCM_FEE,
},
});
let (xcm, _) = EthereumInboundQueue::do_convert([0; 32].into(), message).unwrap();
let _ = EthereumInboundQueue::send_xcm(xcm, AssetHubWestend::para_id().into()).unwrap();

// Check that the send token message was sent using xcm
assert_expected_events!(
BridgeHubWestend,
vec![RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::XcmpMessageSent { .. }) =>{},]
);
});

AssetHubWestend::execute_with(|| {
type RuntimeEvent = <AssetHubWestend as Chain>::RuntimeEvent;
// Check that the assets were issued on AssetHub
assert_expected_events!(
AssetHubWestend,
vec![
RuntimeEvent::ForeignAssets(pallet_assets::Event::Issued { .. }) => {},
RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::XcmpMessageSent { .. }) => {},
]
);
});

PenpalB::execute_with(|| {
type RuntimeEvent = <PenpalB as Chain>::RuntimeEvent;
// Check that the assets were issued on PenPal
assert_expected_events!(
PenpalB,
vec![
RuntimeEvent::ForeignAssets(pallet_assets::Event::Issued { .. }) => {},
]
);
});
}

#[test]
fn transfer_ah_token() {
let assethub_sovereign = BridgeHubWestend::sovereign_account_id_of(
Expand Down
Loading

0 comments on commit 2484821

Please sign in to comment.