Skip to main content

Chapter 5 - State

Yeahhh, it's finally time to start coding! Let's use this repo (cw-template) and rename it to aura-nft:

cargo generate --git https://github.com/CosmWasm/cw-template.git --name aura-nft

We will start in the src/state.rs file. Upon opening it you should see code like the following:

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::Addr;
use cw_storage_plus::Item;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct State {
pub count: i32,
pub owner: Addr,
}

pub const STATE: Item<State> = Item::new("state");

Let's talk through this section by section.

The first 5 lines of code are importing other structs, interfaces and functions from other packages. To give a quick overview without getting bogged down too much:

  • JsonSchema allows structs to be serialized and deserialized to and from JSON.
  • Deserialize and Serialize provide the serialization described above.
  • Addr is a Cosmos address, under the hood it is simply a string.
  • Item is a helper provided by storage plus. It effectively means we can store an item in storage. In this case, the STATE variable is an Item that stores a singular State struct.

We want to make some changes to this, let's rename it accordingly. I prefer our global state to be called Config as it is the configuration of our contract. Let's do this now and remove the count and owner variable from the struct and rename it to Config. Let's also rename STATE to CONFIG.

It should now look like this:

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::Addr;
use cw_storage_plus::Item;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Config {

}

pub const CONFIG: Item<Config> = Item::new("config");

Now let's think about what global configs we want, we probably want an NFT Minter Contract that can mint some amazing NFTs. Let's add some necessary fields representing the configuration parameters of an NFT minter contract.

// Previous code omitted
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Config {
pub owner: Addr,
pub base_token_uri: String,
pub max_tokens: u32,
pub max_tokens_per_batch_mint: u32,
pub max_tokens_per_batch_transfer: u32,
pub cw721_code_id: u64,
pub cw721_address: Option<Addr>,
pub name: String,
pub symbol: String,
pub royalty_percentage: Option<u64>,
pub royalty_payment_address: Option<String>,
}
// Following code omitted

Now I'll explain to you about the above data fields.