Created
September 18, 2023 05:59
-
-
Save damirka/189fd2c1284ee5c0c97a90b7be9fb2a7 to your computer and use it in GitHub Desktop.
Code samples from the BH Singapore presentation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[allow(unused_variable, unused_field)] | |
module wework::cup { | |
/// A generic Cup from the manufacturer. | |
struct Cup has key, store { | |
id: UID, | |
filled: bool | |
} | |
/// Purchase a Cup from the manufacturer. | |
public fun purchase(coin: Coin<SUI>, ctx: &mut TxContext): Cup { | |
/* ... */ abort 0 | |
} | |
/// Fill a Cup with coffee (keeping it simple) | |
public fun fill(cup: &mut Cup) { cup.filled = true; } | |
/// Drink from a Cup. | |
public fun drink(cup: &mut Cup) { cup.filled = false; } | |
/// Look inside a Cup. | |
public fun is_filled(cup: &Cup): bool { cup.filled } | |
use sui::sui::SUI; | |
use sui::coin::Coin; | |
use sui::object::UID; | |
use sui::tx_context::TxContext; | |
} | |
#[allow(unused_variable, unused_field)] | |
module wework::cupboard { | |
use wework::cup::Cup; | |
/// Bag allows attaching objects to objects. | |
/// And we have a ShelfFor every type, eg `Cups`. | |
struct Cupboard has key, store { | |
id: UID, | |
bag: Bag | |
} | |
/// Take a `Cup` from the shelf with cups | |
public fun take_cup(store: &mut Cupboard): Cup { | |
let cups_mut = bag::borrow_mut(&mut store.bag, ShelfFor<Cup> {}); | |
vector::pop_back(cups_mut) | |
} | |
/// Place a `Cup` to the shelf with cups | |
public fun place_cup(store: &mut Cupboard, cup: Cup) { | |
let cups_mut = bag::borrow_mut(&mut store.bag, ShelfFor<Cup> {}); | |
vector::push_back(cups_mut, cup) | |
} | |
public fun take<T: store>(store: &mut Cupboard): T { | |
assert!(bag::contains(&store.bag, ShelfFor<T> {}), ENotThere); | |
let shelf_mut = bag::borrow_mut(&mut store.bag, ShelfFor<T> {}); | |
vector::pop_back(shelf_mut) | |
} | |
public fun place<T: store>(store: &mut Cupboard, item: T) { | |
if (!bag::contains(&store.bag, ShelfFor<T> {})) { | |
bag::add(&mut store.bag, ShelfFor<T> {}, vector<T>[]); | |
}; | |
vector::push_back( | |
bag::borrow_mut(&mut store.bag, ShelfFor<T> {}), | |
item | |
) | |
} | |
struct ShelfFor<phantom T> has copy, store, drop {} | |
struct Cups has copy, drop {} | |
const ENotThere: u64 = 0; | |
use std::vector; | |
use sui::object::UID; | |
use sui::bag::{Self, Bag}; | |
} | |
#[allow(unused_variable, unused_field)] | |
module wework::coffee_machine { | |
/// Max capacity of a CoffeeMachine in cups. | |
const CAPACITY: u8 = 100; | |
/// A liquid? An object? Something! | |
struct Coffee has store, drop {} | |
/// A CoffeeMachine; produces Coffee in portions, can be refilled by an | |
/// operator. | |
struct CoffeeMachine has key { | |
id: UID, | |
refills_left: u8, | |
} | |
/// Install a CoffeeMachine. | |
public fun install(operator: &Operator, ctx: &mut TxContext) { | |
sui::transfer::share_object(CoffeeMachine { | |
id: object::new(ctx), | |
refills_left: CAPACITY, | |
}) | |
} | |
/// Can only be performed by an operator. | |
public fun refill(operator: &Operator, machine: &mut CoffeeMachine) { | |
machine.refills_left = CAPACITY; | |
} | |
/// If only everything in life was this simple. | |
public fun make_coffee(machine: &mut CoffeeMachine): Coffee { | |
assert!(machine.refills_left > 0, EOutOfCoffee); | |
machine.refills_left = machine.refills_left - 1; | |
Coffee {} | |
} | |
/// The worst thing that can happen! | |
const EOutOfCoffee: u64 = 0; | |
struct Operator {} | |
use sui::object::{Self, UID}; | |
use sui::tx_context::TxContext; | |
} | |
module wework::cup_v2 { | |
/// Now it seems that there's no content, but there actually is. | |
struct Cup has key, store { id: UID } | |
/// Add some contents to the Cup. | |
public fun fill<T: store>(self: &mut Cup, contents: T) { | |
dynamic_field::add(&mut self.id, ContentsKey {}, contents); | |
} | |
/// Drink what can be drunk. | |
public fun drink<T: store + drop>(self: &mut Cup) { | |
let _: T = dynamic_field::remove(&mut self.id, ContentsKey {}); | |
} | |
/// Some contents can't be drunk. | |
public fun consume<T: store>(self: &mut Cup): T { | |
dynamic_field::remove(&mut self.id, ContentsKey {}) | |
} | |
/// Look inside a Cup. | |
public fun is_filled(self: &Cup): bool { | |
dynamic_field::exists_(&self.id, ContentsKey {}) | |
} | |
struct ContentsKey has copy, store, drop {} | |
use sui::dynamic_field; | |
use sui::object::UID; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment