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

Commit

Permalink
Remove reanchored prefix from asset_id
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Aug 26, 2024
1 parent 79fb3b5 commit 8609eed
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 120 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

2 changes: 0 additions & 2 deletions bridges/snowbridge/primitives/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ ethabi = { workspace = true }

[dev-dependencies]
hex = { workspace = true, default-features = true }
xcm-executor = { workspace = true, default-features = true }

[features]
default = ["std"]
Expand Down Expand Up @@ -63,5 +62,4 @@ runtime-benchmarks = [
"polkadot-parachain-primitives/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
"xcm-executor/runtime-benchmarks",
]
25 changes: 5 additions & 20 deletions bridges/snowbridge/primitives/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ use sp_core::H256;
use sp_io::hashing::keccak_256;
use sp_runtime::{traits::AccountIdConversion, RuntimeDebug};
use sp_std::prelude::*;
use xcm::prelude::{
GeneralIndex, GeneralKey, GlobalConsensus, Junction::Parachain, Location, PalletInstance,
};
use xcm::prelude::{GeneralIndex, Junction::Parachain, Location, PalletInstance};
use xcm_builder::{DescribeAllTerminal, DescribeFamily, DescribeLocation, HashedDescription};

/// The ID of an agent contract
Expand Down Expand Up @@ -177,28 +175,15 @@ pub struct AssetRegistrarMetadata {

pub type TokenId = H256;

pub type TokenIdOf = HashedDescription<TokenId, DescribeSiblingAssetId>;
pub type TokenIdOf =
HashedDescription<TokenId, (DescribeSiblingAssetId, DescribeFamily<DescribeAllTerminal>)>;

pub struct DescribeSiblingAssetId;
impl DescribeLocation for DescribeSiblingAssetId {
fn describe_location(l: &Location) -> Option<Vec<u8>> {
match l.unpack() {
(1, [GlobalConsensus(network)]) => Some((*network).encode()),
(1, [GlobalConsensus(network), Parachain(id)]) => Some((*network, *id).encode()),
(1, [GlobalConsensus(network), Parachain(id), PalletInstance(instance)]) =>
Some((*network, *id, *instance).encode()),
(1, [GlobalConsensus(network), Parachain(id), GeneralIndex(index)]) =>
Some((*network, *id, *index).encode()),
(1, [GlobalConsensus(network), Parachain(id), GeneralKey { data, .. }]) =>
Some((*network, *id, *data).encode()),
(
1,
[GlobalConsensus(network), Parachain(id), PalletInstance(instance), GeneralIndex(index)],
) => Some((*network, *id, *instance, *index).encode()),
(
1,
[GlobalConsensus(network), Parachain(id), PalletInstance(instance), GeneralKey { data, .. }],
) => Some((*network, *id, *instance, *data).encode()),
(1, [Parachain(id), PalletInstance(instance), GeneralIndex(index)]) =>
Some((*id, *instance, *index).encode()),
_ => None,
}
}
Expand Down
61 changes: 1 addition & 60 deletions bridges/snowbridge/primitives/core/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use crate::{ChannelId, ParaId, TokenIdOf};
use crate::{ChannelId, ParaId};
use hex_literal::hex;
use xcm::prelude::{
GeneralIndex, GeneralKey, GlobalConsensus, Location, PalletInstance, Parachain, Rococo,
};
use xcm_executor::traits::ConvertLocation;

const EXPECT_CHANNEL_ID: [u8; 32] =
hex!("c173fac324158e77fb5840738a1a541f633cbec8884c6a601c567d2b376a0539");
Expand All @@ -15,58 +11,3 @@ fn generate_channel_id() {
let channel_id: ChannelId = para_id.into();
assert_eq!(channel_id, EXPECT_CHANNEL_ID.into());
}

#[test]
fn test_describe_relay_token() {
let asset_location: Location = Location::new(1, [GlobalConsensus(Rococo)]);
let token_id = TokenIdOf::convert_location(&asset_location).unwrap();
assert_eq!(
token_id,
hex!("fb3d635c7cb573d1b9e9bff4a64ab4f25190d29b6fd8db94c605a218a23fa9ad").into()
);
}

#[test]
fn test_describe_primary_token_from_parachain() {
let asset_location: Location = Location::new(1, [GlobalConsensus(Rococo), Parachain(2000)]);
let token_id = TokenIdOf::convert_location(&asset_location).unwrap();
assert_eq!(
token_id,
hex!("6ee1f706bc329f61dada163071e292d853bcfb6fd66c917f20aa2b975225b482").into()
);
}

#[test]
fn test_describe_token_with_pallet_instance_prefix() {
let asset_location: Location =
Location::new(1, [GlobalConsensus(Rococo), Parachain(2000), PalletInstance(8)]);
let token_id = TokenIdOf::convert_location(&asset_location).unwrap();
assert_eq!(
token_id,
hex!("53e05099ca310413b4102daa37ad8ae6e0b1f3b65f014529df584cf0132529e1").into()
);
}

#[test]
fn test_describe_token_with_general_index_prefix() {
let asset_location: Location =
Location::new(1, [GlobalConsensus(Rococo), Parachain(2000), GeneralIndex(1)]);
let token_id = TokenIdOf::convert_location(&asset_location).unwrap();
assert_eq!(
token_id,
hex!("9f08be45307f36434a2dbdbd6093bb3477cdbecf59cc05eebd6b7ffc9af53acc").into()
);
}

#[test]
fn test_describe_token_with_general_key_prefix() {
let asset_location: Location = Location::new(
1,
[GlobalConsensus(Rococo), Parachain(2000), GeneralKey { length: 32, data: [1; 32] }],
);
let token_id = TokenIdOf::convert_location(&asset_location).unwrap();
assert_eq!(
token_id,
hex!("bafbfc63d136de7d2503ce3ee276e5de4353e90c377b2102fcd98a9b9c5eec22").into()
);
}
8 changes: 7 additions & 1 deletion bridges/snowbridge/primitives/router/src/outbound/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ where
}
}

let (asset_id, amount) = match reserve_asset {
let (reanchored_asset_id, amount) = match reserve_asset {
Asset { id: AssetId(inner_location), fun: Fungible(amount) } =>
Some((inner_location.clone(), *amount)),
_ => None,
Expand All @@ -383,6 +383,12 @@ where
// transfer amount must be greater than 0.
ensure!(amount > 0, ZeroAssetTransfer);

// Remove the GlobalConsensus prefix after reanchored and get the original asset_id
let asset_id = Location::new(
reanchored_asset_id.parents,
reanchored_asset_id.interior.split_first().0,
);

let token_id = TokenIdOf::convert_location(&asset_id).ok_or(InvalidAsset)?;

let versioned_asset_id = ConvertAssetId::convert(&token_id).ok_or(InvalidAsset)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ mod imports {
BridgeHubWestendParaPallet as BridgeHubWestendPallet, BridgeHubWestendXcmConfig,
},
penpal_emulated_chain::{
penpal_runtime::xcm_config::UniversalLocation as PenpalUniversalLocation,
penpal_runtime::xcm_config::{
LocalTeleportableToAssetHub as PenpalLocalTeleportableToAssetHub,
UniversalLocation as PenpalUniversalLocation,
},
PenpalAssetOwner, PenpalBParaPallet as PenpalBPallet,
},
westend_emulated_chain::WestendRelayPallet as WestendPallet,
Expand All @@ -61,8 +64,8 @@ mod imports {
AssetHubWestendParaReceiver as AssetHubWestendReceiver,
AssetHubWestendParaSender as AssetHubWestendSender, BridgeHubRococoPara as BridgeHubRococo,
BridgeHubWestendPara as BridgeHubWestend,
BridgeHubWestendParaSender as BridgeHubWestendSender, PenpalBPara as PenpalB,
PenpalBParaSender as PenpalBSender, WestendRelay as Westend,
BridgeHubWestendParaSender as BridgeHubWestendSender, PenpalAPara as PenpalA,
PenpalBPara as PenpalB, PenpalBParaSender as PenpalBSender, WestendRelay as Westend,
};

pub const ASSET_MIN_BALANCE: u128 = 1000;
Expand Down
Loading

0 comments on commit 8609eed

Please sign in to comment.