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

Kensetsu Phase 2 - configurable debt assets #1005

Merged
merged 63 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
2648e6c
feature(kensetsu): do not allow ken and kusd be used as collateral
Alexey-N-Chernyshov Apr 18, 2024
41a41b2
feature(kensetsu): stablecoin parameters
Alexey-N-Chernyshov Apr 18, 2024
b6b2fb7
Merge branch 'master' into feature/kensetsu-more-stablecoins
Alexey-N-Chernyshov Apr 19, 2024
d2d245f
feature(kensetsu): oracle added
Alexey-N-Chernyshov Apr 22, 2024
365b878
fix(kensetsu): bad debt total supply
Alexey-N-Chernyshov Apr 25, 2024
73b3df0
fix(kensetsu): KGOLD added
Alexey-N-Chernyshov Apr 26, 2024
0d7fa93
fix(kensetsu): update collateral info on cdp creation
Alexey-N-Chernyshov Apr 27, 2024
6ae5e03
feature(kensetsu): update fee before repay
Alexey-N-Chernyshov Apr 27, 2024
1c13c61
merge master
Alexey-N-Chernyshov Apr 27, 2024
93b9989
fix(kensetsu): pr clippy
Alexey-N-Chernyshov Apr 27, 2024
ca270e7
Merge branch 'master' into feature/kensetsu-more-stablecoins
Alexey-N-Chernyshov Apr 27, 2024
5f1df77
feature(kensetsu): refactor
Alexey-N-Chernyshov Apr 29, 2024
30c8e04
feature(kensetsu): migration
Alexey-N-Chernyshov May 2, 2024
43f409a
chore(kensetsu): remove cdp_close amount
Alexey-N-Chernyshov May 6, 2024
3513058
feature(kensetsu): check withdrawal asset id
Alexey-N-Chernyshov May 7, 2024
72b4270
merge master
Alexey-N-Chernyshov May 8, 2024
577d3ee
Merge branch 'refs/heads/master' into feature/kensetsu-more-stablecoins
Alexey-N-Chernyshov May 10, 2024
ac404a5
merge master
Alexey-N-Chernyshov May 10, 2024
1c5a796
feature(kensetsu): register_stablecoin
Alexey-N-Chernyshov May 13, 2024
5101614
feature(kensetsu): register stablecoin asset
Alexey-N-Chernyshov May 14, 2024
3c7317d
fix(kensetsu): clippy
Alexey-N-Chernyshov May 14, 2024
9d35ebf
Merge branch 'refs/heads/master' into feature/kensetsu-more-stablecoins
Alexey-N-Chernyshov May 14, 2024
56b3a19
fix clippy
Alexey-N-Chernyshov May 14, 2024
72b93b9
fix: remove token hard_cap
Alexey-N-Chernyshov May 15, 2024
89be930
feature(kensetsu): kensetsu stable asset ids
Alexey-N-Chernyshov May 15, 2024
7f08422
feature(kensetsu): kensetsu reference symbols
Alexey-N-Chernyshov May 15, 2024
df0e317
feature(kensetsu): kensetsu reference symbols
Alexey-N-Chernyshov May 16, 2024
df36c4e
feature(kensetsu): fix clippy
Alexey-N-Chernyshov May 17, 2024
3d2c713
feature(kensetsu): v2 storage migration
Alexey-N-Chernyshov May 17, 2024
4c858dc
fix(kensetsu): clippy
Alexey-N-Chernyshov May 17, 2024
6c63b4f
test(kensetsu): migration test
Alexey-N-Chernyshov May 17, 2024
511ea3b
feature(kensetsu): migration
Alexey-N-Chernyshov May 17, 2024
1fbaffd
merge master
Alexey-N-Chernyshov May 17, 2024
0539f27
feature(kensetsu): borrow tax
Alexey-N-Chernyshov May 20, 2024
f22e3d7
Merge branch 'refs/heads/master' into feature/kensetsu-more-stablecoins
Alexey-N-Chernyshov May 20, 2024
e94ec65
feature(kensetsu): update migration
Alexey-N-Chernyshov May 20, 2024
60ab5f1
Add new tokens (#1042)
vovac12 May 20, 2024
1e6b32b
merge master
Alexey-N-Chernyshov May 21, 2024
c3b9433
feature(kensetsu): add buy back for xor/kxor
Alexey-N-Chernyshov May 21, 2024
647abc6
feature(kensetsu): fix clippy
Alexey-N-Chernyshov May 21, 2024
10c2ea1
feature(kensetsu): add genesis config for stablecoin infos
Alexey-N-Chernyshov May 22, 2024
5581977
feature(migration): register SB to price-tools
Alexey-N-Chernyshov May 24, 2024
d431a78
feature(price-tools): add method is_asset_registered to PriceToolsPro…
Alexey-N-Chernyshov May 24, 2024
5ae8ebf
feature(price-tools): registers asset in PriceToolsProvider
Alexey-N-Chernyshov May 25, 2024
f75de1f
feature(price-tools): rm price tools migration
Alexey-N-Chernyshov May 25, 2024
738838e
Merge branch 'refs/heads/master' into feature/kensetsu-more-stablecoins
Alexey-N-Chernyshov May 27, 2024
588a66a
fix: merge conflict
Alexey-N-Chernyshov May 27, 2024
bc88669
Merge branch 'refs/heads/master' into feature/kensetsu-more-stablecoins
Alexey-N-Chernyshov May 27, 2024
d2f7032
Merge branch 'master' into feature/kensetsu-more-stablecoins
Alexey-N-Chernyshov May 27, 2024
a8e7690
Add KXOR support to XYK pool (#1041)
vovac12 May 27, 2024
634c6db
fix(kensetsu): do not register XOR in price tools
Alexey-N-Chernyshov May 28, 2024
d404f7b
Merge branch 'master' of github.com:sora-xor/sora2-network into featu…
vovac12 May 28, 2024
1ea8bad
fix(kensetsu): migrate v0 and v1
Alexey-N-Chernyshov May 28, 2024
7fe9e4d
feature(kensetsu): configurable borrow taxes
Alexey-N-Chernyshov May 29, 2024
379d1b1
Merge branch 'refs/heads/master' into feature/kensetsu-more-stablecoins
Alexey-N-Chernyshov May 29, 2024
d320bc0
feature(kensetsu): add KGOLD as predefined asset
Alexey-N-Chernyshov May 29, 2024
d044359
feature(kensetsu): collateral info storagemap
Alexey-N-Chernyshov May 29, 2024
730502a
fix(kensetsu): buyback KARMA, TBCD on any collateral
Alexey-N-Chernyshov Jun 4, 2024
a232da6
fix(kensetsu): fix pr issues
Alexey-N-Chernyshov Jun 4, 2024
a030ab5
fix(kensetsu): fix pr issues
Alexey-N-Chernyshov Jun 5, 2024
90b74a6
Merge branch 'master' into feature/kensetsu-more-stablecoins
Alexey-N-Chernyshov Jun 5, 2024
51e385e
Merge branch 'master' into feature/kensetsu-more-stablecoins
vovac12 Jun 7, 2024
4ec29e9
Update common/src/primitives.rs
Alexey-N-Chernyshov Jun 7, 2024
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
4 changes: 3 additions & 1 deletion common/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub enum ComicAssetId {
MichaelJacksonCD,
JesterMarotte,
CrackedBrassBell,
PyriteStone,
}

impl crate::traits::IsRepresentation for ComicAssetId {
Expand Down Expand Up @@ -109,6 +110,7 @@ impl From<PredefinedAssetId> for ComicAssetId {
PredefinedAssetId::KEN => JesterMarotte,
PredefinedAssetId::TBCD => MichaelJacksonCD,
PredefinedAssetId::KUSD => CrackedBrassBell,
PredefinedAssetId::KGOLD => PyriteStone,
}
}
}
Expand Down Expand Up @@ -299,7 +301,7 @@ macro_rules! mock_frame_system_config {
type SystemWeightInfo = ();
type SS58Prefix = ConstU16<42>;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
type MaxConsumers = frame_support::traits::ConstU32<65536>;
}
};
}
Expand Down
7 changes: 7 additions & 0 deletions common/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ mod _allowed_deprecated {
TBCD = 10,
KEN = 11,
KUSD = 12,
KGOLD = 13,
}
}

Expand All @@ -199,6 +200,7 @@ pub const XST: AssetId32<PredefinedAssetId> = AssetId32::from_asset_id(Predefine
pub const TBCD: AssetId32<PredefinedAssetId> = AssetId32::from_asset_id(PredefinedAssetId::TBCD);
pub const KEN: AssetId32<PredefinedAssetId> = AssetId32::from_asset_id(PredefinedAssetId::KEN);
pub const KUSD: AssetId32<PredefinedAssetId> = AssetId32::from_asset_id(PredefinedAssetId::KUSD);
pub const KGOLD: AssetId32<PredefinedAssetId> = AssetId32::from_asset_id(PredefinedAssetId::KGOLD);
pub const CERES_ASSET_ID: AssetId32<PredefinedAssetId> = AssetId32::from_bytes(hex!(
"008bcfd2387d3fc453333557eecb0efe59fcba128769b2feefdd306e98e66440"
));
Expand Down Expand Up @@ -650,6 +652,11 @@ impl SymbolName {
pub fn usd() -> Self {
Self::from_str("USD").expect("`USD` is a valid symbol name")
}

/// Troy ounce of gold
pub fn xau() -> Self {
Self::from_str("XAU").expect("`XAU` is a valid symbol name")
}
}

impl FromStr for SymbolName {
Expand Down
28 changes: 26 additions & 2 deletions node/chain_spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
use common::prelude::{Balance, DEXInfo, FixedWrapper};
use common::{
balance, fixed, hash, our_include, our_include_bytes, vec_push, BalancePrecision, DEXId, Fixed,
TechPurpose, APOLLO_ASSET_ID, DAI, DEFAULT_BALANCE_PRECISION, ETH, HERMES_ASSET_ID, KEN, KUSD,
PSWAP, TBCD, USDT, VAL, XOR, XST, XSTUSD,
TechPurpose, APOLLO_ASSET_ID, DAI, DEFAULT_BALANCE_PRECISION, ETH, HERMES_ASSET_ID, KEN, KGOLD,
KUSD, PSWAP, TBCD, USDT, VAL, XOR, XST, XSTUSD,
};
use frame_support::sp_runtime::Percent;
use framenode_runtime::eth_bridge::{AssetConfig, BridgeAssetData, NetworkConfig};
Expand Down Expand Up @@ -1402,6 +1402,18 @@ fn testnet_genesis(
None,
None,
),
#[cfg(feature = "ready-to-test")] // kensetsu
(
KGOLD.into(),
assets_and_permissions_account_id.clone(),
AssetSymbol(b"KGOLD".to_vec()),
AssetName(b"Kensetsu Gold".to_vec()),
DEFAULT_BALANCE_PRECISION,
Balance::zero(),
true,
None,
None,
),
(
common::AssetId32::from_bytes(hex!(
"008bcfd2387d3fc453333557eecb0efe59fcba128769b2feefdd306e98e66440"
Expand Down Expand Up @@ -2087,6 +2099,18 @@ fn mainnet_genesis(
None,
None,
),
#[cfg(feature = "ready-to-test")] // kensetsu
(
KGOLD.into(),
assets_and_permissions_account_id.clone(),
AssetSymbol(b"KGOLD".to_vec()),
AssetName(b"Kensetsu Gold".to_vec()),
DEFAULT_BALANCE_PRECISION,
Balance::zero(),
true,
None,
None,
),
(
common::AssetId32::from_bytes(hex!(
"008bcfd2387d3fc453333557eecb0efe59fcba128769b2feefdd306e98e66440"
Expand Down
3 changes: 2 additions & 1 deletion pallets/kensetsu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ frame-support = { git = "https://github.com/sora-xor/substrate.git", branch = "p
frame-system = { git = "https://github.com/sora-xor/substrate.git", branch = "polkadot-v0.9.38", default-features = false }
hex-literal = { version = "0.4", optional = true }
pallet-timestamp = { git = "https://github.com/sora-xor/substrate.git", branch = "polkadot-v0.9.38", default-features = false }
scale-info = { version = "2", default-features = false, features = ["derive"] }
permissions = { path = "../permissions", default-features = false }
scale-info = { version = "2", default-features = false, features = ["derive"] }
sp-arithmetic = { git = "https://github.com/sora-xor/substrate.git", branch = "polkadot-v0.9.38", default-features = false }
sp-core = { git = "https://github.com/sora-xor/substrate.git", branch = "polkadot-v0.9.38", default-features = false }
sp-io = { git = "https://github.com/sora-xor/substrate.git", branch = "polkadot-v0.9.38", optional = true }
Expand All @@ -46,6 +46,7 @@ std = [
"codec/std",
"frame-support/std",
"frame-system/std",
"permissions/std",
"sp-std/std",
"sp-core/std",
"sp-runtime/std",
Expand Down
128 changes: 84 additions & 44 deletions pallets/kensetsu/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ use common::{
use frame_benchmarking::benchmarks;
use frame_system::RawOrigin;
use hex_literal::hex;
use kensetsu::{BorrowTax, CdpId, CollateralInfos, CollateralRiskParameters, Event};
use kensetsu::{
BorrowTax, CdpId, CdpType, CollateralInfos, CollateralRiskParameters, Event, PegAsset,
StablecoinInfos, StablecoinParameters,
};
use price_tools::AVG_BLOCK_SPAN;
use sp_arithmetic::{Perbill, Percent};
use sp_core::Get;
Expand All @@ -63,10 +66,28 @@ fn caller<T: Config>() -> T::AccountId {
T::AccountId::decode(&mut &bytes[..]).expect("Failed to decode account ID")
}

/// Configures Kensetsu Dollar stablecoin pegged to DAI.
pub fn set_kensetsu_dollar_stablecoin<T: Config>() {
StablecoinInfos::<T>::set::<AssetIdOf<T>>(
KUSD.into(),
Some(kensetsu::StablecoinInfo {
bad_debt: 0,
stablecoin_parameters: StablecoinParameters {
hard_cap: Balance::MAX,
peg_asset: PegAsset::SoraAssetId(DAI.into()),
minimal_stability_fee_accrue: balance!(0),
},
}),
);
}

/// Sets XOR as collateral type with default risk parameters
fn set_xor_as_collateral_type<T: Config>() {
CollateralInfos::<T>::set::<AssetIdOf<T>>(
set_kensetsu_dollar_stablecoin::<T>();

CollateralInfos::<T>::set::<AssetIdOf<T>, AssetIdOf<T>>(
XOR.into(),
KUSD.into(),
Some(kensetsu::CollateralInfo {
risk_parameters: CollateralRiskParameters {
hard_cap: Balance::MAX,
Expand All @@ -76,7 +97,7 @@ fn set_xor_as_collateral_type<T: Config>() {
minimal_collateral_deposit: balance!(0),
},
total_collateral: balance!(0),
kusd_supply: balance!(0),
stablecoin_supply: balance!(0),
last_fee_update_time: Default::default(),
interest_coefficient: FixedU128::one(),
}),
Expand All @@ -89,8 +110,10 @@ fn create_cdp_with_xor<T: Config>() -> CdpId {
RawOrigin::Signed(caller::<T>()).into(),
XOR.into(),
balance!(0),
KUSD.into(),
balance!(0),
balance!(0),
CdpType::Type2,
)
.expect("Shall create CDP");
kensetsu::NextCDPId::<T>::get()
Expand All @@ -115,17 +138,21 @@ fn deposit_xor_collateral<T: Config>(cdp_id: CdpId, amount: Balance) {

/// Sets liquidation ratio too low, making CDPs unsafe
fn make_cdps_unsafe<T: Config>() {
CollateralInfos::<T>::mutate::<AssetIdOf<T>, _, _>(XOR.into(), |info| {
if let Some(info) = info.as_mut() {
info.risk_parameters = CollateralRiskParameters {
hard_cap: Balance::MAX,
max_liquidation_lot: balance!(100),
liquidation_ratio: Perbill::from_percent(1),
stability_fee_rate: FixedU128::zero(),
minimal_collateral_deposit: balance!(0),
CollateralInfos::<T>::mutate::<AssetIdOf<T>, AssetIdOf<T>, _, _>(
XOR.into(),
KUSD.into(),
|info| {
if let Some(info) = info.as_mut() {
info.risk_parameters = CollateralRiskParameters {
hard_cap: Balance::MAX,
max_liquidation_lot: balance!(100),
liquidation_ratio: Perbill::from_percent(1),
stability_fee_rate: FixedU128::zero(),
minimal_collateral_deposit: balance!(0),
}
}
}
});
},
);
}

/// Initializes and adds liquidity to XYK pool XOR/asset_id.
Expand Down Expand Up @@ -214,10 +241,6 @@ benchmarks! {
create_cdp {
initialize_liquidity_sources::<T>();
set_xor_as_collateral_type::<T>();
kensetsu::Pallet::<T>::update_hard_cap_total_supply(
RawOrigin::Root.into(),
Balance::MAX,
).expect("Shall update hard cap");
let collateral = balance!(10);
let debt = balance!(1);
assets::Pallet::<T>::update_balance(
Expand All @@ -232,8 +255,10 @@ benchmarks! {
RawOrigin::Signed(caller::<T>()).into(),
XOR.into(),
collateral,
KUSD.into(),
debt,
debt
debt,
CdpType::Type2,
).unwrap();
}

Expand Down Expand Up @@ -265,10 +290,6 @@ benchmarks! {
borrow {
initialize_liquidity_sources::<T>();
set_xor_as_collateral_type::<T>();
kensetsu::Pallet::<T>::update_hard_cap_total_supply(
RawOrigin::Root.into(),
Balance::MAX,
).expect("Shall update hard cap");
let cdp_id = create_cdp_with_xor::<T>();
let amount = balance!(10);
deposit_xor_collateral::<T>(cdp_id, amount);
Expand All @@ -289,10 +310,6 @@ benchmarks! {
let amount = balance!(10);
deposit_xor_collateral::<T>(cdp_id, amount);
let debt = balance!(1);
kensetsu::Pallet::<T>::update_hard_cap_total_supply(
RawOrigin::Root.into(),
Balance::MAX,
).expect("Shall update hard cap");
kensetsu::Pallet::<T>::borrow(RawOrigin::Signed(caller::<T>()).into(), cdp_id, debt, debt)
.expect("Shall borrow");
}: {
Expand All @@ -310,10 +327,6 @@ benchmarks! {
let amount = balance!(100);
deposit_xor_collateral::<T>(cdp_id, amount);
let debt = balance!(50);
kensetsu::Pallet::<T>::update_hard_cap_total_supply(
RawOrigin::Root.into(),
Balance::MAX,
).expect("Shall update hard cap");
kensetsu::Pallet::<T>::borrow(RawOrigin::Signed(caller::<T>()).into(), cdp_id, debt, debt)
.expect("Shall borrow");
make_cdps_unsafe::<T>();
Expand All @@ -328,10 +341,6 @@ benchmarks! {
let amount = balance!(1000);
deposit_xor_collateral::<T>(cdp_id, amount);
let debt = balance!(100);
kensetsu::Pallet::<T>::update_hard_cap_total_supply(
RawOrigin::Root.into(),
Balance::MAX,
).expect("Shall update hard cap");
kensetsu::Pallet::<T>::borrow(
RawOrigin::Signed(caller::<T>()).into(),
cdp_id,
Expand All @@ -343,10 +352,13 @@ benchmarks! {
kensetsu::Pallet::<T>::accrue(RawOrigin::Signed(caller::<T>()).into(), cdp_id).unwrap();
}

update_collateral_risk_parameters {}: {
update_collateral_risk_parameters {
set_xor_as_collateral_type::<T>();
}: {
kensetsu::Pallet::<T>::update_collateral_risk_parameters(
RawOrigin::Root.into(),
XOR.into(),
KUSD.into(),
CollateralRiskParameters {
hard_cap: balance!(1000),
liquidation_ratio: Perbill::from_percent(50),
Expand All @@ -357,13 +369,6 @@ benchmarks! {
).unwrap();
}

update_hard_cap_total_supply {}: {
kensetsu::Pallet::<T>::update_hard_cap_total_supply(
RawOrigin::Root.into(),
balance!(1000)
).unwrap();
}

update_borrow_tax {
let new_borrow_tax = Percent::from_percent(1);
}:{
Expand Down Expand Up @@ -393,6 +398,7 @@ benchmarks! {
}

withdraw_profit {
set_kensetsu_dollar_stablecoin::<T>();
let technical_account_id = technical::Pallet::<T>::tech_account_id_to_account_id(
&T::TreasuryTechAccount::get(),
).expect("Shall resolve tech account id");
Expand All @@ -408,11 +414,13 @@ benchmarks! {
kensetsu::Pallet::<T>::withdraw_profit(
RawOrigin::Root.into(),
caller::<T>(),
KUSD.into(),
amount
).unwrap();
}

donate {
set_xor_as_collateral_type::<T>();
let amount = balance!(10);
assets::Pallet::<T>::update_balance(
RawOrigin::Root.into(),
Expand All @@ -421,8 +429,40 @@ benchmarks! {
amount.try_into().unwrap(),
)
.expect("Shall mint KUSD");
kensetsu::BadDebt::<T>::set(balance!(5));
StablecoinInfos::<T>::mutate::<AssetIdOf<T>, _, _>(KUSD.into(), |stablecoin_info| {
stablecoin_info.as_mut().unwrap().bad_debt = balance!(5);
});
}: {
kensetsu::Pallet::<T>::donate(RawOrigin::Signed(caller::<T>()).into(), amount).unwrap();
kensetsu::Pallet::<T>::donate(
RawOrigin::Signed(caller::<T>()).into(),
KUSD.into(),
amount
).unwrap();
}

register_stablecoin {
let stablecoin_asset_id: AssetIdOf<T> = KUSD.into();
let stablecoin_parameters = StablecoinParameters::<AssetIdOf<T>> {
hard_cap: Balance::MAX,
peg_asset: PegAsset::SoraAssetId(DAI.into()),
minimal_stability_fee_accrue: balance!(0.01),
};
}: {
kensetsu::Pallet::<T>::register_stablecoin(
RawOrigin::Root.into(),
stablecoin_asset_id.clone(),
stablecoin_parameters.clone(),
)
.unwrap()
}
verify {
frame_system::Pallet::<T>::assert_has_event(
<T as kensetsu::Config>::RuntimeEvent::from(
Event::<T>::StablecoinRegistered {
stablecoin_asset_id,
new_stablecoin_parameters: stablecoin_parameters,
}
).into()
);
}
}
Loading
Loading