Skip to content

Commit

Permalink
Merge pull request opentensor#955 from opentensor/feat/solidity-get-s…
Browse files Browse the repository at this point in the history
…take

get stake interface in solidity
  • Loading branch information
sam0x17 authored Jan 14, 2025
2 parents bffc8b6 + f4ffb5c commit 7c67ab5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 24 deletions.
47 changes: 26 additions & 21 deletions runtime/src/precompiles/solidity/staking.abi
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,33 @@
"type": "function"
},
{
inputs: [
{
internalType: "bytes32",
name: "hotkey",
type: "bytes32",
},
{
internalType: "bytes32",
name: "coldkey",
type: "bytes32",
},
"inputs": [
{
"internalType": "bytes32",
"name": "hotkey",
"type": "bytes32"
},
{
"internalType": "bytes32",
"name": "coldkey",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "netuid",
"type": "uint256"
}
],
name: "getStake",
outputs: [
{
internalType: "uint64",
name: "",
type: "uint64",
},
"name": "getStake",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
stateMutability: "view",
type: "function",
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
Expand All @@ -64,4 +69,4 @@
"stateMutability": "nonpayable",
"type": "function"
}
]
]
18 changes: 15 additions & 3 deletions runtime/src/precompiles/solidity/staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface IStaking {
* https://github.com/polkadot-evm/frontier/blob/2e219e17a526125da003e64ef22ec037917083fa/frame/evm/src/lib.rs#L739
*
* @param hotkey The hotkey public key (32 bytes).
* @param netuid The subnet to stake to (uint256). Currently a noop, functionality will be enabled with RAO.
* @param netuid The subnet to stake to (uint256).
*
* Requirements:
* - `hotkey` must be a valid hotkey registered on the network, ensuring that the stake is
Expand All @@ -33,13 +33,25 @@ interface IStaking {
*
* @param hotkey The hotkey public key (32 bytes).
* @param amount The amount to unstake in rao.
* @param netuid The subnet to stake to (uint256). Currently a noop, functionality will be enabled with RAO.
* @param netuid The subnet to stake to (uint256).
*
* Requirements:
* - `hotkey` must be a valid hotkey registered on the network, ensuring that the stake is
* correctly attributed.
* - The existing stake amount must be not lower than specified amount
*/
function removeStake(bytes32 hotkey, uint256 amount, uint256 netuid) external;

/**
* @dev Returns the stake amount associated with the specified `hotkey` and `coldkey`.
*
* This function retrieves the current stake amount linked to a specific hotkey and coldkey pair.
* It is a view function, meaning it does not modify the state of the contract and is free to call.
*
* @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 uint256 format.
*/
function getStake(bytes32 hotkey, bytes32 coldkey, uint256 netuid) external view returns (uint256);
}
42 changes: 42 additions & 0 deletions runtime/src/precompiles/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ 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,uint256)") => {
Self::get_stake(&method_input)
}
_ => Err(PrecompileFailure::Error {
exit_status: ExitError::InvalidRange,
}),
Expand Down Expand Up @@ -107,6 +110,45 @@ impl StakingPrecompile {
Self::dispatch(handle, call)
}

fn get_stake(data: &[u8]) -> PrecompileResult {
let (hotkey, coldkey) = Self::parse_hotkey_coldkey(data)?;
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_eth, &mut result);

Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
output: result.into(),
})
}

fn parse_hotkey_coldkey(data: &[u8]) -> Result<([u8; 32], [u8; 32]), PrecompileFailure> {
if data.len() < 64 {
return Err(PrecompileFailure::Error {
exit_status: ExitError::InvalidRange,
});
}
let mut hotkey = [0u8; 32];
hotkey.copy_from_slice(get_slice(data, 0, 32)?);
let mut coldkey = [0u8; 32];
coldkey.copy_from_slice(get_slice(data, 32, 64)?);
Ok((hotkey, coldkey))
}

fn parse_hotkey(data: &[u8]) -> Result<[u8; 32], PrecompileFailure> {
if data.len() < 32 {
return Err(PrecompileFailure::Error {
Expand Down

0 comments on commit 7c67ab5

Please sign in to comment.