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

get stake interface in solidity #955

Merged
merged 16 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
26 changes: 25 additions & 1 deletion runtime/src/precompiles/solidity/staking.abi
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "hotkey",
"type": "bytes32"
},
{
"internalType": "bytes32",
"name": "coldkey",
"type": "bytes32"
}
],
"name": "getStake",
"outputs": [
{
"internalType": "uint64",
"name": "",
"type": "uint64"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand All @@ -40,4 +64,4 @@
"stateMutability": "payable",
"type": "function"
}
]
]
12 changes: 12 additions & 0 deletions runtime/src/precompiles/solidity/staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,16 @@ interface IStaking {
* - The existing stake amount must be not lower than specified amount
*/
function removeStake(bytes32 hotkey, uint256 amount, uint16 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).
gztensor marked this conversation as resolved.
Show resolved Hide resolved
* @return The current stake amount in uint64 format.
*/
function getStake(bytes32 hotkey, bytes32 coldkey) external view returns (uint64);
gztensor marked this conversation as resolved.
Show resolved Hide resolved
}
34 changes: 34 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,uint16)") => {
Self::remove_stake(handle, &method_input)
}
id if id == get_method_id("getStake(bytes32,bytes32)") => {
gztensor marked this conversation as resolved.
Show resolved Hide resolved
Self::get_stake(&method_input)
}
_ => Err(PrecompileFailure::Error {
exit_status: ExitError::InvalidRange,
}),
Expand Down Expand Up @@ -101,6 +104,37 @@ impl StakingPrecompile {
Self::dispatch(handle, call)
}

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

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

let stake_u256 = U256::from(stake);
let mut result = [0_u8; 32];
U256::to_big_endian(&stake_u256, &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
Loading