Skip to content

Commit

Permalink
feature(kensetsu): register SB token pegged to DAI (#1192)
Browse files Browse the repository at this point in the history
* feature(kensetsu): register SB token pegged to DAI

* fix: update version in lib.rs
  • Loading branch information
Alexey-N-Chernyshov authored Sep 6, 2024
1 parent 61cf078 commit 4a26b16
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 6 deletions.
3 changes: 3 additions & 0 deletions common/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ pub const USDT: AssetId32<PredefinedAssetId> = AssetId32::from_asset_id(Predefin
pub const USDT: AssetId32<PredefinedAssetId> = AssetId32::from_bytes(hex!(
"0083a6b3fbc6edae06f115c8953ddd7cbfba0b74579d6ea190f96853073b76f4"
));
pub const SB: AssetId32<PredefinedAssetId> = AssetId32::from_bytes(hex!(
"007f66067c940aeb968b19c8dbf9768447e80c52f73aa175aa8c3936c1bb7e5b"
));

impl IsRepresentation for PredefinedAssetId {
fn is_representation(&self) -> bool {
Expand Down
8 changes: 5 additions & 3 deletions node/chain_spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use common::prelude::{Balance, DEXInfo, FixedWrapper};
use common::{
balance, fixed, hash, our_include, our_include_bytes, vec_push, BalancePrecision, DEXId, Fixed,
SymbolName, TechPurpose, APOLLO_ASSET_ID, DAI, DEFAULT_BALANCE_PRECISION, ETH, HERMES_ASSET_ID,
KARMA, KEN, KGOLD, KUSD, KXOR, PSWAP, TBCD, USDT, VAL, XOR, XST, XSTUSD,
KARMA, KEN, KGOLD, KUSD, KXOR, PSWAP, SB, TBCD, USDT, VAL, XOR, XST, XSTUSD,
};
use frame_support::sp_runtime::Percent;
use framenode_runtime::eth_bridge::{AssetConfig, BridgeAssetData, NetworkConfig};
Expand Down Expand Up @@ -1676,7 +1676,8 @@ fn testnet_genesis(
kensetsu: KensetsuConfig {
predefined_stablecoin_sora_peg: vec![
(KUSD, DAI, balance!(1)),
(KXOR, XOR, balance!(100000))
(KXOR, XOR, balance!(100000)),
(SB, DAI, balance!(1)),
],
predefined_stablecoin_oracle_peg: vec![
(KGOLD, SymbolName::xau(), balance!(0.001)),
Expand Down Expand Up @@ -2536,7 +2537,8 @@ fn mainnet_genesis(
kensetsu: KensetsuConfig {
predefined_stablecoin_sora_peg: vec![
(KUSD, DAI, balance!(1)),
(KXOR, XOR, balance!(100000))
(KXOR, XOR, balance!(100000)),
(SB, DAI, balance!(1)),
],
predefined_stablecoin_oracle_peg: vec![
(KGOLD, SymbolName::xau(), balance!(0.001)),
Expand Down
4 changes: 2 additions & 2 deletions pallets/kensetsu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ pub mod pallet {
pub type CdpId = u128;

/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(3);
const STORAGE_VERSION: StorageVersion = StorageVersion::new(4);

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
Expand Down Expand Up @@ -365,7 +365,7 @@ pub mod pallet {
#[pallet::constant]
type MaxCdpsPerOwner: Get<u32>;

/// Minimal uncollected fee in KUSD that triggers offchain worker to call accrue.
/// Minimal uncollected fee in stablecoin that triggers offchain worker to call accrue.
#[pallet::constant]
type MinimalStabilityFeeAccrue: Get<Balance>;

Expand Down
98 changes: 98 additions & 0 deletions pallets/kensetsu/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,101 @@ pub mod v2_to_v3 {
}
}
}

/// Registers SB as predefined stable pegged to DAI.
pub mod v3_to_v4 {
use crate::{Config, Pallet, PegAsset, StablecoinInfo, StablecoinInfos, StablecoinParameters};
use common::permissions::{BURN, MINT};
use common::{balance, AssetIdOf, DAI, SB};
use core::marker::PhantomData;
use frame_support::dispatch::GetStorageVersion;
use frame_support::log::error;
use frame_support::traits::{OnRuntimeUpgrade, StorageVersion};
use frame_support::weights::Weight;
use permissions::Scope;
use sp_core::Get;

pub struct UpgradeToV4<T>(PhantomData<T>);

impl<T: Config + permissions::Config + technical::Config + pallet_timestamp::Config>
OnRuntimeUpgrade for UpgradeToV4<T>
{
fn on_runtime_upgrade() -> Weight {
let mut weight = Weight::zero();
let version = Pallet::<T>::on_chain_storage_version();
if version == 3 {
if let Ok(technical_account_id) =
technical::Pallet::<T>::tech_account_id_to_account_id(
&T::TreasuryTechAccount::get(),
)
{
let scope = Scope::Limited(common::hash(&SB));
for permission_id in &[MINT, BURN] {
match permissions::Pallet::<T>::assign_permission(
technical_account_id.clone(),
&technical_account_id,
*permission_id,
scope,
) {
Ok(()) => {
weight += <T as frame_system::Config>::DbWeight::get().writes(1)
}
Err(err) => {
error!(
"Failed to grant permission to technical account id: {:?}, error: {:?}",
technical_account_id, err
);
weight += <T as frame_system::Config>::DbWeight::get().reads(1);
}
}
}
}
StablecoinInfos::<T>::insert(
AssetIdOf::<T>::from(SB),
StablecoinInfo {
bad_debt: balance!(0),
stablecoin_parameters: StablecoinParameters {
peg_asset: PegAsset::SoraAssetId(AssetIdOf::<T>::from(DAI)),
minimal_stability_fee_accrue: balance!(1),
},
},
);

StorageVersion::new(4).put::<Pallet<T>>();
weight += <T as frame_system::Config>::DbWeight::get().reads_writes(3, 3)
}

weight
}
}

#[cfg(test)]
mod tests {
use crate::migrations::v3_to_v4::UpgradeToV4;
use crate::mock::{new_test_ext, TestRuntime};
use crate::{Pallet, PegAsset, StablecoinInfos, StablecoinParameters};
use common::{balance, DAI, SB};
use frame_support::traits::{GetStorageVersion, OnRuntimeUpgrade, StorageVersion};

#[test]
fn test() {
new_test_ext().execute_with(|| {
StorageVersion::new(3).put::<Pallet<TestRuntime>>();

UpgradeToV4::<TestRuntime>::on_runtime_upgrade();

assert_eq!(Pallet::<TestRuntime>::on_chain_storage_version(), 4);

assert_eq!(1, StablecoinInfos::<TestRuntime>::iter().count());
let sb_info = StablecoinInfos::<TestRuntime>::get(SB).unwrap();
assert_eq!(
StablecoinParameters {
peg_asset: PegAsset::SoraAssetId(DAI),
minimal_stability_fee_accrue: balance!(1),
},
sb_info.stablecoin_parameters
);
});
}
}
}
2 changes: 1 addition & 1 deletion runtime/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

pub type Migrations = ();
pub type Migrations = (kensetsu::migrations::v3_to_v4::UpgradeToV4<crate::Runtime>,);

0 comments on commit 4a26b16

Please sign in to comment.