Using Injective Queries and Messages
This guide provides a comprehensive overview of how to interact with Injective's modules and queries in CosmWasm using Any
messages and queries. The older injective-cosmwasm package, which relied on JSON-encoded messages, is no longer maintained and may become outdated. This guide focuses on the recommended approach using protobuf-encoded Any
messages and queries, which is more efficient and aligned with modern CosmWasm standards.
What are Any
Messages in CosmWasm?
Any
Messages in CosmWasm?In CosmWasm, Any
messages are part of the CosmosMsg
enum, allowing you to send messages wrapped in a protobuf Any
type supported by the chain. They replace the deprecated Stargate
messages (still available under the stargate
feature flag) with improved naming and syntax. Any
messages are feature-gated with cosmwasm_2_0
, meaning they require a chain running CosmWasm 2.0 which is supported by Injective. Here’s a snippet of the CosmosMsg
definition:
The type_url
specifies the protobuf message type, and value
contains the serialized message data.
Why Use This Method?
The injective-cosmwasm
package used JSON-based messages, which are less efficient and may not remain compatible with future updates. The new Any
message approach uses protobuf encoding, offering better performance, type safety, and compatibility with CosmWasm 2.0+. This is now the recommended method for interacting with Injective's modules and queries.
Sending Messages
To send messages, you create a protobuf message, encode it, and wrap it in an Any
message. Below is an example of creating a spot market order on Injective's exchange module.
Example: Creating a Spot Market Order
Steps:
Construct the
MsgCreateSpotMarketOrder
protobuf message with order details.Encode it into bytes using
prost::Message::encode
.Wrap it in an
AnyMsg
with the correcttype_url
.Return it as a
CosmosMsg::Any
.
This approach can be adapted for other modules (e.g., auction, tokenfactory) by using the appropriate protobuf message and type_url
.
Performing Queries
Queries are performed using QuerierWrapper
with InjectiveQueryWrapper
. You can use pre-built queriers from injective_std
or send raw queries. Below are examples covering different modules.
Example: Querying a Spot Market (Exchange Module)
Steps:
Create an
ExchangeQuerier
fromdeps.querier
.Call
spot_market
with themarket_id
.Serialize the response to JSON and return it as a
Binary
.
Example: Querying Bank Parameters (Bank Module)
Steps:
Create a
BankQuerier
fromdeps.querier
.Call
params
to fetch bank module parameters.Serialize and return the result.
Working with Other Modules
The same principles apply to other Injective modules like auction, insurance, oracle, permissions, and tokenfactory, as well as the Cosmos native modules. For example:
Auction Module: Use
AuctionQuerier
for queries or encodeMsgBid
as anAny
message.Tokenfactory Module: Encode
MsgCreateDenom
or useTokenFactoryQuerier
.
Refer to injective_std for specific message types and queriers.
Last updated