diff --git a/runtime/src/precompiles/staking.rs b/runtime/src/precompiles/staking.rs index daa95ba93..7aa674141 100644 --- a/runtime/src/precompiles/staking.rs +++ b/runtime/src/precompiles/staking.rs @@ -72,11 +72,15 @@ impl StakingPrecompile { ::BalanceConverter::into_substrate_balance(amount) .ok_or(ExitError::OutOfFund)?; + let netuid = + Self::parse_netuid(data.get(56..64).unwrap_or(Err(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + })?))?; + // Create the add_stake call let call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::::add_stake { hotkey, - // TODO update contract to add netuid - netuid: 1, + netuid, amount_staked: amount_sub.unique_saturated_into(), }); // Dispatch the add_stake call @@ -96,10 +100,14 @@ impl StakingPrecompile { ::BalanceConverter::into_substrate_balance(amount) .ok_or(ExitError::OutOfFund)?; + let netuid = + Self::parse_netuid(data.get(64..72).unwrap_or(Err(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + })?))?; + let call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::::remove_stake { hotkey, - // TODO update contract to add netuid - netuid: 1, + netuid, amount_unstaked: amount_sub.unique_saturated_into(), }); Self::dispatch(handle, call) @@ -116,6 +124,23 @@ impl StakingPrecompile { Ok(hotkey) } + fn parse_netuid(data: &[u8]) -> Result { + let netuid = data + .get(0..8) + .map(U256::from_big_endian) + .ok_or(ExitError::InvalidRange)?; + + let u16_max_u256 = U256::from(u16::MAX); + if netuid > u16_max_u256 { + // if netuid.as_u128() > u16::MAX as u128 { + return Err(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + }); + } + + Ok(netuid.as_u32() as u16) + } + fn dispatch(handle: &mut impl PrecompileHandle, call: RuntimeCall) -> PrecompileResult { let account_id = as AddressMapping>::into_account_id(