Skip to content

Commit

Permalink
[tutorial] add tictactoc publish script (MystenLabs#3540)
Browse files Browse the repository at this point in the history
* devnet-0.6.1 release (MystenLabs#3322)

* Add an automated script to publish tictactoe, gitignore sui.log

* update releases/versions to 0.6.2

(cherry picked from commit ad6d37e)

* Update tutorials.md

Minor edits for clarity and style

* update script

* Update tutorials.md

making the references to PlayerX and Player0 consistent

* Update doc/src/explore/tutorials.md

nit

Co-authored-by: Randall DuBois <[email protected]>

* update cargo.lock

Co-authored-by: Tom Harbert <[email protected]>
Co-authored-by: Randall DuBois <[email protected]>
  • Loading branch information
3 people authored Jul 30, 2022
1 parent 4ff3573 commit 50d3335
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ storage/

# logs
wallet.log.*
sui.log.*
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
DevNet releases can be found in the `devnet` branch.

## Current Release
* 22-07-19: [0.6.1](https://github.com/MystenLabs/sui/releases/tag/devnet-0.6.1)
* 22-07-26: [0.6.2](https://github.com/MystenLabs/sui/releases/tag/devnet-0.6.2)

## Past Releases
* 22-07-19: [0.6.1](https://github.com/MystenLabs/sui/releases/tag/devnet-0.6.1)
* 22-07-12: [0.6.0](https://github.com/MystenLabs/sui/releases/tag/devnet-0.6.0-rc)
* 22-06-22: [0.5.0](https://github.com/MystenLabs/sui/releases/tag/devnet-0.5.0-rc)
* 22-06-07: [0.4.0](https://github.com/MystenLabs/sui/releases/tag/devnet-0.4.0-rc)
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sui-node"
version = "0.6.1"
version = "0.6.2"
authors = ["Mysten Labs <[email protected]>"]
license = "Apache-2.0"
publish = false
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-tool/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sui-tool"
version = "0.6.1"
version = "0.6.2"
authors = ["Mysten Labs <[email protected]>"]
license = "Apache-2.0"
publish = false
Expand Down
2 changes: 1 addition & 1 deletion crates/sui/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sui"
version = "0.6.1"
version = "0.6.2"
authors = ["Mysten Labs <[email protected]>"]
license = "Apache-2.0"
publish = false
Expand Down
80 changes: 65 additions & 15 deletions doc/src/explore/tutorials.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,68 @@ Simply leave the terminal with Sui running and start a new terminal for the
remainder of this tutorial.

This tutorial models gas fees under a simplified schema. In practice, the Sui
network will charge for gas using its native currency SUI. This transaction fee
network charges for gas using its native currency SUI. This transaction fee
equals the computational effort of executing operations on the Sui network (i.e.
gas units) times the price of gas in the SUI currency (i.e. the gas price).

When you complete the setup steps, you can use the either use following script to publish and run the sample code, or perform each step manually. Using the script is optional. To manually run each step, follow the steps starting in the [Gather accounts and gas objects](#gather-accounts-and-gas-objects) section.

## Quick script
If you prefer not to enter command step by step, or need to go though it multiple
times (such as when you change some Move source code), the following automated script
may be useful to save some time.
Run this script from the project repo root.
```sh
#!/bin/bash
# a bash script to automate the process of publishing the game package
# this script should be run at root of the repo

# assign address
CLIENT_ADDRESS=$(sui client addresses | tail -n +2)
ADMIN=`echo "${CLIENT_ADDRESS}" | head -n 1`
PLAYER_X=`echo "${CLIENT_ADDRESS}" | sed -n 2p`
PLAYER_O=`echo "${CLIENT_ADDRESS}" | sed -n 3p`
# gas id
IFS='|'
ADMIN_GAS_INFO=$(sui client gas --address $ADMIN | sed -n 3p)
read -a tmparr <<< "$ADMIN_GAS_INFO"
ADMIN_GAS_ID=`echo ${tmparr[0]} | xargs`

X_GAS_INFO=$(sui client gas --address $PLAYER_X | sed -n 3p)
read -a tmparr <<< "$X_GAS_INFO"
X_GAS_ID=`echo ${tmparr[0]} | xargs`

O_GAS_INFO=$(sui client gas --address $PLAYER_O | sed -n 3p)
read -a tmparr <<< "$O_GAS_INFO"
O_GAS_ID=`echo ${tmparr[0]} | xargs`

# publish games
certificate=$(sui client publish --path ./sui_programmability/examples/games --gas $ADMIN_GAS_ID --gas-budget 30000)

package_id_identifier="The newly published package object ID:"
res=$(echo $certificate | awk -v s="$package_id_identifier" 'index($0, s) == 1')
IFS=':'
read -a resarr <<< "$res"
echo ${resarr[1]}
PACKAGE_ID=`echo ${resarr[1]} | xargs`

echo "package id: $PACKAGE_ID"
# Playing TicTacToe

# create a game
sui client call --package $PACKAGE_ID --module tic_tac_toe --function create_game --args $PLAYER_X $PLAYER_O --gas $ADMIN_GAS_ID --gas-budget 1000

# start playing the game ...
```

## Gather accounts and gas objects

In that new terminal, let us take a look at the account addresses we own in
our CLI client with the command:
In the new terminal session you started, use the following command in the Sui CLI client to view the account addresses we own:
```shell
$ sui client addresses
```

Which will result in output resembling:
Which results in output resembling:
```shell
Showing 5 results.
ECF53CE22D1B2FB588573924057E9ADDAD1D8385
Expand All @@ -43,15 +92,16 @@ ECF53CE22D1B2FB588573924057E9ADDAD1D8385
DB4C7667636471AFF396B900EB7B63FACAF629B6
A6BBB1930E01495EE93CE912EA01C29695E07890
```
Note that since these addresses are random generated, they will be different from what you see. We are going to need three addresses to play TicTacToe. Let's pick the first three addresses. Let's call them ADMIN, PLAYER_X and PLAYER_O.
Note that since these addresses are randomly generated, they are different from the values you see when you run the command. We need three addresses to play TicTacToe. Let's pick the first three addresses. Let's call them ADMIN, PLAYER_X and PLAYER_O.

Since we will be using these addresses and gas objects repeatedly in the rest of this tutorial, let's make them environment variables so that we don't have to retype them every time:
```
$ export ADMIN=ECF53CE22D1B2FB588573924057E9ADDAD1D8385
export PLAYER_X=7B61DA6AACED7F28C1187D998955F10464BEAE55
export PLAYER_O=251CF224B6BA3A019D04B6041357C20490F7A322
```

For each of these addresses, let's discover their gas objects for each account address:
For each of these addresses, let's discover the gas objects associated with each account address:
```
$ sui client gas --address $ADMIN
Object ID | Version | Gas Value
Expand Down Expand Up @@ -96,7 +146,7 @@ To publish the game, we run the publish command and specify the path to the sour
$ sui client publish --path ./sui/sui_programmability/examples/games --gas $ADMIN_GAS --gas-budget 30000
```

Which will yield results resembling:
Which yields results resembling:
```shell
----- Certificate ----
Signed Authorities : ...
Expand All @@ -114,7 +164,7 @@ export PACKAGE=A613A7FF8CB03E0DFC0D157E232BBA50C5F19D17
```
## Playing TicTacToe
As we mentioned earlier, we will need 3 parties to participate in this game: Admin, PlayerX and PlayerO.
As we mentioned earlier, we need 3 parties to participate in this game: Admin, PlayerX and PlayerO.
As a high level, the game works as following:
1. The admin creates a game, which also specifies the addresses of the two players. This will also create two capability objects and send to each of the addresses to give them permission to participate in the same game. This ensures that an arbitrary person cannot attempt to join this game.
2. Each player takes turns to send a *Mark* object to the admin indicating where they want to place their mark.
Expand Down Expand Up @@ -149,7 +199,7 @@ F1B8161BD97D3CD6627E739AD675089C5ACFB452 SequenceNumber(1) o#1c92bdf7646cad2a653
Mutated Objects:
38B89FE9F4A4823F1406938E87A8767CBD7F0B93 SequenceNumber(2) o#26dbaf7ec2032a6270a45498ad46ac0b1ddbc361fcff20cadafaf5d39b8181b1
```
The above call created three objects. For each object, it printed out a tuple of three values (object_id, version, object_digest). Object ID is what we care about here. Since we don't have a real application here to display things for us, we need a bit of object printing magic to figure out which object is which. Let's print out the metadata of each created object (replace the object ID with what you see on your screen):
The preceding call created three objects. For each object, it printed out a tuple of three values (object_id, version, object_digest). Object ID is what we care about here. Since we don't have a real application here to display things for us, we need a bit of object printing magic to figure out which object is which. Let's print out the metadata of each created object (replace the object ID with what you see on your screen):
```
$ sui client object --id 5851B7EA07B93E68696BC0CF811D2E266DFB880D
Owner: AddressOwner(k#251cf224b6ba3a019d04b6041357c20490f7a322)
Expand Down Expand Up @@ -184,12 +234,12 @@ export OCAP=5851B7EA07B93E68696BC0CF811D2E266DFB880D
export GAME=F1B8161BD97D3CD6627E739AD675089C5ACFB452
```
By convention, Player X goes first. Player X wants to put a mark at the center of the gameboard ((1, 1)). This needs to take two steps. First Player X creates a Mark object with the placement intention and send it to the admin.
By convention, PlayerX goes first. PlayerX wants to put a mark at the center of the gameboard ((1, 1)). This needs to take two steps. First PlayerX creates a Mark object with the placement intention and send it to the admin.
We will call the `send_mark_to_game` function in `TicTacToe`, whose signature looks like this:
```
public entry fun send_mark_to_game(cap: &mut MarkMintCap, game_address: address, row: u64, col: u64, ctx: &mut TxContext);
```
The `cap` argument will be Player X's capability object (XCAP), and `game_address` argument will be the admin's address (ADMIN):
The `cap` argument will be PlayerX's capability object (XCAP), and `game_address` argument will be the admin's address (ADMIN):
```shell
$ sui client call --package $PACKAGE --module TicTacToe --function send_mark_to_game --args $XCAP $ADMIN 1 1 --gas $X_GAS --gas-budget 1000
```
Expand Down Expand Up @@ -228,7 +278,7 @@ _|X|_
| |
```
Player O now tries to put a mark at (0, 0):
PlayerO now tries to put a mark at (0, 0):
```shell
$ sui client call --package $PACKAGE --module TicTacToe --function send_mark_to_game --args $OCAP $ADMIN 0 0 --gas $O_GAS --gas-budget 1000
```
Expand Down Expand Up @@ -264,7 +314,7 @@ _|X|_
| |
```
Player X puts a mark at (0, 2):
PlayerX puts a mark at (0, 2):
```shell
$ sui client call --package $PACKAGE --module TicTacToe --function send_mark_to_game --args $XCAP $ADMIN 0 2 --gas $X_GAS --gas-budget 1000
```
Expand Down Expand Up @@ -292,7 +342,7 @@ _|X|_
| |
```
Player O places a mark at (1, 0):
PlayerO places a mark at (1, 0):
```shell
$ sui client call --package $PACKAGE --module TicTacToe --function send_mark_to_game --args $OCAP $ADMIN 1 0 --gas $O_GAS --gas-budget 1000
```
Expand All @@ -319,7 +369,7 @@ O|_|X
O|X|_
| |
```
This is a chance for Player X to win! X now mints the winning mark at (2, 0):
This is a chance for PlayerX to win! X now mints the winning mark at (2, 0):
```shell
$ sui client call --package $PACKAGE --module TicTacToe --function send_mark_to_game --args $XCAP $ADMIN 2 0 --gas $X_GAS --gas-budget 1000
```
Expand Down

0 comments on commit 50d3335

Please sign in to comment.