Skip to content

Instantly share code, notes, and snippets.

@mikedotexe
Last active August 10, 2022 09:21
Show Gist options
  • Save mikedotexe/efec8b667f9b2526b4a1f7596a4994cd to your computer and use it in GitHub Desktop.
Save mikedotexe/efec8b667f9b2526b4a1f7596a4994cd to your computer and use it in GitHub Desktop.
Getting the To-Do List contract deployed to a local `wasmd` blockchain

To-Do List locally

We're going to get a local wasmd blockchain going, so we'll install that according to these instructions (after cloning the repo): https://github.com/CosmWasm/wasmd#quick-start

It's not a bad idea to create a new account in Keplr for testnet. We can use that seed phrase to store the same private key on your file system, securly via the operating system. What I mean by that is Mac users will see this when trying to use an app that wants to access it.

After you create an account on Keplr, you can find the mnemonic here:

Now we can import the key:

wasmd keys add mytestnet --recover

Set up wasmd

wasmd init jabroni --chain-id cc-23
wasmd keys add validator
wasmd add-genesis-account $(wasmd keys show validator -a) 10000000000000000000000000stake
wasmd gentx validator 1000000000000000stake --chain-id cc-23
wasmd collect-gentxs
wasmd start

You should have a blockchain running on your computer, bursting out logs every 6 seconds or so as a new block is produced. In blockchain land, blocks are produced even if there aren't any transactions to include in it, and there wouldn't be any because we haven't interacted with it yet.

You can hit Control+C to stop it while we clone a simple smart contract repository on GitHub. To restart it, use:

wasmd start

Get To-Do List

Clone this repository: https://github.com/InterWasm/cw-contracts

Navigate there in your terminal application and make your way into: contracts/cw-to-do-list

Make sure you have Rust and the wasm32… thing covered in these instructions: https://tutorials.cosmos.network/academy/3-my-own-chain/cosmwasm.html#install

Then run: RUSTFLAGS='-C link-arg=-s' cargo build --release --target wasm32-unknown-unknown --locked

This has compiled the smart contract, which is not optimized but it's fine for now. It lives in this annoying location, basically look at this pattern:

…/cw-contracts/contracts/cw-to-do-list/target/wasm32-unknown-unknown/release/cw_to_do_list.wasm

We're going to deploy that smart contract to our local chain.

Deploy locally

Let's create a user that will be deploying the smart contract. Sure, we could use the validator we created, but why not learn something.

wasmd keys add deployer

If we tried to deploy with this account now, we'd get something about key not found because we created a random key, but the blockchain doesn't care yet. It can't care since there are virtually infinite private keys out there, so it only knows about accounts that have had funds sent to them. Only then is an account "stored" on a blockchain, really.

So let's have validator send deployer some tokens:

wasmd tx bank send $(wasmd keys show validator -a) $(wasmd keys show deployer -a) 666000000stake --chain-id cc-23 -y --output json

Now we can deploy:

wasmd tx wasm store target/wasm32-unknown-unknown/release/cw_to_do_list.wasm --from deployer --chain-id cc-23 --gas-prices 0.025stake --gas auto --gas-adjustment 1.3 --broadcast-mode block --output json -y

What I like to do is copy that result and put it into this free, open-source program called Boop and format the JSON so it's readable. (Cmd+B to open the dialogue and type "Format JSON")

Download: https://boop.okat.best

If you copy/paste your result from the upload (wasmd tx wasm store …) you might want to glance at the code first; it's near the top of the structure. If that's 0 it was successful.

Now search for code_id and we'll find the unique number for this upload. This is our code id, which is a simple counter that increases whenever a contract is uploaded.

So now your code — your smart contract bytecode — is stored in this public arena where others also upload bytecode. But this thing doesn't have state yet, only the business logic, so it's actually not a smart contract yet. Put another way: it's a backend that hasn't been hooked up to a database yet. Cosmos does an interesting thing with contracts. You can reduce repetitive bytecode if you instantiate a smart contract from the same source, but let it have its own state. So as soon as we uploaded the contract, anyone (including us) can instantiate a contract, and that command will return us stuff, including the contract address.

Instantiate:

wasmd tx wasm instantiate 1 '{}' --from deployer --label "todo" --chain-id cc-23 --gas-prices 0.025stake --gas auto --gas-adjustment 1.3 --broadcast-mode block --no-admin -y

Note that you may instantiate smart contracts with parameters, and there actually is one optional parameter we could send here, but aren't. It would go in the part with '{}' and look like '{"key": "value"}'.

After instantiating, your terminal will spit out some stuff, and toward the end there's a raw_log thing to look at in pretty form.

This is your smart contract address. I'll use this in future commands but you'll change it.

Calling the contract

Add a new "to do" item:

wasmd tx wasm execute wasm14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s0phg4d '{"new_entry":{"id":0,"description":"light jog"}}' --from deployer --chain-id cc-23 --gas-prices 0.025stake --gas auto --gas-adjustment 1.3 --broadcast-mode block -y

View that:

wasmd query wasm contract-state smart wasm14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s0phg4d '{"query_list":{}}'

and you should get:

data:
  entries:
  - description: light jog
    id: 1
    priority: None
    status: ToDo

Astute readers may wonder why the id is 1 here when we said it should be 0, and you are right to join the confusion. Onward.


Eventually, we can deploy to testnet if we want, since we have a key locally. Note: you won't have any "fake" testnet JUNO in that account you just created, so you'll want to use their Discord bot faucet. Join this Discord, follow directions, and look for the faucet channel: https://discord.gg/Juno

You'll request testnet tokens with a command that looks like: $request juno123abcyouraddresshere…

Note that in Keplr there's a dropdown for different networks. There's likely a better way, but you can "add Juno testnet" by going to https://testnet.daodao.zone and clicking around.

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