Skip to content

Commit cd366c5

Browse files
committed
feat: added getStakeColdkey to staking precompile
1 parent a6bd117 commit cd366c5

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

runtime/src/precompiles/solidity/staking.abi

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,24 @@
3939
"outputs": [],
4040
"stateMutability": "payable",
4141
"type": "function"
42+
},
43+
{
44+
"inputs": [
45+
{
46+
"internalType": "bytes32",
47+
"name": "coldkey",
48+
"type": "bytes32"
49+
}
50+
],
51+
"name": "getStakeColdkey",
52+
"outputs": [
53+
{
54+
"internalType": "uint256",
55+
"name": "",
56+
"type": "uint256"
57+
}
58+
],
59+
"stateMutability": "view",
60+
"type": "function"
4261
}
4362
]

runtime/src/precompiles/solidity/staking.sol

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,16 @@ interface IStaking {
4242
* - The existing stake amount must be not lower than specified amount
4343
*/
4444
function removeStake(bytes32 hotkey, uint256 amount, uint16 netuid) external;
45+
46+
/**
47+
* @dev Returns the amount of RAO staked by the coldkey associated with the hotkey.
48+
*
49+
* This function allows external accounts and contracts to query the amount of RAO staked by the coldkey
50+
* associated with the coldkey, which effectively calls `get_coldkey_stake` on the subtensor pallet with
51+
* specified coldkey as a parameter.
52+
*
53+
* @param coldkey The coldkey public key (32 bytes).
54+
* @return The amount of RAO staked by the coldkey associated with the hotkey.
55+
*/
56+
function getStakeColdkey(bytes32 coldkey) external view returns (uint256);
4557
}

runtime/src/precompiles/staking.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ impl StakingPrecompile {
5959
id if id == get_method_id("removeStake(bytes32,uint256,uint16)") => {
6060
Self::remove_stake(handle, &method_input)
6161
}
62+
id if id == get_method_id("getStakeColdkey(bytes32)") => {
63+
Self::get_stake_coldkey(&method_input)
64+
}
6265
_ => Err(PrecompileFailure::Error {
6366
exit_status: ExitError::InvalidRange,
6467
}),
@@ -110,6 +113,22 @@ impl StakingPrecompile {
110113
Self::dispatch(handle, call)
111114
}
112115

116+
fn get_stake_coldkey(data: &[u8]) -> PrecompileResult {
117+
// TODO: rename parse_hotkey to parse_key or something?
118+
let coldkey: AccountId32 = Self::parse_hotkey(data)?.into();
119+
120+
// get total stake of coldkey
121+
let total_stake = pallet_subtensor::TotalColdkeyStake::<Runtime>::get(coldkey);
122+
let result_u256 = U256::from(total_stake);
123+
let mut result = [0_u8; 32];
124+
U256::to_big_endian(&result_u256, &mut result);
125+
126+
Ok(PrecompileOutput {
127+
exit_status: ExitSucceed::Returned,
128+
output: result.into(),
129+
})
130+
}
131+
113132
fn parse_hotkey(data: &[u8]) -> Result<[u8; 32], PrecompileFailure> {
114133
if data.len() < 32 {
115134
return Err(PrecompileFailure::Error {

0 commit comments

Comments
 (0)