Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bifrost Polkadot insert BNC metadata #1082

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 89 additions & 3 deletions pallets/asset-registry/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use super::{AssetMetadata, Config, CurrencyMetadatas, Weight};
use bifrost_primitives::CurrencyId;
use frame_support::traits::Get;
use crate::*;
use bifrost_primitives::{CurrencyId, BNC};
use frame_support::traits::{Get, OnRuntimeUpgrade};
#[cfg(feature = "try-runtime")]
use sp_runtime::TryRuntimeError;
use xcm::prelude::{GeneralKey, X1};

const LOG_TARGET: &str = "asset-registry::migration";

pub fn update_blp_metadata<T: Config>(pool_count: u32) -> Weight {
for pool_id in 0..pool_count {
Expand All @@ -36,3 +41,84 @@ pub fn update_blp_metadata<T: Config>(pool_count: u32) -> Weight {

T::DbWeight::get().reads(pool_count.into()) + T::DbWeight::get().writes(pool_count.into())
}

pub struct InsertBNCMetadata<T>(PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for InsertBNCMetadata<T> {
fn on_runtime_upgrade() -> Weight {
log::info!(target: LOG_TARGET, "Start to insert BNC Metadata...");
CurrencyMetadatas::<T>::insert(
BNC,
&AssetMetadata {
name: b"Bifrost Native Token".to_vec(),
symbol: b"BNC".to_vec(),
decimals: 12,
minimal_balance: BalanceOf::<T>::unique_saturated_from(10_000_000_000u128),
},
);

CurrencyIdToLocations::<T>::insert(
BNC,
MultiLocation {
parents: 0,
interior: X1(GeneralKey {
length: 2,
data: [
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
],
}),
},
);
Weight::from(T::DbWeight::get().reads_writes(2 as u64 + 1, 2 as u64 + 1))
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
assert!(CurrencyMetadatas::<T>::get(BNC).is_none());
assert!(CurrencyIdToLocations::<T>::get(BNC).is_none());

Ok(sp_std::vec![])
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_cnt: Vec<u8>) -> Result<(), TryRuntimeError> {
let metadata = CurrencyMetadatas::<T>::get(BNC);
assert_eq!(
metadata,
Some(AssetMetadata {
name: b"Bifrost Native Token".to_vec(),
symbol: b"BNC".to_vec(),
decimals: 12,
minimal_balance: BalanceOf::<T>::unique_saturated_from(10_000_000_000u128),
})
);
log::info!(
target: LOG_TARGET,
"InsertBNCMetadata post-migrate storage: {:?}",
metadata
);

let location = CurrencyIdToLocations::<T>::get(BNC);
assert_eq!(
location,
Some(MultiLocation {
parents: 0,
interior: X1(GeneralKey {
length: 2,
data: [
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0
]
}),
})
);

log::info!(
target: LOG_TARGET,
"InsertBNCMetadata post-migrate storage: {:?}",
location
);

Ok(())
}
}
6 changes: 2 additions & 4 deletions runtime/bifrost-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1702,11 +1702,9 @@ pub type Migrations = migrations::Unreleased;

/// The runtime migrations per release.
pub mod migrations {
#[allow(unused)]
use super::*;

use crate::Runtime;
/// Unreleased migrations. Add new ones here:
pub type Unreleased = ();
pub type Unreleased = bifrost_asset_registry::migration::InsertBNCMetadata<Runtime>;
}

/// Executive: handles dispatch to the various modules.
Expand Down
Loading