Skip to content

Commit 7c329f9

Browse files
authored
Merge pull request #1159 from Shr1ftyy/devnet-ready
Adding `getTotalColdkeyStake`and `getTotalHotkeyStake` to the staking precompile
2 parents 220ab34 + 0cdc9cb commit 7c329f9

File tree

4 files changed

+106
-7
lines changed

4 files changed

+106
-7
lines changed

runtime/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
228228
// `spec_version`, and `authoring_version` are the same between Wasm and native.
229229
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
230230
// the compatible custom types.
231-
spec_version: 244,
231+
spec_version: 245,
232232
impl_version: 1,
233233
apis: RUNTIME_API_VERSIONS,
234234
transaction_version: 1,

runtime/src/precompiles/solidity/staking.abi

+38
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,43 @@
9494
"outputs": [],
9595
"stateMutability": "nonpayable",
9696
"type": "function"
97+
},
98+
{
99+
"inputs": [
100+
{
101+
"internalType": "bytes32",
102+
"name": "coldkey",
103+
"type": "bytes32"
104+
}
105+
],
106+
"name": "getTotalColdkeyStake",
107+
"outputs": [
108+
{
109+
"internalType": "uint256",
110+
"name": "",
111+
"type": "uint256"
112+
}
113+
],
114+
"stateMutability": "view",
115+
"type": "function"
116+
},
117+
{
118+
"inputs": [
119+
{
120+
"internalType": "bytes32",
121+
"name": "hotkey",
122+
"type": "bytes32"
123+
}
124+
],
125+
"name": "getTotalHotkeyStake",
126+
"outputs": [
127+
{
128+
"internalType": "uint256",
129+
"name": "",
130+
"type": "uint256"
131+
}
132+
],
133+
"stateMutability": "view",
134+
"type": "function"
97135
}
98136
]

runtime/src/precompiles/solidity/staking.sol

+30-6
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,36 @@ interface IStaking {
4646
uint256 netuid
4747
) external;
4848

49-
/**
50-
* @dev Delegates staking to a proxy account.
51-
*
52-
* @param delegate The public key (32 bytes) of the delegate.
53-
*/
54-
function addProxy(bytes32 delegate) external;
49+
/**
50+
* @dev Returns the amount of RAO staked by the coldkey.
51+
*
52+
* This function allows external accounts and contracts to query the amount of RAO staked by the coldkey
53+
* which effectively calls `get_total_coldkey_stake` on the subtensor pallet with
54+
* specified coldkey as a parameter.
55+
*
56+
* @param coldkey The coldkey public key (32 bytes).
57+
* @return The amount of RAO staked by the coldkey.
58+
*/
59+
function getTotalColdkeyStake(bytes32 coldkey) external view returns (uint256);
60+
61+
/**
62+
* @dev Returns the total amount of stake under a hotkey (delegative or otherwise)
63+
*
64+
* This function allows external accounts and contracts to query the total amount of RAO staked under a hotkey
65+
* which effectively calls `get_total_hotkey_stake` on the subtensor pallet with
66+
* specified hotkey as a parameter.
67+
*
68+
* @param hotkey The hotkey public key (32 bytes).
69+
* @return The total amount of RAO staked under the hotkey.
70+
*/
71+
function getTotalHotkeyStake(bytes32 hotkey) external view returns (uint256);
72+
73+
/**
74+
* @dev Delegates staking to a proxy account.
75+
*
76+
* @param delegate The public key (32 bytes) of the delegate.
77+
*/
78+
function addProxy(bytes32 delegate) external;
5579

5680
/**
5781
* @dev Removes staking proxy account.

runtime/src/precompiles/staking.rs

+37
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,43 @@ impl StakingPrecompile {
9090
handle.try_dispatch_runtime_call(call, RawOrigin::Signed(account_id))
9191
}
9292

93+
#[precompile::public("getTotalColdkeyStake(bytes32)")]
94+
fn get_total_coldkey_stake(
95+
_handle: &mut impl PrecompileHandle,
96+
coldkey_h256: H256,
97+
) -> EvmResult<U256> {
98+
let (coldkey, _) = parse_pubkey(coldkey_h256.as_bytes())?;
99+
100+
// get total stake of coldkey
101+
let total_stake =
102+
pallet_subtensor::Pallet::<Runtime>::get_total_stake_for_coldkey(&coldkey);
103+
// Convert to EVM decimals
104+
let stake_u256 = U256::from(total_stake);
105+
let stake_eth =
106+
<Runtime as pallet_evm::Config>::BalanceConverter::into_evm_balance(stake_u256)
107+
.ok_or(ExitError::InvalidRange)?;
108+
109+
Ok(stake_eth)
110+
}
111+
112+
#[precompile::public("getTotalHotkeyStake(bytes32)")]
113+
fn get_total_hotkey_stake(
114+
_handle: &mut impl PrecompileHandle,
115+
hotkey_h256: H256,
116+
) -> EvmResult<U256> {
117+
let (hotkey, _) = parse_pubkey(hotkey_h256.as_bytes())?;
118+
119+
// get total stake of hotkey
120+
let total_stake = pallet_subtensor::Pallet::<Runtime>::get_total_stake_for_hotkey(&hotkey);
121+
// Convert to EVM decimals
122+
let stake_u256 = U256::from(total_stake);
123+
let stake_eth =
124+
<Runtime as pallet_evm::Config>::BalanceConverter::into_evm_balance(stake_u256)
125+
.ok_or(ExitError::InvalidRange)?;
126+
127+
Ok(stake_eth)
128+
}
129+
93130
#[precompile::public("addProxy(bytes32)")]
94131
fn add_proxy(handle: &mut impl PrecompileHandle, delegate: H256) -> EvmResult<()> {
95132
let account_id = handle.caller_account_id();

0 commit comments

Comments
 (0)