Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Merge branch 'polkadot-native-assets' into polkadot-native-assets-fro…
Browse files Browse the repository at this point in the history
…m-upper-stream
  • Loading branch information
yrong committed Sep 13, 2024
2 parents 1a5b02a + 211b450 commit 6ebc289
Show file tree
Hide file tree
Showing 54 changed files with 2,465 additions and 360 deletions.
19 changes: 15 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions bridges/snowbridge/pallets/inbound-queue/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ parameter_types! {
pub const InboundQueuePalletInstance: u8 = 80;
pub UniversalLocation: InteriorLocation =
[GlobalConsensus(Westend), Parachain(1002)].into();
pub GlobalAssetHub: Location = Location::new(1,[GlobalConsensus(Westend),Parachain(1000)]);
pub AssetHubFromEthereum: Location = Location::new(1,[GlobalConsensus(Westend),Parachain(1000)]);
}

#[cfg(feature = "runtime-benchmarks")]
Expand Down Expand Up @@ -233,7 +233,7 @@ impl inbound_queue::Config for Test {
Balance,
MockTokenIdConvert,
UniversalLocation,
GlobalAssetHub,
AssetHubFromEthereum,
>;
type PricingParameters = Parameters;
type ChannelLookup = MockChannelLookup;
Expand Down
4 changes: 1 addition & 3 deletions bridges/snowbridge/pallets/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,7 @@ pub mod pallet {
let location: Location =
(*location).try_into().map_err(|_| Error::<T>::UnsupportedLocationVersion)?;

let pays_fee = PaysFee::<T>::No;

Self::do_register_token(&location, metadata, pays_fee)?;
Self::do_register_token(&location, metadata, PaysFee::<T>::No)?;

Ok(PostDispatchInfo {
actual_weight: Some(T::WeightInfo::register_token()),
Expand Down
108 changes: 37 additions & 71 deletions bridges/snowbridge/pallets/system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,112 +638,84 @@ fn register_token_with_signed_yields_bad_origin() {
let origin = RuntimeOrigin::signed([14; 32].into());
let location = Location::new(1, [Parachain(2000)]);
let versioned_location: Box<VersionedLocation> = Box::new(location.clone().into());
let asset_metadata = AssetMetadata {
decimals: 10,
name: b"Dot".to_vec().try_into().unwrap(),
symbol: b"DOT".to_vec().try_into().unwrap(),
};

assert_noop!(
EthereumSystem::register_token(origin, versioned_location, asset_metadata),
EthereumSystem::register_token(origin, versioned_location, Default::default()),
BadOrigin
);
});
}

pub struct TokenInfo {
pub location: Location,
pub metadata: AssetMetadata,
pub foreign_token_id: TokenId,
pub struct RegisterTokenTestCase {
/// Input: Location of Polkadot-native token relative to BH
pub native: Location,
/// Output: Reanchored, canonicalized location
pub reanchored: Location,
/// Output: Stable hash of reanchored location
pub foreign: TokenId,
}

#[test]
fn register_all_tokens_succeeds() {
let assets = vec![
let test_cases = vec![
// DOT
TokenInfo {
location: Location::parent(),
metadata: AssetMetadata {
decimals: 10,
name: b"DOT".to_vec().try_into().unwrap(),
symbol: b"DOT".to_vec().try_into().unwrap(),
},
foreign_token_id: hex!(
RegisterTokenTestCase {
native: Location::parent(),
reanchored: Location::new(1, GlobalConsensus(Polkadot)),
foreign: hex!(
"4e241583d94b5d48a27a22064cd49b2ed6f5231d2d950e432f9b7c2e0ade52b2"
)
.into(),
},
// GLMR (Some Polkadot parachain currency)
TokenInfo {
location: Location::new(1, [Parachain(2004)]),
metadata: AssetMetadata {
decimals: 12,
name: b"GLMR".to_vec().try_into().unwrap(),
symbol: b"GLMR".to_vec().try_into().unwrap(),
},
foreign_token_id: hex!(
RegisterTokenTestCase {
native: Location::new(1, [Parachain(2004)]),
reanchored: Location::new(1, [GlobalConsensus(Polkadot), Parachain(2004)]),
foreign: hex!(
"34c08fc90409b6924f0e8eabb7c2aaa0c749e23e31adad9f6d217b577737fafb"
)
.into(),
},
// USDT
TokenInfo {
location: Location::new(1, [Parachain(1000), PalletInstance(50), GeneralIndex(1984)]),
metadata: AssetMetadata {
decimals: 6,
name: b"USDT".to_vec().try_into().unwrap(),
symbol: b"USDT".to_vec().try_into().unwrap(),
},
foreign_token_id: hex!(
RegisterTokenTestCase {
native: Location::new(1, [Parachain(1000), PalletInstance(50), GeneralIndex(1984)]),
reanchored: Location::new(1, [GlobalConsensus(Polkadot), Parachain(1000), PalletInstance(50), GeneralIndex(1984)]),
foreign: hex!(
"14b0579be12d7d7f9971f1d4b41f0e88384b9b74799b0150d4aa6cd01afb4444"
)
.into(),
},
// KSM
TokenInfo {
location: Location::new(2, [GlobalConsensus(Kusama)]),
metadata: AssetMetadata {
decimals: 12,
name: b"KSM".to_vec().try_into().unwrap(),
symbol: b"KSM".to_vec().try_into().unwrap(),
},
foreign_token_id: hex!(
RegisterTokenTestCase {
native: Location::new(2, [GlobalConsensus(Kusama)]),
reanchored: Location::new(1, [GlobalConsensus(Kusama)]),
foreign: hex!(
"03b6054d0c576dd8391e34e1609cf398f68050c23009d19ce93c000922bcd852"
)
.into(),
},
// KAR (Some Kusama parachain currency)
TokenInfo {
location: Location::new(2, [GlobalConsensus(Kusama), Parachain(2000)]),
metadata: AssetMetadata {
decimals: 12,
name: b"KAR".to_vec().try_into().unwrap(),
symbol: b"KAR".to_vec().try_into().unwrap(),
},
foreign_token_id: hex!(
RegisterTokenTestCase {
native: Location::new(2, [GlobalConsensus(Kusama), Parachain(2000)]),
reanchored: Location::new(1, [GlobalConsensus(Kusama), Parachain(2000)]),
foreign: hex!(
"d3e39ad6ea4cee68c9741181e94098823b2ea34a467577d0875c036f0fce5be0"
)
.into(),
},
];
for asset in assets.iter() {
for tc in test_cases.iter() {
new_test_ext(true).execute_with(|| {
let origin = RuntimeOrigin::root();
let versioned_location: Box<VersionedLocation> =
Box::new(asset.location.clone().into());
let asset_metadata = asset.metadata.clone();
let versioned_location: VersionedLocation = tc.native.clone().into();

assert_ok!(EthereumSystem::register_token(origin, versioned_location, asset_metadata));
assert_ok!(EthereumSystem::register_token(origin, Box::new(versioned_location), Default::default()));

let location = asset
.location
.clone()
.reanchored(&EthereumDestination::get(), &UniversalLocation::get())
.unwrap();
assert_eq!(NativeToForeignId::<Test>::get(tc.reanchored.clone()), Some(tc.foreign.clone()));
assert_eq!(ForeignToNativeId::<Test>::get(tc.foreign.clone()), Some(tc.reanchored.clone()));

System::assert_last_event(RuntimeEvent::EthereumSystem(Event::<Test>::RegisterToken {
location: location.into(),
foreign_token_id: asset.foreign_token_id,
location: tc.reanchored.clone().into(),
foreign_token_id: tc.foreign,
}));
});
}
Expand All @@ -764,14 +736,8 @@ fn register_ethereum_native_token_fails() {
],
);
let versioned_location: Box<VersionedLocation> = Box::new(location.clone().into());
let asset_metadata = AssetMetadata {
decimals: 18,
name: b"WETH".to_vec().try_into().unwrap(),
symbol: b"WETH".to_vec().try_into().unwrap(),
};

assert_noop!(
EthereumSystem::register_token(origin, versioned_location, asset_metadata),
EthereumSystem::register_token(origin, versioned_location, Default::default()),
Error::<Test>::LocationConversionFailed
);
});
Expand Down
18 changes: 16 additions & 2 deletions bridges/snowbridge/primitives/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,21 @@ pub const SECONDARY_GOVERNANCE_CHANNEL: ChannelId =
/// Metadata to include in the instantiated ERC20 token contract
#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)]
pub struct AssetMetadata {
pub name: BoundedVec<u8, ConstU32<32>>,
pub symbol: BoundedVec<u8, ConstU32<32>>,
pub name: BoundedVec<u8, ConstU32<METADATA_FIELD_MAX_LEN>>,
pub symbol: BoundedVec<u8, ConstU32<METADATA_FIELD_MAX_LEN>>,
pub decimals: u8,
}

#[cfg(any(test, feature = "std", feature = "runtime-benchmarks"))]
impl Default for AssetMetadata {
fn default() -> Self {
AssetMetadata {
name: BoundedVec::truncate_from(vec![]),
symbol: BoundedVec::truncate_from(vec![]),
decimals: 0,
}
}
}

/// Maximum length of a string field in ERC20 token metada
const METADATA_FIELD_MAX_LEN: u32 = 32;
23 changes: 2 additions & 21 deletions bridges/snowbridge/primitives/core/src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ impl DescribeLocation for DescribeTokenTerminal {
#[cfg(test)]
mod tests {
use crate::TokenIdOf;
use frame_support::assert_ok;
use xcm::prelude::{
GeneralIndex, GeneralKey, GlobalConsensus, InteriorLocation, Junction::*, Location,
NetworkId::*, PalletInstance, Parachain, Reanchorable,
GeneralIndex, GeneralKey, GlobalConsensus, Junction::*, Location, NetworkId::*,
PalletInstance, Parachain,
};
use xcm_executor::traits::ConvertLocation;

Expand Down Expand Up @@ -203,22 +202,4 @@ mod tests {
);
}
}

#[test]
fn test_reanchor_relay_token_from_different_consensus() {
let asset_id: Location = Location::new(2, [GlobalConsensus(Rococo)]);
let ah_context: InteriorLocation = [GlobalConsensus(Westend), Parachain(1000)].into();
let ethereum = Location::new(2, [GlobalConsensus(Ethereum { chain_id: 1 })]);
let mut reanchored_asset = asset_id.clone();
assert_ok!(reanchored_asset.reanchor(&ethereum, &ah_context));
assert_eq!(
reanchored_asset,
Location { parents: 1, interior: [GlobalConsensus(Rococo)].into() }
);
let bh_context: InteriorLocation = [GlobalConsensus(Westend), Parachain(1002)].into();
let ah = Location::new(1, [GlobalConsensus(Westend), Parachain(1000)]);
let mut reanchored_asset = reanchored_asset.clone();
assert_ok!(reanchored_asset.reanchor(&ah, &bh_context));
assert_eq!(reanchored_asset, asset_id);
}
}
5 changes: 0 additions & 5 deletions bridges/snowbridge/primitives/router/src/inbound/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ fn test_reanchor_all_assets() {
let ethereum = Location::new(2, ethereum_context.clone());
let ah_context: InteriorLocation = [GlobalConsensus(Polkadot), Parachain(1000)].into();
let global_ah = Location::new(1, ah_context.clone());
let bh_context: InteriorLocation = [GlobalConsensus(Polkadot), Parachain(1002)].into();
let assets = vec![
// DOT
Location::new(1, []),
Expand All @@ -66,9 +65,5 @@ fn test_reanchor_all_assets() {
let mut reanchored_asset_with_ethereum_context = reanchored_asset.clone();
assert_ok!(reanchored_asset_with_ethereum_context.reanchor(&global_ah, &ethereum_context));
assert_eq!(reanchored_asset_with_ethereum_context, asset.clone());
// reanchor back to original location in context of BH
let mut reanchored_asset_with_bh_context = reanchored_asset.clone();
assert_ok!(reanchored_asset_with_bh_context.reanchor(&global_ah, &bh_context));
assert_eq!(reanchored_asset_with_bh_context, asset.clone());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "coretime-rococo-emulated-chain"
version = "0.0.0"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license = "Apache-2.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "coretime-westend-emulated-chain"
version = "0.0.0"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license = "Apache-2.0"
Expand Down
Loading

0 comments on commit 6ebc289

Please sign in to comment.