Skip to content

Commit

Permalink
Merge pull request #738
Browse files Browse the repository at this point in the history
feat: preregister parachain accounts
  • Loading branch information
mrq1911 authored Jan 5, 2024
2 parents 2248f6f + 72a4bd3 commit d0fb3d9
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 1 deletion.
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.

34 changes: 34 additions & 0 deletions integration-tests/src/referrals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<hydradx_runtime::Runtime>();
for (code, account) in PARACHAIN_CODES.into_iter() {
let code =
ReferralCode::<<Runtime as pallet_referrals::Config>::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
);
}
});
}
1 change: 1 addition & 0 deletions pallets/referrals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions pallets/referrals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
90 changes: 90 additions & 0 deletions pallets/referrals/src/migration.rs
Original file line number Diff line number Diff line change
@@ -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<T: Config>() -> Weight
where
<T as frame_system::Config>::AccountId: From<AccountId32>,
{
let mut weight: Weight = Weight::zero();

let accounts: [(&str, Option<AccountId32>); 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<T::CodeLength> = ReferralCode::<T::CodeLength>::truncate_from(code.as_bytes().to_vec());
if let Some(who) = maybe_who {
let who: T::AccountId = who.into();
if !ReferralCodes::<T>::contains_key(code.clone()) {
ReferralCodes::<T>::insert(&code, &who);
ReferralAccounts::<T>::insert(&who, code);
Referrer::<T>::insert(&who, (Level::default(), Balance::zero()));
weight.saturating_accrue(T::DbWeight::get().writes(3));
}
}
}
weight
}
3 changes: 2 additions & 1 deletion runtime/hydradx/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -10,7 +11,7 @@ impl OnRuntimeUpgrade for OnRuntimeUpgradeMigration {
}

fn on_runtime_upgrade() -> Weight {
Weight::zero()
pallet_referrals::migration::preregister_parachain_codes::<Runtime>()
}

#[cfg(feature = "try-runtime")]
Expand Down

0 comments on commit d0fb3d9

Please sign in to comment.