Skip to content

Commit

Permalink
Merge pull request #69 from QiLOL/doc_update
Browse files Browse the repository at this point in the history
Doc update
  • Loading branch information
uvd authored Feb 16, 2024
2 parents 9a1f4f3 + 41582eb commit 9db5b2e
Show file tree
Hide file tree
Showing 19 changed files with 50 additions and 34 deletions.
2 changes: 1 addition & 1 deletion advanced-topics/BCS_encoding/lessons/BCS_encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Now, let's write the function to deserialize an object in a Sui contract.

```

The varies `peel_*` methods in Sui Frame [`bcs` module](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/bcs.md) are used to "peel" each individual field from the BCS serialized bytes. Note that the order we peel the fields must be exactly the same as the order of the fields in the struct definition.
The varies `peel_*` methods in Sui Frame [`bcs` module](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/sui-framework/bcs.md) are used to "peel" each individual field from the BCS serialized bytes. Note that the order we peel the fields must be exactly the same as the order of the fields in the struct definition.

_Quiz: Why are the results not the same from the first two `peel_address` calls on the same `bcs` object?_

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Sui Foundation, Inc.
// SPDX-License-Identifier: Apache-2.0


module collection::dynamic_fields {

use sui::dynamic_object_field as ofield;
Expand Down Expand Up @@ -90,6 +91,7 @@ module collection::dynamic_fields {
object::delete(id);
}

#[lint_allow(self_transfer)]
// Removes a DOFChild from the parent object and transfer it to the caller
public fun reclaim_dofchild(parent: &mut Parent, child_name: vector<u8>, ctx: &mut TxContext) {
let child = remove_dofchild(parent, child_name);
Expand Down
2 changes: 2 additions & 0 deletions unit-four/example_projects/collections/sources/table.move
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright (c) Sui Foundation, Inc.
// SPDX-License-Identifier: Apache-2.0


module collection::table {

use sui::table::{Table, Self};
use sui::tx_context::{TxContext};

#[allow(unused_field)]
// Defining a table with specified types for the key and value
struct IntegerTable {
table_values: Table<u8, u8>
Expand Down
2 changes: 2 additions & 0 deletions unit-four/example_projects/collections/sources/vector.move
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Copyright (c) Sui Foundation, Inc.
// SPDX-License-Identifier: Apache-2.0


module collection::vector {

use std::vector;

struct Widget {
}

#[allow(unused_field)]
// Vector for a specified type
struct WidgetVector {
widgets: vector<Widget>
Expand Down
14 changes: 8 additions & 6 deletions unit-four/example_projects/marketplace/sources/marketplace.move
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/// Copyright (c) Sui Foundation, Inc.
/// SPDX-License-Identifier: Apache-2.0
///
/// Modified from https://github.com/MystenLabs/sui/blob/main/sui_programmability/examples/nfts/sources/marketplace.move
// Copyright (c) Sui Foundation, Inc.
// SPDX-License-Identifier: Apache-2.0

// Modified from https://github.com/MystenLabs/sui/blob/main/sui_programmability/examples/nfts/sources/marketplace.move


module marketplace::marketplace {
use sui::dynamic_object_field as ofield;
Expand Down Expand Up @@ -68,7 +69,7 @@ module marketplace::marketplace {
fun delist<T: key + store, COIN>(
marketplace: &mut Marketplace<COIN>,
item_id: ID,
ctx: &mut TxContext
ctx: &TxContext
): T {
let Listing {
id,
Expand Down Expand Up @@ -141,11 +142,12 @@ module marketplace::marketplace {
/// Internal function to take profits from selling items on the `Marketplace`.
fun take_profits<COIN>(
marketplace: &mut Marketplace<COIN>,
ctx: &mut TxContext
ctx: &TxContext
): Coin<COIN> {
table::remove<address, Coin<COIN>>(&mut marketplace.payments, tx_context::sender(ctx))
}

#[lint_allow(self_transfer)]
/// Call [`take_profits`] and transfer Coin object to the sender.
public fun take_profits_and_keep<COIN>(
marketplace: &mut Marketplace<COIN>,
Expand Down
11 changes: 7 additions & 4 deletions unit-four/example_projects/marketplace/sources/widget.move
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/// Copyright (c) Sui Foundation, Inc.
/// SPDX-License-Identifier: Apache-2.0
///
/// Modified from https://github.com/MystenLabs/sui/blob/main/sui_programmability/examples/nfts/sources/marketplace.move
// Copyright (c) Sui Foundation, Inc.
// SPDX-License-Identifier: Apache-2.0

// Modified from https://github.com/MystenLabs/sui/blob/main/sui_programmability/examples/nfts/sources/marketplace.move



module marketplace::widget {

Expand All @@ -13,6 +15,7 @@ module marketplace::widget {
id: UID,
}

#[lint_allow(self_transfer)]
public fun mint(ctx: &mut TxContext) {
let object = Widget {
id: object::new(ctx)
Expand Down
4 changes: 2 additions & 2 deletions unit-four/lessons/1_homogeneous_collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ It's important to note that while a vector defined with a generic type can accep

## Table

A `Table` is a map-like collection that dynamically stores key-value pairs. But unlike a traditional map collection, it's keys and values are not stored within the `Table` value, but instead are stored using Sui's object system. The `Table` struct acts only as a handle into the object system to retrieve those keys and values.
A `Table` is a map-like collection that dynamically stores key-value pairs. But unlike a traditional map collection, its keys and values are not stored within the `Table` value, but instead are stored using Sui's object system. The `Table` struct acts only as a handle into the object system to retrieve those keys and values.

The `key` type of a `Table` must have the ability constraint of `copy + drop + store`, and the `value` type must have the ability constraint of `store`.

`Table` is also a type of _homogeneous_ collection where the key and value fields can be specified or generic types, but all values and all keys in a `Table` collection must be of the _same_ types.
`Table` is also a type of _homogeneous_ collection where the key and value fields can be specified or generic types, but all values and all keys in a `Table` collection must be of the _same_ type.

*Quiz: Would two table objects containing the exact same key-value pairs be equal to each other when checked with the `===` operator? Try it out.*

Expand Down
4 changes: 2 additions & 2 deletions unit-four/lessons/2_dynamic_fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ To peek under how collections like `Table` are actually implemented in Sui Move,

There are two sub-types of dynamic fields:

- **Dynamic Fields** can store any value that has the `store` ability, however an object stored in this kind of field will be considered wrapped and will not be accessible directly via its ID by external tools (explorers, wallets, etc) accessing storage.
- **Dynamic Fields** can store any value that has the `store` ability, however, an object stored in this kind of field will be considered wrapped and will not be accessible directly via its ID by external tools (explorers, wallets, etc) accessing storage.
- **Dynamic Object Fields** values *must* be Sui objects (have the `key` and `store` abilities, and `id: UID` as the first field), but will still be directly accessible via their object ID after being attached.

## Dynamic Field Operations
Expand Down Expand Up @@ -142,4 +142,4 @@ For a full explanation of the underlying reason, please check [this forum post](

Now we understand how dynamic fields work, we can think of the `Table` collection as a thin wrapper around dynamic field operations.

You can look through the [source code](https://github.com/MystenLabs/sui/blob/eb866def280bb050838d803f8f72e67e05bf1616/crates/sui-framework/packages/sui-framework/sources/table.move) of the `Table` type in Sui as an exercise, and see how each of the previously introduced operations map to dynamic field operations and with some additional logic to keep track of the size of the `Table`.
You can look through the [source code](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/packages/sui-framework/sources/table.move) of the `Table` type in Sui as an exercise, and see how each of the previously introduced operations map to dynamic field operations and with some additional logic to keep track of the size of the `Table`.
2 changes: 1 addition & 1 deletion unit-four/lessons/3_heterogeneous_collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Homogeneous collections like `Vector` and `Table` can work for marketplaces (or other types of applications) where we need to hold a collection of objects of the same type, but what if we need to hold objects of different types, or if we do not know at compile time what types the objects we need to hold are going to be?

For this type of marketplaces, we need to use a _heterogeneous_ collection to hold the items to be sold. Already having done the heavy lifting of understanding dynamic fields, heterogeneous collection in Sui should be very easy to understand. We will look at the `Bag` collection type more closely here.
For this type of marketplace, we need to use a _heterogeneous_ collection to hold the items to be sold. Already having done the heavy lifting of understanding dynamic fields, heterogeneous collection in Sui should be very easy to understand. We will look at the `Bag` collection type more closely here.

## The `Bag` Type

Expand Down
2 changes: 1 addition & 1 deletion unit-four/lessons/4_marketplace_contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Buying an item is similar to delisting but with additional logic for handling pa

```

The first part is the same as delisting an item from listing, but we also check if the payment sent in is the right amount. The second part will insert the payment coin object into our `payments` `Table`, and depending on if the seller already has some balance, it will either do an a simple `table::add` or `table::borrow_mut` and `coin::join` to merge the payment to existing balance.
The first part is the same as delisting an item from listing, but we also check if the payment sent in is the right amount. The second part will insert the payment coin object into our `payments` `Table`, and depending on if the seller already has some balance, it will either do a simple `table::add` or `table::borrow_mut` and `coin::join` to merge the payment to existing balance.

The entry function `buy_and_take` simply calls `buy` and transfers the purchased item to the buyer.

Expand Down
4 changes: 2 additions & 2 deletions unit-four/lessons/5_deployment_and_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Export the `Marketplace` shared object's ID into an environmental variable:
First, we mint a `widget` item to be listed:

```bash
sui client call --function mint --module widget --package $PACKAGE_ID --gas-budget 10000000
sui client call --function mint --module widget --package $PACKAGE_ID --gas-budget 10000000
```

Save the object item of the minted `widget` to an environmental variable:
Expand Down Expand Up @@ -106,7 +106,7 @@ Now, let's buy back the item that we just listed:
sui client call --function buy_and_take --module marketplace --package $PACKAGE_ID --args $MARKET_ID $ITEM_ID $PAYMENT_ID --type-args $PACKAGE_ID::widget::Widget 0x2::sui::SUI --gas-budget 10000000
```

You should see a long list of transaction effects in the console after submit this transaction. We can verify that the `widget` is owned by our address, and the `payments` `Table` now has an entry with the key of our address and should be of size `1`.
You should see a long list of transaction effects in the console after submitting this transaction. We can verify that the `widget` is owned by our address, and the `payments` `Table` now has an entry with the key of our address and should be of size `1`.

### Take Profits

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) 2022, Sui Foundation
// SPDX-License-Identifier: Apache-2.0

#[lint_allow(self_transfer)]
/// A basic Hello World example for Sui Move, part of the Sui Move intro course:
/// https://github.com/sui-foundation/sui-move-intro-course
///
Expand All @@ -19,6 +18,7 @@ module hello_world::hello_world {
text: string::String
}

#[lint_allow(self_transfer)]
public fun mint(ctx: &mut TxContext) {
let object = HelloWorldObject {
id: object::new(ctx),
Expand Down
2 changes: 1 addition & 1 deletion unit-one/lessons/2_sui_project_structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Sui = { local = "../sui/crates/sui-framework/packages/sui-framework" }

## Sui Module and Package Naming

- Sui Move module and package naming convention uses snake casing, i.e. this_is_snake_casing.
- Sui Move module and package naming convention use snake casing, i.e. this_is_snake_casing.

- A Sui module name uses the Rust path separator `::` to divide the package name and the module name, examples:
1. `unit_one::hello_world` - `hello_world` module in `unit_one` package
Expand Down
2 changes: 2 additions & 0 deletions unit-three/example_projects/generics/sources/generics.move
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ module generics::generics {
id: UID,
}

#[lint_allow(self_transfer)]
public fun create_box<T: store>(value: T, ctx: &mut TxContext){
transfer::transfer(Box<T> {id: object::new(ctx), value }, tx_context::sender(ctx))
}

#[lint_allow(self_transfer)]
public fun create_simple_box(value: u8, ctx: &mut TxContext){
transfer::transfer(SimpleBox {id: object::new(ctx), value }, tx_context::sender(ctx))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) 2022, Sui Foundation
// SPDX-License-Identifier: Apache-2.0

#[lint_allow(self_transfer)]
/// Basic token locking and vesting example for Move on Sui.
/// Part of the the Sui Move intro course:
/// https://github.com/sui-foundation/sui-move-intro-course
Expand Down Expand Up @@ -29,6 +28,7 @@ module locked_coin::locked_coin {
/// Witness
struct LOCKED_COIN has drop {}

#[lint_allow(self_transfer)]
/// Withdraw the available vested amount assuming linear vesting
///
public fun withdraw_vested(locker: &mut Locker, clock: &Clock, ctx: &mut TxContext){
Expand Down
12 changes: 6 additions & 6 deletions unit-three/lessons/1_sui_framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ A common use case for smart contracts is issuing custom fungible tokens (such as

## Sui Framework

[The Sui Framework](https://github.com/MystenLabs/sui/tree/main/crates/sui-framework/docs) is Sui's specific implementation of the Move VM. It contains Sui's native API's including its implementation of the Move standard library, as well as Sui-specific operations such as [crypto primitives](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/groth16.md) and Sui's implementation of [data structures](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/url.md) at the framework level.
[The Sui Framework](https://github.com/MystenLabs/sui/tree/main/crates/sui-framework/docs) is Sui's specific implementation of the Move VM. It contains Sui's native API's including its implementation of the Move standard library, as well as Sui-specific operations such as [crypto primitives](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/sui-framework/groth16.md) and Sui's implementation of [data structures](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/sui-framework/url.md) at the framework level.

An implementation of a custom fungible token in Sui will heavily leverage some of the libraries in the Sui Framework.

## `sui::coin`

The main library we will use to implement a custom fungible token on Sui is the [`sui::coin`](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/coin.md) module.
The main library we will use to implement a custom fungible token on Sui is the [`sui::coin`](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/sui-framework/coin.md) module.

The resources or methods we will directly use in our fungible token example are:

- Resource: [Coin](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/coin.md#resource-coin)
- Resource: [TreasuryCap](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/coin.md#resource-treasurycap)
- Resource: [CoinMetadata](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/coin.md#resource-coinmetadata)
- Method: [coin::create_currency](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/coin.md#function-create_currency)
- Resource: [Coin](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/sui-framework/coin.md#resource-coin)
- Resource: [TreasuryCap](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/sui-framework/coin.md#resource-treasurycap)
- Resource: [CoinMetadata](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/sui-framework/coin.md#resource-coinmetadata)
- Method: [coin::create_currency](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/sui-framework/coin.md#0x2_coin_create_currency)

We will revisit each of these in more depth after introducing some new concepts in the next few sections.

Expand Down
6 changes: 3 additions & 3 deletions unit-three/lessons/4_the_coin_resource_and_create_currency.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Now we know how generics and witness patterns work, let's revisit the `Coin` res

## The `Coin` Resource

Now we understand how generics work. We can revisit the `Coin` resource from `sui::coin`. It's [defined](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/packages/sui-framework/sources/coin.move#L29) as the following:
Now we understand how generics work. We can revisit the `Coin` resource from `sui::coin`. It's [defined](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/packages/sui-framework/sources/coin.move#L28) as the following:

```rust
struct Coin<phantom T> has key, store {
Expand All @@ -15,7 +15,7 @@ struct Coin<phantom T> has key, store {

The `Coin` resource type is a struct that has a generic type `T` and two fields, `id` and `balance`. `id` is of the type `sui::object::UID`, which we have already seen before.

`balance` is of the type [`sui::balance::Balance`](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/balance.md#0x2_balance_Balance), and is [defined](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/packages/sui-framework/sources/balance.move#L25) as:
`balance` is of the type [`sui::balance::Balance`](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/sui-framework/balance.md#0x2_balance_Balance), and is [defined](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/packages/sui-framework/sources/balance.move#L29) as:

```rust
struct Balance<phantom T> has store {
Expand All @@ -29,7 +29,7 @@ Recall our discussion on [`phantom`](./3_witness_design_pattern.md#the-phantom-k

## The `create_currency` Method

Let's look at what `coin::create_currency` actually does in its [source code](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/packages/sui-framework/sources/coin.move#L251):
Let's look at what `coin::create_currency` actually does in its [source code](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/packages/sui-framework/sources/coin.move#L201):

```rust
public fun create_currency<T: drop>(
Expand Down
6 changes: 3 additions & 3 deletions unit-three/lessons/6_clock_and_locked_coin.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ In the second fungible token example, we will introduce how to obtain time on-ch

## Clock

Sui Framework has a native [clock module](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/clock.md) that makes timestamps available in Move smart contracts.
Sui Framework has a native [clock module](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/sui-framework/clock.md) that makes timestamps available in Move smart contracts.

The main method that you will need to access is the following:

```
public fun timestamp_ms(clock: &clock::Clock): u64
```

the [`timestamp_ms`](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/clock.md#function-timestamp_ms) function returns the current system timestamp, as a running total of milliseconds since an arbitrary point in the past.
the [`timestamp_ms`](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/sui-framework/clock.md#0x2_clock_timestamp_ms) function returns the current system timestamp, as a running total of milliseconds since an arbitrary point in the past.

The [`clock`](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/clock.md#0x2_clock_Clock) object has a special reserved identifier, `0x6`, that needs to be passed into function calls using it as one of the inputs.
The [`clock`](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/docs/sui-framework/clock.md#0x2_clock_Clock) object has a special reserved identifier, `0x6`, that needs to be passed into function calls using it as one of the inputs.

## Locked Coin

Expand Down
3 changes: 3 additions & 0 deletions unit-two/lessons/1_working_with_sui_objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ public fun create_transcript_object(history: u8, math: u8, literature: u8, ctx:
}
```

*💡Note: the provided sample code generates a warning message: warning[Lint W01001]: non-composable transfer to sender. For further details, refer to the article ("Sui Linters and Warnings Update Increases Coder Velocity")[https://blog.sui.io/linter-compile-warnings-update/]*

*💡Note: Move supports field punning, which allows us to skip the field values if the field name happens to be the same as the name of the value variable it is bound to.*

0 comments on commit 9db5b2e

Please sign in to comment.