Skip to content

Instantly share code, notes, and snippets.

@the-frey
Last active January 5, 2023 05:12
Show Gist options
  • Save the-frey/39549a9a58dc91c7a57bb5f0ccb4f3f2 to your computer and use it in GitHub Desktop.
Save the-frey/39549a9a58dc91c7a57bb5f0ccb4f3f2 to your computer and use it in GitHub Desktop.
# assuming you already have cargo
# check with
cargo version
# then install
cargo install cargo-generate --features vendored-openssl
cargo install cargo-run-script
# now you can generate
cargo generate --git https://github.com/InterWasm/cw-template.git --name dummies_example
# in another tab, you can run juno
# pull the repo & checkout main first
STAKE_TOKEN=ujunox UNSAFE_CORS=true docker-compose up
# then before the next steps you may find it easier to have txflags set
TXFLAG="--gas-prices 0.1ujunox --gas auto --gas-adjustment 1.3 -y -b block --chain-id testing --node http://localhost:26657/"
# if you are on an M1 mac you will have to run those commands _inside_ the container
# notice that juno_node_1 is the default
# on e.g. a linux box with juno installed
# the correct ports will be open to talk to juno running in docker
# even so, proably easier to use $BINARY as below
BINARY="docker exec -i juno_node_1 junod"
# in your cw repo - AMD/Intel version
docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/rust-optimizer:0.12.6
# on M1 mac/ARM - note this will not work in prod!
docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/rust-optimizer-arm64:0.12.6
# look for artifact
ls -la artifacts
# copy to the container
# the name will depend on whether you were AMD or ARM
docker cp artifacts/<wasm_binary_name>.wasm juno_node_1:/<wasm_binary_name>.wasm
# store
# note that validator key is added automagically
CONTRACT_CODE=$($BINARY tx wasm store "/<wasm_binary_name>.wasm" --from validator $TXFLAG --output json | jq -r '.logs[0].events[-1].attributes[0].value')
echo "Stored: $CONTRACT_CODE"
# if you wanted to provision the default user:
echo "clip hire initial neck maid actor venue client foam budget lock catalog sweet steak waste crater broccoli pipe steak sister coyote moment obvious choose" | $BINARY keys add test-user --recover --keyring-backend test
# instantiate
INIT='{
"count": 42
}'
# now we can instantiate
$BINARY tx wasm instantiate $CONTRACT_CODE "$INIT" --from "validator" --label "my first contract" $TXFLAG --no-admin
# the previous command will return the contract code, but we can query it:
$BINARY q wasm list-contract-by-code 1
# query the contract address
CONTRACT_ADDRESS=$($BINARY q wasm list-contract-by-code $CONTRACT_CODE --output json | jq -r '.contracts[-1]')
echo $CONTRACT_ADDRESS
# query state - should return 42
$BINARY q wasm contract-state smart $CONTRACT_ADDRESS '{"get_count":{}}' --output json
# execute changes state - will inc to 43
$BINARY tx wasm execute $CONTRACT_ADDRESS '{"increment":{}}' --from test-user $TXFLAG
# query state again - should return 43
$BINARY q wasm contract-state smart $CONTRACT_ADDRESS '{"get_count":{}}' --output json
# but we fear change, so we reset it to 42
$BINARY tx wasm execute $CONTRACT_ADDRESS '{"reset":{"count": 42}}' --from test-user $TXFLAG
# query state a final time - it is now 42
$BINARY q wasm contract-state smart $CONTRACT_ADDRESS '{"get_count":{}}' --output json
@arnabghose997
Copy link

@the-frey Hi, I am trying to run the cosmwasm/rust-optimizer container (Intel). However, I am getting this error while running the command:

.
.
.
Building contract in /code ...
   Compiling dummies-example v0.1.0 (/code)
error: can't compile schema generator for the `wasm32` arch
       hint: are you trying to compile a smart contract without specifying `--lib`?
  --> src/bin/schema.rs:6:5
   |
6  | /     write_api! {
7  | |         instantiate: InstantiateMsg,
8  | |         execute: ExecuteMsg,
9  | |         query: QueryMsg,
10 | |     }
   | |_____^
   |
   = note: this error originates in the macro `write_api` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `QueryMsg: QueryResponses` is not satisfied
  --> src/bin/schema.rs:6:5
   |
6  | /     write_api! {
7  | |         instantiate: InstantiateMsg,
8  | |         execute: ExecuteMsg,
9  | |         query: QueryMsg,
10 | |     }
   | |_____^ the trait `QueryResponses` is not implemented for `QueryMsg`
   |
   = note: this error originates in the macro `write_api` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `dummies-example` due to 2 previous errors

@the-frey
Copy link
Author

the-frey commented Jan 4, 2023

I think you're using a newer version of the cosmwasm template than the one in our tutorial so the API and commands have changed slightly (as the error seems to suggest that the schema is the 1.1 API, not the 1.0 API from the tutorial)

@arnabghose997
Copy link

Oops my bad! I noticed it now. Thanks @the-frey !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment