Skip to content

Commit

Permalink
Merge pull request #531 from Setheum-Labs/error-handling-#527
Browse files Browse the repository at this point in the history
Error handling #527
  • Loading branch information
balqaasem authored Sep 3, 2021
2 parents eacd731 + c84a34b commit 4550169
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 41 deletions.
16 changes: 16 additions & 0 deletions lib-serml/serp/serp-ocw/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ fn public(&self) -> T::Public {
}
}

#[allow(unused)]
enum TransactionType {
Signed,
UnsignedForAny,
Expand All @@ -315,6 +316,8 @@ impl<T: Config> Pallet<T> {
/// and local storage usage.
///
/// Returns a type of transaction that should be produced in current run.
///
#[allow(unused)]
fn choose_transaction_type(block_number: T::BlockNumber) -> TransactionType {
/// A friendlier name for the error that is going to be returned in case we are in the grace
/// period.
Expand Down Expand Up @@ -381,6 +384,9 @@ impl<T: Config> Pallet<T> {
}

/// A helper function to fetch the price and send signed transaction.
///
///
#[allow(unused)]
fn fetch_price_and_send_signed() -> Result<(), &'static str> {
let signer = Signer::<T, T::AuthorityId>::all_accounts();
if !signer.can_sign() {
Expand Down Expand Up @@ -414,6 +420,7 @@ impl<T: Config> Pallet<T> {
}

/// A helper function to fetch the price and send a raw unsigned transaction.
#[allow(unused)]
fn fetch_price_and_send_raw_unsigned(block_number: T::BlockNumber) -> Result<(), &'static str> {
// Make sure we don't fetch the price if unsigned transaction is going to be rejected
// anyway.
Expand Down Expand Up @@ -446,6 +453,7 @@ impl<T: Config> Pallet<T> {
}

/// A helper function to fetch the price, sign payload and send an unsigned transaction
#[allow(unused)]
fn fetch_price_and_send_unsigned_for_any_account(
block_number: T::BlockNumber,
) -> Result<(), &'static str> {
Expand Down Expand Up @@ -475,6 +483,7 @@ impl<T: Config> Pallet<T> {
}

/// A helper function to fetch the price, sign payload and send an unsigned transaction
#[allow(unused)]
fn fetch_price_and_send_unsigned_for_all_accounts(
block_number: T::BlockNumber,
) -> Result<(), &'static str> {
Expand Down Expand Up @@ -695,6 +704,8 @@ impl<T: Config> Pallet<T> {
///
///
/// Fetch current SETR price and return the result in cents.
///
#[allow(unused_variables)]
fn setter_on_tes() -> Result<(), http::Error> {
// We want to keep the offchain worker execution time reasonable, so we set a hard-coded
// deadline to 2s to complete the external call.
Expand Down Expand Up @@ -959,6 +970,7 @@ impl<T: Config> Pallet<T> {
}

/// Fetch current SETUSD price and return the result in cents.
#[allow(unused_variables)]
fn setusd_on_tes() -> Result<(), http::Error> {
// We want to keep the offchain worker execution time reasonable, so we set a hard-coded
// deadline to 2s to complete the external call.
Expand Down Expand Up @@ -1082,6 +1094,7 @@ impl<T: Config> Pallet<T> {
}

/// Fetch current SETEUR price and return the result in cents.
#[allow(unused_variables)]
fn seteur_on_tes() -> Result<(), http::Error> {
// We want to keep the offchain worker execution time reasonable, so we set a hard-coded
// deadline to 2s to complete the external call.
Expand Down Expand Up @@ -1205,6 +1218,7 @@ impl<T: Config> Pallet<T> {
}

/// Fetch current SETGBP price and return the result in cents.
#[allow(unused_variables)]
fn setgbp_on_tes() -> Result<(), http::Error> {
// We want to keep the offchain worker execution time reasonable, so we set a hard-coded
// deadline to 2s to complete the external call.
Expand Down Expand Up @@ -1328,6 +1342,7 @@ impl<T: Config> Pallet<T> {
}

/// Fetch current SETCHF price and return the result in cents.
#[allow(unused_variables)]
fn setchf_on_tes() -> Result<(), http::Error> {
// We want to keep the offchain worker execution time reasonable, so we set a hard-coded
// deadline to 2s to complete the external call.
Expand Down Expand Up @@ -1451,6 +1466,7 @@ impl<T: Config> Pallet<T> {
}

/// Fetch current SETSAR price and return the result in cents.
#[allow(unused_variables)]
fn setsar_on_tes() -> Result<(), http::Error> {
// We want to keep the offchain worker execution time reasonable, so we set a hard-coded
// deadline to 2s to complete the external call.
Expand Down
2 changes: 1 addition & 1 deletion lib-serml/serp/serp-prices/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use sp_runtime::{
traits::{IdentityLookup, One as OneT, Zero},
DispatchError, FixedPointNumber,
};
use support::{mocks::MockCurrencyIdMapping, Ratio};
use support::mocks::MockCurrencyIdMapping;

pub type AccountId = u128;
pub type BlockNumber = u64;
Expand Down
82 changes: 42 additions & 40 deletions lib-serml/serp/serp-treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 4550169

Please sign in to comment.