-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlib.rs
More file actions
250 lines (223 loc) · 6.64 KB
/
lib.rs
File metadata and controls
250 lines (223 loc) · 6.64 KB
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
// The Licensed Work is (c) 2022 Sygma
// SPDX-License-Identifier: LGPL-3.0-only
#![cfg_attr(not(feature = "std"), no_std)]
pub use self::pallet::*;
#[cfg(test)]
mod mock;
#[allow(unused_variables)]
#[allow(clippy::large_enum_variant)]
#[frame_support::pallet]
pub mod pallet {
use frame_support::{dispatch::DispatchResult, pallet_prelude::*, traits::StorageVersion};
use frame_system::pallet_prelude::*;
use sygma_traits::{DomainID, FeeHandler};
use xcm::latest::AssetId;
const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);
/// Mapping fungible asset id to corresponding fee amount
#[pallet::storage]
#[pallet::getter(fn asset_fees)]
pub type AssetFees<T: Config> = StorageMap<_, Twox64Concat, (DomainID, AssetId), u128>;
#[pallet::pallet]
#[pallet::generate_store(pub (super) trait Store)]
#[pallet::storage_version(STORAGE_VERSION)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config + sygma_access_segregator::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Origin used to administer the pallet
type BridgeCommitteeOrigin: EnsureOrigin<Self::RuntimeOrigin>;
/// Current pallet index defined in runtime
type PalletIndex: Get<u8>;
}
#[pallet::event]
#[pallet::generate_deposit(pub (super) fn deposit_event)]
pub enum Event<T: Config> {
/// Fee set for a specific asset
/// args: [domain, asset, amount]
FeeSet { domain: DomainID, asset: AssetId, amount: u128 },
}
#[pallet::error]
pub enum Error<T> {
/// Function unimplemented
Unimplemented,
/// Account has not gained access permission
AccessDenied,
}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Set bridge fee for a specific asset
#[pallet::call_index(0)]
#[pallet::weight(195_000_000)]
pub fn set_fee(
origin: OriginFor<T>,
domain: DomainID,
asset: AssetId,
amount: u128,
) -> DispatchResult {
if <T as Config>::BridgeCommitteeOrigin::ensure_origin(origin.clone()).is_err() {
// Ensure bridge committee or the account that has permisson to set fee
let who = ensure_signed(origin)?;
ensure!(
<sygma_access_segregator::pallet::Pallet<T>>::has_access(
<T as Config>::PalletIndex::get(),
b"set_fee".to_vec(),
who
),
Error::<T>::AccessDenied
);
}
// Update asset fee
AssetFees::<T>::insert((domain, &asset), amount);
// Emit FeeSet event
Self::deposit_event(Event::FeeSet { domain, asset, amount });
Ok(())
}
}
impl<T: Config> FeeHandler for Pallet<T> {
fn get_fee(domain: DomainID, asset: &AssetId) -> Option<u128> {
AssetFees::<T>::get((domain, asset))
}
}
#[cfg(test)]
mod test {
use crate as basic_fee_handler;
use crate::{AssetFees, Event as BasicFeeHandlerEvent};
use basic_fee_handler::mock::{
assert_events, new_test_ext, AccessSegregator, BasicFeeHandler, FeeHandlerPalletIndex,
RuntimeEvent as Event, RuntimeOrigin as Origin, Test, ALICE,
};
use frame_support::{assert_noop, assert_ok};
use sygma_traits::DomainID;
use xcm::latest::{prelude::*, MultiLocation};
#[test]
fn set_get_fee() {
new_test_ext().execute_with(|| {
let dest_domain_id: DomainID = 0;
let another_dest_domain_id: DomainID = 1;
let asset_id_a = Concrete(MultiLocation::new(1, Here));
let amount_a = 100u128;
let asset_id_b = Concrete(MultiLocation::new(2, Here));
let amount_b = 101u128;
// set fee 100 with assetId asset_id_a for one domain
assert_ok!(BasicFeeHandler::set_fee(
Origin::root(),
dest_domain_id,
asset_id_a.clone(),
amount_a
));
// set fee 200 with assetId asset_id_a for another domain
assert_ok!(BasicFeeHandler::set_fee(
Origin::root(),
another_dest_domain_id,
asset_id_a.clone(),
amount_a * 2
));
assert_eq!(
AssetFees::<Test>::get(&(dest_domain_id, asset_id_a.clone())).unwrap(),
amount_a
);
assert_eq!(
AssetFees::<Test>::get(&(another_dest_domain_id, asset_id_a.clone())).unwrap(),
amount_a * 2
);
// set fee 101 with assetId asset_id_b
assert_ok!(BasicFeeHandler::set_fee(
Origin::root(),
dest_domain_id,
asset_id_b.clone(),
amount_b
));
assert_eq!(
AssetFees::<Test>::get(&(dest_domain_id, asset_id_b.clone())).unwrap(),
amount_b
);
// fee of asset_id_a should not be equal to amount_b
assert_ne!(
AssetFees::<Test>::get(&(dest_domain_id, asset_id_a.clone())).unwrap(),
amount_b
);
// fee of asset_id_b should not be equal to amount_a
assert_ne!(
AssetFees::<Test>::get(&(dest_domain_id, asset_id_b.clone())).unwrap(),
amount_a
);
// permission test: unauthorized account should not be able to set fee
let unauthorized_account = Origin::from(Some(ALICE));
assert_noop!(
BasicFeeHandler::set_fee(
unauthorized_account,
dest_domain_id,
asset_id_a.clone(),
amount_a
),
basic_fee_handler::Error::<Test>::AccessDenied
);
assert_events(vec![
Event::BasicFeeHandler(BasicFeeHandlerEvent::FeeSet {
domain: dest_domain_id,
asset: asset_id_a.clone(),
amount: amount_a,
}),
Event::BasicFeeHandler(BasicFeeHandlerEvent::FeeSet {
domain: another_dest_domain_id,
asset: asset_id_a,
amount: amount_a * 2,
}),
Event::BasicFeeHandler(BasicFeeHandlerEvent::FeeSet {
domain: dest_domain_id,
asset: asset_id_b,
amount: amount_b,
}),
]);
})
}
#[test]
fn access_control() {
new_test_ext().execute_with(|| {
let dest_domain_id: DomainID = 0;
let asset_id = Concrete(MultiLocation::new(0, Here));
assert_ok!(BasicFeeHandler::set_fee(
Origin::root(),
dest_domain_id,
asset_id.clone(),
100
),);
assert_noop!(
BasicFeeHandler::set_fee(
Some(ALICE).into(),
dest_domain_id,
asset_id.clone(),
200
),
basic_fee_handler::Error::<Test>::AccessDenied
);
// (FeeHandlerPalletIndex:get(), b"set_fee") indicates extrinsic: `set_fee` of this
// pallet
assert!(!AccessSegregator::has_access(
FeeHandlerPalletIndex::get(),
b"set_fee".to_vec(),
ALICE
));
assert_ok!(AccessSegregator::grant_access(
Origin::root(),
FeeHandlerPalletIndex::get(),
b"set_fee".to_vec(),
ALICE
));
assert!(AccessSegregator::has_access(
FeeHandlerPalletIndex::get(),
b"set_fee".to_vec(),
ALICE
));
assert_ok!(BasicFeeHandler::set_fee(
Some(ALICE).into(),
dest_domain_id,
asset_id.clone(),
200
),);
assert_eq!(AssetFees::<Test>::get(&(dest_domain_id, asset_id)).unwrap(), 200);
})
}
}
}