Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 244,
spec_version: 245,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
38 changes: 38 additions & 0 deletions runtime/src/precompiles/solidity/staking.abi
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,43 @@
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "coldkey",
"type": "bytes32"
}
],
"name": "getTotalColdkeyStake",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "hotkey",
"type": "bytes32"
}
],
"name": "getTotalHotkeyStake",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
]
36 changes: 30 additions & 6 deletions runtime/src/precompiles/solidity/staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,36 @@ interface IStaking {
uint256 netuid
) external;

/**
* @dev Delegates staking to a proxy account.
*
* @param delegate The public key (32 bytes) of the delegate.
*/
function addProxy(bytes32 delegate) external;
/**
* @dev Returns the amount of RAO staked by the coldkey.
*
* This function allows external accounts and contracts to query the amount of RAO staked by the coldkey
* which effectively calls `get_total_coldkey_stake` on the subtensor pallet with
* specified coldkey as a parameter.
*
* @param coldkey The coldkey public key (32 bytes).
* @return The amount of RAO staked by the coldkey.
*/
function getTotalColdkeyStake(bytes32 coldkey) external view returns (uint256);

/**
* @dev Returns the total amount of stake under a hotkey (delegative or otherwise)
*
* This function allows external accounts and contracts to query the total amount of RAO staked under a hotkey
* which effectively calls `get_total_hotkey_stake` on the subtensor pallet with
* specified hotkey as a parameter.
*
* @param hotkey The hotkey public key (32 bytes).
* @return The total amount of RAO staked under the hotkey.
*/
function getTotalHotkeyStake(bytes32 hotkey) external view returns (uint256);

/**
* @dev Delegates staking to a proxy account.
*
* @param delegate The public key (32 bytes) of the delegate.
*/
function addProxy(bytes32 delegate) external;

/**
* @dev Removes staking proxy account.
Expand Down
37 changes: 37 additions & 0 deletions runtime/src/precompiles/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,43 @@ impl StakingPrecompile {
handle.try_dispatch_runtime_call(call, RawOrigin::Signed(account_id))
}

#[precompile::public("getTotalColdkeyStake(bytes32)")]
fn get_total_coldkey_stake(
_handle: &mut impl PrecompileHandle,
coldkey_h256: H256,
) -> EvmResult<U256> {
let (coldkey, _) = parse_pubkey(coldkey_h256.as_bytes())?;

// get total stake of coldkey
let total_stake =
pallet_subtensor::Pallet::<Runtime>::get_total_stake_for_coldkey(&coldkey);
// Convert to EVM decimals
let stake_u256 = U256::from(total_stake);
let stake_eth =
<Runtime as pallet_evm::Config>::BalanceConverter::into_evm_balance(stake_u256)
.ok_or(ExitError::InvalidRange)?;

Ok(stake_eth)
}

#[precompile::public("getTotalHotkeyStake(bytes32)")]
fn get_total_hotkey_stake(
_handle: &mut impl PrecompileHandle,
hotkey_h256: H256,
) -> EvmResult<U256> {
let (hotkey, _) = parse_pubkey(hotkey_h256.as_bytes())?;

// get total stake of hotkey
let total_stake = pallet_subtensor::Pallet::<Runtime>::get_total_stake_for_hotkey(&hotkey);
// Convert to EVM decimals
let stake_u256 = U256::from(total_stake);
let stake_eth =
<Runtime as pallet_evm::Config>::BalanceConverter::into_evm_balance(stake_u256)
.ok_or(ExitError::InvalidRange)?;

Ok(stake_eth)
}

#[precompile::public("addProxy(bytes32)")]
fn add_proxy(handle: &mut impl PrecompileHandle, delegate: H256) -> EvmResult<()> {
let account_id = handle.caller_account_id();
Expand Down
Loading