Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples.sui.io/basics/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-fram

[addresses]
basics = "0x0"
patterns = "0x0"
sui = "0000000000000000000000000000000000000002"
44 changes: 44 additions & 0 deletions examples.sui.io/basics/sources/hot_potato.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module patterns::trade_in {
use sui::coin::{Self, Coin};
use sui::transfer;
use sui::sui::SUI;
use sui::object::{Self, UID};
use sui::tx_context::{TxContext};

const MODEL_ONE_PRICE: u64 = 10000;
const MODEL_TWO_PRICE: u64 = 20000;
const EWrongModel: u64 = 1;
const EIncorrectAmount: u64 = 2;

struct Phone has key, store { id: UID, model: u8 }

struct Receipt { price: u64 }

public fun buy_phone(model: u8, ctx: &mut TxContext): (Phone, Receipt) {
assert!(model == 1 || model == 2, EWrongModel);
let price = if (model == 1) MODEL_ONE_PRICE else MODEL_TWO_PRICE;

(
Phone { id: object::new(ctx), model },
Receipt { price }
)
}

public fun pay_full(receipt: Receipt, payment: Coin<SUI>) {
let Receipt { price } = receipt;
assert!(coin::value(&payment) == price, EIncorrectAmount);

transfer::transfer(payment, @patterns);
}

public fun trade_in(receipt: Receipt, old_phone: Phone, payment: Coin<SUI>) {
let Receipt { price } = receipt;
let tradein_price = if (old_phone.model == 1) MODEL_ONE_PRICE else MODEL_TWO_PRICE;
let to_pay = price - (tradein_price / 2);

assert!(coin::value(&payment) == to_pay, EIncorrectAmount);

transfer::transfer(old_phone, @patterns);
transfer::transfer(payment, @patterns);
}
}
71 changes: 71 additions & 0 deletions examples.sui.io/basics/sources/id_pointer.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module patterns::lock_and_key {
use std::option::{Self, Option};
use sui::object::{Self, ID, UID};
use sui::tx_context::{Self, TxContext};
use sui::transfer;

const ELockIsEmpty: u64 = 0;
const EKeyMismatch: u64 = 1;
const ELockIsFull: u64 = 2;

struct Lock<T: store + key> has key {
id: UID,
locked: Option<T>
}

struct Key<phantom T: store + key> has key, store {
id: UID,
for: ID
}

public fun key_for<T: store + key>(key: &Key<T>): ID {
key.for
}

public entry fun create<T: store + key>(obj: T, ctx: &mut TxContext) {
let id = object::new(ctx);
let for = object::uid_to_inner(&id);

transfer::share_object(Lock<T> {
id,
locked: option::some(obj)
});

transfer::transfer(
Key<T> {
for,
id: object::new(ctx)
},
tx_context::sender(ctx)
);
}

public entry fun lock<T: store + key>(
obj: T,
lock: &mut Lock<T>,
key: &Key<T>
) {
assert!(option::is_none(&lock.locked), ELockIsFull);
assert!(&key.for == object::borrow_id(lock), EKeyMismatch);

option::fill(&mut lock.locked, obj);
}

public entry fun unlock<T: store + key>(
lock: &mut Lock<T>,
key: &Key<T>
): T {
assert!(option::is_some(&lock.locked), ELockIsEmpty);
assert!(&key.for == object::borrow_id(lock), EKeyMismatch);

option::extract(&mut lock.locked)
}

public fun take<T: store + key>(
lock: &mut Lock<T>,
key: &Key<T>,
ctx: &mut TxContext
) {
transfer::transfer(unlock(lock, key), tx_context::sender(ctx));
}
}