Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Feb 2, 2025
1 parent 24230d7 commit 66906d5
Show file tree
Hide file tree
Showing 3 changed files with 282 additions and 291 deletions.
2 changes: 1 addition & 1 deletion smoketest/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub const DEFAULT_GATEWAY_PROXY_CONTRACT: [u8; 20] =
pub const DEFAULT_WETH_CONTRACT: [u8; 20] = hex!("b8ea8cb425d85536b158d661da1ef0895bb92f1d");
pub const AGENT_EXECUTOR_CONTRACT: [u8; 20] = hex!("Fc97A6197dc90bef6bbEFD672742Ed75E9768553");

pub const ERC20_DOT_CONTRACT: [u8; 20] = hex!("B8C39CbCe8106c8415472e3AAe88Eb694Cc70B57");
pub const ERC20_DOT_CONTRACT: [u8; 20] = hex!("De45448Ca2d57797c0BEC0ee15A1E42334744219");
pub const ERC20_DOT_TOKEN_ID: [u8; 32] =
hex!("fb3d635c7cb573d1b9e9bff4a64ab4f25190d29b6fd8db94c605a218a23fa9ad");

Expand Down
291 changes: 142 additions & 149 deletions smoketest/tests/v2/outbound/transfer_ena.rs
Original file line number Diff line number Diff line change
@@ -1,158 +1,151 @@
mod transfer_ena {
use assethub::api::polkadot_xcm::calls::TransactionApi;
use ethers::{
prelude::Middleware,
providers::{Provider, Ws},
types::Address,
};
use futures::StreamExt;
use snowbridge_smoketest::{
constants::*,
contracts::{
i_gateway_v2::IGatewayV2,
weth9::{TransferFilter, WETH9},
},
helper::AssetHubConfig,
parachains::assethub::{
api::runtime_types::{
sp_weights::weight_v2::Weight,
staging_xcm::v5::{
asset::{
Asset,
AssetFilter::{Definite, Wild},
AssetId, AssetTransferFilter, Assets,
Fungibility::Fungible,
WildAsset::AllCounted,
},
junction::{Junction, NetworkId},
junctions::Junctions,
location::Location,
Instruction::{DepositAsset, InitiateTransfer, PayFees, WithdrawAsset},
Xcm,
use assethub::api::polkadot_xcm::calls::TransactionApi;
use ethers::{
prelude::Middleware,
providers::{Provider, Ws},
types::Address,
};
use futures::StreamExt;
use snowbridge_smoketest::{
constants::*,
contracts::{
i_gateway_v2::IGatewayV2,
weth9::{TransferFilter, WETH9},
},
helper::AssetHubConfig,
parachains::assethub::{
api::runtime_types::{
sp_weights::weight_v2::Weight,
staging_xcm::v5::{
asset::{
Asset,
AssetFilter::{Definite, Wild},
AssetId, AssetTransferFilter, Assets,
Fungibility::Fungible,
WildAsset::AllCounted,
},
xcm::VersionedXcm,
junction::{Junction, NetworkId},
junctions::Junctions,
location::Location,
Instruction::{DepositAsset, InitiateTransfer, PayFees, WithdrawAsset},
Xcm,
},
{self},
xcm::VersionedXcm,
},
{self},
},
};
use std::{str::FromStr, sync::Arc, time::Duration};
use subxt::OnlineClient;
use subxt_signer::{sr25519, SecretUri};

#[tokio::test]
async fn transfer_ena() {
let ethereum_provider = Provider::<Ws>::connect((*ETHEREUM_API).to_string())
.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_addr: Address = (*GATEWAY_PROXY_CONTRACT).into();
let gateway = IGatewayV2::new(gateway_addr, ethereum_client.clone());

let agent_src =
gateway.agent_of(ASSET_HUB_AGENT_ID).await.expect("could not get agent address");
println!("agent_src: {:?}", agent_src);

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

let destination = Location {
parents: 2,
interior: Junctions::X1([Junction::GlobalConsensus(NetworkId::Ethereum {
chain_id: ETHEREUM_CHAIN_ID,
})]),
};
use std::{str::FromStr, sync::Arc, time::Duration};
use subxt::OnlineClient;
use subxt_signer::{sr25519, SecretUri};

#[tokio::test]
async fn transfer_ena() {
let ethereum_provider = Provider::<Ws>::connect((*ETHEREUM_API).to_string())
.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_addr: Address = (*GATEWAY_PROXY_CONTRACT).into();
let gateway = IGatewayV2::new(gateway_addr, ethereum_client.clone());

let agent_src =
gateway.agent_of(ASSET_HUB_AGENT_ID).await.expect("could not get agent address");
println!("agent_src: {:?}", agent_src);

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

let destination = Location {
parents: 2,
interior: Junctions::X1([Junction::GlobalConsensus(NetworkId::Ethereum {
chain_id: ETHEREUM_CHAIN_ID,
})]),
};

let beneficiary = Location {
parents: 0,
interior: Junctions::X1([Junction::AccountKey20 {
network: None,
key: (*ETHEREUM_RECEIVER).into(),
}]),
};

let local_fee_amount: u128 = 800_000_000_000;
let local_fee_asset = Asset {
id: AssetId(Location { parents: 1, interior: Junctions::Here }),
fun: Fungible(local_fee_amount),
};
let amount: u128 = 1_000_000_000;
let asset_location = Location {
parents: 2,
interior: Junctions::X2([
Junction::GlobalConsensus(NetworkId::Ethereum { chain_id: ETHEREUM_CHAIN_ID }),
Junction::AccountKey20 { network: None, key: (*WETH_CONTRACT).into() },
]),
};
let remote_fee_asset =
Asset { id: AssetId(asset_location.clone()), fun: Fungible(amount / 2) };
let reserved_asset =
Asset { id: AssetId(asset_location.clone()), fun: Fungible(amount / 2) };

let assets = vec![
local_fee_asset.clone(),
Asset { id: AssetId(asset_location.clone()), fun: Fungible(amount) },
];

let xcm = VersionedXcm::V5(Xcm(vec![
WithdrawAsset(Assets(assets.into())),
PayFees { asset: local_fee_asset.clone() },
InitiateTransfer {
destination,
remote_fees: Some(AssetTransferFilter::ReserveWithdraw(Definite(Assets(
vec![remote_fee_asset.clone()].into(),
)))),
preserve_origin: true,
assets: vec![AssetTransferFilter::ReserveWithdraw(Definite(Assets(vec![
reserved_asset.clone(),
])))],
remote_xcm: Xcm(vec![DepositAsset { assets: Wild(AllCounted(2)), beneficiary }]),
},
]));

let suri = SecretUri::from_str(&SUBSTRATE_KEY).expect("Parse SURI");

let signer = sr25519::Keypair::from_uri(&suri).expect("valid keypair");

let token_transfer_call =
TransactionApi.execute(xcm, Weight { ref_time: 8_000_000_000, proof_size: 80_000 });

let _ = assethub
.tx()
.sign_and_submit_then_watch_default(&token_transfer_call, &signer)
.await
.expect("call success");

let wait_for_blocks = 500;
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 {
if transfer.src.eq(&agent_src) {
println!(
"Transfer event found at ethereum block {:?}",
block.number.unwrap()
);
assert_eq!(transfer.src, agent_src.into());
assert_eq!(transfer.dst, (*ETHEREUM_RECEIVER).into());
transfer_event_found = true;
}

let beneficiary = Location {
parents: 0,
interior: Junctions::X1([Junction::AccountKey20 {
network: None,
key: (*ETHEREUM_RECEIVER).into(),
}]),
};

let local_fee_amount: u128 = 800_000_000_000;
let local_fee_asset = Asset {
id: AssetId(Location { parents: 1, interior: Junctions::Here }),
fun: Fungible(local_fee_amount),
};
let amount: u128 = 1_000_000_000;
let asset_location = Location {
parents: 2,
interior: Junctions::X2([
Junction::GlobalConsensus(NetworkId::Ethereum { chain_id: ETHEREUM_CHAIN_ID }),
Junction::AccountKey20 { network: None, key: (*WETH_CONTRACT).into() },
]),
};
let remote_fee_asset = Asset { id: AssetId(asset_location.clone()), fun: Fungible(amount / 2) };
let reserved_asset = Asset { id: AssetId(asset_location.clone()), fun: Fungible(amount / 2) };

let assets = vec![
local_fee_asset.clone(),
Asset { id: AssetId(asset_location.clone()), fun: Fungible(amount) },
];

let xcm = VersionedXcm::V5(Xcm(vec![
WithdrawAsset(Assets(assets.into())),
PayFees { asset: local_fee_asset.clone() },
InitiateTransfer {
destination,
remote_fees: Some(AssetTransferFilter::ReserveWithdraw(Definite(Assets(
vec![remote_fee_asset.clone()].into(),
)))),
preserve_origin: true,
assets: vec![AssetTransferFilter::ReserveWithdraw(Definite(Assets(vec![
reserved_asset.clone(),
])))],
remote_xcm: Xcm(vec![DepositAsset { assets: Wild(AllCounted(2)), beneficiary }]),
},
]));

let suri = SecretUri::from_str(&SUBSTRATE_KEY).expect("Parse SURI");

let signer = sr25519::Keypair::from_uri(&suri).expect("valid keypair");

let token_transfer_call =
TransactionApi.execute(xcm, Weight { ref_time: 8_000_000_000, proof_size: 80_000 });

let _ = assethub
.tx()
.sign_and_submit_then_watch_default(&token_transfer_call, &signer)
.await
.expect("call success");

let wait_for_blocks = 500;
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 {
if transfer.src.eq(&agent_src) {
println!("Transfer event found at ethereum block {:?}", block.number.unwrap());
assert_eq!(transfer.src, agent_src.into());
assert_eq!(transfer.dst, (*ETHEREUM_RECEIVER).into());
transfer_event_found = true;
}
}
if transfer_event_found {
break
}
}
assert!(transfer_event_found);
if transfer_event_found {
break
}
}
assert!(transfer_event_found);
}
Loading

0 comments on commit 66906d5

Please sign in to comment.