From c3b00ba7d1088de0f65a307ff1a4bf7d20aec9b8 Mon Sep 17 00:00:00 2001 From: Muhammad-Jibril Date: Sat, 14 Aug 2021 21:38:07 +0800 Subject: [PATCH] Fix SettMint --- Cargo.lock | 2 ++ .../settmint/settmint-manager/Cargo.toml | 17 ++++++---- .../settmint/settmint-manager/src/lib.rs | 8 ++--- .../settmint/settmint-manager/src/mock.rs | 27 ++++++++++------ .../settmint/settmint-manager/src/tests.rs | 32 ++++--------------- 5 files changed, 39 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bb4b83fa4..725ba88ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8593,6 +8593,7 @@ version = "0.8.0" dependencies = [ "frame-support 3.0.0 (git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8)", "frame-system 3.0.0 (git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8)", + "max-encoded-len 3.0.0 (git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8)", "orml-currencies", "orml-tokens", "orml-traits", @@ -8600,6 +8601,7 @@ dependencies = [ "parity-scale-codec", "serde", "serp-treasury", + "setheum-dex", "setheum-primitives", "setheum-support", "sp-core 3.0.0 (git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.8)", diff --git a/lib-serml/settmint/settmint-manager/Cargo.toml b/lib-serml/settmint/settmint-manager/Cargo.toml index b8fc064fa..5c8dd541a 100644 --- a/lib-serml/settmint/settmint-manager/Cargo.toml +++ b/lib-serml/settmint/settmint-manager/Cargo.toml @@ -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 } @@ -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"] diff --git a/lib-serml/settmint/settmint-manager/src/lib.rs b/lib-serml/settmint/settmint-manager/src/lib.rs index f428b7458..65f28bf52 100644 --- a/lib-serml/settmint/settmint-manager/src/lib.rs +++ b/lib-serml/settmint/settmint-manager/src/lib.rs @@ -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; @@ -328,8 +328,4 @@ impl Pallet { let reserve_currency = T::GetReserveCurrencyId::get(); T::Currency::free_balance(reserve_currency, &Self::account_id()) } - - fn get_total_reserve() -> Balance { - Self::total_reserve() - } } diff --git a/lib-serml/settmint/settmint-manager/src/mock.rs b/lib-serml/settmint/settmint-manager/src/mock.rs index b83ae7145..de3bcbf47 100644 --- a/lib-serml/settmint/settmint-manager/src/mock.rs +++ b/lib-serml/settmint/settmint-manager/src/mock.rs @@ -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; @@ -109,6 +109,7 @@ impl orml_tokens::Config for Runtime { parameter_types! { pub const ExistentialDeposit: Balance = 1; + pub const MaxReserves: u32 = 50; } impl pallet_balances::Config for Runtime { @@ -116,8 +117,10 @@ impl pallet_balances::Config for Runtime { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = frame_system::Module; + type AccountStore = frame_system::Pallet; type MaxLocks = (); + type MaxReserves = MaxReserves; + type ReserveIdentifier = ReserveIdentifier; type WeightInfo = (); } @@ -292,8 +295,8 @@ impl StandardValidator 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")), } } @@ -342,21 +345,27 @@ construct_runtime!( Tokens: orml_tokens::{Pallet, Storage, Event, Config}, PalletBalances: pallet_balances::{Pallet, Call, Storage, Event}, Currencies: orml_currencies::{Pallet, Call, Event}, - SerpTreasuryModule: serp_treasury::{Pallet, Storage, Call, Event}, + SerpTreasuryModule: serp_treasury::{Pallet, Storage, Event}, SetheumDEX: setheum_dex::{Pallet, Storage, Call, Event, Config}, } ); 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), ], } } @@ -368,7 +377,7 @@ impl ExtBuilder { .build_storage::() .unwrap(); orml_tokens::GenesisConfig:: { - endowed_accounts: self.endowed_accounts, + balances: self.balances, } .assimilate_storage(&mut t) .unwrap(); diff --git a/lib-serml/settmint/settmint-manager/src/tests.rs b/lib-serml/settmint/settmint-manager/src/tests.rs index 97f5a1d13..5bb8af06a 100644 --- a/lib-serml/settmint/settmint-manager/src/tests.rs +++ b/lib-serml/settmint/settmint-manager/src/tests.rs @@ -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); }); @@ -59,17 +59,8 @@ 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::::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); @@ -77,22 +68,22 @@ fn adjust_position_should_work() { 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); @@ -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); - }); -}