Skip to content

This is a basic CosmWasm smart contract that allows you to set a counter and then either increment or reset it. You can also query the current counter value.

License

Notifications You must be signed in to change notification settings

burnt-labs/cw-counter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CW Counter Starter Contract

This is a basic CosmWasm smart contract that allows you to set a counter and then either increment or reset it. You can also query the current counter value.


Prerequisites

Before deploying the contract, ensure you have the following:

  1. XION Daemon (xiond)
    Follow the official guide to install xiond:
    Interact with XION Chain: Setup XION Daemon

  2. Docker
    Install and run Docker, as it is required to compile the contract.


Deploy and Interact with the Contract

Step 1: Clone the Repository

git clone https://github.com/burnt-labs/cw-counter
cd cw-counter

Step 2: Compile and Optimize the Wasm Bytecode

Run the following command to compile and optimize the contract:

docker run --rm -v "$(pwd)":/code \
  --mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \
  --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
  cosmwasm/optimizer:0.16.0

Note:
This step uses CosmWasm's Optimizing Compiler, which reduces the contract's binary size, making it more efficient for deployment.
Learn more here.

The optimized contract will be stored as:

cw-counter/artifacts/cw_counter.wasm

Step 3: Upload the Bytecode to the Blockchain

First, set your wallet address:

WALLET="your-wallet-address-here"

Now, upload the contract to the blockchain:

RES=$(xiond tx wasm store ./artifacts/cw_counter.wasm \
      --chain-id xion-testnet-1 \
      --gas-adjustment 1.3 \
      --gas-prices 0.1uxion \
      --gas auto \
      -y --output json \
      --node https://rpc.xion-testnet-1.burnt.com:443 \
      --from $WALLET)

After running the command, extract the transaction hash:

echo $RES

Example output:

{
  "height": "0",
  "txhash": "B557242F3BBF2E68D228EBF6A792C3C617C8C8C984440405A578FBBB8A385035",
  ...
}

Copy the transaction hash for the next step.


Step 4: Retrieve the Code ID

Set your transaction hash:

TXHASH="your-txhash-here"

Query the blockchain to get the Code ID:

CODE_ID=$(xiond query tx $TXHASH \
  --node https://rpc.xion-testnet-1.burnt.com:443 \
  --output json | jq -r '.events[-1].attributes[1].value')

Now, display the retrieved Code ID:

echo $CODE_ID

Example output:

1213

Step 5: Instantiate the Contract

Set the contract's initialization message:

MSG='{ "count": 1 }'

Instantiate the contract with the Code ID from the previous step:

xiond tx wasm instantiate $CODE_ID "$MSG" \
  --from $WALLET \
  --label "cw-counter" \
  --gas-prices 0.025uxion \
  --gas auto \
  --gas-adjustment 1.3 \
  -y --no-admin \
  --chain-id xion-testnet-1 \
  --node https://rpc.xion-testnet-1.burnt.com:443

Example output:

gas estimate: 217976
code: 0
txhash: 09D48FE11BE8D8BD4FCE11D236D80D180E7ED7707186B1659F5BADC4EC116F30

Copy the new transaction hash for the next step.


Step 6: Retrieve the Contract Address

Set the new transaction hash:

TXHASH="your-txhash-here"

Query the blockchain to get the contract address:

CONTRACT=$(xiond query tx $TXHASH \
  --node https://rpc.xion-testnet-1.burnt.com:443 \
  --output json | jq -r '.events[] | select(.type == "instantiate") | .attributes[] | select(.key == "_contract_address") | .value')

Display the contract address:

echo $CONTRACT

Example output:

xion1v6476wrjmw8fhsh20rl4h6jadeh5sdvlhrt8jyk2szrl3pdj4musyxj6gl

About

This is a basic CosmWasm smart contract that allows you to set a counter and then either increment or reset it. You can also query the current counter value.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published