Skip to content

Commit

Permalink
PNA system pallet cleanups (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeddes authored Sep 2, 2024
1 parent 071ffbe commit 07f8e82
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
45 changes: 27 additions & 18 deletions bridges/snowbridge/pallets/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@
//!
//! Typically, Polkadot governance will use the `force_transfer_native_from_agent` and
//! `force_update_channel` and extrinsics to manage agents and channels for system parachains.
//!
//! ## Polkadot-native tokens on Ethereum
//!
//! Tokens deposited on AssetHub pallet can be bridged to Ethereum as wrapped ERC20 tokens. As a prerequisite,
//! the token should be registered first.
//!
//! * [`Call:register_token`]: Register a token location as a wrapped ERC20 contract on Ethereum.
//!
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(test)]
mod mock;

Expand Down Expand Up @@ -214,7 +221,7 @@ pub mod pallet {
PricingParametersChanged {
params: PricingParametersOf<T>,
},
/// Register token
/// Register Polkadot-native token as a wrapped ERC20 token on Ethereum
RegisterToken {
asset_id: VersionedLocation,
token_id: H256,
Expand Down Expand Up @@ -252,14 +259,16 @@ pub mod pallet {
pub type PricingParameters<T: Config> =
StorageValue<_, PricingParametersOf<T>, ValueQuery, T::DefaultPricingParameters>;

/// Lookup table for foreign to native token ID
#[pallet::storage]
#[pallet::getter(fn tokens)]
pub type Tokens<T: Config> =
pub type ForeignToNativeId<T: Config> =
StorageMap<_, Twox64Concat, TokenId, xcm::v4::Location, OptionQuery>;

/// Lookup table for native to foreign token ID
#[pallet::storage]
#[pallet::getter(fn location_tokens)]
pub type LocationToToken<T: Config> =
pub type NativeToForeignId<T: Config> =
StorageMap<_, Twox64Concat, xcm::v4::Location, TokenId, OptionQuery>;

#[pallet::genesis_config]
Expand Down Expand Up @@ -594,10 +603,9 @@ pub mod pallet {
Ok(())
}

/// Sends a message to the Gateway contract to register a new
/// token that represents `asset`.
/// Registers a Polkadot-native token as a wrapped ERC20 token on Ethereum.
///
/// - `origin`: Must be `MultiLocation` from sibling parachain
/// - `origin`: Must be root
#[pallet::call_index(10)]
#[pallet::weight(T::WeightInfo::register_token())]
pub fn register_token(
Expand All @@ -607,12 +615,12 @@ pub mod pallet {
) -> DispatchResult {
ensure_root(origin)?;

let asset_loc: Location =
let location: Location =
(*location).try_into().map_err(|_| Error::<T>::UnsupportedLocationVersion)?;

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

Self::do_register_token(asset_loc, metadata, pays_fee)?;
Self::do_register_token(location, metadata, pays_fee)?;

Ok(())
}
Expand Down Expand Up @@ -707,15 +715,16 @@ pub mod pallet {
}

pub(crate) fn do_register_token(
asset_loc: Location,
location: Location,
metadata: AssetMetadata,
pays_fee: PaysFee<T>,
) -> Result<(), DispatchError> {
// Record the token id or fail if it has already been created
let token_id = TokenIdOf::convert_location(&asset_loc)
let token_id = TokenIdOf::convert_location(&location)
.ok_or(Error::<T>::LocationConversionFailed)?;
Tokens::<T>::insert(token_id, asset_loc.clone());
LocationToToken::<T>::insert(asset_loc.clone(), token_id);

ForeignToNativeId::<T>::insert(token_id, location.clone());
NativeToForeignId::<T>::insert(location.clone(), token_id);

let command = Command::RegisterForeignToken {
token_id,
Expand All @@ -725,7 +734,7 @@ pub mod pallet {
};
Self::send(SECONDARY_GOVERNANCE_CHANNEL, command, pays_fee)?;

Self::deposit_event(Event::<T>::RegisterToken { asset_id: asset_loc.into(), token_id });
Self::deposit_event(Event::<T>::RegisterToken { asset_id: location.into(), token_id });

Ok(())
}
Expand All @@ -752,11 +761,11 @@ pub mod pallet {
}

impl<T: Config> MaybeEquivalence<TokenId, Location> for Pallet<T> {
fn convert(id: &TokenId) -> Option<Location> {
Tokens::<T>::get(id)
fn convert(foreign_id: &TokenId) -> Option<Location> {
ForeignToNativeId::<T>::get(id)
}
fn convert_back(loc: &Location) -> Option<TokenId> {
LocationToToken::<T>::get(loc)
fn convert_back(location: &Location) -> Option<TokenId> {
NativeToForeignId::<T>::get(location)
}
}
}
2 changes: 1 addition & 1 deletion bridges/snowbridge/primitives/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl DescribeLocation for DescribeHere {
pub type AgentIdOf =
HashedDescription<AgentId, (DescribeHere, DescribeFamily<DescribeAllTerminal>)>;

#[derive(Clone, Default, Eq, Debug, PartialEq, Ord, PartialOrd, Encode, Decode, TypeInfo)]
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct AssetMetadata {
pub name: BoundedVec<u8, ConstU32<32>>,
pub symbol: BoundedVec<u8, ConstU32<32>>,
Expand Down

0 comments on commit 07f8e82

Please sign in to comment.