Skip to content

Commit 663b7bb

Browse files
committed
Add subnet and staking helper precompile functions to test e2e
1 parent 8aa9430 commit 663b7bb

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

runtime/src/precompiles/staking.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//
2727

2828
use pallet_evm::BalanceConverter;
29-
use pallet_evm::{ExitError, PrecompileFailure, PrecompileHandle, PrecompileResult};
29+
use pallet_evm::{ExitError, ExitSucceed, PrecompileOutput, PrecompileFailure, PrecompileHandle, PrecompileResult};
3030
use sp_core::U256;
3131
use sp_runtime::traits::UniqueSaturatedInto;
3232

@@ -54,11 +54,39 @@ impl StakingPrecompile {
5454
id if id == get_method_id("removeStake(bytes32,uint256,uint16)") => {
5555
Self::remove_stake(handle, &method_input)
5656
}
57+
id if id == get_method_id("getStake(bytes32,bytes32,uint16)") => {
58+
Self::get_stake(&method_input)
59+
}
5760
_ => Err(PrecompileFailure::Error {
5861
exit_status: ExitError::InvalidRange,
5962
}),
6063
}
6164
}
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+
}
6290

6391
fn add_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
6492
let hotkey = Self::parse_hotkey(data)?.into();
@@ -106,4 +134,5 @@ impl StakingPrecompile {
106134
hotkey.copy_from_slice(get_slice(data, 0, 32)?);
107135
Ok(hotkey)
108136
}
137+
109138
}

runtime/src/precompiles/subnet.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use crate::precompiles::{dispatch, get_method_id, get_slice};
22
use crate::{ Runtime, RuntimeCall};
3-
use pallet_evm::{AddressMapping, ExitError, PrecompileFailure, PrecompileHandle, PrecompileResult, HashedAddressMapping };
3+
use pallet_evm::{AddressMapping, ExitError, ExitSucceed, PrecompileFailure, PrecompileOutput, PrecompileHandle, PrecompileResult, HashedAddressMapping };
44
use sp_std::vec;
55
use sp_runtime::AccountId32;
6+
use sp_core::{ U256};
7+
68
use sp_runtime::traits::BlakeTwo256;
79

810
pub const SUBNET_PRECOMPILE_INDEX: u64 = 2051;
@@ -38,6 +40,9 @@ impl SubnetPrecompile {
3840
id if id == get_method_id("registerNetwork()") => {
3941
Self::register_network(handle, &[0_u8; 0])
4042
}
43+
id if id == get_method_id("getTempo(uint16)") => {
44+
Self::get_tempo(&method_input)
45+
}
4146
_ => Err(PrecompileFailure::Error {
4247
exit_status: ExitError::InvalidRange,
4348
}),
@@ -60,6 +65,18 @@ impl SubnetPrecompile {
6065
dispatch(handle, call, STAKING_CONTRACT_ADDRESS)
6166
}
6267

68+
fn get_tempo(data: &[u8]) -> PrecompileResult {
69+
let netuid = Self::parse_netuid(data)?;
70+
let tempo = pallet_subtensor::Pallet::<Runtime>::get_tempo(netuid);
71+
let result_u256 = U256::from(tempo);
72+
let mut result = [0_u8; 32];
73+
U256::to_big_endian(&result_u256, &mut result);
74+
Ok(PrecompileOutput {
75+
exit_status: ExitSucceed::Returned,
76+
output: result.into(),
77+
})
78+
}
79+
6380
fn parse_netuid(data: &[u8]) -> Result<u16, PrecompileFailure> {
6481
if data.len() < 32 {
6582
return Err(PrecompileFailure::Error {

0 commit comments

Comments
 (0)