Skip to content

Commit

Permalink
Adds transfer token test (paritytech#964)
Browse files Browse the repository at this point in the history
* adds transfer token test

* assert agent id is the source

* complete transfer token test

---------

Co-authored-by: claravanstaden <Cats 4 life!>
Co-authored-by: Alistair Singh <alistair.singh7@gmail.com>
  • Loading branch information
claravanstaden and alistair-singh authored Sep 15, 2023
1 parent 1ffd01e commit 5ee2f71
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 14 deletions.
26 changes: 13 additions & 13 deletions parachain/pallets/inbound-queue/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use snowbridge_core::inbound::{Message, Proof};
use snowbridge_ethereum::Log;

use hex_literal::hex;
use xcm::v3::{SendXcm, MultiAssets, prelude::*};
use xcm::v3::{prelude::*, MultiAssets, SendXcm};

use crate::{self as inbound_queue, envelope::Envelope, Error, Event as InboundQueueEvent};

Expand Down Expand Up @@ -151,19 +151,19 @@ impl SendXcm for MockXcmSender {
type Ticket = ();

fn validate(
dest: &mut Option<MultiLocation>,
_: &mut Option<xcm::v3::Xcm<()>>,
) -> xcm::v3::SendResult<Self::Ticket> {
match dest {
Some(MultiLocation { parents: _, interior }) => {
if let X1(Parachain(1001)) = interior {
return Err(XcmpSendError::NotApplicable);
}
Ok(((), MultiAssets::default()))
dest: &mut Option<MultiLocation>,
_: &mut Option<xcm::v3::Xcm<()>>,
) -> xcm::v3::SendResult<Self::Ticket> {
match dest {
Some(MultiLocation { parents: _, interior }) => {
if let X1(Parachain(1001)) = interior {
return Err(XcmpSendError::NotApplicable)
}
_ => Ok(((), MultiAssets::default()))
}
Ok(((), MultiAssets::default()))
},
_ => Ok(((), MultiAssets::default())),
}
}

fn deliver(_: Self::Ticket) -> core::result::Result<XcmHash, XcmpSendError> {
Ok(H256::zero().into())
Expand Down Expand Up @@ -267,7 +267,7 @@ fn test_submit_happy_path() {
expect_events(vec![InboundQueueEvent::MessageReceived {
dest: dest_para,
nonce: 1,
xcm_hash: H256::zero().into()
xcm_hash: H256::zero().into(),
}
.into()]);
});
Expand Down
5 changes: 5 additions & 0 deletions smoketest/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub const ETHEREUM_API: &str = "ws://localhost:8546";
pub const ETHEREUM_HTTP_API: &str = "http://localhost:8545";
pub const BRIDGE_HUB_WS_URL: &str = "ws://127.0.0.1:11144";
pub const BRIDGE_HUB_PARA_ID: u32 = 1013;
pub const ASSET_HUB_WS_URL: &str = "ws://127.0.0.1:12144";

pub const TEMPLATE_NODE_WS_URL: &str = "ws://127.0.0.1:13144";

Expand All @@ -17,7 +18,11 @@ pub const ETHEREUM_ADDRESS: [u8; 20] = hex!("90A987B944Cb1dCcE5564e5FDeCD7a54D3d

// GatewayProxy in local setup
pub const GATEWAY_PROXY_CONTRACT: [u8; 20] = hex!("EDa338E4dC46038493b885327842fD3E301CaB39");
pub const WETH_CONTRACT: [u8; 20] = hex!("87d1f7fdfEe7f651FaBc8bFCB6E086C278b77A7d");

// Agent for sibling parachain 1001
pub const SIBLING_AGENT_ID: [u8; 32] =
hex!("2075b9f5bc236462eb1473c9a6236c3588e33ed19ead53aa3d9c62ed941cb793");
// Agent for asset hub parachain 1000
pub const ASSET_HUB_AGENT_ID: [u8; 32] =
hex!("72456f48efed08af20e5b317abf8648ac66e86bb90a411d9b0b713f7364b75b4");
16 changes: 15 additions & 1 deletion smoketest/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::time::Duration;
use subxt::blocks::ExtrinsicEvents;
use subxt::events::StaticEvent;
use subxt::tx::{PairSigner, TxPayload};
use subxt::{Config, OnlineClient, PolkadotConfig};
use subxt::{Config, OnlineClient, PolkadotConfig, SubstrateConfig};
use templateXcm::{
v3::{junction::Junction, junctions::Junctions, multilocation::MultiLocation},
VersionedMultiLocation, VersionedXcm,
Expand All @@ -36,6 +36,20 @@ impl Config for TemplateConfig {
type ExtrinsicParams = <PolkadotConfig as Config>::ExtrinsicParams;
}

/// Custom config that works with Statemint
pub enum AssetHubConfig {}

impl Config for AssetHubConfig {
type Index = <PolkadotConfig as Config>::Index;
type Hash = <PolkadotConfig as Config>::Hash;
type AccountId = <PolkadotConfig as Config>::AccountId;
type Address = <PolkadotConfig as Config>::Address;
type Signature = <PolkadotConfig as Config>::Signature;
type Hasher = <PolkadotConfig as Config>::Hasher;
type Header = <PolkadotConfig as Config>::Header;
type ExtrinsicParams = <SubstrateConfig as Config>::ExtrinsicParams;
}

pub struct TestClients {
pub bridge_hub_client: Box<OnlineClient<PolkadotConfig>>,
pub template_client: Box<OnlineClient<TemplateConfig>>,
Expand Down
117 changes: 117 additions & 0 deletions smoketest/tests/transfer_token.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use ethers::{
providers::{Provider, Ws},
types::Address,
};
use std::{sync::Arc, time::Duration};
use ethers::prelude::Middleware;
use snowbridge_smoketest::contracts::{i_gateway::IGateway, weth9::WETH9};
use subxt::{
tx::{PairSigner},
OnlineClient,
};
use snowbridge_smoketest::constants::{ASSET_HUB_WS_URL, ETHEREUM_API, GATEWAY_PROXY_CONTRACT, WETH_CONTRACT, ASSET_HUB_AGENT_ID};
use sp_core::{sr25519::Pair, Pair as PairT};
use snowbridge_smoketest::{
contracts::weth9::{TransferFilter},
parachains::{
assethub::api::runtime_types::xcm::{
v3::{
junction::{Junction, NetworkId},
junctions::Junctions,
multiasset::{AssetId, Fungibility, MultiAsset, MultiAssets},
multilocation::MultiLocation,
},
VersionedMultiAssets, VersionedMultiLocation,
},
assethub::{self},
},
};
use hex_literal::hex;
use assethub::api::bridge_transfer::calls::TransactionApi;
use futures::StreamExt;
use snowbridge_smoketest::helper::AssetHubConfig;

const DESTINATION_ADDRESS: [u8; 20] = hex!("44a57ee2f2FCcb85FDa2B0B18EBD0D8D2333700e");

#[tokio::test]
async fn transfer_token() {
let ethereum_provider = Provider::<Ws>::connect(ETHEREUM_API)
.await
.unwrap()
.interval(Duration::from_millis(10u64));


let ethereum_client = Arc::new(ethereum_provider);

let weth_addr: Address = WETH_CONTRACT.into();
let weth = WETH9::new(weth_addr, ethereum_client.clone());

let gateway = IGateway::new(GATEWAY_PROXY_CONTRACT, ethereum_client.clone());
let agent_src = gateway.agent_of(ASSET_HUB_AGENT_ID)
.await
.expect("could not get agent address");

let assethub: OnlineClient<AssetHubConfig> =
OnlineClient::from_url(ASSET_HUB_WS_URL).await.unwrap();

let keypair: Pair = Pair::from_string("//Ferdie", None).expect("cannot create keypair");

let signer: PairSigner<AssetHubConfig, _> = PairSigner::new(keypair);

let amount: u128 = 1_000_000_000;
let assets = VersionedMultiAssets::V3(MultiAssets(vec![MultiAsset {
id: AssetId::Concrete(MultiLocation {
parents: 2,
interior: Junctions::X3(
Junction::GlobalConsensus(NetworkId::Ethereum { chain_id: 15 }),
Junction::AccountKey20 { network: None, key: GATEWAY_PROXY_CONTRACT.into() },
Junction::AccountKey20 { network: None, key: WETH_CONTRACT.into() },
),
}),
fun: Fungibility::Fungible(amount),
}]));

let destination = VersionedMultiLocation::V3(MultiLocation {
parents: 2,
interior: Junctions::X2(
Junction::GlobalConsensus(NetworkId::Ethereum { chain_id: 15 }),
Junction::AccountKey20 { network: None, key: DESTINATION_ADDRESS.into() },
),
});

let bridge_transfer_call = TransactionApi.transfer_asset_via_bridge(assets, destination);

let result = assethub
.tx()
.sign_and_submit_then_watch_default(&bridge_transfer_call, &signer)
.await
.expect("send through call.")
.wait_for_finalized_success()
.await
.expect("call success");

println!("bridge_transfer call issued at assethub block hash {:?}", result.block_hash());

let wait_for_blocks = 50;
let mut stream = ethereum_client.subscribe_blocks().await.unwrap().take(wait_for_blocks);

let mut transfer_event_found = false;
while let Some(block) = stream.next().await {
println!("Polling ethereum block {:?} for transfer event", block.number.unwrap());
if let Ok(transfers) =
weth.event::<TransferFilter>().at_block_hash(block.hash.unwrap()).query().await
{
for transfer in transfers {
println!("Transfer event found at ethereum block {:?}", block.number.unwrap());
assert_eq!(transfer.src, agent_src.into());
assert_eq!(transfer.dst, DESTINATION_ADDRESS.into());
assert_eq!(transfer.wad, amount.into());
transfer_event_found = true;
}
}
if transfer_event_found {
break;
}
}
assert!(transfer_event_found);
}

0 comments on commit 5ee2f71

Please sign in to comment.