From 56eec35c8090b1dc28f51b1a279c11f476c0f315 Mon Sep 17 00:00:00 2001 From: C H Date: Sat, 18 May 2024 13:33:59 +0800 Subject: [PATCH] Added query for subnets and verse metadata --- schema/cybernet.json | 178 +++++++++++++++++- schema/raw/query.json | 58 +++++- .../raw/response_to_get_subnet_metadata.json | 33 ++++ .../raw/response_to_get_subnets_metadata.json | 53 ++++++ .../raw/response_to_get_verse_metadata.json | 33 ++++ src/contract.rs | 43 ++++- src/msg.rs | 11 +- 7 files changed, 396 insertions(+), 13 deletions(-) create mode 100644 schema/raw/response_to_get_subnet_metadata.json create mode 100644 schema/raw/response_to_get_subnets_metadata.json create mode 100644 schema/raw/response_to_get_verse_metadata.json diff --git a/schema/cybernet.json b/schema/cybernet.json index f050407..8d98b2d 100644 --- a/schema/cybernet.json +++ b/schema/cybernet.json @@ -2489,10 +2489,64 @@ { "type": "object", "required": [ - "get_verse_type" + "get_subnet_metadata" ], "properties": { - "get_verse_type": { + "get_subnet_metadata": { + "type": "object", + "required": [ + "netuid" + ], + "properties": { + "netuid": { + "type": "integer", + "format": "uint16", + "minimum": 0.0 + } + }, + "additionalProperties": false + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "get_subnets_metadata" + ], + "properties": { + "get_subnets_metadata": { + "type": "object", + "properties": { + "limit": { + "type": [ + "integer", + "null" + ], + "format": "uint16", + "minimum": 0.0 + }, + "start_after": { + "type": [ + "integer", + "null" + ], + "format": "uint16", + "minimum": 0.0 + } + }, + "additionalProperties": false + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "get_verse_metadata" + ], + "properties": { + "get_verse_metadata": { "type": "object", "additionalProperties": false } @@ -6310,6 +6364,39 @@ } } }, + "get_subnet_metadata": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Metadata", + "type": "object", + "required": [ + "description", + "extra", + "logo", + "name", + "particle", + "types" + ], + "properties": { + "description": { + "type": "string" + }, + "extra": { + "type": "string" + }, + "logo": { + "type": "string" + }, + "name": { + "type": "string" + }, + "particle": { + "type": "string" + }, + "types": { + "type": "string" + } + } + }, "get_subnet_owner": { "$schema": "http://json-schema.org/draft-07/schema#", "title": "Nullable_String", @@ -6469,6 +6556,59 @@ } } }, + "get_subnets_metadata": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Array_of_Tuple_of_uint16_and_Metadata", + "type": "array", + "items": { + "type": "array", + "items": [ + { + "type": "integer", + "format": "uint16", + "minimum": 0.0 + }, + { + "$ref": "#/definitions/Metadata" + } + ], + "maxItems": 2, + "minItems": 2 + }, + "definitions": { + "Metadata": { + "type": "object", + "required": [ + "description", + "extra", + "logo", + "name", + "particle", + "types" + ], + "properties": { + "description": { + "type": "string" + }, + "extra": { + "type": "string" + }, + "logo": { + "type": "string" + }, + "name": { + "type": "string" + }, + "particle": { + "type": "string" + }, + "types": { + "type": "string" + } + } + } + } + }, "get_tempo": { "$schema": "http://json-schema.org/draft-07/schema#", "title": "Nullable_uint16", @@ -6537,10 +6677,38 @@ "format": "uint16", "minimum": 0.0 }, - "get_verse_type": { + "get_verse_metadata": { "$schema": "http://json-schema.org/draft-07/schema#", - "title": "String", - "type": "string" + "title": "Metadata", + "type": "object", + "required": [ + "description", + "extra", + "logo", + "name", + "particle", + "types" + ], + "properties": { + "description": { + "type": "string" + }, + "extra": { + "type": "string" + }, + "logo": { + "type": "string" + }, + "name": { + "type": "string" + }, + "particle": { + "type": "string" + }, + "types": { + "type": "string" + } + } }, "get_weights": { "$schema": "http://json-schema.org/draft-07/schema#", diff --git a/schema/raw/query.json b/schema/raw/query.json index 8409fbe..11b2002 100644 --- a/schema/raw/query.json +++ b/schema/raw/query.json @@ -855,10 +855,64 @@ { "type": "object", "required": [ - "get_verse_type" + "get_subnet_metadata" ], "properties": { - "get_verse_type": { + "get_subnet_metadata": { + "type": "object", + "required": [ + "netuid" + ], + "properties": { + "netuid": { + "type": "integer", + "format": "uint16", + "minimum": 0.0 + } + }, + "additionalProperties": false + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "get_subnets_metadata" + ], + "properties": { + "get_subnets_metadata": { + "type": "object", + "properties": { + "limit": { + "type": [ + "integer", + "null" + ], + "format": "uint16", + "minimum": 0.0 + }, + "start_after": { + "type": [ + "integer", + "null" + ], + "format": "uint16", + "minimum": 0.0 + } + }, + "additionalProperties": false + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "get_verse_metadata" + ], + "properties": { + "get_verse_metadata": { "type": "object", "additionalProperties": false } diff --git a/schema/raw/response_to_get_subnet_metadata.json b/schema/raw/response_to_get_subnet_metadata.json new file mode 100644 index 0000000..3310951 --- /dev/null +++ b/schema/raw/response_to_get_subnet_metadata.json @@ -0,0 +1,33 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Metadata", + "type": "object", + "required": [ + "description", + "extra", + "logo", + "name", + "particle", + "types" + ], + "properties": { + "description": { + "type": "string" + }, + "extra": { + "type": "string" + }, + "logo": { + "type": "string" + }, + "name": { + "type": "string" + }, + "particle": { + "type": "string" + }, + "types": { + "type": "string" + } + } +} diff --git a/schema/raw/response_to_get_subnets_metadata.json b/schema/raw/response_to_get_subnets_metadata.json new file mode 100644 index 0000000..72d9948 --- /dev/null +++ b/schema/raw/response_to_get_subnets_metadata.json @@ -0,0 +1,53 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Array_of_Tuple_of_uint16_and_Metadata", + "type": "array", + "items": { + "type": "array", + "items": [ + { + "type": "integer", + "format": "uint16", + "minimum": 0.0 + }, + { + "$ref": "#/definitions/Metadata" + } + ], + "maxItems": 2, + "minItems": 2 + }, + "definitions": { + "Metadata": { + "type": "object", + "required": [ + "description", + "extra", + "logo", + "name", + "particle", + "types" + ], + "properties": { + "description": { + "type": "string" + }, + "extra": { + "type": "string" + }, + "logo": { + "type": "string" + }, + "name": { + "type": "string" + }, + "particle": { + "type": "string" + }, + "types": { + "type": "string" + } + } + } + } +} diff --git a/schema/raw/response_to_get_verse_metadata.json b/schema/raw/response_to_get_verse_metadata.json new file mode 100644 index 0000000..3310951 --- /dev/null +++ b/schema/raw/response_to_get_verse_metadata.json @@ -0,0 +1,33 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Metadata", + "type": "object", + "required": [ + "description", + "extra", + "logo", + "name", + "particle", + "types" + ], + "properties": { + "description": { + "type": "string" + }, + "extra": { + "type": "string" + }, + "logo": { + "type": "string" + }, + "name": { + "type": "string" + }, + "particle": { + "type": "string" + }, + "types": { + "type": "string" + } + } +} diff --git a/src/contract.rs b/src/contract.rs index ad916e0..82ed564 100644 --- a/src/contract.rs +++ b/src/contract.rs @@ -4,6 +4,7 @@ use cosmwasm_std::{Addr, BankMsg, Binary, Coin, CosmosMsg, Decimal, Deps, DepsMu #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cw2::{ContractVersion, get_contract_version, set_contract_version}; +use cw_storage_plus::Bound; use cyber_std::{create_forget_thought_msg, Response}; use cyber_std::{create_creat_thought_msg, Load, Trigger}; @@ -694,7 +695,13 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult { QueryMsg::GetBlockRewards {} => { to_json_binary(&get_block_rewards(deps.storage)?) } - QueryMsg::GetVerseType {} => to_json_binary(&get_verse_type(deps.storage)?), + QueryMsg::GetSubnetMetadata { netuid } => { + to_json_binary(&get_subnet_metadata(deps.storage, netuid)?) + } + QueryMsg::GetSubnetsMetadata { start_after, limit } => { + to_json_binary(&get_subnets_metadata(deps.storage, start_after, limit)?) + } + QueryMsg::GetVerseMetadata {} => to_json_binary(&get_verse_metadata(deps.storage)?), QueryMsg::GetEconomy {} => to_json_binary(&get_economy(deps.storage)?), } } @@ -934,11 +941,39 @@ pub fn get_block_rewards( Ok(Coin::new(u128::from(block_rewards), denom)) } -pub fn get_verse_type( +pub fn get_subnet_metadata( + store: &dyn Storage, + netuid: u16, +) -> StdResult { + let subnet_meta = NETWORKS_METADATA.load(store, netuid)?; + Ok(subnet_meta) +} + +pub fn get_subnets_metadata( + store: &dyn Storage, + start_after: Option, + limit: Option, +) -> StdResult> { + let start = start_after.map(Bound::exclusive); + let subnets_limit = limit.unwrap_or(32) as usize; + + let subnets = NETWORKS_METADATA + .range(store, start, None, Order::Ascending) + .take(subnets_limit) + .map(|item| { + let (k, v) = item.unwrap(); + (k, v) + }) + .collect::>(); + + Ok(subnets) +} + +pub fn get_verse_metadata( store: &dyn Storage, ) -> StdResult { - let verse_type = VERSE_METADATA.load(store)?; - Ok(verse_type) + let verse_meta = VERSE_METADATA.load(store)?; + Ok(verse_meta) } pub fn get_economy( diff --git a/src/msg.rs b/src/msg.rs index bd85ad8..bf3c6d7 100644 --- a/src/msg.rs +++ b/src/msg.rs @@ -336,8 +336,15 @@ pub enum QueryMsg { #[returns(Coin)] GetBlockRewards {}, - #[returns(String)] - GetVerseType {}, + #[returns(Metadata)] + GetSubnetMetadata { netuid: u16 }, + #[returns(Vec<(u16, Metadata)>)] + GetSubnetsMetadata { + start_after: Option, + limit: Option, + }, + #[returns(Metadata)] + GetVerseMetadata {}, #[returns(EconomyData)] GetEconomy {},