Skip to content

Commit

Permalink
e2e test: add test for transacting on Parachain as user from Ethereum
Browse files Browse the repository at this point in the history
  • Loading branch information
acatangiu committed Oct 24, 2024
1 parent 55bf5da commit 2113ebb
Show file tree
Hide file tree
Showing 10 changed files with 362 additions and 77 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.

7 changes: 6 additions & 1 deletion bridges/snowbridge/primitives/router/src/inbound/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,10 @@ where
{
fn convert_location(location: &Location) -> Option<AccountId> {
match location.unpack() {
(_, [GlobalConsensus(Ethereum { chain_id })]) =>
(2, [GlobalConsensus(Ethereum { chain_id })]) =>
Some(Self::from_chain_id(chain_id).into()),
(2, [GlobalConsensus(Ethereum { chain_id }), AccountKey20 { network: _, key }]) =>
Some(Self::from_chain_id_with_key(chain_id, key.clone()).into()),
_ => None,
}
}
Expand All @@ -470,4 +472,7 @@ impl<AccountId> GlobalConsensusEthereumConvertsFor<AccountId> {
pub fn from_chain_id(chain_id: &u64) -> [u8; 32] {
(b"ethereum-chain", chain_id).using_encoded(blake2_256)
}
pub fn from_chain_id_with_key(chain_id: &u64, key: [u8; 20]) -> [u8; 32] {
(b"ethereum-chain", chain_id, key).using_encoded(blake2_256)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use crate::{create_pool_with_wnd_on, foreign_balance_on, imports::*};
use frame_support::traits::tokens::fungibles::Mutate;
use xcm::DoubleEncoded;
use xcm_builder::{DescribeAllTerminal, DescribeFamily, HashedDescription};
use xcm_executor::traits::ConvertLocation;

Expand All @@ -25,7 +24,7 @@ fn transfer_and_transact_in_same_xcm(
destination: Location,
usdt: Asset,
beneficiary: Location,
call: DoubleEncoded<()>,
call: xcm::DoubleEncoded<()>,
) {
let signed_origin = <PenpalA as Chain>::RuntimeOrigin::signed(PenpalASender::get().into());
let context = PenpalUniversalLocation::get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ mod imports {
BridgeHubWestendPara as BridgeHubWestend,
BridgeHubWestendParaReceiver as BridgeHubWestendReceiver,
BridgeHubWestendParaSender as BridgeHubWestendSender, PenpalBPara as PenpalB,
PenpalBParaSender as PenpalBSender, WestendRelay as Westend,
WestendRelayReceiver as WestendReceiver, WestendRelaySender as WestendSender,
PenpalBParaReceiver as PenpalBReceiver, PenpalBParaSender as PenpalBSender,
WestendRelay as Westend, WestendRelayReceiver as WestendReceiver,
WestendRelaySender as WestendSender,
};

pub const ASSET_ID: u32 = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::tests::*;

use crate::{create_pool_with_native_on, tests::*};
use xcm::latest::AssetTransferFilter;

fn send_assets_over_bridge<F: FnOnce()>(send_fn: F) {
Expand Down Expand Up @@ -128,7 +129,12 @@ fn send_wnds_usdt_and_weth_from_asset_hub_westend_to_asset_hub_rococo() {
let bridged_wnd_at_asset_hub_rococo = bridged_wnd_at_ah_rococo();

create_foreign_on_ah_rococo(bridged_wnd_at_asset_hub_rococo.clone(), true);
set_up_pool_with_roc_on_ah_rococo(bridged_wnd_at_asset_hub_rococo.clone(), true);
create_pool_with_native_on!(
AssetHubRococo,
bridged_wnd_at_asset_hub_rococo.clone(),
true,
AssetHubRococoSender::get()
);

////////////////////////////////////////////////////////////
// Let's first send over just some WNDs as a simple example
Expand Down Expand Up @@ -206,7 +212,12 @@ fn send_wnds_usdt_and_weth_from_asset_hub_westend_to_asset_hub_rococo() {
);
create_foreign_on_ah_rococo(bridged_weth_at_ah.clone(), true);
create_foreign_on_ah_rococo(bridged_usdt_at_asset_hub_rococo.clone(), true);
set_up_pool_with_roc_on_ah_rococo(bridged_usdt_at_asset_hub_rococo.clone(), true);
create_pool_with_native_on!(
AssetHubRococo,
bridged_usdt_at_asset_hub_rococo.clone(),
true,
AssetHubRococoSender::get()
);

let receiver_usdts_before =
foreign_balance_on_ah_rococo(bridged_usdt_at_asset_hub_rococo.clone(), &receiver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ mod asset_transfers;
mod claim_assets;
mod register_bridged_assets;
mod send_xcm;
mod teleport;

mod snowbridge;
mod teleport;
mod transact;

pub(crate) fn asset_hub_rococo_location() -> Location {
Location::new(2, [GlobalConsensus(Rococo), Parachain(AssetHubRococo::para_id().into())])
Expand Down Expand Up @@ -102,61 +102,70 @@ pub(crate) fn foreign_balance_on_ah_westend(id: v5::Location, who: &AccountId) -
})
}

// set up pool
pub(crate) fn set_up_pool_with_roc_on_ah_rococo(asset: v5::Location, is_foreign: bool) {
let roc: v5::Location = v5::Parent.into();
AssetHubRococo::execute_with(|| {
type RuntimeEvent = <AssetHubRococo as Chain>::RuntimeEvent;
let owner = AssetHubRococoSender::get();
let signed_owner = <AssetHubRococo as Chain>::RuntimeOrigin::signed(owner.clone());
/// note: asset1 needs to be prefunded outside this function
#[macro_export]
macro_rules! create_pool_with_native_on {
( $chain:ident, $asset:expr, $is_foreign:expr, $asset_owner:expr ) => {
emulated_integration_tests_common::impls::paste::paste! {
<$chain>::execute_with(|| {
type RuntimeEvent = <$chain as Chain>::RuntimeEvent;
let owner = $asset_owner;
let signed_owner = <$chain as Chain>::RuntimeOrigin::signed(owner.clone());
let native_asset: Location = Parent.into();

if is_foreign {
assert_ok!(<AssetHubRococo as AssetHubRococoPallet>::ForeignAssets::mint(
signed_owner.clone(),
asset.clone().into(),
owner.clone().into(),
3_000_000_000_000,
));
} else {
let asset_id = match asset.interior.last() {
Some(v5::Junction::GeneralIndex(id)) => *id as u32,
_ => unreachable!(),
};
assert_ok!(<AssetHubRococo as AssetHubRococoPallet>::Assets::mint(
signed_owner.clone(),
asset_id.into(),
owner.clone().into(),
3_000_000_000_000,
));
if $is_foreign {
assert_ok!(<$chain as [<$chain Pallet>]>::ForeignAssets::mint(
signed_owner.clone(),
$asset.clone().into(),
owner.clone().into(),
10_000_000_000_000, // For it to have more than enough.
));
} else {
let asset_id = match $asset.interior.last() {
Some(GeneralIndex(id)) => *id as u32,
_ => unreachable!(),
};
assert_ok!(<$chain as [<$chain Pallet>]>::Assets::mint(
signed_owner.clone(),
asset_id.into(),
owner.clone().into(),
10_000_000_000_000, // For it to have more than enough.
));
}

assert_ok!(<$chain as [<$chain Pallet>]>::AssetConversion::create_pool(
signed_owner.clone(),
Box::new(native_asset.clone()),
Box::new($asset.clone()),
));

assert_expected_events!(
$chain,
vec![
RuntimeEvent::AssetConversion(pallet_asset_conversion::Event::PoolCreated { .. }) => {},
]
);

assert_ok!(<$chain as [<$chain Pallet>]>::AssetConversion::add_liquidity(
signed_owner,
Box::new(native_asset),
Box::new($asset),
1_000_000_000_000,
2_000_000_000_000, // $asset1 is worth half of native_asset
0,
0,
owner.into()
));

assert_expected_events!(
$chain,
vec![
RuntimeEvent::AssetConversion(pallet_asset_conversion::Event::LiquidityAdded { .. }) => {},
]
);
});
}
assert_ok!(<AssetHubRococo as AssetHubRococoPallet>::AssetConversion::create_pool(
signed_owner.clone(),
Box::new(roc.clone()),
Box::new(asset.clone()),
));
assert_expected_events!(
AssetHubRococo,
vec![
RuntimeEvent::AssetConversion(pallet_asset_conversion::Event::PoolCreated { .. }) => {},
]
);
assert_ok!(<AssetHubRococo as AssetHubRococoPallet>::AssetConversion::add_liquidity(
signed_owner.clone(),
Box::new(roc),
Box::new(asset),
1_000_000_000_000,
2_000_000_000_000,
1,
1,
owner.into()
));
assert_expected_events!(
AssetHubRococo,
vec![
RuntimeEvent::AssetConversion(pallet_asset_conversion::Event::LiquidityAdded {..}) => {},
]
);
});
};
}

pub(crate) fn send_assets_from_asset_hub_westend(
Expand Down
Loading

0 comments on commit 2113ebb

Please sign in to comment.