How to deploy a CosmWasm contract

After setting up your Landslide local network, follow these steps to deploy and interact with smart contracts.

Important Note on Terminal Usage

Before proceeding, it's crucial to understand the correct setup:

  1. Keep your original terminal window running the Landslide network (make run-wasm command) open and active. This terminal maintains your local network.

  2. Open a new terminal window to execute the following commands. This separation allows you to interact with the network without interrupting its operation.

This two-terminal approach provides several benefits:

  • Keeps the network running uninterrupted

  • Allows easy reference to network logs while executing commands

  • Prevents accidental network stoppage

  • Facilitates easier troubleshooting

Now, in your new terminal window, proceed with the following steps:

1. Build and Install Wasmd

Wasmd is the CLI tool for interacting with CosmWasm contracts.

a. Clone the Wasmd repository:

git clone https://github.com/CosmWasm/wasmd.git && cd wasmd

b. Checkout the most stable version (replace v0.53.0 if needed):

git checkout v0.53.0

c. Install Wasmd:

make install

d. Verify the installation:

wasmd version

or

~/go/bin/wasmd version

2. Import Private Key

When running a local testnet, as opposed to connecting to a live testnet or mainnet, you need to use a pre-configured account that has been allocated tokens in the genesis state. This differs from a live network, where you would typically create a new account and obtain tokens through a faucet or a transfer.

In a local testnet environment:

  • The network is initialized with a set of pre-defined accounts.

  • These accounts are already funded with tokens in the genesis block.

  • One of these accounts is designated for testing and development purposes.

To interact with the network, you'll need to import this specific testnet private key:

~/go/bin/wasmd keys add privatekey --recover

When prompted, enter the following bip39 mnemonic (seed phrase):

tip yard art tape orchard universe angle flame wave gadget raven coyote crater ethics able evoke luxury predict leopard delay peanut embody blast soap

This mnemonic corresponds to a pre-funded account in your local testnet. By importing this key, you're gaining access to an account that already has tokens, allowing you to perform transactions, deploy contracts, and interact with the network without acquiring tokens first.

Important Notes:

  • This specific key should only be used for local testing purposes.

  • Never use this key on a public testnet or mainnet, as the private key is publicly known.

  • In a production environment, you would generate your own secure keys and properly manage them.

Understanding this process helps clarify the difference between working with a local testnet and interacting with a live network, where account creation and token acquisition would be handled differently.

3. Deploy Smart Contract

Now, you're ready to deploy a smart contract to your Landslide network.

a. Upload an example contract (in this case, hackatom.wasm):

~/go/bin/wasmd tx wasm store "./x/wasm/keeper/testdata/hackatom.wasm" \
 --from privatekey \
 --gas=auto --gas-adjustment=1.15 \
 --node "REPLACE_WITH_YOUR_RPC_URL" \
 --chain-id=landslide-test

Important:

  • Replace [REPLACE_WITH_YOUR_RPC_URL] with the RPC URL provided in your Landslide network output. This is visible in the first terminal where you started your Landslide node. It should look something like http://127.0.0.1:9750/ext/bc/....

  • At this point, you'll be prompted to confirm the transaction. Review the details carefully and enter 'y' to confirm and proceed with signing and broadcasting, or 'N' to cancel the transaction.

After executing the command to upload the smart contract, you should see an output similar to this:

Copycode: 0
codespace: ""
data: ""
events: []
gas_used: "0"
gas_wanted: "0"
height: "0"
info: ""
logs: []
raw_log: ""
timestamp: ""
tx: null
txhash: F15A7FB824FA83D58E556224FE2D5BD59A4FC7FB8E50400D5B0649E78D873D53

Great! Your smart contract has been successfully uploaded to the Landslide network. Here's what you need to know:

  1. The code: 0 indicates that the transaction was successful.

  2. The most important piece of information is the txhash. Yours will look different, but in this case, it's: F15A7FB824FA83D58E556224FE2D5BD59A4FC7FB8E50400D5B0649E78D873D53

Next Steps: You'll need this transaction hash for the next step, which is to fetch the code_id of your uploaded contract. The code_id is crucial for future interactions with your contract.

Proceed to the next step in the documentation, where you'll query this transaction to get the code_id. Make sure to replace the placeholder transaction hash with the one you received in your output.

If you encountered any errors or didn't receive a similar output, double-check that you've correctly replaced the RPC URL in the command and that your Landslide network is running properly.

b. Fetch the code ID using the transaction hash from the previous output:

~/go/bin/wasmd q tx YOUR_TRANSACTION_HASH \
 --node "REPLACE_WITH_YOUR_RPC_URL" \
 -o json

After executing the command to query the transaction, you should see an output similar to this:

{
  "height": "2",
  "txhash": "F15A7FB824FA83D58E556224FE2D5BD59A4FC7FB8E50400D5B0649E78D873D53",
  "codespace": "",
  "code": 0,
  "data": "124E0A262F636F736D7761736D2E7761736D2E76312E4D736753746F7265436F6465526573706F6E73651224080112203F4CD47C39C57FE1733FB41ED176EEBD9D5C67BAF5DF8A1EEDA1455E758F8514",
  "raw_log": "",
  "logs": [],
  "info": "",
  "gas_wanted": "1463549",
  "gas_used": "1274349",
  "tx": {
    // ... (tx details)
  },
  "timestamp": "2024-09-06T12:13:32Z",
  "events": [
    // ... (other events)
    {
      "type": "store_code",
      "attributes": [
        {
          "key": "code_checksum",
          "value": "3f4cd47c39c57fe1733fb41ed176eebd9d5c67baf5df8a1eeda1455e758f8514",
          "index": true
        },
        {
          "key": "code_id",
          "value": "1",
          "index": true
        },
        {
          "key": "msg_index",
          "value": "0",
          "index": true
        }
      ]
    }
  ]
}

Here's what to look for in this output:

  1. Check that the code field is 0, which indicates the transaction was successful.

  2. Look for the events array in the output. Find the event with "type": "store_code".

  3. Within the store_code event, there are attributes. Find the attribute with "key": "code_id".

  4. The value associated with this code_id is the unique identifier assigned to your uploaded smart contract. In this example, the code_id is 1.

This code_id is crucial for future interactions with your contract, such as instantiation or execution. Make note of it for use in subsequent steps.

6. Query Code ID Metadata

After uploading your smart contract, you can retrieve its metadata using the code ID. Run the following command:

~/go/bin/wasmd q wasm code-info 1 \
  --node "[REPLACE_WITH_YOUR_RPC_URL]" \
  -o json

Replace <CODE_ID> with the code ID you obtained in the previous step, and [REPLACE_WITH_YOUR_RPC_URL] with your Landslide network's RPC URL.

Example output:

{
  "code_id": "1",
  "creator": "wasm1kng6sqkm0mjuh09cwz6u86f75lmeflj9h0fqhr",
  "data_hash": "3F4CD47C39C57FE1733FB41ED176EEBD9D5C67BAF5DF8A1EEDA1455E758F8514",
  "instantiate_permission": {
    "permission": "Everybody",
    "addresses": []
  }
}

This output provides important information about your uploaded smart contract:

  • code_id: Confirms the ID of your uploaded contract.

  • creator: The address that uploaded the contract.

  • data_hash: A unique hash of the contract's bytecode.

  • instantiate_permission: Indicates who can instantiate this contract. In this case, "Everybody" means anyone can create an instance of this contract.

This information is useful for verifying the contract's details and understanding its deployment permissions. The data_hash can be used to ensure the integrity of the uploaded contract.

Next Steps

With your contract deployed, you can now interact with it using Wasmd commands. Consider exploring:

  • Instantiating your contract

  • Querying contract state

  • Executing contract functions

Remember to always use the correct node URL and chain ID in your commands, as provided by your Landslide network setup (visible in the first terminal).

Troubleshooting

  • If commands fail, double-check your node URL and chain ID from the first terminal.

  • Ensure your private key has sufficient funds for gas fees.

  • Verify that your Landslide network is still running and healthy in the first terminal.

For more advanced usage or specific contract interactions, refer to the CosmWasm documentation and your contract's specific interface.

Run Landslide local network following the previous steps

Last updated

Logo

©2024 Gaia Labs LTD