Simple and Complex States
Simple State
The state serves as the repository for storing and accessing data within a smart contract. Conceptually, it operates in a manner akin to the database interaction layer found in conventional applications.
A basic approach to managing state entails the storage of a single entity. For instance, in the case of the cw20 contract, the TokenInfo is established and recorded during the contract's initialization phase.
Initially, a TokenInfo type is defined in state.rs:
Then the storage is initialized:
Within the contract, the instantiate function demonstrates the process of saving data to this:
Complex State
In more intricate solutions, there might be a requirement to store supplementary information. One approach to accomplish this is by serializing comprehensive JSON data structures, which enables data retrieval using key-value pairs.
In the context of CW20, the association of addresses with their CW20 balances is accomplished using a Map data structure
There's a lot of complexity here, so let's dissect it step by step:
deps.storage is an input provided, originating from the contract context. Think of deps as similar to the ctx you might have encountered in the Cosmos SDK.
&rcpt_addr is a borrowed reference to the validated recipient address. It's been verified and confirmed as valid; otherwise, the "let" statement would have triggered an error. This reference corresponds to the key within the key/value pair.
The third statement consists of an anonymous function (lambda) that returns a StdResult and carries out a computation based on the current value of balance. In this context, balance signifies the value within the key/value pair, while &rcpt_addr serves as the key.
Last updated
Was this helpful?