Using Injective Queries and Messages
Last updated
Last updated
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 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.
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.
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.
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.
Steps:
Construct the MsgCreateSpotMarketOrder
protobuf message with order details.
Encode it into bytes using prost::Message::encode
.
Wrap it in an AnyMsg
with the correct type_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
.
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.
Steps:
Create an ExchangeQuerier
from deps.querier
.
Call spot_market
with the market_id
.
Serialize the response to JSON and return it as a Binary
.
Steps:
Create a BankQuerier
from deps.querier
.
Call params
to fetch bank module parameters.
Serialize and return the result.
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 encode MsgBid
as an Any
message.
Tokenfactory Module: Encode MsgCreateDenom
or use TokenFactoryQuerier
.
Refer to for specific message types and queriers.