Everything I learned in one month of #Rust & #CosmWasm - from a front-end guy's perspective.
(And 6 months of working alongside the CosmWasm geniuses at @StargazeZone)

A thread 🧵
@cosmos, the Internet of Blockchains, is the SDK upon which @CosmWasm is built. It provides a "layer 0" software development kit for blockchains and dApps.
cosmos.network
@CosmWasm is the natural extension of the Cosmos SDK. With CosmWasm, you can build Ethereum-style smart contracts that are 100% interoperable with Cosmos.
cosmwasm.com
@CosmWasm contracts are built with the Rust programming language, consistently scored as the most loved programming language by the StackOverflow developer survey every year since 2016.

Let's take a look at how to build these contracts🔎
Quickly install cargo-generate and cargo-run-script and generate a CosmWasm project. Does that sound confusing? It's explained in detail here: github.com/InterWasm/cw-t…
Navigate into the newly created folder and you'll see cargo generated a full CosmWasm project for you, including example code!

The `examples` isn't that important, but `src` and `schema` sure are!
`src`, or source folder, contains most of the actual code that runs our contract.

`src/contract.rs` contains the contract logic
`src/helpers.rs` contains, well..., helpers
`src/msg.rs` defines the different types of messages our contract accepts
`src/state.rs` defines the state of our contract, that is - variables that will change over time and are stored in the contract; think of React's useState!
`src/error.rs` defines error messages for when things take a turn for the worst
`src/integration_tests.rs` does... something
`schema` includes templates and descriptions for the different types of messages our contract will receive. Let's take a look at `schema/instantiate_msg.json`🔎
Have a look at the lines of JSON I've highlighted.

In this file, we're defining the name of the message, `InstantiateMsg`, as well as the required properties for sending the message, in this case, `count`. We then define the type of this property in the `properties` object.
In `src/contract.rs`, the `instantiate` function is receiving this object. Let's go over what's happening here step-by-step.
`instantiate` accepts 4 arguments, just like `execute` and `query` functions.

`deps` allows us to alter state
`_env` represents our environment variable object
`info` contains important information about the origin, like who sent the message

But most importantly...
The `msg` object is of type `InstantiateMsg`, the exact message that was defined in `schema/instantiate_msg.json`. This is the message that the client sends the contract, and usually contains crucial data that the contract will act upon.

And what does it do with this data?
~ Quick interlude: I explained `deps` a little poorly, but this CosmWasm docs section explains it well, have a look: docs.cosmwasm.com/docs/1.0/smart…
First, the `instantiate` function creates a new State object, and stores two values: count and owner. We get the value of count through the message object, and owner by getting the sender through the info object.

We then set the version of the contract and save the state.
Finally, we compose an `Ok` response object.

These attributes that are being added will then appear in the JSON response from the contract, and you'll be using these in your frontend to display the contract's status.

Notice how we're returning the state values we set earlier.
That's it! These are some of the basics that I wanted to share, and if you haven't already you should check out this basic incrementer contract, it only takes a couple commands to set up!

More contract examples are available here: github.com/InterWasm/cw-c…
I also highly recommend taking a look at the Getting Started section of the CosmWasm documentation: docs.cosmwasm.com/docs/1.0/getti…
I hope you appreciated this thread; I'll be posting a lot more content like this soon, stay tuned!

On that note, see you soon👋

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Josef Leventon

Josef Leventon Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(