Skip to content

Commit

Permalink
Fix get_stake in staking precompile
Browse files Browse the repository at this point in the history
  • Loading branch information
gztensor committed Jan 14, 2025
1 parent af7a541 commit f4ffb5c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions runtime/src/precompiles/solidity/staking.abi
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
"name": "getStake",
"outputs": [
{
"internalType": "uint64",
"internalType": "uint256",
"name": "",
"type": "uint64"
"type": "uint256"
}
],
"stateMutability": "view",
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/precompiles/solidity/staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ interface IStaking {
* @param hotkey The hotkey public key (32 bytes).
* @param coldkey The coldkey public key (32 bytes).
* @param netuid The subnet the stake is on (uint256).
* @return The current stake amount in uint64 format.
* @return The current stake amount in uint256 format.
*/
function getStake(bytes32 hotkey, bytes32 coldkey, uint256 netuid) external view returns (uint64);
function getStake(bytes32 hotkey, bytes32 coldkey, uint256 netuid) external view returns (uint256);
}
12 changes: 9 additions & 3 deletions runtime/src/precompiles/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl StakingPrecompile {
id if id == get_method_id("removeStake(bytes32,uint256,uint256)") => {
Self::remove_stake(handle, &method_input)
}
id if id == get_method_id("getStake(bytes32,bytes32,uint16)") => {
id if id == get_method_id("getStake(bytes32,bytes32,uint256)") => {
Self::get_stake(&method_input)
}
_ => Err(PrecompileFailure::Error {
Expand Down Expand Up @@ -112,17 +112,23 @@ impl StakingPrecompile {

fn get_stake(data: &[u8]) -> PrecompileResult {
let (hotkey, coldkey) = Self::parse_hotkey_coldkey(data)?;
let netuid = Self::parse_netuid(data, 0x3E)?;
let netuid = Self::parse_netuid(data, 0x5E)?;

let stake = pallet_subtensor::Pallet::<Runtime>::get_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey.into(),
&coldkey.into(),
netuid,
);

// Convert to EVM decimals
let stake_u256 = U256::from(stake);
let stake_eth =
<Runtime as pallet_evm::Config>::BalanceConverter::into_evm_balance(stake_u256)
.ok_or(ExitError::InvalidRange)?;

// Format output
let mut result = [0_u8; 32];
U256::to_big_endian(&stake_u256, &mut result);
U256::to_big_endian(&stake_eth, &mut result);

Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
Expand Down

0 comments on commit f4ffb5c

Please sign in to comment.