Queries
This section details queries and their structure. For general information on writing conditions, visit the Conditions and expressions page. To view an example of a query variable in a template, visit the template guide.
Queries are written as variables that select a value from a query message. The structure of a QueryVariable contains a selector, a query request, and a name. Any type of query supported by Cosmos can be used in a condition. For more information on query variables and update functions, visit the Variables page.
Query variables can be used as one or both of the values in a general expression, or in boolean expressions. When query expressions are used in a condition, the JSON of a query message is sent out, and the value specified by the selector string is returned as the value to be compared in the general expression.
_10pub struct QueryExpr {
_10 pub selector: String,
_10 pub query: QueryRequest<String>,
QueryRequest
Warp conditions support any query type available using the QueryRequest
struct, which outlines all possible query calls. The possible query types are Bank
, Custom
, Staking
, Stargate
, IBC
, and Wasm
.
_21pub enum QueryRequest<C> {
_21 #[cfg(feature = "staking")]
_21 Staking(StakingQuery),
_21 /// A Stargate query is encoded the same way as abci_query, with path and protobuf encoded request data.
_21 /// The format is defined in [ADR-21](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-021-protobuf-query-encoding.md).
_21 /// The response is protobuf encoded data directly without a JSON response wrapper.
_21 /// The caller is responsible for compiling the proper protobuf definitions for both requests and responses.
_21 #[cfg(feature = "stargate")]
_21 /// this is the fully qualified service path used for routing,
_21 /// eg. custom/cosmos_sdk.x.bank.v1.Query/QueryBalance
_21 /// this is the expected protobuf message type (not any), binary encoded
_21 #[cfg(feature = "stargate")]
Bank Queries
Bank queries are used to query information from the Cosmos bank module.
_14 /// This calls into the native bank module for querying the total supply of one denomination.
_14 /// It does the same as the SupplyOf call in Cosmos SDK's RPC API.
_14 /// Return value is of type SupplyResponse.
_14 #[cfg(feature = "cosmwasm_1_1")]
_14 Supply { denom: String },
_14 /// This calls into the native bank module for one denomination
_14 /// Return value is BalanceResponse
_14 Balance { address: String, denom: String },
_14 /// This calls into the native bank module for all denominations.
_14 /// Note that this may be much more expensive than Balance and should be avoided if possible.
_14 /// Return value is AllBalanceResponse.
_14 AllBalances { address: String },
Supply
Returns the total supply of a denomination.
Balance
Queries the amount of a specific denomination for a given address.
_10 "address": "<String>",
AllBalances
This query calls into the native bank module for all denominations. Note that this may be much more expensive than Balance and should be avoided if possible. The return value is AllBalanceResponse.
_10 "address": "<String>"
Staking Queries
Staking queries are used to query information from the Cosmos staking module.
_24pub enum StakingQuery {
_24 /// Returns the denomination that can be bonded (if there are multiple native tokens on the chain)
_24 /// AllDelegations will return all delegations by the delegator
_24 AllDelegations { delegator: String },
_24 /// Delegation will return more detailed info on a particular
_24 /// delegation, defined by delegator/validator pair
_24 /// Returns all validators in the currently active validator set.
_24 /// The query response type is `AllValidatorsResponse`.
_24 /// Returns the validator at the given address. Returns None if the validator is
_24 /// not part of the currently active validator set.
_24 /// The query response type is `ValidatorResponse`.
_24 /// The validator's address (e.g. (e.g. cosmosvaloper1...))
BondedDenom
Returns all bonded denominations. Terra only uses uluna
.
AllDelegations
Returns all delegations of a delegator.
_10 "delegator": "<terra_address>"
_13 "delegator": "<terra_address>",
_13 "validator": "<terravaloper_address>",
_13 "amount": "190886529"
Delegation
Returns detailed delegation information.
_10 "delegator": "<terra_address>",
_10 "validator": "<terravaloper_address>"
_20 "delegator": "<terra_address>",
_20 "validator": "<terravaloper_address>",
_20 "amount": "190886529"
_20 "accumulated_rewards": [
_20 "amount": "190886529"
AllValidators
Returns the entire active validator set.
_19 "address": "<terravaloper_address>",
_19 "commission": "0.000000000000000000",
_19 "max_commission": "0.200000000000000000",
_19 "max_change_rate": "0.010000000000000000"
_19 "address": "<terravaloper_address>",
_19 "commission": "0.050000000000000000",
_19 "max_commission": "0.100000000000000000",
_19 "max_change_rate": "0.010000000000000000"
Validator
Returns the validator at the given address.
_10 "address": "<String>"
_10 "address": "<terravaloper_address>",
_10 "commission": "0.050000000000000000",
_10 "max_commission": "0.200000000000000000",
_10 "max_change_rate": "0.200000000000000000"
Wasm Queries
Wasm queries are used to query information from the Cosmos Wasm module. For WASM queries, you must know the format of your Response.
_19 /// this queries the public API of another contract at a known address (with known ABI)
_19 /// Return value is whatever the contract returns (caller should know), wrapped in a
_19 /// ContractResult that is JSON encoded.
_19 contract_addr: String,
_19 /// msg is the json-encoded QueryMsg struct
_19 /// this queries the raw kv-store of the contract.
_19 /// returns the raw, unparsed data stored at that key, which may be an empty vector if not present
_19 contract_addr: String,
_19 /// Key is the raw key used in the contracts Storage
_19 /// returns a ContractInfoResponse with metadata on the contract from the runtime
_19 ContractInfo { contract_addr: String },
Smart
Queries the public API of another contract at a known address (with known ABI). The return value is wrapped in a ContractResult that is JSON encoded. The msg
is the json-encoded QueryMsg
struct.
_10 "contract_addr": "<String>",
Raw
Queries the raw kv-store of the contract. Returns the raw, unparsed data stored at that key, which may be an empty vector if not present.
_10 "contract_addr": "<String>",
ContractInfo
Returns a ContractInfoResponse
with metadata on the contract from the runtime
_10 "contract_addr": "<String>"
_25pub struct ContractInfoResponse {
_25 /// address that instantiated this contract
_25 /// admin who can run migrations (if any)
_25 pub admin: Option<String>,
_25 /// if set, the contract is pinned to the cache, and thus uses less gas when called
_25 /// set if this contract has bound an IBC port
_25 pub ibc_port: Option<String>,
_25impl ContractInfoResponse {
_25 /// Convenience constructor for tests / mocks
_25 pub fn new(code_id: u64, creator: impl Into<String>) -> Self {
_25 creator: creator.into(),
Selectors
Selector strings are used in query expressions to select the JSON path of the query response. You can think of a selector string as the path that Warp needs to take within that particular query response to get the value that needs comparing.
Example:
To select "bend"
as the desired portion of the query response below, the $.address.city
selector is used. The $
in the selector refers to the root object/element.
_10 "streetAddress": "high street",
_10 "postalCode": "97701"
For a hands-on example of JSON paths and selectors, visit the JSON path evaluator.
To learn more about JSON selectors, visit the JSON path Github repo.