snowbridge_test_utils/
mock_converter.rs1use codec::Encode;
5use frame_support::sp_runtime::traits::MaybeConvert;
6use snowbridge_core::TokenIdOf;
7use sp_core::H256;
8use std::{cell::RefCell, collections::HashMap};
9use xcm::{
10 latest::InteriorLocation,
11 prelude::{Location, Reanchorable},
12};
13use xcm_executor::traits::ConvertLocation;
14
15thread_local! {
16 pub static IDENTIFIER_TO_LOCATION: RefCell<HashMap<H256, Location>> = RefCell::new(HashMap::new());
17 pub static LOCATION_TO_IDENTIFIER: RefCell<HashMap<Vec<u8>, H256>> = RefCell::new(HashMap::new());
18}
19
20pub fn add_location_override(location: Location, ethereum: Location, bh_context: InteriorLocation) {
21 let (token_id, reanchored_location) = reanchor_to_ethereum(location, ethereum, bh_context);
22 IDENTIFIER_TO_LOCATION.with(|b| b.borrow_mut().insert(token_id, reanchored_location.clone()));
23 LOCATION_TO_IDENTIFIER.with(|b| b.borrow_mut().insert(reanchored_location.encode(), token_id));
24}
25
26pub fn reanchor_to_ethereum(
27 location: Location,
28 ethereum: Location,
29 bh_context: InteriorLocation,
30) -> (H256, Location) {
31 let mut reanchored_lol = location.clone();
32 let _ = reanchored_lol.reanchor(ðereum, &bh_context);
33 let token_id = TokenIdOf::convert_location(&reanchored_lol).unwrap();
34 (token_id, reanchored_lol)
35}
36
37pub struct LocationIdConvert;
38impl MaybeConvert<H256, Location> for LocationIdConvert {
39 fn maybe_convert(id: H256) -> Option<Location> {
40 IDENTIFIER_TO_LOCATION.with(|b| b.borrow().get(&id).and_then(|l| Option::from(l.clone())))
41 }
42}