Skip to content

Commit

Permalink
Merge pull request #449 from Setheum-Labs/settmint
Browse files Browse the repository at this point in the history
Fix SettMint
  • Loading branch information
balqaasem authored Aug 14, 2021
2 parents 66d9ac9 + c3b00ba commit d594702
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 47 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

17 changes: 10 additions & 7 deletions lib-serml/settmint/settmint-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ serde = { version = "1.0.101", optional = true }
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }

# Substrate dependencies
frame-system = { default-features = false, version = "3.0.0", git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
frame-support = { default-features = false, version = "3.0.0", git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
sp-std = { default-features = false, version = "3.0.0", git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
sp-runtime = { default-features = false, version = "3.0.0", git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
max-encoded-len = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }

# orml dependencies
orml-traits = { path = "../../../lib-openrml/traits", default-features = false }
Expand All @@ -24,11 +25,13 @@ primitives = { package = "setheum-primitives", path = "../../../primitives", def
support = { package = "setheum-support", path = "../../support", default-features = false }

[dev-dependencies]
sp-core = { version = "3.0.0", git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
sp-io = { version = "3.0.0", git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
pallet-balances = { version = "3.0.0", git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
orml-currencies = { path = "../../../lib-openrml/currencies" }
serp-treasury = { package = "serp-treasury", path = "../../serp/serp-treasury" }
setheum-dex = { path = "../..//dex/dex" }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }

[features]
default = ["std"]
Expand Down
8 changes: 2 additions & 6 deletions lib-serml/settmint/settmint-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
#![allow(clippy::unused_unit)]

use frame_support::{log, pallet_prelude::*, traits::MaxEncodedLen, transactional, PalletId};
use orml_traits::{Happened, MultiCurrency, MultiCurrencyExtended};
use orml_traits::{MultiCurrency, MultiCurrencyExtended};
use primitives::{Amount, Balance, CurrencyId};
use sp_runtime::{
traits::{AccountIdConversion, Convert, Zero},
ArithmeticError, DispatchResult, RuntimeDebug,
};
use sp_std::{convert::TryInto, result};
use support::{BlockNumber, SerpTreasury, StandardValidator};
use support::{SerpTreasury, StandardValidator};

mod mock;
mod tests;
Expand Down Expand Up @@ -328,8 +328,4 @@ impl<T: Config> Pallet<T> {
let reserve_currency = T::GetReserveCurrencyId::get();
T::Currency::free_balance(reserve_currency, &Self::account_id())
}

fn get_total_reserve() -> Balance {
Self::total_reserve()
}
}
27 changes: 18 additions & 9 deletions lib-serml/settmint/settmint-manager/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ use primitives::{Amount, ReserveIdentifier, TokenSymbol, TradingPair};
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{AccountIdConversion, IdentityLookup},
traits::{IdentityLookup, One as OneT},
};
use support::{Price, Ratio, StandardValidator};
use support::{Price, PriceProvider, Ratio, StandardValidator};
use sp_std::cell::RefCell;

pub type AccountId = u128;
Expand Down Expand Up @@ -109,15 +109,18 @@ impl orml_tokens::Config for Runtime {

parameter_types! {
pub const ExistentialDeposit: Balance = 1;
pub const MaxReserves: u32 = 50;
}

impl pallet_balances::Config for Runtime {
type Balance = Balance;
type DustRemoval = ();
type Event = Event;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = frame_system::Module<Runtime>;
type AccountStore = frame_system::Pallet<Runtime>;
type MaxLocks = ();
type MaxReserves = MaxReserves;
type ReserveIdentifier = ReserveIdentifier;
type WeightInfo = ();
}

Expand Down Expand Up @@ -292,8 +295,8 @@ impl StandardValidator<AccountId, CurrencyId, Balance, Balance> for MockStandard
_standard_balance: Balance,
) -> DispatchResult {
match currency_id {
SETT => Err(sp_runtime::DispatchError::Other("mock error")),
SETT => Ok(()),
CHFJ => Err(sp_runtime::DispatchError::Other("mock error")),
EURJ => Ok(()),
_ => Err(sp_runtime::DispatchError::Other("mock error")),
}
}
Expand Down Expand Up @@ -342,21 +345,27 @@ construct_runtime!(
Tokens: orml_tokens::{Pallet, Storage, Event<T>, Config<T>},
PalletBalances: pallet_balances::{Pallet, Call, Storage, Event<T>},
Currencies: orml_currencies::{Pallet, Call, Event<T>},
SerpTreasuryModule: serp_treasury::{Pallet, Storage, Call, Event<T>},
SerpTreasuryModule: serp_treasury::{Pallet, Storage, Event<T>},
SetheumDEX: setheum_dex::{Pallet, Storage, Call, Event<T>, Config<T>},
}
);

pub struct ExtBuilder {
endowed_accounts: Vec<(AccountId, CurrencyId, Balance)>,
balances: Vec<(AccountId, CurrencyId, Balance)>,
}

impl Default for ExtBuilder {
fn default() -> Self {
Self {
endowed_accounts: vec![
balances: vec![
(ALICE, SETT, 1000),
(ALICE, USDJ, 1000),
(ALICE, EURJ, 1000),
(ALICE, CHFJ, 1000),
(BOB, SETT, 1000),
(BOB, USDJ, 1000),
(BOB, EURJ, 1000),
(BOB, CHFJ, 1000),
],
}
}
Expand All @@ -368,7 +377,7 @@ impl ExtBuilder {
.build_storage::<Runtime>()
.unwrap();
orml_tokens::GenesisConfig::<Runtime> {
endowed_accounts: self.endowed_accounts,
balances: self.balances,
}
.assimilate_storage(&mut t)
.unwrap();
Expand Down
32 changes: 7 additions & 25 deletions lib-serml/settmint/settmint-manager/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn standards_key() {
assert_eq!(SettmintManagerModule::positions(EURJ, &ALICE).standard, 0);
assert_ok!(SettmintManagerModule::adjust_position(&ALICE, EURJ, 200, 200));
assert_eq!(SettmintManagerModule::positions(EURJ, &ALICE).standard, 200);
assert_eq!(Currencies::free_balance(EURJ, &SettmintManagerModule::account_id()), 200);
assert_eq!(Currencies::free_balance(SETT, &SettmintManagerModule::account_id()), 100);
assert_ok!(SettmintManagerModule::adjust_position(&ALICE, EURJ, -100, -100));
assert_eq!(SettmintManagerModule::positions(EURJ, &ALICE).standard, 100);
});
Expand Down Expand Up @@ -59,40 +59,31 @@ fn adjust_position_should_work() {
System::set_block_number(1);
assert_eq!(Currencies::free_balance(SETT, &ALICE), 1000);

// balance too low
assert_eq!(SettmintManagerModule::adjust_position(&ALICE, EURJ, 2000, 0).is_ok(), false);

// mock can't pass position valid check
assert_eq!(SettmintManagerModule::adjust_position(&ALICE, USDJ, 500, 0).is_ok(), false);

// reserve_adjustment is positive
assert_noop!(
SettmintManagerModule::adjust_position(&ALICE, EURJ, 1000, 0),
orml_tokens::Error::<Runtime>::KeepAlive,
);
assert_eq!(SettmintManagerModule::adjust_position(&ALICE, CHFJ, 500, 0).is_ok(), false);

assert_eq!(Currencies::free_balance(SETT, &ALICE), 1000);
assert_eq!(Currencies::free_balance(EURJ, &SettmintManagerModule::account_id()), 0);
assert_eq!(SettmintManagerModule::total_positions(EURJ).standard, 0);
assert_eq!(SettmintManagerModule::total_positions(EURJ).reserve, 0);
assert_eq!(SettmintManagerModule::positions(EURJ, &ALICE).standard, 0);
assert_eq!(SettmintManagerModule::positions(EURJ, &ALICE).reserve, 0);
assert_eq!(Currencies::free_balance(EURJ, &ALICE), 0);
assert_eq!(Currencies::free_balance(EURJ, &ALICE), 1000);

// success
assert_ok!(SettmintManagerModule::adjust_position(&ALICE, EURJ, 500, 300));
assert_eq!(Currencies::free_balance(SETT, &ALICE), 500);
assert_eq!(Currencies::free_balance(SETT, &SettmintManagerModule::account_id()), 500);
assert_eq!(Currencies::free_balance(SETT, &ALICE), 750);
assert_eq!(Currencies::free_balance(SETT, &SettmintManagerModule::account_id()), 250);
assert_eq!(SettmintManagerModule::total_positions(EURJ).standard, 300);
assert_eq!(SettmintManagerModule::total_positions(EURJ).reserve, 500);
assert_eq!(SettmintManagerModule::positions(EURJ, &ALICE).standard, 300);
assert_eq!(SettmintManagerModule::positions(EURJ, &ALICE).reserve, 500);
assert_eq!(Currencies::free_balance(EURJ, &ALICE), 150);
assert_eq!(Currencies::free_balance(EURJ, &ALICE), 1150);
System::assert_last_event(Event::SettmintManagerModule(crate::Event::PositionUpdated(ALICE, EURJ, 500, 300)));

// reserve_adjustment is negatives
// remove module account.
assert_eq!(Currencies::total_balance(EURJ, &SettmintManagerModule::account_id()), 500);
assert_eq!(Currencies::total_balance(EURJ, &SettmintManagerModule::account_id()), 0);
assert_eq!(System::account_exists(&SettmintManagerModule::account_id()), true);
assert_ok!(SettmintManagerModule::adjust_position(&ALICE, EURJ, -500, 0));
assert_eq!(Currencies::free_balance(EURJ, &SettmintManagerModule::account_id()), 0);
Expand Down Expand Up @@ -166,16 +157,7 @@ fn update_position_should_work() {
#[test]
fn total_reserve_works() {
ExtBuilder::default().build().execute_with(|| {
assert_eq!(SettmintManagerModule::total_reserve(), 10);
assert_ok!(Currencies::deposit(SETT, &SettmintManagerModule::account_id(), 10));
assert_eq!(SettmintManagerModule::total_reserve(), 10);
});
}

#[test]
fn get_total_reserve_works() {
ExtBuilder::default().build().execute_with(|| {
assert_ok!(Currencies::deposit(SETT, &ALICE, 500));
assert_eq!(SettmintManagerModule::get_total_reserve(), 500);
});
}

0 comments on commit d594702

Please sign in to comment.