Skip to content

Commit

Permalink
error handling for SerpTreasury #527
Browse files Browse the repository at this point in the history
  • Loading branch information
balqaasem committed Sep 2, 2021
1 parent 77a4a07 commit 4606f38
Showing 1 changed file with 43 additions and 41 deletions.
84 changes: 43 additions & 41 deletions lib-serml/serp/serp-treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//!
//! ## Overview
//!
//! SERP Treasury manages the Settmint, and handle excess serplus
//! SERP Treasury manages the Setmint, and handle excess serplus
//! and stabilize SetCurrencies standards timely in order to keep the
//! system healthy. It manages the TES (Token Elasticity of Supply).

Expand All @@ -35,15 +35,11 @@ use frame_system::pallet_prelude::*;
use orml_traits::{GetByKey, MultiCurrency, MultiCurrencyExtended};
use primitives::{Balance, CurrencyId};
use sp_runtime::{
DispatchError, DispatchResult,
DispatchResult,
traits::{
AccountIdConversion, Bounded, CheckedSub, Convert, DispatchInfoOf, One, PostDispatchInfoOf, SaturatedConversion, Saturating,
SignedExtension, UniqueSaturatedInto, Zero,
AccountIdConversion, Bounded, One, Saturating, UniqueSaturatedInto, Zero,
},
transaction_validity::{
InvalidTransaction, TransactionPriority, TransactionValidity, TransactionValidityError, ValidTransaction,
},
FixedPointNumber, FixedPointOperand, FixedU128, Perquintill,
FixedPointNumber
};
use sp_std::{convert::TryInto, prelude::*, vec};
use support::{
Expand Down Expand Up @@ -162,7 +158,7 @@ pub mod module {
/// CashDrop is not available.
CashdropNotAvailable,
/// Transfer is too low for CashDrop.
TransferTooLowForCashDrop
TransferTooLowForCashDrop,
}

#[pallet::event]
Expand Down Expand Up @@ -470,6 +466,7 @@ impl<T: Config> SerpTreasury<T::AccountId> for Pallet<T> {
impl<T: Config> SerpTreasuryExtended<T::AccountId> for Pallet<T> {
/// swap Dinar to get exact Setter,
/// return actual supply Dinar amount
#[allow(unused_variables)]
fn swap_dinar_to_exact_setter(
target_amount: Balance,
) {
Expand Down Expand Up @@ -503,23 +500,23 @@ impl<T: Config> SerpTreasuryExtended<T::AccountId> for Pallet<T> {
CurrencyBalanceOf::<T>::max_value()
};

T::Currency::deposit(
if T::Currency::deposit(
dinar_currency_id,
&Self::account_id(),
max_supply_limit.unique_saturated_into()
);

if T::Dex::swap_with_exact_target(
&Self::account_id(),
&path,
target_amount.unique_saturated_into(),
<T as Config>::Currency::free_balance(dinar_currency_id, &Self::account_id())
.min(max_supply_limit.unique_saturated_into()),
)
.is_ok()
{
// successfully swap, break iteration
break;
).is_ok() {
if T::Dex::swap_with_exact_target(
&Self::account_id(),
&path,
target_amount.unique_saturated_into(),
<T as Config>::Currency::free_balance(dinar_currency_id, &Self::account_id())
.min(max_supply_limit.unique_saturated_into()),
)
.is_ok()
{
// successfully swap, break iteration
break;
}
}
}
_ => {}
Expand All @@ -532,6 +529,8 @@ impl<T: Config> SerpTreasuryExtended<T::AccountId> for Pallet<T> {
///
///
/// When SetCurrency needs SerpDown
///
#[allow(unused_variables)]
fn swap_setter_to_exact_setcurrency(
currency_id: CurrencyId,
target_amount: Balance,
Expand Down Expand Up @@ -565,23 +564,23 @@ impl<T: Config> SerpTreasuryExtended<T::AccountId> for Pallet<T> {
CurrencyBalanceOf::<T>::max_value()
};

T::Currency::deposit(
if T::Currency::deposit(
setter_currency_id,
&Self::account_id(),
max_supply_limit.unique_saturated_into()
);

if T::Dex::swap_with_exact_target(
&Self::account_id(),
&path,
target_amount.unique_saturated_into(),
<T as Config>::Currency::free_balance(setter_currency_id, &Self::account_id())
.min(max_supply_limit.unique_saturated_into()),
)
.is_ok()
{
// successfully swap, break iteration
break;
).is_ok() {
if T::Dex::swap_with_exact_target(
&Self::account_id(),
&path,
target_amount.unique_saturated_into(),
<T as Config>::Currency::free_balance(setter_currency_id, &Self::account_id())
.min(max_supply_limit.unique_saturated_into()),
)
.is_ok()
{
// successfully swap, break iteration
break;
}
}
}
_ => {}
Expand All @@ -594,6 +593,7 @@ impl<T: Config> SerpTreasuryExtended<T::AccountId> for Pallet<T> {
///
///
/// When Setter gets SerpUp
#[allow(unused_variables)]
fn swap_exact_setter_to_dinar(
supply_amount: Balance,
) {
Expand Down Expand Up @@ -627,16 +627,17 @@ impl<T: Config> SerpTreasuryExtended<T::AccountId> for Pallet<T> {
CurrencyBalanceOf::<T>::max_value()
};

// Swap and burn Native Reserve asset (Dinar (DNAR))
if T::Dex::swap_with_exact_supply(
&Self::account_id(),
&path,
supply_amount.unique_saturated_into(),
min_target_limit.unique_saturated_into(),
)
.is_ok()
&& T::Currency::withdraw( T::GetNativeCurrencyId::get(), &Self::account_id(), min_target_limit)
.is_ok()
{
// Burn Native Reserve asset (Dinar (DNAR))
T::Currency::withdraw( T::GetNativeCurrencyId::get(), &Self::account_id(), min_target_limit);
// successfully swap, break iteration.
break;
}
Expand All @@ -651,6 +652,7 @@ impl<T: Config> SerpTreasuryExtended<T::AccountId> for Pallet<T> {
///
///
/// When Setter gets SerpUp
#[allow(unused_variables)]
fn swap_exact_setcurrency_to_dinar(
currency_id: CurrencyId,
supply_amount: Balance,
Expand Down Expand Up @@ -691,9 +693,9 @@ impl<T: Config> SerpTreasuryExtended<T::AccountId> for Pallet<T> {
min_target_limit.unique_saturated_into(),
)
.is_ok()
&& T::Currency::withdraw( T::GetNativeCurrencyId::get(), &Self::account_id(), min_target_limit)
.is_ok()
{
// Burn Native Reserve asset (Dinar (DNAR))
T::Currency::withdraw( T::GetNativeCurrencyId::get(), &Self::account_id(), min_target_limit);
// successfully swap, break iteration
break;
}
Expand Down

0 comments on commit 4606f38

Please sign in to comment.