Skip to content

Commit 7c67ab5

Browse files
authored
Merge pull request #955 from opentensor/feat/solidity-get-stake
get stake interface in solidity
2 parents bffc8b6 + f4ffb5c commit 7c67ab5

File tree

3 files changed

+83
-24
lines changed

3 files changed

+83
-24
lines changed

runtime/src/precompiles/solidity/staking.abi

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,33 @@
1818
"type": "function"
1919
},
2020
{
21-
inputs: [
22-
{
23-
internalType: "bytes32",
24-
name: "hotkey",
25-
type: "bytes32",
26-
},
27-
{
28-
internalType: "bytes32",
29-
name: "coldkey",
30-
type: "bytes32",
31-
},
21+
"inputs": [
22+
{
23+
"internalType": "bytes32",
24+
"name": "hotkey",
25+
"type": "bytes32"
26+
},
27+
{
28+
"internalType": "bytes32",
29+
"name": "coldkey",
30+
"type": "bytes32"
31+
},
32+
{
33+
"internalType": "uint256",
34+
"name": "netuid",
35+
"type": "uint256"
36+
}
3237
],
33-
name: "getStake",
34-
outputs: [
35-
{
36-
internalType: "uint64",
37-
name: "",
38-
type: "uint64",
39-
},
38+
"name": "getStake",
39+
"outputs": [
40+
{
41+
"internalType": "uint256",
42+
"name": "",
43+
"type": "uint256"
44+
}
4045
],
41-
stateMutability: "view",
42-
type: "function",
46+
"stateMutability": "view",
47+
"type": "function"
4348
},
4449
{
4550
"inputs": [
@@ -64,4 +69,4 @@
6469
"stateMutability": "nonpayable",
6570
"type": "function"
6671
}
67-
]
72+
]

runtime/src/precompiles/solidity/staking.sol

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface IStaking {
1414
* https://github.com/polkadot-evm/frontier/blob/2e219e17a526125da003e64ef22ec037917083fa/frame/evm/src/lib.rs#L739
1515
*
1616
* @param hotkey The hotkey public key (32 bytes).
17-
* @param netuid The subnet to stake to (uint256). Currently a noop, functionality will be enabled with RAO.
17+
* @param netuid The subnet to stake to (uint256).
1818
*
1919
* Requirements:
2020
* - `hotkey` must be a valid hotkey registered on the network, ensuring that the stake is
@@ -33,13 +33,25 @@ interface IStaking {
3333
*
3434
* @param hotkey The hotkey public key (32 bytes).
3535
* @param amount The amount to unstake in rao.
36-
* @param netuid The subnet to stake to (uint256). Currently a noop, functionality will be enabled with RAO.
37-
36+
* @param netuid The subnet to stake to (uint256).
3837
*
3938
* Requirements:
4039
* - `hotkey` must be a valid hotkey registered on the network, ensuring that the stake is
4140
* correctly attributed.
4241
* - The existing stake amount must be not lower than specified amount
4342
*/
4443
function removeStake(bytes32 hotkey, uint256 amount, uint256 netuid) external;
44+
45+
/**
46+
* @dev Returns the stake amount associated with the specified `hotkey` and `coldkey`.
47+
*
48+
* This function retrieves the current stake amount linked to a specific hotkey and coldkey pair.
49+
* It is a view function, meaning it does not modify the state of the contract and is free to call.
50+
*
51+
* @param hotkey The hotkey public key (32 bytes).
52+
* @param coldkey The coldkey public key (32 bytes).
53+
* @param netuid The subnet the stake is on (uint256).
54+
* @return The current stake amount in uint256 format.
55+
*/
56+
function getStake(bytes32 hotkey, bytes32 coldkey, uint256 netuid) external view returns (uint256);
4557
}

runtime/src/precompiles/staking.rs

Lines changed: 42 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,uint256)") => {
6060
Self::remove_stake(handle, &method_input)
6161
}
62+
id if id == get_method_id("getStake(bytes32,bytes32,uint256)") => {
63+
Self::get_stake(&method_input)
64+
}
6265
_ => Err(PrecompileFailure::Error {
6366
exit_status: ExitError::InvalidRange,
6467
}),
@@ -107,6 +110,45 @@ impl StakingPrecompile {
107110
Self::dispatch(handle, call)
108111
}
109112

113+
fn get_stake(data: &[u8]) -> PrecompileResult {
114+
let (hotkey, coldkey) = Self::parse_hotkey_coldkey(data)?;
115+
let netuid = Self::parse_netuid(data, 0x5E)?;
116+
117+
let stake = pallet_subtensor::Pallet::<Runtime>::get_stake_for_hotkey_and_coldkey_on_subnet(
118+
&hotkey.into(),
119+
&coldkey.into(),
120+
netuid,
121+
);
122+
123+
// Convert to EVM decimals
124+
let stake_u256 = U256::from(stake);
125+
let stake_eth =
126+
<Runtime as pallet_evm::Config>::BalanceConverter::into_evm_balance(stake_u256)
127+
.ok_or(ExitError::InvalidRange)?;
128+
129+
// Format output
130+
let mut result = [0_u8; 32];
131+
U256::to_big_endian(&stake_eth, &mut result);
132+
133+
Ok(PrecompileOutput {
134+
exit_status: ExitSucceed::Returned,
135+
output: result.into(),
136+
})
137+
}
138+
139+
fn parse_hotkey_coldkey(data: &[u8]) -> Result<([u8; 32], [u8; 32]), PrecompileFailure> {
140+
if data.len() < 64 {
141+
return Err(PrecompileFailure::Error {
142+
exit_status: ExitError::InvalidRange,
143+
});
144+
}
145+
let mut hotkey = [0u8; 32];
146+
hotkey.copy_from_slice(get_slice(data, 0, 32)?);
147+
let mut coldkey = [0u8; 32];
148+
coldkey.copy_from_slice(get_slice(data, 32, 64)?);
149+
Ok((hotkey, coldkey))
150+
}
151+
110152
fn parse_hotkey(data: &[u8]) -> Result<[u8; 32], PrecompileFailure> {
111153
if data.len() < 32 {
112154
return Err(PrecompileFailure::Error {

0 commit comments

Comments
 (0)