Skip to content

Commit

Permalink
Transfer relay token to Ethereum
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Mar 26, 2024
1 parent c23f8bb commit 8c8afac
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 7 deletions.
2 changes: 1 addition & 1 deletion smoketest/make-bindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mkdir -p src/contracts

# Generate Rust bindings for contracts
forge bind --module --overwrite \
--select 'IGateway|WETH9|GatewayUpgradeMock' \
--select 'IGateway|WETH9|GatewayUpgradeMock|AgentExecutor' \
--bindings-path src/contracts \
--root ../contracts

Expand Down
1 change: 1 addition & 0 deletions smoketest/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub const ETHEREUM_ADDRESS: [u8; 20] = hex!("90A987B944Cb1dCcE5564e5FDeCD7a54D3d
// the order in contracts are deployed in DeployScript.sol.
pub const GATEWAY_PROXY_CONTRACT: [u8; 20] = hex!("EDa338E4dC46038493b885327842fD3E301CaB39");
pub const WETH_CONTRACT: [u8; 20] = hex!("87d1f7fdfEe7f651FaBc8bFCB6E086C278b77A7d");
pub const AGENT_EXECUTOR_CONTRACT: [u8; 20] = hex!("2ffA5ecdBe006d30397c7636d3e015EEE251369F");

// Agent for bridge hub parachain 1013
pub const BRIDGE_HUB_AGENT_ID: [u8; 32] =
Expand Down
13 changes: 7 additions & 6 deletions smoketest/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::{
api::{
runtime_types,
runtime_types::{
snowbridge_core::outbound::v1::OperatingMode, xcm::VersionedLocation,
snowbridge_core::outbound::v1::OperatingMode,
staging_xcm::v4::junction::NetworkId, xcm::VersionedLocation,
},
},
},
Expand Down Expand Up @@ -172,7 +173,7 @@ pub async fn wait_for_ethereum_event<Ev: EthEvent>(ethereum_client: &Box<Arc<Pro
let gateway_addr: Address = GATEWAY_PROXY_CONTRACT.into();
let gateway = i_gateway::IGateway::new(gateway_addr, (*ethereum_client).deref().clone());

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

let mut ethereum_event_found = false;
Expand Down Expand Up @@ -376,12 +377,12 @@ pub async fn construct_register_relay_token_call(
});
let asset = VersionedLocation::V4(runtime_types::staging_xcm::v4::location::Location {
parents: 1,
interior: Junctions::Here,
interior: Junctions::X1([Junction::GlobalConsensus(NetworkId::Rococo)]),
});
let metadata = runtime_types::snowbridge_core::AssetRegistrarMetadata {
name: "dot".as_bytes().to_vec(),
symbol: "dot".as_bytes().to_vec(),
decimals: 10,
name: "roc".as_bytes().to_vec(),
symbol: "roc".as_bytes().to_vec(),
decimals: 12,
};
let call = bridgehub::api::ethereum_system::calls::TransactionApi
.force_register_token(location, asset, metadata)
Expand Down
109 changes: 109 additions & 0 deletions smoketest/tests/transfer_polkadot_token.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use assethub::api::polkadot_xcm::calls::TransactionApi;
use ethers::{
addressbook::Address,
prelude::Middleware,
providers::{Provider, Ws},
};
use futures::StreamExt;
use hex_literal::hex;
use snowbridge_smoketest::{
constants::*,
contracts::{agent_executor, agent_executor::TokenMintedFilter, i_gateway::IGateway},
helper::AssetHubConfig,
parachains::assethub::{
api::runtime_types::{
staging_xcm::v3::multilocation::MultiLocation,
xcm::{
v3::{
junction::{Junction, NetworkId},
junctions::Junctions,
multiasset::{AssetId, Fungibility, MultiAsset, MultiAssets},
},
VersionedAssets, VersionedLocation,
},
},
{self},
},
};
use std::{sync::Arc, time::Duration};
use subxt::OnlineClient;
use subxt_signer::sr25519::dev;

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

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

let ethereum_client = Arc::new(ethereum_provider);

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 amount: u128 = 1_000_000_000;
let assets = VersionedAssets::V3(MultiAssets(vec![MultiAsset {
id: AssetId::Concrete(MultiLocation { parents: 1, interior: Junctions::Here }),
fun: Fungibility::Fungible(amount),
}]));

let destination = VersionedLocation::V3(MultiLocation {
parents: 2,
interior: Junctions::X1(Junction::GlobalConsensus(NetworkId::Ethereum {
chain_id: ETHEREUM_CHAIN_ID,
})),
});

let beneficiary = VersionedLocation::V3(MultiLocation {
parents: 0,
interior: Junctions::X1(Junction::AccountKey20 {
network: None,
key: DESTINATION_ADDRESS.into(),
}),
});

let signer = dev::bob();

let token_transfer_call =
TransactionApi.reserve_transfer_assets(destination, beneficiary, assets, 0);

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

let agent_executor_addr: Address = AGENT_EXECUTOR_CONTRACT.into();
let agent_executor =
agent_executor::AgentExecutor::new(agent_executor_addr, ethereum_client.clone());

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) = agent_executor
.event::<TokenMintedFilter>()
.at_block_hash(block.hash.unwrap())
.query()
.await
{
for transfer in transfers {
println!("Transfer event found at ethereum block {:?}", block.number.unwrap());
println!("token id {:?}", transfer.token_id);
transfer_event_found = true;
}
}
if transfer_event_found {
break
}
}
assert!(transfer_event_found);
}

0 comments on commit 8c8afac

Please sign in to comment.