Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add queries to wasmd section #141

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 76 additions & 4 deletions src/pages/wasmd/getting-started/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,14 @@ sleep 6
wasmd q tx $(echo "$RESP" | jq -r '.txhash') -o json | jq
```

### Query contract state
### Query Contract State: All

To query the state of a WASM contract, you can use the following command.
`All` queries allow you to retrieve all the key-value pairs stored in the contract's state. This
method directly accesses the contract's underlying key-value storage without invoking any contract
logic. It's useful for inspecting the entire state of a contract, especially during development or
debugging.

To query the state of a WASM contract, you can use the following command:

```sh
wasmd query wasm contract-state all "$CONTRACT" -o json
Expand All @@ -539,9 +544,10 @@ The output will look similar to this:
}
```

`Models` are key-value pairs representing the state data of the contract base64-encoded.
The `"models"` array contains key-value pairs representing the state data of the contract, with both
keys and values base64-encoded.

We can decode the contract state using the following command:
You can decode the contract state using the following command:

```sh
wasmd query wasm contract-state all "$CONTRACT" -o json | jq -r '.models[0].value' | base64 -d
Expand All @@ -557,6 +563,72 @@ The output will be similar to the following:
}
```

### Query Contract State: Raw

`Raw` queries allow you to fetch data directly from the contract's key-value storage using a
specific key. This method bypasses the contract's logic and retrieves the raw stored data, which is
base64-encoded. It's useful when you know the exact key of the data you're interested in.

To query the state of a WASM contract for a specific key, you can use the following command:

```sh
# Set the state key you want to query
KEY="636F6E666967"
wasmd q wasm contract-state raw $CONTRACT $KEY -o json
```

The output will look similar to this:

```json
{
"data": "eyJ2ZXJpZmllciI6Indhc20xNzlhdnc5NmFheTcwcHM5OXVtdWFlc3h4Y3p3YzBxbTVnd3VmeGciLCJiZW5lZmljaWFyeSI6Indhc20xNDI3a3BxOW1tbmZwMG1hZGs1YXhoMnVrbWpncGZoNnNremR4a3UiLCJmdW5kZXIiOiJ3YXNtMTc5YXZ3OTZhYXk3MHBzOTl1bXVhZXN4eGN6d2MwcW01Z3d1ZnhnIn0="
}
```

`"data"` represents the base64-encoded state data of the contract for the given key.

You can decode the `data` field using the following command:

```sh
wasmd q wasm contract-state raw $CONTRACT $KEY -o json | jq -r '.data' | base64 -d
```

The output will be similar to the following:

```json
{
"verifier": "wasm1hvgm6p76gccgg4dl4caa8a7v03dsqww6r9sk4g",
"beneficiary": "wasm1pa29lac5s85kgj7pn9z6gc0t4sqgzllcguhf24",
"funder": "wasm1hvgm6p76gccgg4dl4caa8a7v03dsqww6r9sk4g"
}
```

### Query Contract State: Smart

`Smart` queries allow you to retrieve structured data by interacting with the contract's query
endpoints. This method invokes the contract's logic with a specific query message, allowing you to
get processed and meaningful data from the contract. The content of the query message depends on the
specific contract you're interacting with, as each contract defines its own set of query messages
and expected formats (see the [query section](../../core/entrypoints/query) for more details).

To query the state of a WASM contract for specific data, you can use the following command:
chipshort marked this conversation as resolved.
Show resolved Hide resolved

```sh
# Set the query for the data you want to retrieve
QUERY='{"verifier": {}}'
wasmd q wasm contract-state smart $CONTRACT $QUERY -o json
```

The output will look similar to this:

```json
{
"data": {
"verifier": "wasm179avw96aay70ps99umuaesxxczwc0qm5gwufxg"
}
}
```

## Migration

Migration is the process of upgrading an existing contract to a new version without changing its
Expand Down