@@ -6,9 +6,8 @@ use core::marker::PhantomData;
6
6
use frame_support:: dispatch:: { GetDispatchInfo , Pays } ;
7
7
8
8
use pallet_evm:: {
9
- AddressMapping , BalanceConverter , ExitError , ExitSucceed , GasWeightMapping ,
10
- HashedAddressMapping , IsPrecompileResult , Precompile , PrecompileFailure , PrecompileHandle ,
11
- PrecompileOutput , PrecompileResult , PrecompileSet ,
9
+ ExitError , ExitSucceed , GasWeightMapping , IsPrecompileResult , Precompile , PrecompileFailure ,
10
+ PrecompileHandle , PrecompileOutput , PrecompileResult , PrecompileSet ,
12
11
} ;
13
12
use pallet_evm_precompile_modexp:: Modexp ;
14
13
use pallet_evm_precompile_sha3fips:: Sha3FIPS256 ;
@@ -20,10 +19,6 @@ use crate::{Runtime, RuntimeCall};
20
19
21
20
use frame_system:: RawOrigin ;
22
21
23
- use sp_core:: crypto:: Ss58Codec ;
24
- use sp_core:: U256 ;
25
- use sp_runtime:: traits:: { BlakeTwo256 , UniqueSaturatedInto } ;
26
-
27
22
use sp_std:: vec;
28
23
29
24
// Include custom precompiles
@@ -147,84 +142,6 @@ pub fn get_slice(data: &[u8], from: usize, to: usize) -> Result<&[u8], Precompil
147
142
}
148
143
}
149
144
150
- /// The function return the token to smart contract
151
- fn transfer_back_to_caller (
152
- smart_contract_address : & str ,
153
- account_id : & AccountId32 ,
154
- amount : U256 ,
155
- ) -> Result < ( ) , PrecompileFailure > {
156
- // this is staking smart contract's(0x0000000000000000000000000000000000000801) sr25519 address
157
- let smart_contract_account_id = match AccountId32 :: from_ss58check ( smart_contract_address) {
158
- // match AccountId32::from_ss58check("5CwnBK9Ack1mhznmCnwiibCNQc174pYQVktYW3ayRpLm4K2X") {
159
- Ok ( addr) => addr,
160
- Err ( _) => {
161
- return Err ( PrecompileFailure :: Error {
162
- exit_status : ExitError :: Other ( "Invalid SS58 address" . into ( ) ) ,
163
- } ) ;
164
- }
165
- } ;
166
- let amount_sub =
167
- <Runtime as pallet_evm:: Config >:: BalanceConverter :: into_substrate_balance ( amount)
168
- . ok_or ( ExitError :: OutOfFund ) ?;
169
-
170
- // Create a transfer call from the smart contract to the caller
171
- let transfer_call =
172
- RuntimeCall :: Balances ( pallet_balances:: Call :: < Runtime > :: transfer_allow_death {
173
- dest : account_id. clone ( ) . into ( ) ,
174
- value : amount_sub. unique_saturated_into ( ) ,
175
- } ) ;
176
-
177
- // Execute the transfer
178
- let transfer_result =
179
- transfer_call. dispatch ( RawOrigin :: Signed ( smart_contract_account_id) . into ( ) ) ;
180
-
181
- if let Err ( dispatch_error) = transfer_result {
182
- log:: error!(
183
- "Transfer back to caller failed. Error: {:?}" ,
184
- dispatch_error
185
- ) ;
186
- return Err ( PrecompileFailure :: Error {
187
- exit_status : ExitError :: Other ( "Transfer back to caller failed" . into ( ) ) ,
188
- } ) ;
189
- }
190
-
191
- Ok ( ( ) )
192
- }
193
-
194
- fn dispatch (
195
- handle : & mut impl PrecompileHandle ,
196
- call : RuntimeCall ,
197
- smart_contract_address : & str ,
198
- ) -> PrecompileResult {
199
- let account_id =
200
- <HashedAddressMapping < BlakeTwo256 > as AddressMapping < AccountId32 > >:: into_account_id (
201
- handle. context ( ) . caller ,
202
- ) ;
203
-
204
- // Transfer the amount back to the caller before executing the staking operation
205
- // let caller = handle.context().caller;
206
- let amount = handle. context ( ) . apparent_value ;
207
-
208
- if !amount. is_zero ( ) {
209
- transfer_back_to_caller ( smart_contract_address, & account_id, amount) ?;
210
- }
211
-
212
- let result = call. dispatch ( RawOrigin :: Signed ( account_id. clone ( ) ) . into ( ) ) ;
213
- match & result {
214
- Ok ( post_info) => log:: info!( "Dispatch succeeded. Post info: {:?}" , post_info) ,
215
- Err ( dispatch_error) => log:: error!( "Dispatch failed. Error: {:?}" , dispatch_error) ,
216
- }
217
- match result {
218
- Ok ( _) => Ok ( PrecompileOutput {
219
- exit_status : ExitSucceed :: Returned ,
220
- output : vec ! [ ] ,
221
- } ) ,
222
- Err ( _) => Err ( PrecompileFailure :: Error {
223
- exit_status : ExitError :: Other ( "Subtensor call failed" . into ( ) ) ,
224
- } ) ,
225
- }
226
- }
227
-
228
145
pub fn get_pubkey ( data : & [ u8 ] ) -> Result < ( AccountId32 , vec:: Vec < u8 > ) , PrecompileFailure > {
229
146
let mut pubkey = [ 0u8 ; 32 ] ;
230
147
pubkey. copy_from_slice ( get_slice ( data, 0 , 32 ) ?) ;
@@ -235,6 +152,26 @@ pub fn get_pubkey(data: &[u8]) -> Result<(AccountId32, vec::Vec<u8>), Precompile
235
152
. map_or_else ( vec:: Vec :: new, |slice| slice. to_vec ( ) ) ,
236
153
) )
237
154
}
155
+
156
+ fn parse_netuid ( data : & [ u8 ] , offset : usize ) -> Result < u16 , PrecompileFailure > {
157
+ if data. len ( ) < offset + 2 {
158
+ return Err ( PrecompileFailure :: Error {
159
+ exit_status : ExitError :: InvalidRange ,
160
+ } ) ;
161
+ }
162
+
163
+ let mut netuid_bytes = [ 0u8 ; 2 ] ;
164
+ netuid_bytes. copy_from_slice ( get_slice ( data, offset, offset + 2 ) ?) ;
165
+ let netuid: u16 = netuid_bytes[ 1 ] as u16 | ( ( netuid_bytes[ 0 ] as u16 ) << 8u16 ) ;
166
+
167
+ Ok ( netuid)
168
+ }
169
+
170
+ fn contract_to_origin ( contract : & [ u8 ; 32 ] ) -> Result < RawOrigin < AccountId32 > , PrecompileFailure > {
171
+ let ( account_id, _) = get_pubkey ( contract) ?;
172
+ Ok ( RawOrigin :: Signed ( account_id) )
173
+ }
174
+
238
175
/// Dispatches a runtime call, but also checks and records the gas costs.
239
176
fn try_dispatch_runtime_call (
240
177
handle : & mut impl PrecompileHandle ,
0 commit comments