State

To Share and +4 nLEARNs

Smart contracts store data in their account’s state, which is public on the chain. The storage starts empty until a contract is deployed and the state is initialized.

It is important to know that the account’s code and account’s storage are independent. Updating the code does not erase the state.


### Defining the State
The attributes of the `class` marked as the contract define the data that will be stored.

The contract can store all native types (e.g. `number`, `string`, `Array`, `Map`) as well as complex objects.

For example, our Auction contract stores when the auction ends, and an object representing the highest bid.

**Note:** The SDK also provides [collections](https://raw.githubusercontent.com/Techbridge3/docs/master/docs/2.build/2.smart-contracts/anatomy/./collections.md) to efficiently store collections of data.

### Defining the State
The attributes of the `struct` marked as the contract define the data that will be stored.

The contract can store all native types (e.g. `u8`, `string`, `HashMap`, `Vector`) as well as complex objects.

For example, our Auction contract stores when the auction ends, and an object representing the highest bid.

**Note:** The structures that will be saved need a special macro, that tells the SDK to store them [serialized in Borsh](https://raw.githubusercontent.com/Techbridge3/docs/master/docs/2.build/2.smart-contracts/anatomy/./serialization.md).

**Note:** The SDK also provides [collections](https://raw.githubusercontent.com/Techbridge3/docs/master/docs/2.build/2.smart-contracts/anatomy/./collections.md) to efficiently store collections of data.

### Initializing the State
After the contract is deployed, its state is empty and needs to be initialized with
some initial values.

There are two ways to initialize a state:
1. By creating an initialization function
2. By setting default values

### I. Initialization Functions
An option to initialize the state is to create an `initialization` function, which needs to be called before executing any other function.

In our Auction example, the contract has an initialization function that sets when the auction ends. Note the `@initialization` decorator, and the forced initialization on `NearBindgen`.

**Note:** It is a good practice to mark initialization functions as private. We will cover function types in the [functions section](https://raw.githubusercontent.com/Techbridge3/docs/master/docs/2.build/2.smart-contracts/anatomy/./functions.md).

### I. Initialization Functions
An option to initialize the state is to create an `initialization` function, which needs to be called before executing any other function.

In our Auction example, the contract has an initialization function that sets when the auction ends. The contract derives the `PanicOnDefault`, which forces the user to call the init method denoted by the `#[init]` macro.

**Note:** It is a good practice to mark initialization functions as private. We will cover function types in the [functions section](https://raw.githubusercontent.com/Techbridge3/docs/master/docs/2.build/2.smart-contracts/anatomy/./functions.md).

### II. Default State
Another option to initialize the state is to set default values for the attributes of the class.

Such is the case for our “Hello World” contract, which stores a `greeting` with the default value `”Hello”`.

The first time the contract is called (somebody executes `get_greeting` or `set_greeting`), the default values will be stored in the state, and the state will be considered initialized.

**Note:** The state can only be initialized once.

### II. Default State
Another option to initialize the state is to create a `Default` version of our contract’s `struct`.

For example, our “Hello World” contract has a default state with a `greeting` set to `”Hello”`.

The first time the contract executes, the `Default` will be stored in the state, and the state will be considered initialized.

**Note:** The state can only be initialized once.

### Lifecycle of the State
When a function is called, the contract’s state is loaded from the storage and put into memory.

The state is actually [stored serialized](https://raw.githubusercontent.com/Techbridge3/docs/master/docs/2.build/2.smart-contracts/anatomy/./serialization.md), and the SDK takes a bit of time to deserialize it before the method can access it.

When the method finishes executing successfully, all the changes to the state are serialized, and saved back to the storage.

Generate comment with AI 2 nL
Scroll to Top