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

MORE SERP FIXES #446

Merged
merged 1 commit into from
Aug 13, 2021
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
46 changes: 3 additions & 43 deletions lib-serml/serp/serp-treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl<T: Config> SerpTreasury<T::AccountId> for Pallet<T> {
)?;

// Burn Native Reserve asset (Dinar (DNAR))
Self::burn_dinar(&Self::account_id(), serping_amount)?;
T::Currency::withdraw( T::GetNativeCurrencyId::get(), &Self::account_id(), serping_amount)?;

<Pallet<T>>::deposit_event(Event::SerpUpDelivery(amount, currency_id));
Ok(())
Expand All @@ -227,7 +227,7 @@ impl<T: Config> SerpTreasury<T::AccountId> for Pallet<T> {
let six: Balance = 6;
let serping_amount: Balance = six.saturating_mul(amount / 10);
// Issue the SerpUp propper to the SettPayVault
Self::issue_propper(currency_id, &settpay_account, serping_amount)?;
Self::issue_standard(currency_id, &settpay_account, serping_amount)?;

<Pallet<T>>::deposit_event(Event::SerpUpDelivery(amount, currency_id));
Ok(())
Expand All @@ -239,7 +239,7 @@ impl<T: Config> SerpTreasury<T::AccountId> for Pallet<T> {
// Charity Fund SerpUp Pool - 10%
let serping_amount: Balance = amount / 10;
// Issue the SerpUp propper to the SettPayVault
Self::issue_propper(currency_id, &charity_fund_account, serping_amount)?;
Self::issue_standard(currency_id, &charity_fund_account, serping_amount)?;

<Pallet<T>>::deposit_event(Event::SerpUpDelivery(amount, currency_id));
Ok(())
Expand Down Expand Up @@ -366,15 +366,6 @@ impl<T: Config> SerpTreasury<T::AccountId> for Pallet<T> {
T::Currency::withdraw(currency_id, who, standard)
}

fn issue_propper(currency_id: CurrencyId, who: &T::AccountId, propper: Self::Balance) -> DispatchResult {
T::Currency::deposit(currency_id, who, propper)?;
Ok(())
}

fn burn_propper(currency_id: CurrencyId, who: &T::AccountId, propper: Self::Balance) -> DispatchResult {
T::Currency::withdraw(currency_id, who, propper)
}

fn issue_setter(who: &T::AccountId, setter: Self::Balance) -> DispatchResult {
T::Currency::deposit(T::SetterCurrencyId::get(), who, setter)?;
Ok(())
Expand All @@ -385,41 +376,10 @@ impl<T: Config> SerpTreasury<T::AccountId> for Pallet<T> {
T::Currency::withdraw(T::SetterCurrencyId::get(), who, setter)
}

fn issue_dinar(who: &T::AccountId, dinar: Self::Balance) -> DispatchResult {
T::Currency::deposit(T::GetNativeCurrencyId::get(), who, dinar)?;
Ok(())
}

/// Burn Native Reserve asset (Dinar (DNAR))
fn burn_dinar(who: &T::AccountId, dinar: Self::Balance) -> DispatchResult {
T::Currency::withdraw(T::GetNativeCurrencyId::get(), who, dinar)
}

/// Issue Dexer (`DRAM` in Setheum). `dexer` here just referring to the Dex token balance.
fn issue_dexer(who: &T::AccountId, dexer: Self::Balance) -> DispatchResult {
T::Currency::deposit(T::DirhamCurrencyId::get(), who, dexer)?;
Ok(())
}

/// Burn Dexer (`DRAM` in Setheum). `dexer` here just referring to the Dex token balance.
fn burn_dexer(who: &T::AccountId, dexer: Self::Balance) -> DispatchResult {
T::Currency::withdraw(T::DirhamCurrencyId::get(), who, dexer)
}

/// deposit surplus(propper stable currency) to serp treasury by `from`
fn deposit_serplus(currency_id: CurrencyId, from: &T::AccountId, serplus: Self::Balance) -> DispatchResult {
T::Currency::transfer(currency_id, from, &Self::account_id(), serplus)
}

/// deposit reserve asset (Setter (SETT)) to serp treasury by `who`
fn deposit_setter(from: &T::AccountId, amount: Self::Balance) -> DispatchResult {
T::Currency::transfer(T::SetterCurrencyId::get(), from, &Self::account_id(), amount)
}

// refund remain Dinar to refund recipient from SERP
fn withdraw_dinar(to: &T::AccountId, amount: Self::Balance) -> DispatchResult {
T::Currency::transfer(T::GetNativeCurrencyId::get(), &Self::account_id(), to, amount)
}
}

impl<T: Config> SerpTreasuryExtended<T::AccountId> for Pallet<T> {
Expand Down
16 changes: 16 additions & 0 deletions lib-serml/serp/serp-treasury/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ parameter_types! {
TradingPair::from_currency_ids(SEKJ, SETT).unwrap(),
TradingPair::from_currency_ids(SGDJ, SETT).unwrap(),
TradingPair::from_currency_ids(USDJ, SETT).unwrap(),
TradingPair::from_currency_ids(AUDJ, DNAR).unwrap(),
TradingPair::from_currency_ids(CADJ, DNAR).unwrap(),
TradingPair::from_currency_ids(CHFJ, DNAR).unwrap(),
TradingPair::from_currency_ids(EURJ, DNAR).unwrap(),
TradingPair::from_currency_ids(GBPJ, DNAR).unwrap(),
TradingPair::from_currency_ids(JPYJ, DNAR).unwrap(),
TradingPair::from_currency_ids(SARJ, DNAR).unwrap(),
TradingPair::from_currency_ids(SEKJ, DNAR).unwrap(),
TradingPair::from_currency_ids(SGDJ, DNAR).unwrap(),
TradingPair::from_currency_ids(USDJ, DNAR).unwrap(),
];
}

Expand Down Expand Up @@ -299,10 +309,16 @@ impl Default for ExtBuilder {
balances: vec![
(ALICE, USDJ, 1000),
(ALICE, SETT, 1000),
(ALICE, DNAR, 1000),
(ALICE, DRAM, 1000),
(BOB, USDJ, 1000),
(BOB, SETT, 1000),
(BOB, DNAR, 1000),
(BOB, DRAM, 1000),
(CHARITY_FUND, USDJ, 1000),
(CHARITY_FUND, SETT, 1000),
(CHARITY_FUND, DNAR, 1000),
(CHARITY_FUND, DRAM, 1000),
],
}
}
Expand Down
133 changes: 112 additions & 21 deletions lib-serml/serp/serp-treasury/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,24 @@ fn burn_standard_works() {
}

#[test]
fn issue_dexer_works() {
fn issue_setter_works() {
ExtBuilder::default().build().execute_with(|| {
assert_eq!(Currencies::free_balance(DRAM, &ALICE), 1000);
assert_eq!(Currencies::free_balance(SETT, &ALICE), 1000);

assert_ok!(SerpTreasuryModule::issue_dexer(&ALICE, 1000));
assert_eq!(Currencies::free_balance(DRAM, &ALICE), 2000);
assert_ok!(SerpTreasuryModule::issue_setter(&ALICE, 1000));
assert_eq!(Currencies::free_balance(SETT, &ALICE), 2000);

assert_ok!(SerpTreasuryModule::issue_dexer(&ALICE, 1000));
assert_eq!(Currencies::free_balance(DRAM, &ALICE), 3000);
assert_ok!(SerpTreasuryModule::issue_setter(&ALICE, 1000));
assert_eq!(Currencies::free_balance(SETT, &ALICE), 3000);
});
}

#[test]
fn burn_dexer_works() {
fn burn_setter_works() {
ExtBuilder::default().build().execute_with(|| {
assert_eq!(Currencies::free_balance(DRAM, &ALICE), 1000);
assert_ok!(SerpTreasuryModule::burn_dexer(&ALICE, 300));
assert_eq!(Currencies::free_balance(DRAM, &ALICE), 700);
});
}

#[test]
fn deposit_serplus_works() {
ExtBuilder::default().build().execute_with(|| {
assert_eq!(Currencies::free_balance(USDJ, &ALICE), 1000);
assert_eq!(Currencies::free_balance(USDJ, &SerpTreasuryModule::account_id()), 0);
assert_ok!(SerpTreasuryModule::deposit_serplus(USDJ, &ALICE, 300));
assert_eq!(Currencies::free_balance(USDJ, &ALICE), 700);
assert_eq!(Currencies::free_balance(USDJ, &SerpTreasuryModule::account_id()), 300);
assert_eq!(Currencies::free_balance(SETT, &ALICE), 1000);
assert_ok!(SerpTreasuryModule::burn_setter(&ALICE, 300));
assert_eq!(Currencies::free_balance(SETT, &ALICE), 700);
});
}

Expand All @@ -91,3 +80,105 @@ fn deposit_setter_works() {
assert_eq!(Currencies::free_balance(SETT, &ALICE), 500);
});
}

#[test]
fn swap_dinar_to_exact_setter() {
ExtBuilder::default().build().execute_with(|| {
assert_ok!(SetheumDEX::add_liquidity(
Origin::signed(ALICE),
SETT,
USDJ,
100,
1000,
0,
));
assert_ok!(SetheumDEX::add_liquidity(
Origin::signed(ALICE),
SETT,
DNAR,
900,
1000,
0,
));
assert_ok!(SetheumDEX::add_liquidity(
Origin::signed(BOB),
DNAR,
USDJ,
1000,
1000,
0,
));
assert_ok!(Currencies::deposit(DNAR, &SerpTreasuryModule::account_id(), 10000));
assert_ok!(SerpTreasuryModule::swap_dinar_to_exact_setter(
100, None
));
assert_noop!(
SerpTreasuryModule::swap_dinar_to_exact_setter(100, Some(&vec![SETT])),
Error::<Runtime>::InvalidSwapPath
);
assert_noop!(
SerpTreasuryModule::swap_dinar_to_exact_setter(100, Some(&vec![DNAR, USDJ])),
Error::<Runtime>::InvalidSwapPath
);
assert_ok!(SerpTreasuryModule::swap_dinar_to_exact_setter(
100,
Some(&vec![SETT, DNAR, USDJ]),
));
assert_eq!(Currencies::free_balance(SETT, &SerpTreasuryModule::account_id()), 200);
});
}

#[test]
fn swap_exact_stablecurrency_to_dinar_work() {
ExtBuilder::default().build().execute_with(|| {
assert_ok!(SetheumDEX::add_liquidity(
Origin::signed(ALICE),
SETT,
USDJ,
100,
1000,
0,
));
assert_ok!(SetheumDEX::add_liquidity(
Origin::signed(ALICE),
SETT,
DNAR,
900,
1000,
0,
));
assert_ok!(SetheumDEX::add_liquidity(
Origin::signed(BOB),
DNAR,
USDJ,
1000,
1000,
0,
));
assert_ok!(Currencies::deposit(SETT, &SerpTreasuryModule::account_id(), 10000));
assert_ok!(SerpTreasuryModule::swap_exact_stablecurrency_to_dinar(
SETT, 100, None
));
assert_eq!(Currencies::free_balance(SETT, &SerpTreasuryModule::account_id()), 100);

assert_noop!(
SerpTreasuryModule::swap_exact_stablecurrency_to_dinar(SETT, 100, Some(&vec![SETT])),
Error::<Runtime>::InvalidSwapPath
);
assert_noop!(
SerpTreasuryModule::swap_exact_stablecurrency_to_dinar(SETT, 100, Some(&vec![SETT, DNAR])),
Error::<Runtime>::InvalidSwapPath
);
assert_noop!(
SerpTreasuryModule::swap_exact_stablecurrency_to_dinar(SETT, 100, Some(&vec![DNAR, USDJ])),
Error::<Runtime>::InvalidSwapPath
);

assert_ok!(SerpTreasuryModule::swap_exact_stablecurrency_to_dinar(
SETT,
100,
Some(&vec![SETT, DNAR, USDJ])
));
assert_eq!(Currencies::free_balance(SETT, &SerpTreasuryModule::account_id()), 100);
});
}
24 changes: 0 additions & 24 deletions lib-serml/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,38 +250,14 @@ pub trait SerpTreasury<AccountId> {
/// burn standard(stable currency) of `who`
fn burn_standard(currency_id: Self::CurrencyId, who: &AccountId, standard: Self::Balance) -> DispatchResult;

/// issue propper(stable currency) of amount propper to `who`
fn issue_propper(currency_id: Self::CurrencyId, who: &AccountId, propper: Self::Balance) -> DispatchResult;

/// burn propper(stable currency) of `who`
fn burn_propper(currency_id: Self::CurrencyId, who: &AccountId, propper: Self::Balance) -> DispatchResult;

/// issue setter of amount setter to `who`
fn issue_setter(who: &AccountId, setter: Self::Balance) -> DispatchResult;

/// burn setter of `who`
fn burn_setter(who: &AccountId, setter: Self::Balance) -> DispatchResult;

/// issue dinar of amount dinar to `who`
fn issue_dinar(who: &AccountId, dinar: Self::Balance) -> DispatchResult;

/// burn dinar of `who`
fn burn_dinar(who: &AccountId, dinar: Self::Balance) -> DispatchResult;

/// Issue Dexer (`DRAM` in Setheum). `dexer` here just referring to the DEX token balance.
fn issue_dexer(who: &AccountId, dexer: Self::Balance) -> DispatchResult;

/// Burn Dexer (`DRAM` in Setheum). `dexer` here just referring to the DEX token balance.
fn burn_dexer(who: &AccountId, dexer: Self::Balance) -> DispatchResult;

/// deposit surplus(propperstable currency) to serp treasury by `from`
fn deposit_serplus(currency_id: Self::CurrencyId, from: &AccountId, surplus: Self::Balance) -> DispatchResult;

/// deposit reserve asset (Setter (SETT)) to serp treasury by `who`
fn deposit_setter(from: &AccountId, amount: Self::Balance) -> DispatchResult;

// refund remain Dinar to refund recipient from SERP
fn withdraw_dinar(to: &AccountId, amount: Self::Balance) -> DispatchResult;
}

pub trait SerpTreasuryExtended<AccountId>: SerpTreasury<AccountId> {
Expand Down