-
Notifications
You must be signed in to change notification settings - Fork 653
/
Copy pathmock.rs
307 lines (269 loc) · 8.54 KB
/
mock.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// This file is part of Acala.
// Copyright (C) 2020-2022 Acala Foundation.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Mocks for the auction manager module.
#![cfg(test)]
use super::*;
use frame_support::{
construct_runtime, ord_parameter_types, parameter_types,
traits::{ConstU32, ConstU64, Everything, Nothing},
PalletId,
};
use frame_system::EnsureSignedBy;
use orml_traits::parameter_type_with_key;
use primitives::{TokenSymbol, TradingPair};
use sp_core::H256;
use sp_runtime::{
testing::{Header, TestXt},
traits::{AccountIdConversion, IdentityLookup, One as OneT},
};
use sp_std::cell::RefCell;
pub use support::Price;
use support::{mocks::MockStableAsset, SpecificJointsSwap};
pub type AccountId = u128;
pub type BlockNumber = u64;
pub type AuctionId = u32;
pub type Amount = i64;
pub const ALICE: AccountId = 1;
pub const BOB: AccountId = 2;
pub const CAROL: AccountId = 3;
pub const AUSD: CurrencyId = CurrencyId::Token(TokenSymbol::AUSD);
pub const BTC: CurrencyId = CurrencyId::Token(TokenSymbol::RENBTC);
pub const DOT: CurrencyId = CurrencyId::Token(TokenSymbol::DOT);
mod auction_manager {
pub use super::super::*;
}
impl frame_system::Config for Runtime {
type Origin = Origin;
type Index = u64;
type BlockNumber = BlockNumber;
type Call = Call;
type Hash = H256;
type Hashing = ::sp_runtime::traits::BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = ConstU64<250>;
type BlockWeights = ();
type BlockLength = ();
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type DbWeight = ();
type BaseCallFilter = Everything;
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = ConstU32<16>;
}
parameter_type_with_key! {
pub ExistentialDeposits: |_currency_id: CurrencyId| -> Balance {
Default::default()
};
}
impl orml_tokens::Config for Runtime {
type Event = Event;
type Balance = Balance;
type Amount = Amount;
type CurrencyId = CurrencyId;
type WeightInfo = ();
type ExistentialDeposits = ExistentialDeposits;
type OnDust = ();
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type DustRemovalWhitelist = Nothing;
type OnNewTokenAccount = ();
type OnKilledTokenAccount = ();
}
impl orml_auction::Config for Runtime {
type Event = Event;
type Balance = Balance;
type AuctionId = AuctionId;
type Handler = AuctionManagerModule;
type WeightInfo = ();
}
ord_parameter_types! {
pub const One: AccountId = 1;
}
parameter_types! {
pub const GetStableCurrencyId: CurrencyId = AUSD;
pub const MaxAuctionsCount: u32 = 10_000;
pub const CDPTreasuryPalletId: PalletId = PalletId(*b"aca/cdpt");
pub TreasuryAccount: AccountId = PalletId(*b"aca/hztr").into_account_truncating();
pub AlternativeSwapPathJointList: Vec<Vec<CurrencyId>> = vec![
vec![DOT],
];
}
impl cdp_treasury::Config for Runtime {
type Event = Event;
type Currency = Tokens;
type GetStableCurrencyId = GetStableCurrencyId;
type AuctionManagerHandler = AuctionManagerModule;
type UpdateOrigin = EnsureSignedBy<One, AccountId>;
type DEX = DEXModule;
type Swap = SpecificJointsSwap<DEXModule, AlternativeSwapPathJointList>;
type MaxAuctionsCount = MaxAuctionsCount;
type PalletId = CDPTreasuryPalletId;
type TreasuryAccount = TreasuryAccount;
type WeightInfo = ();
type StableAsset = MockStableAsset<CurrencyId, Balance, AccountId, BlockNumber>;
}
thread_local! {
static RELATIVE_PRICE: RefCell<Option<Price>> = RefCell::new(Some(Price::one()));
}
pub struct MockPriceSource;
impl MockPriceSource {
pub fn set_relative_price(price: Option<Price>) {
RELATIVE_PRICE.with(|v| *v.borrow_mut() = price);
}
}
impl PriceProvider<CurrencyId> for MockPriceSource {
fn get_relative_price(_base: CurrencyId, _quote: CurrencyId) -> Option<Price> {
RELATIVE_PRICE.with(|v| *v.borrow_mut())
}
fn get_price(_currency_id: CurrencyId) -> Option<Price> {
None
}
}
parameter_types! {
pub const DEXPalletId: PalletId = PalletId(*b"aca/dexm");
pub const TreasuryPalletId: PalletId = PalletId(*b"aca/trea");
pub const GetExchangeFee: (u32, u32) = (0, 100);
pub EnabledTradingPairs: Vec<TradingPair> = vec![
TradingPair::from_currency_ids(AUSD, BTC).unwrap(),
TradingPair::from_currency_ids(DOT, BTC).unwrap(),
TradingPair::from_currency_ids(AUSD, DOT).unwrap()
];
}
impl module_dex::Config for Runtime {
type Event = Event;
type Currency = Tokens;
type GetExchangeFee = GetExchangeFee;
type TradingPathLimit = ConstU32<4>;
type SingleTokenTradingLimit = ConstU32<10>;
type TradingKeysUpdateFrequency = ConstU64<1>;
type UnsignedPriority = ConstU64<1048576>; // 1 << 20
type PalletId = DEXPalletId;
type TreasuryPallet = TreasuryPalletId;
type Erc20InfoMapping = ();
type DEXIncentives = ();
type WeightInfo = ();
type ListingOrigin = EnsureSignedBy<One, AccountId>;
type ExtendedProvisioningBlocks = ConstU64<0>;
type OnLiquidityPoolUpdated = ();
}
thread_local! {
static IS_SHUTDOWN: RefCell<bool> = RefCell::new(false);
}
pub fn mock_shutdown() {
IS_SHUTDOWN.with(|v| *v.borrow_mut() = true)
}
pub struct MockEmergencyShutdown;
impl EmergencyShutdown for MockEmergencyShutdown {
fn is_shutdown() -> bool {
IS_SHUTDOWN.with(|v| *v.borrow_mut())
}
}
parameter_types! {
pub MinimumIncrementSize: Rate = Rate::saturating_from_rational(1, 20);
}
impl Config for Runtime {
type Event = Event;
type Currency = Tokens;
type Auction = AuctionModule;
type MinimumIncrementSize = MinimumIncrementSize;
type AuctionTimeToClose = ConstU64<100>;
type AuctionDurationSoftCap = ConstU64<2000>;
type GetStableCurrencyId = GetStableCurrencyId;
type CDPTreasury = CDPTreasuryModule;
type PriceSource = MockPriceSource;
type UnsignedPriority = ConstU64<1048576>; // 1 << 20
type EmergencyShutdown = MockEmergencyShutdown;
type WeightInfo = ();
}
pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<u32, Call, u32, ()>;
construct_runtime!(
pub enum Runtime where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Storage, Config, Event<T>},
AuctionManagerModule: auction_manager::{Pallet, Storage, Call, Event<T>, ValidateUnsigned},
Tokens: orml_tokens::{Pallet, Storage, Event<T>, Config<T>},
AuctionModule: orml_auction::{Pallet, Storage, Call, Event<T>},
CDPTreasuryModule: cdp_treasury::{Pallet, Storage, Call, Event<T>},
DEXModule: module_dex::{Pallet, Storage, Call, Event<T>, Config<T>},
}
);
pub type Extrinsic = TestXt<Call, ()>;
impl<LocalCall> frame_system::offchain::SendTransactionTypes<LocalCall> for Runtime
where
Call: From<LocalCall>,
{
type OverarchingCall = Call;
type Extrinsic = Extrinsic;
}
pub struct ExtBuilder {
balances: Vec<(AccountId, CurrencyId, Balance)>,
}
impl Default for ExtBuilder {
fn default() -> Self {
Self {
balances: vec![
(ALICE, AUSD, 1000),
(BOB, AUSD, 1000),
(CAROL, AUSD, 1000),
(ALICE, BTC, 1000),
(BOB, BTC, 1000),
(CAROL, BTC, 1000),
(ALICE, DOT, 1000),
(BOB, DOT, 1000),
(CAROL, DOT, 1000),
],
}
}
}
impl ExtBuilder {
pub fn build(self) -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default()
.build_storage::<Runtime>()
.unwrap();
orml_tokens::GenesisConfig::<Runtime> {
balances: self.balances,
}
.assimilate_storage(&mut t)
.unwrap();
module_dex::GenesisConfig::<Runtime> {
initial_listing_trading_pairs: vec![],
initial_enabled_trading_pairs: EnabledTradingPairs::get(),
initial_added_liquidity_pools: vec![],
}
.assimilate_storage(&mut t)
.unwrap();
t.into()
}
pub fn lots_of_accounts() -> Self {
let mut balances = Vec::new();
for i in 0..1001 {
let account_id: AccountId = i;
balances.push((account_id, BTC, 1000));
}
Self { balances }
}
}