Understanding Cosmwasm File Structure

This tutorial will help you understand the Cosmwasm project structure which will simplify your journey as an oraichain smart contract dev so lets start .

🦀 If you are new to oraichain and haven’t set up the smart contract dev environment for oraichain , plz follow this guide for setting it up from scratch .

What this tutorial will cover ❔

  • What are all these folders ?

  • The src directory

  • Contents of src directory .

What are all these folders ?

If you have followed the steps discussed in this guide , you might be having a plethora of folders

in your working directory to overwhelm you , don’t worry by the end of this section you will understand their significance

the significance of each folder is :

./cargo

this has the configuration for all the cargo commands , you can understand it as a library where our command line refers for which cargo commands actually exist and how to give output for them .

./circleci

This folder will not be much of our use , it is for all the devops guys who are working on CI-CD pipelines of the project to keep it up to date and readily available for users .

./github-workflows

It houses the github actions required for the project (Not much of our use) .

examples /schema.rs

it has the logic to generate schemas when we enter cargo schema , the generate schemas are then stored into schema folder . in future articles we will be using this folder for declaring our schemas .

the example of a schema.rs file is below

we will discuss how to write schema files like these in future articles .

./schema

This folder stores all the schemas we have generated for our smart contract for example , the below is a voting contract where schemas are generated for the main properties of our contract , later on when we will be building a dApp on oraichain we will be needing these schemas for interacting with our contract in our client .

🦀 For devs who are from EVM dev background , you can understand schemas like ABIs (Abstract binary interfaces ) in EVM world which are used for interacting with our contract by the client side of our application

example of how a schema file looks like

the below flowchart can simplify the concept of schemas for you :)

Now we have discussed the majority of folders in our smart contract project but most of our code resides in ./src . so let’s discuss about it .

The src directory 📁

it is the directory which has all the main logic of our project , the smart contract , the query functions , the execution calls all are in this calls . as an oraichain dev you will be spending most of your time coding in this directory only .

Inside the src directory you should be seeing the similar file structure

let’s discuss about each file and understand their significance .

  • Contract.rs : It stores all the logic of our contract , such as query, instantiate and execution .

  • error.rs : it will contain all the errors and their definitions for our contract , we can also declare custom errors such as notLoggedIn (An example where user is not logged in) and much more .

  • lib.rs : it has all the modules we will be using in our smart contract , our contract can refer to it for using functionality of the module we have created .

  • msg.rs : the module where we will be coding the execution and query logic for our contract .

  • state.rs : module which has information of all the states of our smart contract .

🦀 You might be thinking that why we can’t just code all of the logic into a single file , the reason is the separation of concerns where we pack our logic into single small modules thus making it easy for us to handle the project and ensures code readability .

The following flowchart will summarize the src directory for you

file structure of src folder in a nutshell .

with this we have understood the files we will be working with when developing on Oraichain , in future tutorials you will be learning the art of writing cosmwasm contracts and actually building dapps out of them .till then stay tuned and happy hacking ✌️.

Last updated