Skip to content

Commit da415c2

Browse files
committed
Added queries
1 parent e19aada commit da415c2

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

src/contract.rs

+79-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const CONTRACT_NAME: &str = "cybernet";
6262
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
6363

6464
use cyber_std::Response;
65+
use crate::uids::get_registered_networks_for_hotkey;
6566

6667
#[cfg_attr(not(feature = "library"), entry_point)]
6768
pub fn instantiate(
@@ -586,11 +587,36 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
586587
QueryMsg::GetTotalStake {} => to_json_binary(&query_total_stake(deps.storage)?),
587588
QueryMsg::GetTxRateLimit {} => to_json_binary(&query_tx_rate_limit(deps.storage)?),
588589

590+
QueryMsg::GetAxonInfo { netuid, hotkey } => {
591+
let hotkey_address = deps.api.addr_validate(&hotkey)?;
592+
to_json_binary(&query_get_axon_info(deps.storage, netuid, &hotkey_address)?)
593+
}
594+
QueryMsg::GetPrometheusInfo { netuid, hotkey } => {
595+
let hotkey_address = deps.api.addr_validate(&hotkey)?;
596+
to_json_binary(&query_get_prometheus_info(deps.storage, netuid, &hotkey_address)?)
597+
}
598+
QueryMsg::GetTotalStakeForHotkey { address } => {
599+
let hotkey_address = deps.api.addr_validate(&address)?;
600+
to_json_binary(&query_get_total_stake_for_hotkey(deps.storage, &hotkey_address)?)
601+
}
602+
QueryMsg::GetTotalStakeForColdkey { address } => {
603+
let hotkey_address = deps.api.addr_validate(&address)?;
604+
to_json_binary(&query_get_total_stake_for_coldkey(deps.storage, &hotkey_address)?)
605+
}
606+
QueryMsg::GetHotkeyExist { hotkey } => {
607+
let hotkey_address = deps.api.addr_validate(&hotkey)?;
608+
to_json_binary(&query_get_hotkey_exist(deps.storage, &hotkey_address)?)
609+
}
610+
QueryMsg::GetStake { hotkey } => {
611+
let hotkey_address = deps.api.addr_validate(&hotkey)?;
612+
to_json_binary(&query_get_stake(deps.storage, &hotkey_address)?)
613+
}
614+
589615
// TODO added for debugging, remove later
616+
QueryMsg::GetState {} => to_json_binary(&get_state_info(deps.storage)?),
590617
QueryMsg::GetWeights { netuid } => {
591618
to_json_binary(&get_network_weights(deps.storage, netuid)?)
592619
}
593-
QueryMsg::GetState {} => to_json_binary(&get_state_info(deps.storage)?),
594620
}
595621
}
596622

@@ -753,6 +779,58 @@ pub fn query_tx_rate_limit(store: &dyn Storage) -> StdResult<u64> {
753779
Ok(limit)
754780
}
755781

782+
pub fn query_get_axon_info(store: &dyn Storage, netuid: u16, hotkey: &Addr) -> StdResult<Option<AxonInfo>> {
783+
let axon = AXONS.may_load(store, (netuid, hotkey))?;
784+
if axon.is_some() {
785+
Ok(Some(axon.unwrap()))
786+
} else {
787+
Ok(None)
788+
}
789+
}
790+
pub fn query_get_prometheus_info(store: &dyn Storage, netuid: u16, hotkey: &Addr) -> StdResult<Option<PrometheusInfo>> {
791+
let axon = PROMETHEUS.may_load(store, (netuid, hotkey))?;
792+
if axon.is_some() {
793+
Ok(Some(axon.unwrap()))
794+
} else {
795+
Ok(None)
796+
}
797+
}
798+
pub fn query_get_total_stake_for_hotkey(store: &dyn Storage, hotkey: &Addr) -> StdResult<Option<u64>> {
799+
let stake = TOTAL_HOTKEY_STAKE.may_load(store, hotkey)?;
800+
if stake.is_some() {
801+
Ok(Some(stake.unwrap()))
802+
} else {
803+
Ok(None)
804+
}
805+
}
806+
pub fn query_get_total_stake_for_coldkey(store: &dyn Storage, coldkey: &Addr) -> StdResult<Option<u64>> {
807+
let stake = TOTAL_COLDKEY_STAKE.may_load(store, coldkey)?;
808+
if stake.is_some() {
809+
Ok(Some(stake.unwrap()))
810+
} else {
811+
Ok(None)
812+
}
813+
}
814+
pub fn query_get_hotkey_exist(store: &dyn Storage, hotkey: &Addr) -> StdResult<bool> {
815+
let owner = OWNER.may_load(store, hotkey)?;
816+
if owner.is_some() {
817+
Ok(true)
818+
} else {
819+
Ok(false)
820+
}
821+
}
822+
pub fn query_get_stake(store: &dyn Storage, hotkey: &Addr) -> StdResult<Vec<(String, u64)>> {
823+
let stakes = STAKE
824+
.prefix(hotkey)
825+
.range(store, None, None, Order::Ascending)
826+
.map(|item| {
827+
let (address, stake) = item.unwrap();
828+
(address.to_string(), stake)
829+
}).collect::<Vec<(String, u64)>>();
830+
Ok(stakes)
831+
}
832+
833+
756834
#[cfg_attr(not(feature = "library"), entry_point)]
757835
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
758836
let storage_version: ContractVersion = get_contract_version(deps.storage)?;

src/msg.rs

+18
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,24 @@ pub enum QueryMsg {
312312
#[returns(u64)]
313313
GetTxRateLimit {},
314314

315+
#[returns(Option<AxonInfo>)]
316+
GetAxonInfo { netuid: u16, hotkey: String },
317+
318+
#[returns(Option<PrometheusInfo>)]
319+
GetPrometheusInfo { netuid: u16, hotkey: String },
320+
321+
#[returns(Option<u64>)]
322+
GetTotalStakeForHotkey { address: String },
323+
324+
#[returns(Option<u64>)]
325+
GetTotalStakeForColdkey { address: String },
326+
327+
#[returns(bool)]
328+
GetHotkeyExist { hotkey: String },
329+
330+
#[returns(Vec<(String, u64)>)]
331+
GetStake { hotkey: String },
332+
315333
// TODO added for debugging, remove later
316334
#[returns(Vec<Vec<u16>>)]
317335
GetWeights { netuid: u16 },

0 commit comments

Comments
 (0)