@@ -33,10 +33,13 @@ use pallet_evm::{
33
33
use sp_core:: crypto:: Ss58Codec ;
34
34
use sp_core:: U256 ;
35
35
use sp_runtime:: traits:: Dispatchable ;
36
- use sp_runtime:: traits:: { BlakeTwo256 , UniqueSaturatedInto } ;
36
+ use sp_runtime:: traits:: { BlakeTwo256 , StaticLookup , UniqueSaturatedInto } ;
37
37
use sp_runtime:: AccountId32 ;
38
38
39
- use crate :: precompiles:: { get_method_id, get_slice} ;
39
+ use crate :: {
40
+ precompiles:: { get_method_id, get_slice} ,
41
+ ProxyType ,
42
+ } ;
40
43
use sp_std:: vec;
41
44
42
45
use crate :: { Runtime , RuntimeCall } ;
@@ -52,24 +55,25 @@ impl StakingPrecompile {
52
55
. get ( 4 ..)
53
56
. map_or_else ( vec:: Vec :: new, |slice| slice. to_vec ( ) ) ; // Avoiding borrowing conflicts
54
57
55
- match method_id {
56
- id if id == get_method_id ( "addStake(bytes32,uint256)" ) => {
57
- Self :: add_stake ( handle, & method_input)
58
- }
59
- id if id == get_method_id ( "removeStake(bytes32,uint256,uint256)" ) => {
60
- Self :: remove_stake ( handle, & method_input)
61
- }
62
- id if id == get_method_id ( "getStake(bytes32,bytes32,uint256)" ) => {
63
- Self :: get_stake ( & method_input)
64
- }
65
- _ => Err ( PrecompileFailure :: Error {
58
+ if method_id == get_method_id ( "addStake(bytes32,uint256)" ) {
59
+ Self :: add_stake ( handle, & method_input)
60
+ } else if method_id == get_method_id ( "removeStake(bytes32,uint256,uint256)" ) {
61
+ Self :: remove_stake ( handle, & method_input)
62
+ } else if method_id == get_method_id ( "getStake(bytes32,bytes32,uint256)" ) {
63
+ Self :: get_stake ( & method_input)
64
+ } else if method_id == get_method_id ( "addProxy(bytes32)" ) {
65
+ Self :: add_proxy ( handle, & method_input)
66
+ } else if method_id == get_method_id ( "removeProxy(bytes32)" ) {
67
+ Self :: remove_proxy ( handle, & method_input)
68
+ } else {
69
+ Err ( PrecompileFailure :: Error {
66
70
exit_status : ExitError :: InvalidRange ,
67
- } ) ,
71
+ } )
68
72
}
69
73
}
70
74
71
75
fn add_stake ( handle : & mut impl PrecompileHandle , data : & [ u8 ] ) -> PrecompileResult {
72
- let hotkey = Self :: parse_hotkey ( data) ?. into ( ) ;
76
+ let hotkey = Self :: parse_pub_key ( data) ?. into ( ) ;
73
77
let amount: U256 = handle. context ( ) . apparent_value ;
74
78
let netuid = Self :: parse_netuid ( data, 0x3E ) ?;
75
79
@@ -88,7 +92,7 @@ impl StakingPrecompile {
88
92
}
89
93
90
94
fn remove_stake ( handle : & mut impl PrecompileHandle , data : & [ u8 ] ) -> PrecompileResult {
91
- let hotkey = Self :: parse_hotkey ( data) ?. into ( ) ;
95
+ let hotkey = Self :: parse_pub_key ( data) ?. into ( ) ;
92
96
let netuid = Self :: parse_netuid ( data, 0x5E ) ?;
93
97
94
98
// We have to treat this as uint256 (because of Solidity ABI encoding rules, it pads uint64),
@@ -110,6 +114,30 @@ impl StakingPrecompile {
110
114
Self :: dispatch ( handle, call)
111
115
}
112
116
117
+ fn add_proxy ( handle : & mut impl PrecompileHandle , data : & [ u8 ] ) -> PrecompileResult {
118
+ let delegate = AccountId32 :: from ( Self :: parse_pub_key ( data) ?) ;
119
+ let delegate = <Runtime as frame_system:: Config >:: Lookup :: unlookup ( delegate) ;
120
+ let call = RuntimeCall :: Proxy ( pallet_proxy:: Call :: < Runtime > :: add_proxy {
121
+ delegate,
122
+ proxy_type : ProxyType :: Staking ,
123
+ delay : 0 ,
124
+ } ) ;
125
+
126
+ Self :: dispatch ( handle, call)
127
+ }
128
+
129
+ fn remove_proxy ( handle : & mut impl PrecompileHandle , data : & [ u8 ] ) -> PrecompileResult {
130
+ let delegate = AccountId32 :: from ( Self :: parse_pub_key ( data) ?) ;
131
+ let delegate = <Runtime as frame_system:: Config >:: Lookup :: unlookup ( delegate) ;
132
+ let call = RuntimeCall :: Proxy ( pallet_proxy:: Call :: < Runtime > :: remove_proxy {
133
+ delegate,
134
+ proxy_type : ProxyType :: Staking ,
135
+ delay : 0 ,
136
+ } ) ;
137
+
138
+ Self :: dispatch ( handle, call)
139
+ }
140
+
113
141
fn get_stake ( data : & [ u8 ] ) -> PrecompileResult {
114
142
let ( hotkey, coldkey) = Self :: parse_hotkey_coldkey ( data) ?;
115
143
let netuid = Self :: parse_netuid ( data, 0x5E ) ?;
@@ -149,15 +177,15 @@ impl StakingPrecompile {
149
177
Ok ( ( hotkey, coldkey) )
150
178
}
151
179
152
- fn parse_hotkey ( data : & [ u8 ] ) -> Result < [ u8 ; 32 ] , PrecompileFailure > {
180
+ fn parse_pub_key ( data : & [ u8 ] ) -> Result < [ u8 ; 32 ] , PrecompileFailure > {
153
181
if data. len ( ) < 32 {
154
182
return Err ( PrecompileFailure :: Error {
155
183
exit_status : ExitError :: InvalidRange ,
156
184
} ) ;
157
185
}
158
- let mut hotkey = [ 0u8 ; 32 ] ;
159
- hotkey . copy_from_slice ( get_slice ( data, 0 , 32 ) ?) ;
160
- Ok ( hotkey )
186
+ let mut pubkey = [ 0u8 ; 32 ] ;
187
+ pubkey . copy_from_slice ( get_slice ( data, 0 , 32 ) ?) ;
188
+ Ok ( pubkey )
161
189
}
162
190
163
191
fn parse_netuid ( data : & [ u8 ] , offset : usize ) -> Result < u16 , PrecompileFailure > {
0 commit comments