Skip to content

Commit

Permalink
fix: 🐛 register_blp_metadata & rpc (#1039)
Browse files Browse the repository at this point in the history
* fix: 🐛 register_blp_metadata & rpc

* feat: 🎸 BLP migration

* Fix migration

* Fix weight

* Fix check-all
  • Loading branch information
yooml authored Sep 12, 2023
1 parent 0323af5 commit e4960e4
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 6 deletions.
9 changes: 5 additions & 4 deletions pallets/asset-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use xcm::{
use xcm_builder::TakeRevenue;
use xcm_executor::{traits::WeightTrader, Assets};

pub mod migration;
mod mock;
mod tests;
pub mod weights;
Expand Down Expand Up @@ -839,10 +840,10 @@ impl<T: Config> CurrencyIdRegister<CurrencyId> for AssetIdMaps<T> {
}

fn register_blp_metadata(pool_id: PoolId, decimals: u8) -> DispatchResult {
let mut name = "Bifrost Stable Pool Token ".as_bytes().to_vec();
name.extend_from_slice(&pool_id.to_be_bytes());
let mut symbol = "BLP".as_bytes().to_vec();
symbol.extend_from_slice(&pool_id.to_be_bytes());
let name = scale_info::prelude::format!("Bifrost Stable Pool Token {}", pool_id)
.as_bytes()
.to_vec();
let symbol = scale_info::prelude::format!("BLP{}", pool_id).as_bytes().to_vec();
Pallet::<T>::do_register_metadata(
CurrencyId::BLP(pool_id),
&AssetMetadata {
Expand Down
40 changes: 40 additions & 0 deletions pallets/asset-registry/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// This file is part of Bifrost.

// Copyright (C) 2019-2022 Liebi Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![cfg_attr(not(feature = "std"), no_std)]

use super::{AssetMetadata, Config, CurrencyMetadatas, Weight};
use frame_support::traits::Get;
use primitives::CurrencyId;

pub fn update_blp_metadata<T: Config>(pool_count: u32) -> Weight {
for pool_id in 0..pool_count {
if let Some(old_metadata) = CurrencyMetadatas::<T>::get(CurrencyId::BLP(pool_id)) {
let name = scale_info::prelude::format!("Bifrost Stable Pool Token {}", pool_id)
.as_bytes()
.to_vec();
let symbol = scale_info::prelude::format!("BLP{}", pool_id).as_bytes().to_vec();
CurrencyMetadatas::<T>::insert(
CurrencyId::BLP(pool_id),
&AssetMetadata { name, symbol, ..old_metadata },
)
}
}

T::DbWeight::get().reads(pool_count.into()) + T::DbWeight::get().writes(pool_count.into())
}
32 changes: 31 additions & 1 deletion pallets/stable-pool/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ pub trait StablePoolRpcApi<BlockHash> {
amount: Balance,
at: Option<BlockHash>,
) -> RpcResult<NumberOrHex>;

#[method(name = "stable_pool_addLiquidityAmount")]
fn add_liquidity_amount(
&self,
pool_id: u32,
amounts: Vec<Balance>,
at: Option<BlockHash>,
) -> RpcResult<NumberOrHex>;
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -81,7 +89,29 @@ where
Ok(amount) => Ok(NumberOrHex::Hex(amount.into())),
Err(e) => Err(CallError::Custom(ErrorObject::owned(
ErrorCode::InternalError.code(),
"Failed to get stable_pool rewards.",
"Failed to get stable_pool swap output amount.",
Some(format!("{:?}", e)),
))),
}
.map_err(|e| jsonrpsee::core::Error::Call(e))
}

fn add_liquidity_amount(
&self,
pool_id: u32,
amounts: Vec<Balance>,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<NumberOrHex> {
let lm_rpc_api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);

let rs: Result<Balance, _> = lm_rpc_api.add_liquidity_amount(at, pool_id, amounts);

match rs {
Ok(amount) => Ok(NumberOrHex::Hex(amount.into())),
Err(e) => Err(CallError::Custom(ErrorObject::owned(
ErrorCode::InternalError.code(),
"Failed to get stable_pool add liquidity amount.",
Some(format!("{:?}", e)),
))),
}
Expand Down
53 changes: 52 additions & 1 deletion runtime/bifrost-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1923,9 +1923,60 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
migration::XcmInterfaceMigration,
(migration::XcmInterfaceMigration, BLPOnRuntimeUpgrade<Runtime>),
>;

use frame_support::traits::OnRuntimeUpgrade;
pub struct BLPOnRuntimeUpgrade<T>(PhantomData<T>);
impl<T: bifrost_asset_registry::Config> OnRuntimeUpgrade for BLPOnRuntimeUpgrade<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<sp_std::prelude::Vec<u8>, &'static str> {
#[allow(unused_imports)]
use frame_support::{migration, Identity};
log::info!("Bifrost `pre_upgrade`...");

let pool_count = nutsfinance_stable_asset::PoolCount::<Runtime>::get();
for pool_id in 0..pool_count {
if let Some(old_metadata) =
bifrost_asset_registry::CurrencyMetadatas::<Runtime>::get(CurrencyId::BLP(pool_id))
{
log::info!("Old currency_metadatas is {:?}", old_metadata);
}
}

Ok(vec![])
}

fn on_runtime_upgrade() -> Weight {
log::info!("Bifrost `on_runtime_upgrade`...");

let pool_count = nutsfinance_stable_asset::PoolCount::<Runtime>::get();
let weight = bifrost_asset_registry::migration::update_blp_metadata::<Runtime>(pool_count);

log::info!("Bifrost `on_runtime_upgrade finished`");

weight
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: sp_std::prelude::Vec<u8>) -> Result<(), &'static str> {
#[allow(unused_imports)]
use frame_support::{migration, Identity};
log::info!("Bifrost `post_upgrade`...");

let pool_count = nutsfinance_stable_asset::PoolCount::<Runtime>::get();
for pool_id in 0..pool_count {
if let Some(new_metadata) =
bifrost_asset_registry::CurrencyMetadatas::<Runtime>::get(CurrencyId::BLP(pool_id))
{
log::info!("New currency_metadatas is {:?}", new_metadata);
}
}

Ok(())
}
}

#[cfg(feature = "runtime-benchmarks")]
#[macro_use]
extern crate frame_benchmarking;
Expand Down

0 comments on commit e4960e4

Please sign in to comment.