Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add netuid argument #991

Open
wants to merge 8 commits into
base: feat/rao-devnet-ready-2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions runtime/src/precompiles/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,14 @@ impl StakingPrecompile {
<Runtime as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount)
.ok_or(ExitError::OutOfFund)?;

let netuid_vec = data.get(56..64).ok_or(PrecompileFailure::Error {
exit_status: ExitError::InvalidRange,
})?;
let netuid = Self::parse_netuid(netuid_vec)?;
// Create the add_stake call
let call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::<Runtime>::add_stake {
hotkey,
// TODO update contract to add netuid
netuid: 1,
netuid,
amount_staked: amount_sub.unique_saturated_into(),
});
// Dispatch the add_stake call
Expand All @@ -96,10 +99,14 @@ impl StakingPrecompile {
<Runtime as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount)
.ok_or(ExitError::OutOfFund)?;

let netuid_vec = data.get(88..96).ok_or(PrecompileFailure::Error {
exit_status: ExitError::InvalidRange,
})?;
let netuid = Self::parse_netuid(netuid_vec)?;

let call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::<Runtime>::remove_stake {
hotkey,
// TODO update contract to add netuid
netuid: 1,
netuid,
amount_unstaked: amount_sub.unique_saturated_into(),
});
Self::dispatch(handle, call)
Expand All @@ -116,6 +123,26 @@ impl StakingPrecompile {
Ok(hotkey)
}

fn parse_netuid(data: &[u8]) -> Result<u16, PrecompileFailure> {
let mut bytes = [0_u8; 8];
bytes.copy_from_slice(data.get(0..8).ok_or(ExitError::InvalidRange)?);

let netuid = U256::from_big_endian(&bytes);

let u16_max_u256 = U256::from(u16::MAX);

if netuid > u16_max_u256 {
return Err(PrecompileFailure::Error {
exit_status: ExitError::InvalidRange,
});
}
let mut two_bytes = [0_u8; 2];
two_bytes.copy_from_slice(&bytes[6..8]);
let result: u16 = u16::from_be_bytes(two_bytes);

Ok(result)
}

fn dispatch(handle: &mut impl PrecompileHandle, call: RuntimeCall) -> PrecompileResult {
let account_id =
<HashedAddressMapping<BlakeTwo256> as AddressMapping<AccountId32>>::into_account_id(
Expand Down
Loading