generic-query

Crates.iogeneric-query
lib.rsgeneric-query
version0.1.1
sourcesrc
created_at2022-10-17 05:08:40.426034
updated_at2022-12-12 20:40:07.451319
descriptionGeneralizing ways to query and order
homepage
repository
max_upload_size
id689802
size19,520
Trevor Clarke (TrevorJTClarke)

documentation

README

Generic query

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.

GenericQuery structure

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.

Commit count: 0

cargo fmt