Initialize the Contract

We don't specify it here, but in almost all cases you should provide an --admin address when instantiating a contract. If you do not, you will not be able to migrate the contract later.

CosmWasm Smart Contracts take their arguments as serialized JSON. This can be created a number of ways, but as we showed in the previous examples, it may well be easiest to use the node command line, if that is available to you.

There is a Typescript helpers file for most contracts, and extensions for CosmJS, but at the time of writing, they are broken. This will no doubt be fixed soon, providing an alternative way of interacting with contracts other than the CLI.

To use the node REPL, type node in the terminal.

const initobj = {
  admins: ["<your-validator-self-delegate-key>"],
  mutable: false
};

< undefined

JSON.stringify(initobj);

< '{"admins":["<your-validator-self-delegate-key>"],"mutable":false}'

With these encoded arguments, you can now instantiate the contract, using the code_id from the previous step.

sourced tx wasm instantiate <code-id> '{"admins":["<your-validator-self-delegate-key>"],"mutable":false}' --amount 50000usource --label "CW1 example contract" --from <your-key> --chain-id <chain-id> \
  --gas-prices 0.1usource --gas auto --gas-adjustment 1.3 -b block -y

Once the contract is instantiated, you can find out its contract address:

sourced query wasm list-contract-by-code <code-id>

You will need this to interact with the contract.

Last updated