Skip to content

Commit

Permalink
change fee asset from weth to eth
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Dec 10, 2024
1 parent d0624e2 commit c66fcb1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 222 deletions.
65 changes: 2 additions & 63 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use sp_runtime::{
};
use sp_std::{convert::From, default::Default};
use xcm::{latest::SendXcm, prelude::*};
use xcm_executor::{traits::TransactAsset, AssetsInHolding};
use xcm::opaque::latest::WESTEND_GENESIS_HASH;

type Block = frame_system::mocking::MockBlock<Test>;

Expand Down Expand Up @@ -100,7 +100,6 @@ impl Verifier for MockVerifier {
}

const GATEWAY_ADDRESS: [u8; 20] = hex!["eda338e4dc46038493b885327842fd3e301cab39"];
const WETH_ADDRESS: [u8; 20] = hex!["fff9976782d46cc05630d1f6ebab18b2324d6b14"];

#[cfg(feature = "runtime-benchmarks")]
impl<T: snowbridge_pallet_ethereum_client::Config> BenchmarkHelper<T> for Test {
Expand Down Expand Up @@ -146,7 +145,6 @@ impl MaybeEquivalence<TokenId, Location> for MockTokenIdConvert {
parameter_types! {
pub const EthereumNetwork: xcm::v5::NetworkId = xcm::v5::NetworkId::Ethereum { chain_id: 11155111 };
pub const GatewayAddress: H160 = H160(GATEWAY_ADDRESS);
pub const WethAddress: H160 = H160(WETH_ADDRESS);
pub const InboundQueuePalletInstance: u8 = 84;
pub AssetHubLocation: InteriorLocation = Parachain(1000).into();
pub UniversalLocation: InteriorLocation =
Expand All @@ -166,7 +164,7 @@ impl inbound_queue_v2::Config for Test {
EthereumNetwork,
InboundQueuePalletInstance,
MockTokenIdConvert,
WethAddress,
GatewayAddress,
UniversalLocation,
AssetHubFromEthereum,
>;
Expand All @@ -176,52 +174,6 @@ impl inbound_queue_v2::Config for Test {
type Helper = Test;
}

pub struct SuccessfulTransactor;
impl TransactAsset for SuccessfulTransactor {
fn can_check_in(_origin: &Location, _what: &Asset, _context: &XcmContext) -> XcmResult {
Ok(())
}

fn can_check_out(_dest: &Location, _what: &Asset, _context: &XcmContext) -> XcmResult {
Ok(())
}

fn deposit_asset(_what: &Asset, _who: &Location, _context: Option<&XcmContext>) -> XcmResult {
Ok(())
}

fn withdraw_asset(
_what: &Asset,
_who: &Location,
_context: Option<&XcmContext>,
) -> Result<AssetsInHolding, XcmError> {
Ok(AssetsInHolding::default())
}

fn internal_transfer_asset(
_what: &Asset,
_from: &Location,
_to: &Location,
_context: &XcmContext,
) -> Result<AssetsInHolding, XcmError> {
Ok(AssetsInHolding::default())
}
}

pub fn last_events(n: usize) -> Vec<RuntimeEvent> {
frame_system::Pallet::<Test>::events()
.into_iter()
.rev()
.take(n)
.rev()
.map(|e| e.event)
.collect()
}

pub fn expect_events(e: Vec<RuntimeEvent>) {
assert_eq!(last_events(e.len()), e);
}

pub fn setup() {
System::set_block_number(1);
}
Expand Down Expand Up @@ -253,19 +205,6 @@ pub fn mock_event_log() -> Log {
}
}

pub fn mock_event_log_invalid_channel() -> Log {
Log {
address: hex!("eda338e4dc46038493b885327842fd3e301cab39").into(),
topics: vec![
hex!("7153f9357c8ea496bba60bf82e67143e27b64462b49041f8e689e1b05728f84f").into(),
// invalid channel id
hex!("0000000000000000000000000000000000000000000000000000000000000000").into(),
hex!("5f7060e971b0dc81e63f0aa41831091847d97c1a4693ac450cc128c7214e65e0").into(),
],
data: hex!("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001e000f000000000000000087d1f7fdfee7f651fabc8bfcb6e086c278b77a7d0000").into(),
}
}

pub fn mock_event_log_invalid_gateway() -> Log {
Log {
// gateway address
Expand Down
63 changes: 1 addition & 62 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,11 @@
use super::*;

use frame_support::{assert_noop, assert_ok};
use hex_literal::hex;
use snowbridge_core::inbound::Proof;
use sp_keyring::AccountKeyring as Keyring;
use sp_runtime::DispatchError;

use crate::{mock::*, Error, Event as InboundQueueEvent};
use codec::DecodeLimit;
use snowbridge_router_primitives::inbound::v2::Asset;
use sp_core::H256;
use xcm::{
opaque::latest::prelude::{ClearOrigin, ReceiveTeleportedAsset},
prelude::*,
VersionedXcm, MAX_XCM_DECODE_DEPTH,
};

#[test]
fn test_submit_happy_path() {
new_tester().execute_with(|| {
let relayer: AccountId = Keyring::Bob.into();

let origin = RuntimeOrigin::signed(relayer.clone());

// Submit message
let message = Message {
event_log: mock_event_log(),
proof: Proof {
receipt_proof: Default::default(),
execution_proof: mock_execution_proof(),
},
};

assert_ok!(InboundQueue::submit(origin.clone(), message.clone()));
expect_events(vec![InboundQueueEvent::MessageReceived {
nonce: 1,
message_id: [
183, 243, 1, 130, 170, 254, 104, 45, 116, 181, 146, 237, 14, 139, 138, 89, 43, 166,
182, 24, 163, 222, 112, 238, 215, 83, 21, 160, 24, 88, 112, 9,
],
}
.into()]);
});
}
use crate::{mock::*, Error};

#[test]
fn test_submit_with_invalid_gateway() {
Expand All @@ -67,30 +30,6 @@ fn test_submit_with_invalid_gateway() {
});
}

#[test]
fn test_submit_with_invalid_nonce() {
new_tester().execute_with(|| {
let relayer: AccountId = Keyring::Bob.into();
let origin = RuntimeOrigin::signed(relayer);

// Submit message
let message = Message {
event_log: mock_event_log(),
proof: Proof {
receipt_proof: Default::default(),
execution_proof: mock_execution_proof(),
},
};
assert_ok!(InboundQueue::submit(origin.clone(), message.clone()));

// Submit the same again
assert_noop!(
InboundQueue::submit(origin.clone(), message.clone()),
Error::<Test>::InvalidNonce
);
});
}

#[test]
fn test_set_operating_mode() {
new_tester().execute_with(|| {
Expand Down
51 changes: 7 additions & 44 deletions bridges/snowbridge/primitives/router/src/inbound/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,13 @@ pub struct MessageToXcm<
EthereumNetwork,
InboundQueuePalletInstance,
ConvertAssetId,
WethAddress,
GatewayProxyAddress,
EthereumUniversalLocation,
GlobalAssetHubLocation,
> where
EthereumNetwork: Get<NetworkId>,
InboundQueuePalletInstance: Get<u8>,
ConvertAssetId: MaybeEquivalence<TokenId, Location>,
WethAddress: Get<H160>,
GatewayProxyAddress: Get<H160>,
EthereumUniversalLocation: Get<InteriorLocation>,
GlobalAssetHubLocation: Get<Location>,
Expand All @@ -93,7 +91,6 @@ pub struct MessageToXcm<
EthereumNetwork,
InboundQueuePalletInstance,
ConvertAssetId,
WethAddress,
GatewayProxyAddress,
EthereumUniversalLocation,
GlobalAssetHubLocation,
Expand All @@ -104,7 +101,6 @@ impl<
EthereumNetwork,
InboundQueuePalletInstance,
ConvertAssetId,
WethAddress,
GatewayProxyAddress,
EthereumUniversalLocation,
GlobalAssetHubLocation,
Expand All @@ -113,7 +109,6 @@ impl<
EthereumNetwork,
InboundQueuePalletInstance,
ConvertAssetId,
WethAddress,
GatewayProxyAddress,
EthereumUniversalLocation,
GlobalAssetHubLocation,
Expand All @@ -122,7 +117,6 @@ where
EthereumNetwork: Get<NetworkId>,
InboundQueuePalletInstance: Get<u8>,
ConvertAssetId: MaybeEquivalence<TokenId, Location>,
WethAddress: Get<H160>,
GatewayProxyAddress: Get<H160>,
EthereumUniversalLocation: Get<InteriorLocation>,
GlobalAssetHubLocation: Get<Location>,
Expand All @@ -149,19 +143,14 @@ where

let network = EthereumNetwork::get();

// use weth as asset
let fee_asset = Location::new(
2,
[
GlobalConsensus(EthereumNetwork::get()),
AccountKey20 { network: None, key: WethAddress::get().into() },
],
);
// use eth as asset
let fee_asset = Location::new(2, [GlobalConsensus(EthereumNetwork::get())]);
let fee: XcmAsset = (fee_asset.clone(), message.execution_fee).into();
let eth: XcmAsset = (fee_asset.clone(), message.execution_fee.saturating_add(message.value)).into();
let mut instructions = vec![
DescendOrigin(PalletInstance(InboundQueuePalletInstance::get()).into()),
UniversalOrigin(GlobalConsensus(network)),
ReserveAssetDeposited(fee.clone().into()),
ReserveAssetDeposited(eth.into()),
PayFees { asset: fee },
];
let mut reserve_assets = vec![];
Expand Down Expand Up @@ -253,12 +242,9 @@ mod tests {
use sp_runtime::traits::MaybeEquivalence;
use xcm::{opaque::latest::WESTEND_GENESIS_HASH, prelude::*};
const GATEWAY_ADDRESS: [u8; 20] = hex!["eda338e4dc46038493b885327842fd3e301cab39"];
const WETH_ADDRESS: [u8; 20] = hex!["fff9976782d46cc05630d1f6ebab18b2324d6b14"];

parameter_types! {
pub const EthereumNetwork: xcm::v5::NetworkId = xcm::v5::NetworkId::Ethereum { chain_id: 11155111 };
pub const GatewayAddress: H160 = H160(GATEWAY_ADDRESS);
pub const WethAddress: H160 = H160(WETH_ADDRESS);
pub const InboundQueuePalletInstance: u8 = 84;
pub AssetHubLocation: InteriorLocation = Parachain(1000).into();
pub UniversalLocation: InteriorLocation =
Expand Down Expand Up @@ -329,7 +315,6 @@ mod tests {
EthereumNetwork,
InboundQueuePalletInstance,
MockTokenIdConvert,
WethAddress,
GatewayAddress,
UniversalLocation,
AssetHubFromEthereum,
Expand Down Expand Up @@ -361,27 +346,15 @@ mod tests {
}
}
if let PayFees { ref asset } = instruction {
let fee_asset = Location::new(
2,
[
GlobalConsensus(EthereumNetwork::get()),
AccountKey20 { network: None, key: WethAddress::get().into() },
],
);
let fee_asset = Location::new(2, [GlobalConsensus(EthereumNetwork::get())]);
assert_eq!(asset.id, AssetId(fee_asset));
assert_eq!(asset.fun, Fungible(execution_fee));
pay_fees_found = true;
}
if let ReserveAssetDeposited(ref reserve_assets) = instruction {
reserve_deposited_found = reserve_deposited_found + 1;
if reserve_deposited_found == 1 {
let fee_asset = Location::new(
2,
[
GlobalConsensus(EthereumNetwork::get()),
AccountKey20 { network: None, key: WethAddress::get().into() },
],
);
let fee_asset = Location::new(2, [GlobalConsensus(EthereumNetwork::get())]);
let fee: XcmAsset = (fee_asset, execution_fee).into();
let fee_assets: Assets = fee.into();
assert_eq!(fee_assets, reserve_assets.clone());
Expand Down Expand Up @@ -464,7 +437,6 @@ mod tests {
EthereumNetwork,
InboundQueuePalletInstance,
MockTokenIdConvert,
WethAddress,
GatewayAddress,
UniversalLocation,
AssetHubFromEthereum,
Expand Down Expand Up @@ -524,7 +496,6 @@ mod tests {
EthereumNetwork,
InboundQueuePalletInstance,
MockFailedTokenConvert,
WethAddress,
GatewayAddress,
UniversalLocation,
AssetHubFromEthereum,
Expand Down Expand Up @@ -573,7 +544,6 @@ mod tests {
EthereumNetwork,
InboundQueuePalletInstance,
MockTokenIdConvert,
WethAddress,
GatewayAddress,
UniversalLocation,
AssetHubFromEthereum,
Expand Down Expand Up @@ -609,13 +579,7 @@ mod tests {
assert!(last.is_some());
assert!(second_last.is_some());

let fee_asset = Location::new(
2,
[
GlobalConsensus(EthereumNetwork::get()),
AccountKey20 { network: None, key: WethAddress::get().into() },
],
);
let fee_asset = Location::new(2, [GlobalConsensus(EthereumNetwork::get())]);
assert_eq!(
last,
Some(DepositAsset {
Expand Down Expand Up @@ -657,7 +621,6 @@ mod tests {
EthereumNetwork,
InboundQueuePalletInstance,
MockTokenIdConvert,
WethAddress,
GatewayAddress,
UniversalLocation,
AssetHubFromEthereum,
Expand Down
Loading

0 comments on commit c66fcb1

Please sign in to comment.