25
25
// - Precompile checks the result of do_remove_stake and, in case of a failure, reverts the transaction.
26
26
//
27
27
28
- use frame_system:: RawOrigin ;
29
- use pallet_evm:: { AddressMapping , BalanceConverter , HashedAddressMapping } ;
30
- use pallet_evm:: {
31
- ExitError , ExitSucceed , PrecompileFailure , PrecompileHandle , PrecompileOutput , PrecompileResult ,
32
- } ;
33
- use sp_core:: crypto:: Ss58Codec ;
28
+ use pallet_evm:: BalanceConverter ;
29
+ use pallet_evm:: { ExitError , PrecompileFailure , PrecompileHandle , PrecompileResult } ;
34
30
use sp_core:: U256 ;
35
- use sp_runtime:: traits:: Dispatchable ;
36
- use sp_runtime:: traits:: { BlakeTwo256 , UniqueSaturatedInto } ;
37
- use sp_runtime:: AccountId32 ;
31
+ use sp_runtime:: traits:: UniqueSaturatedInto ;
38
32
39
- use crate :: precompiles:: { get_method_id, get_slice} ;
33
+ use crate :: precompiles:: { dispatch , get_method_id, get_slice} ;
40
34
use sp_std:: vec;
41
35
42
36
use crate :: { Runtime , RuntimeCall } ;
@@ -78,7 +72,7 @@ impl StakingPrecompile {
78
72
amount_staked : amount_sub. unique_saturated_into ( ) ,
79
73
} ) ;
80
74
// Dispatch the add_stake call
81
- Self :: dispatch ( handle, call)
75
+ dispatch ( handle, call)
82
76
}
83
77
fn remove_stake ( handle : & mut impl PrecompileHandle , data : & [ u8 ] ) -> PrecompileResult {
84
78
let hotkey = Self :: parse_hotkey ( data) ?. into ( ) ;
@@ -98,7 +92,7 @@ impl StakingPrecompile {
98
92
hotkey,
99
93
amount_unstaked : amount_sub. unique_saturated_into ( ) ,
100
94
} ) ;
101
- Self :: dispatch ( handle, call)
95
+ dispatch ( handle, call)
102
96
}
103
97
104
98
fn parse_hotkey ( data : & [ u8 ] ) -> Result < [ u8 ; 32 ] , PrecompileFailure > {
@@ -112,75 +106,75 @@ impl StakingPrecompile {
112
106
Ok ( hotkey)
113
107
}
114
108
115
- fn dispatch ( handle : & mut impl PrecompileHandle , call : RuntimeCall ) -> PrecompileResult {
116
- let account_id =
117
- <HashedAddressMapping < BlakeTwo256 > as AddressMapping < AccountId32 > >:: into_account_id (
118
- handle. context ( ) . caller ,
119
- ) ;
120
-
121
- // Transfer the amount back to the caller before executing the staking operation
122
- // let caller = handle.context().caller;
123
- let amount = handle. context ( ) . apparent_value ;
124
-
125
- if !amount. is_zero ( ) {
126
- Self :: transfer_back_to_caller ( & account_id, amount) ?;
127
- }
128
-
129
- let result = call. dispatch ( RawOrigin :: Signed ( account_id. clone ( ) ) . into ( ) ) ;
130
- match & result {
131
- Ok ( post_info) => log:: info!( "Dispatch succeeded. Post info: {:?}" , post_info) ,
132
- Err ( dispatch_error) => log:: error!( "Dispatch failed. Error: {:?}" , dispatch_error) ,
133
- }
134
- match result {
135
- Ok ( _) => Ok ( PrecompileOutput {
136
- exit_status : ExitSucceed :: Returned ,
137
- output : vec ! [ ] ,
138
- } ) ,
139
- Err ( _) => Err ( PrecompileFailure :: Error {
140
- exit_status : ExitError :: Other ( "Subtensor call failed" . into ( ) ) ,
141
- } ) ,
142
- }
143
- }
144
-
145
- fn transfer_back_to_caller (
146
- account_id : & AccountId32 ,
147
- amount : U256 ,
148
- ) -> Result < ( ) , PrecompileFailure > {
149
- // this is staking smart contract's(0x0000000000000000000000000000000000000801) sr25519 address
150
- let smart_contract_account_id =
151
- match AccountId32 :: from_ss58check ( "5CwnBK9Ack1mhznmCnwiibCNQc174pYQVktYW3ayRpLm4K2X" ) {
152
- Ok ( addr) => addr,
153
- Err ( _) => {
154
- return Err ( PrecompileFailure :: Error {
155
- exit_status : ExitError :: Other ( "Invalid SS58 address" . into ( ) ) ,
156
- } ) ;
157
- }
158
- } ;
159
- let amount_sub =
160
- <Runtime as pallet_evm:: Config >:: BalanceConverter :: into_substrate_balance ( amount)
161
- . ok_or ( ExitError :: OutOfFund ) ?;
162
-
163
- // Create a transfer call from the smart contract to the caller
164
- let transfer_call =
165
- RuntimeCall :: Balances ( pallet_balances:: Call :: < Runtime > :: transfer_allow_death {
166
- dest : account_id. clone ( ) . into ( ) ,
167
- value : amount_sub. unique_saturated_into ( ) ,
168
- } ) ;
169
-
170
- // Execute the transfer
171
- let transfer_result =
172
- transfer_call. dispatch ( RawOrigin :: Signed ( smart_contract_account_id) . into ( ) ) ;
173
-
174
- if let Err ( dispatch_error) = transfer_result {
175
- log:: error!(
176
- "Transfer back to caller failed. Error: {:?}" ,
177
- dispatch_error
178
- ) ;
179
- return Err ( PrecompileFailure :: Error {
180
- exit_status : ExitError :: Other ( "Transfer back to caller failed" . into ( ) ) ,
181
- } ) ;
182
- }
183
-
184
- Ok ( ( ) )
185
- }
109
+ // fn dispatch(handle: &mut impl PrecompileHandle, call: RuntimeCall) -> PrecompileResult {
110
+ // let account_id =
111
+ // <HashedAddressMapping<BlakeTwo256> as AddressMapping<AccountId32>>::into_account_id(
112
+ // handle.context().caller,
113
+ // );
114
+
115
+ // // Transfer the amount back to the caller before executing the staking operation
116
+ // // let caller = handle.context().caller;
117
+ // let amount = handle.context().apparent_value;
118
+
119
+ // if !amount.is_zero() {
120
+ // Self::transfer_back_to_caller(&account_id, amount)?;
121
+ // }
122
+
123
+ // let result = call.dispatch(RawOrigin::Signed(account_id.clone()).into());
124
+ // match &result {
125
+ // Ok(post_info) => log::info!("Dispatch succeeded. Post info: {:?}", post_info),
126
+ // Err(dispatch_error) => log::error!("Dispatch failed. Error: {:?}", dispatch_error),
127
+ // }
128
+ // match result {
129
+ // Ok(_) => Ok(PrecompileOutput {
130
+ // exit_status: ExitSucceed::Returned,
131
+ // output: vec![],
132
+ // }),
133
+ // Err(_) => Err(PrecompileFailure::Error {
134
+ // exit_status: ExitError::Other("Subtensor call failed".into()),
135
+ // }),
136
+ // }
137
+ // }
138
+
139
+ // fn transfer_back_to_caller(
140
+ // account_id: &AccountId32,
141
+ // amount: U256,
142
+ // ) -> Result<(), PrecompileFailure> {
143
+ // // this is staking smart contract's(0x0000000000000000000000000000000000000801) sr25519 address
144
+ // let smart_contract_account_id =
145
+ // match AccountId32::from_ss58check("5CwnBK9Ack1mhznmCnwiibCNQc174pYQVktYW3ayRpLm4K2X") {
146
+ // Ok(addr) => addr,
147
+ // Err(_) => {
148
+ // return Err(PrecompileFailure::Error {
149
+ // exit_status: ExitError::Other("Invalid SS58 address".into()),
150
+ // });
151
+ // }
152
+ // };
153
+ // let amount_sub =
154
+ // <Runtime as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount)
155
+ // .ok_or(ExitError::OutOfFund)?;
156
+
157
+ // // Create a transfer call from the smart contract to the caller
158
+ // let transfer_call =
159
+ // RuntimeCall::Balances(pallet_balances::Call::<Runtime>::transfer_allow_death {
160
+ // dest: account_id.clone().into(),
161
+ // value: amount_sub.unique_saturated_into(),
162
+ // });
163
+
164
+ // // Execute the transfer
165
+ // let transfer_result =
166
+ // transfer_call.dispatch(RawOrigin::Signed(smart_contract_account_id).into());
167
+
168
+ // if let Err(dispatch_error) = transfer_result {
169
+ // log::error!(
170
+ // "Transfer back to caller failed. Error: {:?}",
171
+ // dispatch_error
172
+ // );
173
+ // return Err(PrecompileFailure::Error {
174
+ // exit_status: ExitError::Other("Transfer back to caller failed".into()),
175
+ // });
176
+ // }
177
+
178
+ // Ok(())
179
+ // }
186
180
}
0 commit comments