Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecosystem-modules/stable-asset
2 changes: 2 additions & 0 deletions modules/auction-manager/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use sp_runtime::{
traits::{AccountIdConversion, IdentityLookup, One as OneT},
};
use sp_std::cell::RefCell;
use support::mocks::MockStableAsset;
pub use support::Price;

pub type AccountId = u128;
Expand Down Expand Up @@ -136,6 +137,7 @@ impl cdp_treasury::Config for Runtime {
type TreasuryAccount = TreasuryAccount;
type AlternativeSwapPathJointList = AlternativeSwapPathJointList;
type WeightInfo = ();
type StableAsset = MockStableAsset<CurrencyId, Balance, AccountId, BlockNumber>;
}

thread_local! {
Expand Down
1 change: 1 addition & 0 deletions modules/cdp-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ support = { package = "module-support", path = "../support", default-features =
loans = { package = "module-loans", path = "../loans", default-features = false }
primitives = { package = "acala-primitives", path = "../../primitives", default-features = false }
rand_chacha = { version = "0.2", default-features = false }
nutsfinance-stable-asset = { version = "0.1.0", default-features = false, path = "../../ecosystem-modules/stable-asset/lib/stable-asset", package = "nutsfinance-stable-asset" }

[dev-dependencies]
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.17" }
Expand Down
4 changes: 3 additions & 1 deletion modules/cdp-engine/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use sp_runtime::{
traits::{AccountIdConversion, IdentityLookup, One as OneT},
};
use sp_std::cell::RefCell;
use support::mocks::MockStableAsset;
use support::{AuctionManager, EmergencyShutdown};

pub type AccountId = u128;
Expand Down Expand Up @@ -196,7 +197,7 @@ impl AuctionManager<AccountId> for MockAuctionManager {
amount: Self::Balance,
target: Self::Balance,
) -> DispatchResult {
AUCTION.with(|v| *v.borrow_mut() = Some((refund_recipient.clone(), currency_id, amount, target)));
AUCTION.with(|v| *v.borrow_mut() = Some((*refund_recipient, currency_id, amount, target)));
Ok(())
}

Expand Down Expand Up @@ -242,6 +243,7 @@ impl cdp_treasury::Config for Runtime {
type TreasuryAccount = TreasuryAccount;
type AlternativeSwapPathJointList = AlternativeSwapPathJointList;
type WeightInfo = ();
type StableAsset = MockStableAsset<CurrencyId, Balance, AccountId, BlockNumber>;
}

parameter_types! {
Expand Down
4 changes: 2 additions & 2 deletions modules/cdp-engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,11 +863,11 @@ fn liquidate_unsafe_cdp_by_collateral_auction_when_limited_by_slippage() {
// pool is enough, but slippage limit the swap
MockPriceSource::set_price(BTC, Some(Price::saturating_from_rational(2, 1)));
assert_eq!(
DEXModule::get_swap_amount(&vec![BTC, AUSD], SwapLimit::ExactTarget(Balance::MAX, 60)),
DEXModule::get_swap_amount(&[BTC, AUSD], SwapLimit::ExactTarget(Balance::MAX, 60)),
Some((99, 60))
);
assert_eq!(
DEXModule::get_swap_amount(&vec![BTC, AUSD], SwapLimit::ExactSupply(100, 0)),
DEXModule::get_swap_amount(&[BTC, AUSD], SwapLimit::ExactSupply(100, 0)),
Some((100, 60))
);
assert_ok!(CDPEngineModule::liquidate_unsafe_cdp(ALICE, BTC));
Expand Down
1 change: 1 addition & 0 deletions modules/cdp-treasury/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v
orml-traits = { path = "../../orml/traits", default-features = false }
support = { package = "module-support", path = "../support", default-features = false }
primitives = { package = "acala-primitives", path = "../../primitives", default-features = false }
nutsfinance-stable-asset = { version = "0.1.0", default-features = false, path = "../../ecosystem-modules/stable-asset/lib/stable-asset", package = "nutsfinance-stable-asset" }

[dev-dependencies]
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.17" }
Expand Down
88 changes: 80 additions & 8 deletions modules/cdp-treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

use frame_support::{log, pallet_prelude::*, transactional, PalletId};
use frame_system::pallet_prelude::*;
use nutsfinance_stable_asset::traits::StableAsset;
use nutsfinance_stable_asset::RedeemProportionResult;
use orml_traits::{MultiCurrency, MultiCurrencyExtended};
use primitives::{Balance, CurrencyId};
use sp_runtime::{
Expand Down Expand Up @@ -72,6 +74,14 @@ pub mod module {
/// currency
type DEX: DEXManager<Self::AccountId, CurrencyId, Balance>;

type StableAsset: StableAsset<
AssetId = CurrencyId,
AtLeast64BitUnsigned = Balance,
Balance = Balance,
AccountId = Self::AccountId,
BlockNumber = Self::BlockNumber,
>;

/// The cap of lots number when create collateral auction on a
/// liquidation or to create debit/surplus auction on block end.
/// If set to 0, does not work.
Expand Down Expand Up @@ -376,6 +386,11 @@ impl<T: Config> CDPTreasuryExtended<T::AccountId> for Pallet<T> {
SwapLimit::ExactSupply(supply_amount, _) => supply_amount,
SwapLimit::ExactTarget(max_supply_amount, _) => max_supply_amount,
};
let target_limit = match limit {
SwapLimit::ExactSupply(_, minimum_target_amount) => minimum_target_amount,
SwapLimit::ExactTarget(_, exact_target_amount) => exact_target_amount,
};

if collateral_in_auction {
ensure!(
Self::total_collaterals(currency_id) >= supply_limit
Expand All @@ -389,14 +404,71 @@ impl<T: Config> CDPTreasuryExtended<T::AccountId> for Pallet<T> {
);
}

let swap_path = T::DEX::get_best_price_swap_path(
currency_id,
T::GetStableCurrencyId::get(),
limit,
T::AlternativeSwapPathJointList::get(),
)
.ok_or(Error::<T>::CannotSwap)?;
T::DEX::swap_with_specific_path(&Self::account_id(), &swap_path, limit)
match currency_id {
CurrencyId::StableAssetPoolToken(stable_asset_id) => {
let pool_info = T::StableAsset::pool(stable_asset_id).ok_or(Error::<T>::CannotSwap)?;
let updated_balance_info =
T::StableAsset::get_balance_update_amount(&pool_info).ok_or(Error::<T>::CannotSwap)?;
let yield_info =
T::StableAsset::get_collect_yield_amount(&updated_balance_info).ok_or(Error::<T>::CannotSwap)?;
ensure!(
yield_info.total_supply >= pool_info.total_supply,
Error::<T>::CannotSwap,
);
let RedeemProportionResult {
amounts,
balances: _,
fee_amount: _,
total_supply: _,
redeem_amount: _,
Comment thread
ukby1234 marked this conversation as resolved.
Outdated
} = T::StableAsset::get_redeem_proportion_amount(&yield_info, supply_limit)
.ok_or(Error::<T>::CannotSwap)?;
let mut sum = 0;
let mut swap_paths = vec![];
let mut redeem_limits = vec![];
let mut swap_limits = vec![];
Comment thread
wangjj9219 marked this conversation as resolved.
Outdated

for i in 0..amounts.len() {
Comment thread
ukby1234 marked this conversation as resolved.
let currency = pool_info.assets[i];
let amount = amounts[i];
let swap_limit = SwapLimit::ExactSupply(amount, 0);
swap_limits.push(swap_limit);
let swap_path = T::DEX::get_best_price_swap_path(
currency,
T::GetStableCurrencyId::get(),
swap_limit,
T::AlternativeSwapPathJointList::get(),
)
.ok_or(Error::<T>::CannotSwap)?;
let swap_amount = T::DEX::get_swap_amount(&swap_path, swap_limit).ok_or(Error::<T>::CannotSwap)?;
swap_paths.push(swap_path);
redeem_limits.push(0);
sum += swap_amount.1;
}
ensure!(sum >= target_limit, Error::<T>::CannotSwap,);
T::StableAsset::redeem_proportion(&Self::account_id(), stable_asset_id, supply_limit, redeem_limits)?;

let mut supply_sum = 0;
let mut target_sum = 0;
for i in 0..amounts.len() {
let response =
T::DEX::swap_with_specific_path(&Self::account_id(), &swap_paths[i], swap_limits[i])?;
supply_sum += response.0;
target_sum += response.1;
Comment thread
wangjj9219 marked this conversation as resolved.
Outdated
}
Ok((supply_sum, target_sum))
}
_ => {
let swap_path = T::DEX::get_best_price_swap_path(
currency_id,
T::GetStableCurrencyId::get(),
limit,
T::AlternativeSwapPathJointList::get(),
)
.ok_or(Error::<T>::CannotSwap)?;
T::DEX::swap_with_specific_path(&Self::account_id(), &swap_path, limit)
}
}
}

fn create_collateral_auctions(
Expand Down
Loading