|
26 | 26 | //
|
27 | 27 |
|
28 | 28 | use pallet_evm::BalanceConverter;
|
29 |
| -use pallet_evm::{ExitError, PrecompileFailure, PrecompileHandle, PrecompileResult}; |
| 29 | +use pallet_evm::{ExitError, ExitSucceed, PrecompileOutput, PrecompileFailure, PrecompileHandle, PrecompileResult}; |
30 | 30 | use sp_core::U256;
|
31 | 31 | use sp_runtime::traits::UniqueSaturatedInto;
|
32 | 32 |
|
@@ -54,11 +54,39 @@ impl StakingPrecompile {
|
54 | 54 | id if id == get_method_id("removeStake(bytes32,uint256,uint16)") => {
|
55 | 55 | Self::remove_stake(handle, &method_input)
|
56 | 56 | }
|
| 57 | + id if id == get_method_id("getStake(bytes32,bytes32,uint16)") => { |
| 58 | + Self::get_stake(&method_input) |
| 59 | + } |
57 | 60 | _ => Err(PrecompileFailure::Error {
|
58 | 61 | exit_status: ExitError::InvalidRange,
|
59 | 62 | }),
|
60 | 63 | }
|
61 | 64 | }
|
| 65 | + fn get_stake(data: &[u8]) -> PrecompileResult { |
| 66 | + let mut coldkey = [0u8; 32]; |
| 67 | + coldkey.copy_from_slice(get_slice(data, 0, 32)?); |
| 68 | + |
| 69 | + let mut hotkey = [0u8; 32]; |
| 70 | + hotkey.copy_from_slice(get_slice(data, 32, 64)?); |
| 71 | + |
| 72 | + let coldkey = coldkey.into(); |
| 73 | + let hotkey = hotkey.into(); |
| 74 | + |
| 75 | + let stake = pallet_subtensor::Pallet::<Runtime>::get_stake_for_coldkey_and_hotkey(&coldkey, &hotkey); |
| 76 | + |
| 77 | + let result_u256 = U256::from(stake); |
| 78 | + let amount_sub = |
| 79 | + <Runtime as pallet_evm::Config>::BalanceConverter::into_evm_balance(result_u256) |
| 80 | + .ok_or(ExitError::OutOfFund)?; |
| 81 | + |
| 82 | + let mut result = [0_u8; 32]; |
| 83 | + U256::to_big_endian(&amount_sub, &mut result); |
| 84 | + |
| 85 | + Ok(PrecompileOutput { |
| 86 | + exit_status: ExitSucceed::Returned, |
| 87 | + output: result.into(), |
| 88 | + }) |
| 89 | + } |
62 | 90 |
|
63 | 91 | fn add_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
|
64 | 92 | let hotkey = Self::parse_hotkey(data)?.into();
|
@@ -106,4 +134,5 @@ impl StakingPrecompile {
|
106 | 134 | hotkey.copy_from_slice(get_slice(data, 0, 32)?);
|
107 | 135 | Ok(hotkey)
|
108 | 136 | }
|
| 137 | + |
109 | 138 | }
|
0 commit comments