use cosmwasm_std::{Addr, Deps, Order, StdResult};
use cw_storage_plus::Bound;
use crate::msg::{EntryResponse, ListResponse};
use crate::state::entries;
const MAX_LIMIT: u32 = 30;
const DEFAULT_LIMIT: u32 = 20;
pub fn query_entry(deps: Deps, id: u64) -> StdResult<EntryResponse> {
let entry = entries().load(deps.storage, id)?;
Ok(EntryResponse {
id,
neuron: entry.neuron,
network: entry.network,
protocol: entry.protocol,
endpoint: entry.endpoint,
owner: entry.owner.to_string(),
particle: entry.particle,
})
}
pub fn query_list_by_owner(
deps: Deps,
start_after: Option<u64>,
limit: Option<u32>,
owner: Addr,
) -> StdResult<ListResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(|id| Bound::exclusive(id.to_string()));
let entries: StdResult<Vec<_>> = entries()
.idx
.owner
.prefix(owner.to_string())
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.collect();
Ok(ListResponse {
entries: entries?.into_iter().map(|l| l.1).collect(),
})
}
pub fn query_list_by_network(
deps: Deps,
start_after: Option<u64>,
limit: Option<u32>,
network: String,
) -> StdResult<ListResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(|id| Bound::exclusive(id.to_string()));
let entries: StdResult<Vec<_>> = entries()
.idx
.network
.prefix(network)
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.collect();
Ok(ListResponse {
entries: entries?.into_iter().map(|l| l.1).collect(),
})
}
pub fn query_list_by_protocol(
deps: Deps,
start_after: Option<u64>,
limit: Option<u32>,
protocol: String,
) -> StdResult<ListResponse> {
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(|id| Bound::exclusive(id.to_string()));
let entries: StdResult<Vec<_>> = entries()
.idx
.protocol
.prefix(protocol)
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.collect();
Ok(ListResponse {
entries: entries?.into_iter().map(|l| l.1).collect(),
})
}
cw-cyber/contracts/hub-skills/src/query.rs
ฯ 0.0%
use ;
use Bound;
use crate;
use crateentries;
const MAX_LIMIT: u32 = 30;
const DEFAULT_LIMIT: u32 = 20;