Crates.io | generic-query |
lib.rs | generic-query |
version | 0.1.1 |
source | src |
created_at | 2022-10-17 05:08:40.426034 |
updated_at | 2022-12-12 20:40:07.451319 |
description | Generalizing ways to query and order |
homepage | |
repository | |
max_upload_size | |
id | 689802 |
size | 19,520 |
This CosmWasm based library helps querying contracts and dealing with query results.
You can see how it is used for qeneric query in rules contract, which is a part of CronCat contracts.
The main idea is to provide the structure with both information about query request and anticipated result:
pub struct GenericQuery {
pub contract_addr: String,
pub msg: Binary,
pub gets: Vec<ValueIndex>,
pub ordering: ValueOrdering,
pub value: Binary,
}
With contract_addr
and msg
one can create a query to the address with this message and parse this binary into json-like result.
gets
field defines the "path" throgh this result to specific element. For example, if the query result looks like this:
{
"members": [
{
"addr": "alice",
"weight": 1
},
{
"addr": "bob",
"weight": 2
}
]
}
then
let gets = vec![
ValueIndex::Key("members".to_string()),
ValueIndex::Index(1),
ValueIndex::Key("weight".to_string()),
]
points to the weight of Bob, which is 2.
value
field is the amount which will be compared with the query result.
This library also implements ValueOrd
trait for Value
.
pub trait ValueOrd {
fn lt_g(&self, other: &Self) -> StdResult<bool>;
fn le_g(&self, other: &Self) -> StdResult<bool>;
fn bt_g(&self, other: &Self) -> StdResult<bool>;
fn be_g(&self, other: &Self) -> StdResult<bool>;
fn equal(&self, other: &Self) -> bool;
}
That allows as to compare anticipated and received results (depending on whether they should be equal or one bigger than another).
You can see the usage example in the implementation of generic_query
from CronCat rules contract.