diff --git a/Cargo.lock b/Cargo.lock index c4b9d60e3..097350ce8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7810,6 +7810,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "hex-literal 0.3.4", "hydra-dx-math", "hydradx-traits", "orml-tokens", diff --git a/integration-tests/src/referrals.rs b/integration-tests/src/referrals.rs index 02978d167..fef85eb9b 100644 --- a/integration-tests/src/referrals.rs +++ b/integration-tests/src/referrals.rs @@ -6,6 +6,7 @@ use hydradx_runtime::{Currencies, Omnipool, Referrals, Runtime, RuntimeOrigin, S use orml_traits::MultiCurrency; use pallet_referrals::{FeeDistribution, ReferralCode}; use primitives::AccountId; +use sp_core::crypto::Ss58AddressFormat; use sp_runtime::FixedU128; use sp_runtime::Permill; use xcm_emulator::TestExt; @@ -490,3 +491,36 @@ fn seed_pot_account() { (10 * UNITS) as i128, )); } + +use sp_core::crypto::Ss58Codec; +pub const PARACHAIN_CODES: [(&str, &str); 12] = [ + ("MOONBEAM", "7LCt6dFmtiRrwZv2YyEgQWW3GxsGX3Krmgzv9Xj7GQ9tG2j8"), + ("ASSETHUB", "7LCt6dFqtxzdKVB2648jWW9d85doiFfLSbZJDNAMVJNxh5rJ"), + ("INTERLAY", "7LCt6dFsW7xwUutdYad3oeQ1zfQvZ9THXbBupWLqpd72bmnM"), + ("CENTRIFUGE", "7LCt6dFsJVukxnxpix9KcTkwu2kWQnXARsy6BuBHEL54NcS6"), + ("ASTAR", "7LCt6dFnHxYDyomeCEC8nsnBUEC6omC6y7SZQk4ESzDpiDYo"), + ("BIFROST", "7LCt6dFs6sraSg31uKfbRH7soQ66GRb3LAkGZJ1ie3369crq"), + ("ZEITGEIST", "7LCt6dFCEKr7CctCKBb6CcQdV9iHDue3JcpxkkFCqJZbk3Xk"), + ("PHALA", "7LCt6dFt6z8V3Gg41U4EPCKEHZQAzEFepirNiKqXbWCwHECN"), + ("UNIQUE", "7LCt6dFtWEEr5WXfej1gmZbNUpj1Gx7u29J1yYAen6GsjQTj"), + ("NODLE", "7LCt6dFrJPdrNCKncokgeYZbQsSRgyrYwKrz2sMUGruDF9gJ"), + ("SUBSOCIAL", "7LCt6dFE2vLjshEThqtdwGAGMqg2XA39C1pMSCjG9wsKnR2Q"), + ("POLKADOT", "7KQx4f7yU3hqZHfvDVnSfe6mpgAT8Pxyr67LXHV6nsbZo3Tm"), +]; + +#[test] +fn verify_preregisters_codes() { + Hydra::execute_with(|| { + pallet_referrals::migration::preregister_parachain_codes::(); + for (code, account) in PARACHAIN_CODES.into_iter() { + let code = + ReferralCode::<::CodeLength>::try_from(code.as_bytes().to_vec()) + .unwrap(); + let a = Referrals::referral_account(code); + assert_eq!( + a.unwrap().to_ss58check_with_version(Ss58AddressFormat::custom(63)), + account + ); + } + }); +} diff --git a/pallets/referrals/Cargo.toml b/pallets/referrals/Cargo.toml index 144885508..a1105c7ff 100644 --- a/pallets/referrals/Cargo.toml +++ b/pallets/referrals/Cargo.toml @@ -13,6 +13,7 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] +hex-literal = "0.3.4" # parity scale-info = { version = "2.3.1", default-features = false, features = ["derive"] } codec = { default-features = false, features = ["derive"], package = "parity-scale-codec", version = "3.4.0" } diff --git a/pallets/referrals/src/lib.rs b/pallets/referrals/src/lib.rs index fcc880345..487426d72 100644 --- a/pallets/referrals/src/lib.rs +++ b/pallets/referrals/src/lib.rs @@ -43,6 +43,7 @@ pub mod weights; #[cfg(any(feature = "runtime-benchmarks", test))] mod benchmarking; +pub mod migration; #[cfg(test)] mod tests; pub mod traits; diff --git a/pallets/referrals/src/migration.rs b/pallets/referrals/src/migration.rs new file mode 100644 index 000000000..8bc45fe50 --- /dev/null +++ b/pallets/referrals/src/migration.rs @@ -0,0 +1,90 @@ +// Copyright (C) 2020-2024 Intergalactic, Limited (GIB). +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use super::*; +use frame_support::{traits::Get, weights::Weight}; +use hex_literal::hex; +use sp_core::crypto::AccountId32; + +pub fn preregister_parachain_codes() -> Weight +where + ::AccountId: From, +{ + let mut weight: Weight = Weight::zero(); + + let accounts: [(&str, Option); 12] = [ + ( + "MOONBEAM", + AccountId32::try_from(hex!["7369626cd4070000000000000000000000000000000000000000000000000000"]).ok(), + ), + ( + "ASSETHUB", + AccountId32::try_from(hex!["7369626ce8030000000000000000000000000000000000000000000000000000"]).ok(), + ), + ( + "INTERLAY", + AccountId32::try_from(hex!["7369626cf0070000000000000000000000000000000000000000000000000000"]).ok(), + ), + ( + "CENTRIFUGE", + AccountId32::try_from(hex!["7369626cef070000000000000000000000000000000000000000000000000000"]).ok(), + ), + ( + "ASTAR", + AccountId32::try_from(hex!["7369626cd6070000000000000000000000000000000000000000000000000000"]).ok(), + ), + ( + "BIFROST", + AccountId32::try_from(hex!["7369626cee070000000000000000000000000000000000000000000000000000"]).ok(), + ), + ( + "ZEITGEIST", + AccountId32::try_from(hex!["7369626c2c080000000000000000000000000000000000000000000000000000"]).ok(), + ), + ( + "PHALA", + AccountId32::try_from(hex!["7369626cf3070000000000000000000000000000000000000000000000000000"]).ok(), + ), + ( + "UNIQUE", + AccountId32::try_from(hex!["7369626cf5070000000000000000000000000000000000000000000000000000"]).ok(), + ), + ( + "NODLE", + AccountId32::try_from(hex!["7369626cea070000000000000000000000000000000000000000000000000000"]).ok(), + ), + ( + "SUBSOCIAL", + AccountId32::try_from(hex!["7369626c35080000000000000000000000000000000000000000000000000000"]).ok(), + ), + ( + "POLKADOT", + AccountId32::try_from(hex!["506172656e740000000000000000000000000000000000000000000000000000"]).ok(), + ), + ]; + for (code, maybe_who) in accounts.into_iter() { + let code: ReferralCode = ReferralCode::::truncate_from(code.as_bytes().to_vec()); + if let Some(who) = maybe_who { + let who: T::AccountId = who.into(); + if !ReferralCodes::::contains_key(code.clone()) { + ReferralCodes::::insert(&code, &who); + ReferralAccounts::::insert(&who, code); + Referrer::::insert(&who, (Level::default(), Balance::zero())); + weight.saturating_accrue(T::DbWeight::get().writes(3)); + } + } + } + weight +} diff --git a/runtime/hydradx/src/migrations.rs b/runtime/hydradx/src/migrations.rs index 5e4f6833b..b0a734773 100644 --- a/runtime/hydradx/src/migrations.rs +++ b/runtime/hydradx/src/migrations.rs @@ -2,6 +2,7 @@ use crate::Vec; use frame_support::{codec::alloc::vec, traits::OnRuntimeUpgrade, weights::Weight}; pub struct OnRuntimeUpgradeMigration; +use super::Runtime; impl OnRuntimeUpgrade for OnRuntimeUpgradeMigration { #[cfg(feature = "try-runtime")] @@ -10,7 +11,7 @@ impl OnRuntimeUpgrade for OnRuntimeUpgradeMigration { } fn on_runtime_upgrade() -> Weight { - Weight::zero() + pallet_referrals::migration::preregister_parachain_codes::() } #[cfg(feature = "try-runtime")]