bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 54쪽 중 51쪽
&'a [u8], Token, TokenIndexes<'a>> {
let indexes = TokenIndexes {
identifier: UniqueIndex::new(|d| U8Key::new(d.identifier), "token_identifier"),
};
IndexedMap::new(TOKEN_NAMESPACE, indexes)
}
```
Let's go over the code step by step:
```
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Token {
pub owner: Addr,
pub ticker: String,
pub identifier: u8, // <---- unique value
}
```
Within the attributes of a Token, the identifier stands out as a distinct and unique value:
```
// TokenIndexes structs keeps a list of indexers
pub struct TokenIndexes<'a> {
// token.identifier
pub identifier: UniqueIndex<'a, U8Key, Token>,
}
```
TokenIndexes is a struct for defining indexes of Token struct:
```
impl<'a> IndexList<Token> for TokenIndexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item=&'_ dyn Index<Token>> + '_> {
let v: Vec<&dyn Index<Token>> = vec![&self.identifier];
Box::new(v.into_iter())
}
}
```
IndexList is an interface for building the indexes:
```
pub fn tokens<'a>() -> IndexedMap<'a, &'a [u8], Token, TokenIndexes<'a>> {
let indexes = TokenIndexes {
identifier: UniqueIndex::new(|d| U8Key::new(d.identifier), "token_identifier"),
};
IndexedMap::new(TOKEN_NAMESPACE, indexes)
}
tokens() is storage function used to build IndexedMap.
identifier: UniqueIndex::new( | d| U8Key::new(d.identifier), "token_identifier"),
```
The above code is an index builder function. It builds composite keys with the given function, and accepts a key to identify the index bucket.
See test code below:
```
#[test]
fn test_tokens() {
let mut store = MockStorage::new();
let owner1 = Addr::unchecked("addr1");
let ticker1 = "TOKEN1".to_string();
let token1 = Token {
owner: owner1.clone(),
ticker: ticker1,
identifier: 0,
};
let token_id = increment_tokens(store.borrow_mut()).unwrap();
tokens().save(store.borrow_mut(), &U64Key::from(token_id).joined_key(), &token1).unwrap();
let ticker2 = "TOKEN2".to_string();
let token2 = Token {
owner: owner1.clone(),
ticker: ticker2,
identifier: 0,
};
let token_id = increment_tokens(store.borrow_mut()).unwrap();
// identifier clashes, must return error
tokens().save(store.borrow_mut(), &U64Key::from(token_id).joined_key(), &token1).unwrap();
}
```
The last line will crash with an error:
```
called `Result::unwrap()` on an **Err** value: GenericErr { msg: "Violates unique constraint on index" }
thread 'state::test::test_tokens' panicked at 'called `Result::unwrap()` on an **Err** value: GenericErr { msg: "Violates unique constraint on index" }', src/state.rs:197:90
stack backtrace:
```
### Multi indexes
Multi indexes are used when the structure is indexed by non-unique values. Here is a case from the [cw721 smart contract](https://github.com/CosmWasm/cw-nfts/blob/main/packages/cw721/README.md):
```
pub struct TokenIndexes<'a> {
// secondary index by owner address
// the last U64Key is the primary key which is an auto incremented token counter
pub owner: MultiIndex<'a, (Vec<u8>, Vec<u8>), Token>,
}
// this may become a macro, not important just boilerplate, builds the list of indexes for later use
impl<'a> IndexList<Token> for TokenIndexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item=&'_ dyn Index<Token>> + '_> {
let v: Vec<&dyn Index<Token>> = vec![&self.owner];
Box::new(v.into_iter())
}
}
const TOKEN_NAMESPACE: &str = "tokens";
pub fn tokens<'a>() -> IndexedMap<'a, &'a [u8], Token, TokenIndexes<'a>> {
let indexes = TokenIndexes {
owner: MultiIndex::new(
|d, k| (index_string(d.owner.as_str()), k),
TOKEN_NAMESPACE,
"tokens__owner",
)
};
IndexedMap::new(TOKEN_NAMESPACE, indexes)
}
```
We observe that the owner index is a MultiIndex, which permits duplicate keys. This is why the primary key is appended as the final element in the multi-index key. As the name suggests, this is an index organized by owner, enabling us to manage and iterate through all the tokens associated with a particular owner.
It's essential to emphasize that the key, along with its components in the case of a composite key, must adhere to the PrimaryKey trait. In this instance, both the 2-tuple (\_, \_) and Vec are compliant with the PrimaryKey requirement.
```
impl<'a, T: PrimaryKey<'a> + Prefixer<'a>, U: PrimaryKey<'a>> PrimaryKey<'a> for (T, U) {
type Prefix = T;
type SubPrefix = ();
fn key(&self) -> Vec<&[u8]> {
let mut keys = self.0.key();
keys.extend(&self.1.key());
keys
}
}
pub fn tokens<'a>() -> IndexedMap<'a, &'a str, TokenInfo, TokenIndexes<'a>> {
let indexes = TokenIndexes {
owner: MultiIndex::new(
|d, k| (Vec::from(d.owner.as_ref()), k),
"tokens",
"tokens__owner",
),
};
IndexedMap::new("tokens", indexes)
}
```
During index creation, we must supply an index function per index.
```
owner: MultiIndex::new(|d, k| (Vec::from(d.owner.as_ref()), k),
```
The index function plays a crucial role in generating the index key from the value and the primary key (always in Vec format) of the original map. Naturally, this necessitates the presence of the essential elements required for constructing the index key within the value.
In addition to the index function, it is essential to specify both the primary key's namespace and the namespace for the new index.
```
IndexedMap::new("tokens", indexes)
```
In this context, it's essential that the primary key's namespace corresponds to the one utilized during the creation of the index. We supply our TokenIndexes, specified as an IndexList-type parameter, as the second argument. This effectively links the underlying Map with the primary key and the defined indexes.
IndexedMap (along with the other Indexed\* variants) essentially acts as an enhanced wrapper for the Map, offering a range of index functions and designated namespaces for establishing indexes over the original Map data. Moreover, it takes care of executing these index functions seamlessly during data storage, modification, or removal, allowing you to work with indexed data without having to be concerned with implementation intricacies.
Here is an example of how to use indexes in code:
```
#[test]
fn test_tokens() {
let mut store = MockStorage::new();
let owner1 = Addr::unchecked("addr1");
let ticker1 = "TOKEN1".to_string();
let token1 = Token {
owner: owner1.clone(),
ticker: ticker1,
};
let ticker2 = "TOKEN2".to_string();
let token2 = Token {
owner: owner1.clone(),
ticker: ticker2,
};
let token_id = increment_tokens(store.borrow_mut()).unwrap();
tokens().save(store.borrow_mut(), &U64Key::from(token_id).joined_key(), &token1).unwrap();
let token_id = increment_tokens(store.borrow_mut()).unwrap();
tokens().save(store.borrow_mut(), &U64Key::from(token_id).joined_key(), &token1).unwrap();
// want to load token using owner1 and ticker1
let list: Vec<_> = tokens()
.idx.owner
.prefix(index_string(owner1.as_str()))
.range(&store, None, None, Order::Ascending)
.collect::<StdResult<_>>().unwrap();
let (_, t) = &list[0];
assert_eq!(t, &token1);
assert_eq!(2, list.len());
}
```
### Composite Multi Indexing
Let's delve into the following scenario: we have multiple batches stored based on their numeric batch ID, and these IDs can change. Furthermore, these batches need to be automatically promoted after any modifications. Our goal is to process all the pending batches with a status ranging from "Pending" to "Promoted," depending on various interactions with them. Additionally, each batch comes with an associated expiration time. We are primarily interested in those pending batches that have already expired, as these can be promoted. To accomplish this, we can create an index for the batches, using a composite key that incorporates both the batch status and its expiration timestamp. This composite key allows us to exclude both the already promoted batches and the pending ones that haven't reached their expiration.
To achieve this, we'll construct the index, generate the composite key, and iterate through all the pending batches with an expiration timestamp earlier than the current time.
Here's an example illustrating how to accomplish this using the Batch struct:
```
/// A Batch is a group of members who got voted in together. We need this to
/// calculate moving from *Paid, Pending Voter* to *Voter*
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct Batch {
/// Timestamp (seconds) when all members are no longer pending
pub grace_ends_at: u64,
/// How many must still pay in their escrow before the batch is early authorized
pub waiting_escrow: u32,
/// All paid members promoted. We do this once when grace ends or waiting escrow hits 0.
/// Store this one done so we don't loop through that anymore.
pub batch_promoted: bool,
/// List of all members that are part of this batch (look up ESCROWS with these keys)
pub members: Vec<Addr>,
}
IndexedMap definitions:
// We need a secondary index for batches, such that we can look up batches that have
// not been promoted, ordered by expiration (ascending) from now.
// Index: (U8Key/bool: batch_promoted, U64Key: grace_ends_at) -> U64Key: pk
pub struct BatchIndexes<'a> {
pub promotion_time: MultiIndex<'a, (U8Key, U64Key, U64Key), Batch>,
}
impl<'a> IndexList<Batch> for BatchIndexes<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item=&'_ dyn Index<Batch>> + '_> {
let v: Vec<&dyn Index<Batch>> = vec![&self.promotion_time];
Box::new(v.into_iter())
}
}
pub fn batches<'a>() -> IndexedMap<'a, U64Key, Batch, BatchIndexes<'a>> {
let indexes = BatchIndexes {
promotion_time: MultiIndex::new(
|b: &Batch, pk: Vec<u8>| {
let promoted = if b.batch_promoted { 1u8 } else { 0u8 };
(promoted.into(), b.grace_ends_at.into(), pk.into())
},
"batch",
"batch__promotion",
),
};
IndexedMap::new("batch", indexes)
}
```
This example is similar to the previous one, above. The only differences are:
The composite key now has three elements: the batch status, the expiration timestamp, and the batch id (which is the primary key for the Batch data). We're using a U64Key for the batch id / pk. This is just for convenience. We could as well have used a plain Vec for it.
Now, here's how to use the indexed data:
```
let batch_map = batches();
// Limit to batches that have not yet been promoted (0), using sub_prefix.
// Iterate which have expired at or less than the current time (now), using a bound.
// These are all eligible for timeout-based promotion
let now = block.time.nanos() / 1_000_000_000;
// as we want to keep the last item (pk) unbounded, we increment time by 1 and use exclusive (below the next tick)
let max_key = (U64Key::from(now + 1), U64Key::from(0)).joined_key();
let bound = Bound::Exclusive(max_key);
let ready = batch_map
.idx
.promotion_time
.sub_prefix(0u8.into())
.range(storage, None, Some(bound), Order::Ascending)
.collect::<StdResult<Vec<_ >>>() ?;
```
### A couple of important points to note:
**joined\_key() Function**: The joined\_key() function is utilized to generate the range key. This helpful function takes the (partial) composite key, which consists of the batch expiration timestamp and batch ID, and transforms it into a Vec with the correct format. This Vec is then used to establish a range bound.
**sub\_prefix() Function**: The sub\_prefix() function is employed to adjust the initial element of the composite key, which corresponds to the batch status. This adjustment is necessary because using prefix() in this context, where we have a 3-tuple, would mean fixing the first two elements of the key, which isn't required for our purpose.
The iteration begins from a None state and progresses to the bound key formed from the current timestamp. This approach allows us to effectively list only the pending batches that have already expired but remain unprocessed.
That's the gist of it. Following this process, we can iterate through the results and carry out actions such as changing their status from "Pending" to "Promoted," or any other necessary tasks.
\
\ <br>
# State Modeling
The design of key-value storage can appear challenging to individuals with a background in SQL. Even though databases like MongoDB or other streamlined systems incorporate key-value storage, their libraries shield developers from the underlying intricacies.
This complexity is why comprehending the storage system in Cosmos-SDK might not come easily initially. Nevertheless, once you grasp the concept, it becomes straightforward.
When implementing a state model, it's essential to pause and consider some questions before commencing the implementation. For instance:
Do you truly need to persist that data in the blockchain state?
Is the connection genuinely indispensable, or could it be handled by an off-chain database collector for the UI?
By posing these inquiries, you can sidestep unnecessary data writes to the state and prevent excessive storage utilization, ultimately resulting in more cost-effective execution.
#### Tutorial Introduction
This tutorial guides you through the process of crafting a state model to address a specific business scenario.
The use case is outlined as follows:
* The system encompasses individuals.
* Individuals possess the capability to join various groups.
* Groups have the capacity to encompass multiple individuals.
* Members within these groups can assume distinct roles, including but not limited to admin, super-admin, and regular members, among others.
## Naive Implementation
Below is a design for an any-to-any relation to store data utilizing IDs. Initially, individual data is organized and indexed through an auto-incremented ID:
```
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Person {
pub name: String,
pub age: i32,
pub membership_ids: Vec<String>
}
pub const PEOPLE: Map<&[u8], Person> = Map::new("people");
```
Within this design, groups are similarly organized and identified through the use of an ID.
```
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Group {
pub name: String,
pub membership_ids: Vec<String>
}
pub const GROUPS: Map<&[u8], Group> = Map::new("groups");
```
The connection between groups and individuals is established through a membership structure.
```
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Membership {
pub person_id: String,
pub group_id: String,
pub membership_status_id: String
}
pub const MEMBERSHIPS: Map<&[u8], Membership> = Map::new("memberships");
```
The membership status is determined through the utilization of a "status" string field.
```
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct MembershipStatus {
pub status: String,
pub membership_ids: Vec<String>
}
pub const MEMBERSHIP_STATUSES: Map<&[u8], MembershipStatus> = Map::new("membership_statuses");
```
## Optimized Implementation
Although utilizing an ID for person identification might initially appear intuitive, it introduces redundancy. IDs essentially serve as values for user identification, yet users are already uniquely identified by their Address. Consequently, it is more advisable to index individuals based on their Address instead of relying on auto-incremented integers.
```
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Person {
pub name: String,
pub age: u8, // changed to u8 since ages are unsigned and 100 years max.
}
// Addr -> Person
pub const PEOPLE: Map<&[u8], Person> = Map::new("people");
```
The "membership\_id" has been eliminated, and the data type "i32" has been replaced with "u8." Optimizing variable types enhances gas efficiency, leading to reduced transaction fees.
## Now for the Group
Since groups lack an address, it is logical to distinguish them through auto-incremented IDs. If uniqueness in group names is desired, it's more suitable to employ the group name as the indexing mechanism.
```
pub const GROUP_COUNTER: Item<u64> = Item::new("group_counter");
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Group {
pub name: String,
}
// u64 ID -> Group
pub const GROUPS: Map<U64Key, Group> = Map::new("groups");
```
When saving a group, it's imperative to obtain and store the auto-incremented ID in the GROUP\_COUNTER item. To execute this logic, it is advisable to encapsulate it within a dedicated function:
```
pub fn next_group_counter(store: &mut dyn Storage) -> StdResult<u64> {
let id: u64 = GROUP_COUNTER.may_load(store)?.unwrap_or_default() + 1;
GROUP_COUNTER.save(store, &id)?;
Ok(id)
}
pub fn save_group(store: &mut dyn Storage, group: &Group) -> StdResult<()> {
let id = next_group_counter(store)?;
let key = U64Key::new(id);
NEW_GROUPS.save(store, key, group)
}
```
### Establishing Relations and Defining Roles
To establish a relationship between groups and individuals, as well as define an individual's role, the following steps are necessary:
1. Create a list of users associated with a specific group.
2. Develop a list of groups to which a particular user belongs.
These tasks can be achieved by constructing secondary indexes.
*It is strongly encouraged to undertake the remaining implementation as a personal exercise.*
<br>
# Cosmwasm By Example
# Math
In this section, you will find examples of a CosmWasm contract that demonstrates basic mathematical operations, including addition, subtraction, multiplication, division, modulo, and exponentiation.
The "execute" function showcased here accepts an enum of "ExecuteMsg," which encapsulates all the contract functions and matches them with the function the user is invoking.
```
pub fn execute(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Operations { a, b } => execute_operations(deps,a,b),
}
}
```
The "execute" function receives an enum of "ExecuteMsg," which serves as a container for all the contract functions and matches them with the user's requested function. Subsequently, it invokes the "execute\_operations" function:
```
pub fn execute_operations(deps: DepsMut, a: u128, b: u128) -> Result<Response, ContractError> {
// Checking if numbers are not zero
if a == 0 && b == 0 {
return Err(ContractError::CanNotBeZero());
}
// Addition
let addition_result = a + b;
// Subtraction
let subtraction_result = a - b;
// Multiplication
let multiplication_result = a * b;
// Division
let division_result = a / b;
// Modulo
let modulo_result = a % b;
// Exponentiation
let exponent: u32 = 3;
let exponentiation_result: u128 = a.pow(exponent);
// Create the response
let response = OperationsResponse {
addition_result,
subtraction_result,
multiplication_result,
division_result,
modulo_result,
exponentiation_result,
};
// Fetching the state
RESULT.load(deps.storage).unwrap();
// Update the state
RESULT.save(deps.storage, &response).unwrap();
let res = Response::new().add_attributes(vec![
("action", "operations"),
("a", &a.to_string()),
("b", &b.to_string()),
("addition_res", &addition_result.to_string()),
("substraction_res", &subtraction_result.to_string()),
("multiplicationn_res", &multiplication_result.to_string()),
("division_res", &division_result.to_string()),
("modulo_res", &modulo_result.to_string()),
("exponential_res", &exponentiation_result.to_string()),
]);
Ok(res)
}
```
The execute\_operations function accepts two parameters, 'a' and 'b,' for mathematical operations and stores the resulting value in the RESULT global state variable, which is located within the state.rs file:
```
pub const RESULT: Item<OperationsResponse> = Item::new("result");
```
You can utilize the following query endpoint to retrieve the results of mathematical operations:
```
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result<QueryResponse, StdError> {
match msg {
QueryMsg::GetResponse {} => get_response(deps),
}
}
```
The query function above accepts an enum of QueryMsg, which encapsulates all the contract query프로젝트로 돌아가기