-
Notifications
You must be signed in to change notification settings - Fork 653
/
Copy pathmock.rs
271 lines (234 loc) · 7.65 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
// This file is part of Acala.
// Copyright (C) 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 Aggregated DEX module.
#![cfg(test)]
use super::*;
use frame_support::{
match_types, ord_parameter_types, parameter_types,
traits::{ConstU128, ConstU32, ConstU64, Everything, Nothing},
PalletId,
};
use frame_system::EnsureSignedBy;
use orml_tokens::ConvertBalance;
pub use orml_traits::{parameter_type_with_key, MultiCurrency};
use primitives::{Amount, TokenSymbol, TradingPair};
use sp_runtime::{
testing::{Header, H256},
traits::{Bounded, IdentityLookup},
AccountId32, FixedPointNumber,
};
pub use support::{ExchangeRate, RebasedStableAsset};
pub type AccountId = AccountId32;
pub type BlockNumber = u64;
mod aggregated_dex {
pub use super::super::*;
}
pub const ALICE: AccountId = AccountId32::new([1u8; 32]);
pub const BOB: AccountId = AccountId32::new([2u8; 32]);
pub const AUSD: CurrencyId = CurrencyId::Token(TokenSymbol::AUSD);
pub const DOT: CurrencyId = CurrencyId::Token(TokenSymbol::DOT);
pub const LDOT: CurrencyId = CurrencyId::Token(TokenSymbol::LDOT);
pub const STABLE_ASSET: CurrencyId = CurrencyId::StableAssetPoolToken(0);
impl frame_system::Config for Runtime {
type BaseCallFilter = Everything;
type BlockWeights = ();
type BlockLength = ();
type Origin = Origin;
type Call = Call;
type Index = u64;
type BlockNumber = BlockNumber;
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 DbWeight = ();
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
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 = ();
}
ord_parameter_types! {
pub const Admin: AccountId = BOB;
}
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![];
}
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<Admin, AccountId>;
type ExtendedProvisioningBlocks = ConstU64<0>;
type OnLiquidityPoolUpdated = ();
}
pub struct EnsurePoolAssetId;
impl nutsfinance_stable_asset::traits::ValidateAssetId<CurrencyId> for EnsurePoolAssetId {
fn validate(currency_id: CurrencyId) -> bool {
matches!(currency_id, CurrencyId::StableAssetPoolToken(_))
}
}
pub struct ConvertBalanceHoma;
impl ConvertBalance<Balance, Balance> for ConvertBalanceHoma {
type AssetId = CurrencyId;
fn convert_balance(balance: Balance, asset_id: CurrencyId) -> Balance {
match asset_id {
LDOT => ExchangeRate::saturating_from_rational(1, 10)
.checked_mul_int(balance)
.unwrap_or(Bounded::max_value()),
_ => balance,
}
}
fn convert_balance_back(balance: Balance, asset_id: CurrencyId) -> Balance {
match asset_id {
LDOT => ExchangeRate::saturating_from_rational(10, 1)
.checked_mul_int(balance)
.unwrap_or(Bounded::max_value()),
_ => balance,
}
}
}
match_types! {
pub type IsLiquidToken: impl Contains<CurrencyId> = {
CurrencyId::Token(TokenSymbol::LDOT)
};
}
type RebaseTokens = orml_tokens::Combiner<
AccountId,
IsLiquidToken,
orml_tokens::Mapper<AccountId, Tokens, ConvertBalanceHoma, Balance, GetLiquidCurrencyId>,
Tokens,
>;
parameter_types! {
pub const StableAssetPalletId: PalletId = PalletId(*b"nuts/sta");
}
impl nutsfinance_stable_asset::Config for Runtime {
type Event = Event;
type AssetId = CurrencyId;
type Balance = Balance;
type Assets = RebaseTokens;
type PalletId = StableAssetPalletId;
type AtLeast64BitUnsigned = u128;
type FeePrecision = ConstU128<10_000_000_000>; // 10 decimals
type APrecision = ConstU128<100>; // 2 decimals
type PoolAssetLimit = ConstU32<5>;
type SwapExactOverAmount = ConstU128<100>;
type WeightInfo = ();
type ListingOrigin = EnsureSignedBy<Admin, AccountId>;
type EnsurePoolAssetId = EnsurePoolAssetId;
}
parameter_types! {
pub static DexSwapJointList: Vec<Vec<CurrencyId>> = vec![];
pub const GetLiquidCurrencyId: CurrencyId = LDOT;
}
impl Config for Runtime {
type DEX = Dex;
type StableAsset = StableAssetWrapper;
type GovernanceOrigin = EnsureSignedBy<Admin, AccountId>;
type DexSwapJointList = DexSwapJointList;
type SwapPathLimit = ConstU32<3>;
type WeightInfo = ();
}
pub type StableAssetWrapper =
RebasedStableAsset<StableAsset, ConvertBalanceHoma, RebasedStableAssetErrorConvertor<Runtime>>;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Runtime>;
type Block = frame_system::mocking::MockBlock<Runtime>;
frame_support::construct_runtime!(
pub enum Runtime where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
AggregatedDex: aggregated_dex::{Pallet, Call, Storage},
Dex: module_dex::{Pallet, Call, Storage, Config<T>, Event<T>},
Tokens: orml_tokens::{Pallet, Storage, Event<T>, Config<T>},
StableAsset: nutsfinance_stable_asset::{Pallet, Call, Storage, Event<T>},
}
);
pub type Extrinsic = sp_runtime::testing::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, DOT, 100_000_000_000),
(BOB, AUSD, 1_000_000_000_000_000_000),
(BOB, DOT, 1_000_000_000_000_000_000),
(BOB, LDOT, 1_000_000_000_000_000_000),
],
}
}
}
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();
t.into()
}
}