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:
Keep your original terminal window running the Landslide network (
make run-wasm
command) open and active. This terminal maintains your local network.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:
b. Checkout the most stable version (replace v0.53.0 if needed):
c. Install Wasmd:
d. Verify the installation:
or
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:
When prompted, enter the following bip39 mnemonic (seed phrase):
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):
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 likehttp://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:
Great! Your smart contract has been successfully uploaded to the Landslide network. Here's what you need to know:
The
code: 0
indicates that the transaction was successful.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:
After executing the command to query the transaction, you should see an output similar to this:
Here's what to look for in this output:
Check that the
code
field is 0, which indicates the transaction was successful.Look for the
events
array in the output. Find the event with"type": "store_code"
.Within the
store_code
event, there areattributes
. Find the attribute with"key": "code_id"
.The
value
associated with thiscode_id
is the unique identifier assigned to your uploaded smart contract. In this example, thecode_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:
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:
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