11use sp_core:: { bounded:: BoundedVec , ConstU32 } ;
22use xcm:: v5:: prelude:: * ;
33
4- pub trait Parse {
5- /// Returns the "chain" location part. It could be parent, sibling
6- /// parachain, or child parachain.
7- fn chain_part ( & self ) -> Option < Location > ;
8- /// Returns "non-chain" location part.
9- fn non_chain_part ( & self ) -> Option < Location > ;
10- }
11-
12- fn is_chain_junction ( junction : Option < & Junction > ) -> bool {
13- matches ! ( junction, Some ( Parachain ( _) ) )
14- }
15-
16- impl Parse for Location {
17- fn chain_part ( & self ) -> Option < Location > {
18- match ( self . parents , self . first_interior ( ) ) {
19- // sibling parachain
20- ( 1 , Some ( Parachain ( id) ) ) => Some ( Location :: new ( 1 , [ Parachain ( * id) ] ) ) ,
21- // parent
22- ( 1 , _) => Some ( Location :: parent ( ) ) ,
23- // children parachain
24- ( 0 , Some ( Parachain ( id) ) ) => Some ( Location :: new ( 0 , [ Parachain ( * id) ] ) ) ,
25- _ => None ,
26- }
27- }
28-
29- fn non_chain_part ( & self ) -> Option < Location > {
30- let mut junctions = self . interior ( ) . clone ( ) ;
31- while is_chain_junction ( junctions. first ( ) ) {
32- let _ = junctions. take_first ( ) ;
33- }
34-
35- if junctions != Here {
36- Some ( Location :: new ( 0 , junctions) )
37- } else {
38- None
39- }
40- }
41- }
4+ pub const ASSET_HUB_ID : u32 = 1000 ;
425
436pub trait Reserve {
447 /// Returns assets reserve location.
458 fn reserve ( asset : & Asset ) -> Option < Location > ;
469}
4710
48- // Provide reserve in absolute path view
49- pub struct AbsoluteReserveProvider ;
50-
51- impl Reserve for AbsoluteReserveProvider {
52- fn reserve ( asset : & Asset ) -> Option < Location > {
53- let AssetId ( location) = & asset. id ;
54- location. chain_part ( )
55- }
56- }
57-
58- // Provide reserve in relative path view
59- // Self tokens are represeneted as Here
60- pub struct RelativeReserveProvider ;
61-
62- impl Reserve for RelativeReserveProvider {
63- fn reserve ( asset : & Asset ) -> Option < Location > {
64- let AssetId ( location) = & asset. id ;
65- if location. parents == 0 && !is_chain_junction ( location. first_interior ( ) ) {
66- Some ( Location :: here ( ) )
67- } else {
68- location. chain_part ( )
69- }
70- }
71- }
72-
7311pub trait RelativeLocations {
7412 fn sibling_parachain_general_key ( para_id : u32 , general_key : BoundedVec < u8 , ConstU32 < 32 > > ) -> Location ;
7513}
@@ -79,93 +17,3 @@ impl RelativeLocations for Location {
7917 Location :: new ( 1 , [ Parachain ( para_id) , general_key. as_bounded_slice ( ) . into ( ) ] )
8018 }
8119}
82-
83- #[ cfg( test) ]
84- mod tests {
85- use super :: * ;
86-
87- const PARACHAIN : Junction = Parachain ( 1 ) ;
88- const GENERAL_INDEX : Junction = GeneralIndex ( 1 ) ;
89-
90- fn concrete_fungible ( id : Location ) -> Asset {
91- ( id, 1 ) . into ( )
92- }
93-
94- #[ test]
95- fn parent_as_reserve_chain ( ) {
96- assert_eq ! (
97- AbsoluteReserveProvider :: reserve( & concrete_fungible( Location :: new( 1 , [ GENERAL_INDEX ] ) ) ) ,
98- Some ( Location :: parent( ) )
99- ) ;
100- assert_eq ! (
101- RelativeReserveProvider :: reserve( & concrete_fungible( Location :: new( 1 , [ GENERAL_INDEX ] ) ) ) ,
102- Some ( Location :: parent( ) )
103- ) ;
104- }
105-
106- #[ test]
107- fn sibling_parachain_as_reserve_chain ( ) {
108- assert_eq ! (
109- AbsoluteReserveProvider :: reserve( & concrete_fungible( Location :: new( 1 , [ PARACHAIN , GENERAL_INDEX ] ) ) ) ,
110- Some ( Location :: new( 1 , [ PARACHAIN ] ) )
111- ) ;
112- assert_eq ! (
113- RelativeReserveProvider :: reserve( & concrete_fungible( Location :: new( 1 , [ PARACHAIN , GENERAL_INDEX ] ) ) ) ,
114- Some ( Location :: new( 1 , [ PARACHAIN ] ) )
115- ) ;
116- }
117-
118- #[ test]
119- fn child_parachain_as_reserve_chain ( ) {
120- assert_eq ! (
121- AbsoluteReserveProvider :: reserve( & concrete_fungible( Location :: new( 0 , [ PARACHAIN , GENERAL_INDEX ] ) ) ) ,
122- Some ( PARACHAIN . into( ) )
123- ) ;
124- assert_eq ! (
125- RelativeReserveProvider :: reserve( & concrete_fungible( Location :: new( 0 , [ PARACHAIN , GENERAL_INDEX ] ) ) ) ,
126- Some ( PARACHAIN . into( ) )
127- ) ;
128- }
129-
130- #[ test]
131- fn no_reserve_chain_for_absolute_self_for_relative ( ) {
132- assert_eq ! (
133- AbsoluteReserveProvider :: reserve( & concrete_fungible( Location :: new(
134- 0 ,
135- [ Junction :: from( BoundedVec :: try_from( b"DOT" . to_vec( ) ) . unwrap( ) ) ]
136- ) ) ) ,
137- None
138- ) ;
139- assert_eq ! (
140- RelativeReserveProvider :: reserve( & concrete_fungible( Location :: new(
141- 0 ,
142- [ Junction :: from( BoundedVec :: try_from( b"DOT" . to_vec( ) ) . unwrap( ) ) ]
143- ) ) ) ,
144- Some ( Location :: here( ) )
145- ) ;
146- }
147-
148- #[ test]
149- fn non_chain_part_works ( ) {
150- assert_eq ! ( Location :: parent( ) . non_chain_part( ) , None ) ;
151- assert_eq ! ( Location :: new( 1 , [ PARACHAIN ] ) . non_chain_part( ) , None ) ;
152- assert_eq ! ( Location :: new( 0 , [ PARACHAIN ] ) . non_chain_part( ) , None ) ;
153-
154- assert_eq ! (
155- Location :: new( 1 , [ GENERAL_INDEX ] ) . non_chain_part( ) ,
156- Some ( GENERAL_INDEX . into( ) )
157- ) ;
158- assert_eq ! (
159- Location :: new( 1 , [ GENERAL_INDEX , GENERAL_INDEX ] ) . non_chain_part( ) ,
160- Some ( ( GENERAL_INDEX , GENERAL_INDEX ) . into( ) )
161- ) ;
162- assert_eq ! (
163- Location :: new( 1 , [ PARACHAIN , GENERAL_INDEX ] ) . non_chain_part( ) ,
164- Some ( GENERAL_INDEX . into( ) )
165- ) ;
166- assert_eq ! (
167- Location :: new( 0 , [ PARACHAIN , GENERAL_INDEX ] ) . non_chain_part( ) ,
168- Some ( GENERAL_INDEX . into( ) )
169- ) ;
170- }
171- }
0 commit comments