Query A Contract
Describes how to query a cosmwasm smart contract with the CLI & REST API.
Command Line Interface
The most common way to query a cosmwasm smart contract is within the junod wasm smart query command. This follows the following format where query is a JSON string with no spaces. By default, the least amount of data this can be is an empty JSON payload '{}'.
For this example, we are going to use a random NFT contract on the juno chain. This will show you how to brute force query a contract if you have no idea what the query schema of the contract is. At this time, there is no way to query the format of a contract's requests, but this is something many are actively working on.
Now we attempt to query this contract address and extract some data from it and get which queries are allowed. As you can see, we pass through a random payload for abcde so that the contract will return actual valid query requests
NOTE: A Query can never be empty such as '{}'
given you need to specify the path of data you want to reach.
The query shows CW721 Base is this contracts name. As this is a standard contract, all messages can be found in the CosmWasm/cw-nfts repository on github https://github.com/CosmWasm/cw-nfts/blob/main/contracts/cw721-base/src/msg.rs
From this, we now know all of the query endpoints and can requests something more specific from the contract for our usage. Let's get
Here we can see there are 8 tokens in this set. Lets query one of the NFTs information
Just like the first query, we can see that the payload needs more information. It returned an error that we need to specify the token_id we want the nft_info for. Note, Uint128 sized numbers are read as a string
Rest API Query
If you wish to query the data more programmatically with an application such as Python, you may be better suited to use the rest API. You can find these endpoints on https://cosmos.directory/juno/nodes in the REST section.
This query endpoint can be found via Juno's SwaggerUI. However, some modules you will not be able to easily find the endpoint. To do this, you will need to search through the proto files. Here we know we want to query the cosmwasm module, which is called wasmd on chain. This repo is found at https://github.com/cosmwasm/wasmd. In this module, you can see the proto folder in the root of the repo. This will house the endpoints the module exposes so we can find the above path which. This is a query so we find the query proto file https://github.com/CosmWasm/wasmd/blob/main/proto/cosmwasm/wasm/v1/query.proto\
You must base64 encode the JSON payload for REST API Request. Just take your JSON payload and - put it into https://www.base64encode.org/ - or use Mac / Linux built in command echo '{"all_tokens":{}}' | base64 # eyJhbGxfdG9rZW5zIjp7fX0K
With this, we can now query the contract and gather the data. You can use your web browser, or a library like httpx / requests in Python for automated bots. Be aware that many API providers will late limit the number of requests you can make. https://api.juno.strange.love/cosmwasm/wasm/v1/contract/juno1anh4pf98fe8uh64uuhaasqdmg89qe6kk5xsklxuvtjmu6rhpg53sj9uejj/smart/eyJhbGxfdG9rZW5zIjp7fX0K
Cosmology Smart Contract Query
Using CosmWasm/ts-codegen, you can create an NPM module to make interactions and queries into dev-friendly Typescript classes to allow you to focus on shipping code.
Here are a few tutorials from cosmology:
CosmJS Query
TODO: Add how to query a smart contract data with Typescript + Example through the RPC endpoint (proto encoded) & httpbatch tendermint client
Internal
TODO: internal rust impl of how to WasmQuery a contract and get some data back.
Last updated