Skip to content

Commit

Permalink
add correct location for centrifuge
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Jun 3, 2024
1 parent 3d2de3c commit eabaf12
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions runtime/centrifuge/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const IDENTITY_MIGRATION_KEY_LIMIT: u64 = 1000;
/// The migration set for Centrifuge @ Polkadot.
/// It includes all the migrations that have to be applied on that chain.
pub type UpgradeCentrifuge1029 = (
runtime_common::migrations::restricted_location::MigrateRestrictedTransferLocation<
crate::Runtime,
>,
runtime_common::migrations::increase_storage_version::Migration<OraclePriceFeed, 0, 1>,
runtime_common::migrations::increase_storage_version::Migration<OraclePriceCollection, 0, 1>,
pallet_collator_selection::migration::v1::MigrateToV1<crate::Runtime>,
Expand Down
1 change: 1 addition & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository.workspace = true
documentation.workspace = true

[dependencies]
hex = { workspace = true }
hex-literal = { workspace = true }
log = { workspace = true }
num_enum = { workspace = true }
Expand Down
76 changes: 45 additions & 31 deletions runtime/common/src/migrations/restricted_location.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cfg_primitives::AccountId;
use cfg_types::{locations::RestrictedTransferLocation, tokens::CurrencyId};
use cfg_types::{locations::RestrictedTransferLocation, tokens::FilterCurrency};
use frame_support::{
traits::{Get, OnRuntimeUpgrade},
weights::Weight,
Expand All @@ -12,9 +12,10 @@ use staging_xcm::v4;

mod old {
use cfg_primitives::AccountId;
use cfg_types::{domain_address::DomainAddress, tokens::CurrencyId};
use cfg_types::{domain_address::DomainAddress, tokens::FilterCurrency};
use frame_support::{pallet_prelude::*, storage_alias};
use frame_system::pallet_prelude::*;
use hex::FromHex;
use pallet_transfer_allowlist::AllowanceDetails;
use sp_core::H256;
use staging_xcm::v3;
Expand All @@ -33,28 +34,62 @@ mod old {
pallet_transfer_allowlist::Pallet<T>,
(
NMapKey<Twox64Concat, AccountId>,
NMapKey<Twox64Concat, CurrencyId>,
NMapKey<Twox64Concat, FilterCurrency>,
NMapKey<Blake2_128Concat, RestrictedTransferLocation>,
),
AllowanceDetails<BlockNumberFor<T>>,
OptionQuery,
>;

pub fn create_apps_location_v3(account_id: &AccountId) -> v3::Location {
pub fn location_v3_created_by_apps(_account_id: &AccountId) -> v3::Location {
// Ref: https://github.com/centrifuge/apps/blob/b59bdd34561a4ccd90e0d803c14a3729fc2f3a6d/centrifuge-app/src/utils/usePermissions.tsx#L386
//v3::Location::new(1, v3::Junctions::X2((), ()))
todo!()
// for account_id == "4dTeMxuPJCK7zQGhFcgCivSJqBs9Wo2SuMSQeYCCuVJ9xrE2"
v3::Location::new(
1,
v3::Junctions::X2(
v3::Junction::Parachain(1000), // AssetHub
v3::Junction::AccountId32 {
network: None,
id: <[u8; 32]>::from_hex(
// Address used by Anemoy to withdraw in AssetHub
"10c03288a534d77418e3c19e745dfbc952423e179e1e3baa89e287092fc7802f",
)
.expect("keep in the array"),
},
),
)
}
}

const LOG_PREFIX: &str = "MigrateRestrictedTransferLocation:";

fn migrate_location_key(account_id: &AccountId, hash: H256) -> Option<RestrictedTransferLocation> {
let old_location = old::location_v3_created_by_apps(account_id);
if BlakeTwo256::hash(&old_location.encode()) == hash {
match v4::Location::try_from(old_location) {
Ok(location) => {
log::info!("{LOG_PREFIX} Hash: '{hash}' migrated!");
let new_restricted_location = RestrictedTransferLocation::XCM(location);

Some(new_restricted_location)
}
Err(_) => {
log::error!("{LOG_PREFIX} Non isometric location v3 -> v4");
None
}
}
} else {
log::error!("{LOG_PREFIX} Hash can not be recovered");
None
}
}

pub struct MigrateRestrictedTransferLocation<T>(sp_std::marker::PhantomData<T>);
impl<T> OnRuntimeUpgrade for MigrateRestrictedTransferLocation<T>
where
T: pallet_transfer_allowlist::Config<
AccountId = AccountId,
CurrencyId = CurrencyId,
CurrencyId = FilterCurrency,
Location = RestrictedTransferLocation,
>,
{
Expand Down Expand Up @@ -95,32 +130,11 @@ where

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<sp_std::vec::Vec<u8>, sp_runtime::TryRuntimeError> {
todo!()
Ok(Vec::new())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(pre_state: sp_std::vec::Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
todo!()
}
}

fn migrate_location_key(account_id: &AccountId, hash: H256) -> Option<RestrictedTransferLocation> {
let old_location = old::create_apps_location_v3(account_id);
if BlakeTwo256::hash(&old_location.encode()) == hash {
match v4::Location::try_from(old_location) {
Ok(location) => {
log::info!("{LOG_PREFIX} Hash: '{hash}' migrated!");
let new_restricted_location = RestrictedTransferLocation::XCM(location);

Some(new_restricted_location)
}
Err(_) => {
log::error!("{LOG_PREFIX} Non isometric location v3 -> v4");
None
}
}
} else {
log::error!("{LOG_PREFIX} Hash can not be recovered");
None
fn post_upgrade(_pre_state: sp_std::vec::Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
Ok(())
}
}

0 comments on commit eabaf12

Please sign in to comment.