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

Fix leverage staking #1110

Merged
merged 9 commits into from
Dec 22, 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
2 changes: 0 additions & 2 deletions pallets/lend-market/src/lend_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ impl<T: Config> Inspect<T::AccountId> for Pallet<T> {
fn reducible_balance(
lend_token_id: Self::AssetId,
who: &T::AccountId,
// _keep_alive: bool,
_preservation: Preservation,
_force: Fortitude,
) -> Self::Balance {
Expand All @@ -65,7 +64,6 @@ impl<T: Config> Inspect<T::AccountId> for Pallet<T> {
lend_token_id: Self::AssetId,
who: &T::AccountId,
amount: Self::Balance,
// _mint: bool,
_provenance: Provenance,
) -> DepositConsequence {
let underlying_id = match Self::underlying_id(lend_token_id) {
Expand Down
41 changes: 26 additions & 15 deletions pallets/lend-market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub mod pallet {
/// Invalid asset id
InvalidCurrencyId,
/// Invalid lend token id
InvalidPtokenId,
InvalidLendTokenId,
/// Market does not exist
MarketDoesNotExist,
/// Market already exists
Expand Down Expand Up @@ -472,7 +472,8 @@ pub mod pallet {
/// by the new provided value.
///
/// The lend token id and asset id are bound, the lend token id of new provided market
/// cannot be duplicated with the existing one, otherwise it will return `InvalidPtokenId`.
/// cannot be duplicated with the existing one, otherwise it will return
/// `InvalidLendTokenId`.
///
/// - `asset_id`: Market related currency
/// - `market`: The market that is going to be stored
Expand Down Expand Up @@ -665,7 +666,7 @@ pub mod pallet {
if UnderlyingAssetId::<T>::contains_key(market.lend_token_id) {
ensure!(
Self::underlying_id(market.lend_token_id)? == asset_id,
Error::<T>::InvalidPtokenId
Error::<T>::InvalidLendTokenId
);
}
UnderlyingAssetId::<T>::insert(market.lend_token_id, asset_id);
Expand Down Expand Up @@ -865,13 +866,7 @@ pub mod pallet {
asset_id: AssetIdOf<T>,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
Self::ensure_active_market(asset_id)?;
Self::accrue_interest(asset_id)?;
let exchange_rate = Self::exchange_rate_stored(asset_id)?;
Self::update_earned_stored(&who, asset_id, exchange_rate)?;
let deposits = AccountDeposits::<T>::get(asset_id, &who);
let redeem_amount = Self::do_redeem_voucher(&who, asset_id, deposits.voucher_balance)?;
Self::deposit_event(Event::<T>::Redeemed(who, asset_id, redeem_amount));
let _ = Self::do_redeem_all(&who, asset_id)?;

Ok(().into())
}
Expand Down Expand Up @@ -1847,10 +1842,13 @@ impl<T: Config> Pallet<T> {
// Ensures a given `lend_token_id` is unique in `Markets` and `UnderlyingAssetId`.
fn ensure_lend_token(lend_token_id: CurrencyId) -> DispatchResult {
// The lend token id is unique, cannot be repeated
ensure!(!UnderlyingAssetId::<T>::contains_key(lend_token_id), Error::<T>::InvalidPtokenId);
ensure!(
!UnderlyingAssetId::<T>::contains_key(lend_token_id),
Error::<T>::InvalidLendTokenId
);

// The lend token id should not be the same as the id of any asset in markets
ensure!(!Markets::<T>::contains_key(lend_token_id), Error::<T>::InvalidPtokenId);
ensure!(!Markets::<T>::contains_key(lend_token_id), Error::<T>::InvalidLendTokenId);

Ok(())
}
Expand Down Expand Up @@ -1901,9 +1899,8 @@ impl<T: Config> Pallet<T> {
asset_id,
&Self::account_id(),
Preservation::Expendable,
Fortitude::Force,
Fortitude::Polite,
)
// T::Assets::balance(asset_id, &Self::account_id())
}

// Returns the uniform format price.
Expand Down Expand Up @@ -1978,7 +1975,7 @@ impl<T: Config> Pallet<T> {
// Returns `Err` if asset_id does not exist, it also means that lend_token_id is invalid.
pub fn underlying_id(lend_token_id: AssetIdOf<T>) -> Result<AssetIdOf<T>, DispatchError> {
UnderlyingAssetId::<T>::try_get(lend_token_id)
.map_err(|_err| Error::<T>::InvalidPtokenId.into())
.map_err(|_err| Error::<T>::InvalidLendTokenId.into())
}

// Returns the lend_token_id of the related asset
Expand All @@ -1998,6 +1995,20 @@ impl<T: Config> Pallet<T> {
let entropy = (b"lend-market/incentive", &[account_id]).using_encoded(blake2_256);
Ok(T::AccountId::decode(&mut &entropy[..]).map_err(|_| Error::<T>::CodecError)?)
}

pub fn do_redeem_all(
who: &AccountIdOf<T>,
asset_id: AssetIdOf<T>,
) -> Result<BalanceOf<T>, DispatchError> {
Self::ensure_active_market(asset_id)?;
Self::accrue_interest(asset_id)?;
let exchange_rate = Self::exchange_rate_stored(asset_id)?;
Self::update_earned_stored(&who, asset_id, exchange_rate)?;
let deposits = AccountDeposits::<T>::get(asset_id, &who);
let redeem_amount = Self::do_redeem_voucher(&who, asset_id, deposits.voucher_balance)?;
Self::deposit_event(Event::<T>::Redeemed(who.clone(), asset_id, redeem_amount));
Ok(redeem_amount)
}
}

impl<T: Config> LendMarketTrait<AssetIdOf<T>, AccountIdOf<T>, BalanceOf<T>> for Pallet<T> {
Expand Down
2 changes: 1 addition & 1 deletion pallets/lend-market/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ impl pallet_prices::Config for Test {
}

parameter_types! {
pub const LendMarketPalletId: PalletId = PalletId(*b"par/loan");
pub const LendMarketPalletId: PalletId = PalletId(*b"bf/ldmkt");
pub const RewardAssetId: CurrencyId = BNC;
pub const LiquidationFreeAssetId: CurrencyId = DOT;
}
Expand Down
4 changes: 2 additions & 2 deletions pallets/lend-market/src/tests/lend_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ fn lend_token_unique_works() {
// lend_token_id already exists in `UnderlyingAssetId`
assert_noop!(
LendMarket::add_market(RuntimeOrigin::root(), LKSM, market_mock(VBNC)),
Error::<Test>::InvalidPtokenId
Error::<Test>::InvalidLendTokenId
);

// lend_token_id cannot as the same as the asset id in `Markets`
assert_noop!(
LendMarket::add_market(RuntimeOrigin::root(), LKSM, market_mock(KSM)),
Error::<Test>::InvalidPtokenId
Error::<Test>::InvalidLendTokenId
);
})
}
Expand Down
2 changes: 1 addition & 1 deletion pallets/lend-market/src/tests/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ fn force_update_market_works() {
// New lend_token_id must not be in use
assert_noop!(
LendMarket::force_update_market(RuntimeOrigin::root(), DOT, market_mock(LUSDT)),
Error::<Test>::InvalidPtokenId
Error::<Test>::InvalidLendTokenId
);
assert_ok!(LendMarket::force_update_market(RuntimeOrigin::root(), DOT, market_mock(LDOT)));
assert_eq!(LendMarket::market(DOT).unwrap().lend_token_id, LDOT);
Expand Down
2 changes: 1 addition & 1 deletion pallets/lend-market/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub struct Market<Balance> {
pub supply_cap: Balance,
/// Upper bound of borrowing
pub borrow_cap: Balance,
/// Ptoken asset id
/// Lend token asset id
pub lend_token_id: CurrencyId,
}

Expand Down
1 change: 1 addition & 0 deletions pallets/leverage-staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ std = [
"bifrost-stable-pool/std",
"pallet-prices/std",
"log/std",
"orml-xtokens/std",
]
try-runtime = ['frame-support/try-runtime']

Expand Down
13 changes: 7 additions & 6 deletions pallets/leverage-staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use sp_runtime::{
traits::{StaticLookup, UniqueSaturatedFrom},
FixedPointNumber,
};
use sp_std::vec;

pub fn unit(d: u128) -> u128 {
d.saturating_mul(10_u128.pow(12))
Expand Down Expand Up @@ -153,6 +154,11 @@ fn init<
bifrost_vtoken_minting::BalanceOf::<T>::unique_saturated_from(unit(100u128)),
BoundedVec::default()
));
assert_ok!(lend_market::Pallet::<T>::mint(
SystemOrigin::Signed(caller.clone()).into(),
VKSM,
lend_market::BalanceOf::<T>::unique_saturated_from(unit(1u128))
));

Ok(())
}
Expand All @@ -173,12 +179,7 @@ mod benchmarks {
let rate = FixedU128::from_inner(unit(990_000));

#[extrinsic_call]
Pallet::<T>::flash_loan_deposit(
SystemOrigin::Signed(caller.clone()),
coin0.into(),
rate,
Some(unit(1).into()),
);
Pallet::<T>::flash_loan_deposit(SystemOrigin::Signed(caller.clone()), coin0.into(), rate);

Ok(())
}
Expand Down
Loading
Loading