Understanding CosmWasm File Structure

Understanding CosmWasm File Structure

This guide will help you understand the Cosmwasm project structure, simplifying your journey of building on MANTRA Chain as a smart contract developer. Let's get started.

What this guide will cover ❔

  • What are all these folders?

  • The src directory

  • Contents of the src directory

What are these folders?

After having done the setup of your project , you might see numerous folders in your working directory. We will break down each one of them to have a better understanding of what we are developing and its purpose.

  • ./cargo: This folder contains configurations for all the cargo commands. Think of it as a reference library that tells our command line which cargo commands exist and how to execute them.

  • ./circleci: Primarily for DevOps teams working on CI/CD pipelines to keep the project updated and available. It won't be directly useful for our current purposes.

  • ./github-workflows: Contains GitHub actions required for the project, but again, it's not of much use for us now.

  • ./examples/schema.rs: This folder includes the logic to generate schemas when we run cargo schema. The generated schemas are stored in the schema folder. We'll use this folder for declaring our schemas..

Example of a schema.rs file:

  • /schema: This folder stores all the schemas generated for our smart contract. For example, in a voting contract, schemas are generated for the main properties of the contract. When building a dApp on MANTRA Chain, we will need these schemas for interacting with our contract from the client side.

Tip for EVM Developers: You can think of schemas like ABIs (Abstract Binary Interfaces) in the EVM world, which are used for interacting with the contract from the client side.

Example of a schema file:

With the majority of the folders covered, let's focus on the most critical one: ./src.

The src Directory

This directory houses all the main logic of our project, including the smart contract, query functions, and execution calls. As a CosmWasm contract developer, you'll spend most of your coding time here.

The src directory typically looks like this:

Understanding Each File:

  • contract.rs: Contains all the logic of our contract, including query, instantiate, and execution functions.

  • error.rs: Defines all the errors for our contract. Custom errors, like NotLoggedIn (an example where a user isn't logged in), can also be declared here.

  • lib.rs: Includes all the modules we will use in our smart contract. Our contract can refer to this file to utilize the functionality of the created modules.

  • msg.rs: This module contains the execution and query logic for our contract.

  • state.rs: Holds information about all the states of our smart contract.

Why Separate the Logic into Different Files? Separating the logic into smaller modules makes the project easier to manage and enhances code readability.

Now that we've explored the files involved in developing on MANTRA Chain, upcoming guides will walk you through writing CosmWasm contracts and deploying them.

Last updated