Created
August 24, 2022 04:09
-
-
Save sblackshear/7bc539d6a531b361a89249c5ec074dd8 to your computer and use it in GitHub Desktop.
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
// Copyright (c) 2022, Mysten Labs, Inc. | |
// SPDX-License-Identifier: Apache-2.0 | |
/// This example demonstrates a basic use of a shared object. | |
/// Rules: | |
/// - anyone can create and share a counter | |
/// - everyone can increment a counter by 1 | |
/// - the owner of the counter can reset it to any value | |
module basics::counter { | |
use sui::transfer; | |
use sui::object::{Self, UID}; | |
use sui::tx_context::{Self, TxContext}; | |
/// A shared counter. | |
struct Counter has key { | |
id: UID, | |
owner: address, | |
value: u64 | |
} | |
public fun owner(counter: &Counter): address { | |
counter.owner | |
} | |
public fun value(counter: &Counter): u64 { | |
counter.value | |
} | |
/// Create and share a Counter object. | |
public entry fun create(ctx: &mut TxContext) { | |
transfer::share_object(Counter { | |
id: object::new(ctx), | |
owner: tx_context::sender(ctx), | |
value: 0 | |
}) | |
} | |
public fun make_child(parent: &mut Counter, ctx: &mut TxContext) { | |
let c = Counter { | |
id: object::new(ctx), | |
owner: tx_context::sender(ctx), | |
value: 0 | |
}; | |
transfer::transfer_to_object(c, parent) | |
} | |
public fun delete_child(_parent: &mut Counter, child: Counter) { | |
let Counter { id, owner: _, value: _ } = child; | |
object::delete(id) | |
} | |
/// Increment a counter by 1. | |
public entry fun increment(counter: &mut Counter) { | |
counter.value = counter.value + 1; | |
} | |
/// Set value (only runnable by the Counter owner) | |
public entry fun set_value(counter: &mut Counter, value: u64, ctx: &mut TxContext) { | |
assert!(counter.owner == tx_context::sender(ctx), 0); | |
counter.value = value; | |
} | |
/// Assert a value for the counter. | |
public entry fun assert_value(counter: &Counter, value: u64) { | |
assert!(counter.value == value, 0) | |
} | |
} | |
#[test_only] | |
module basics::counter_test { | |
use sui::test_scenario; | |
use basics::counter; | |
#[test] | |
fun child_counter() { | |
let user = @0xA1; | |
let scenario = &mut test_scenario::begin(&user); | |
{ | |
counter::create(test_scenario::ctx(scenario)); | |
}; | |
test_scenario::next_tx(scenario, &user); | |
{ | |
let counter_wrapper = test_scenario::take_shared<counter::Counter>(scenario); | |
let counter = test_scenario::borrow_mut(&mut counter_wrapper); | |
counter::make_child(counter, test_scenario::ctx(scenario)); | |
test_scenario::return_shared(scenario, counter_wrapper); | |
}; | |
test_scenario::next_tx(scenario, &user); | |
{ | |
let counter_wrapper = test_scenario::take_shared<counter::Counter>(scenario); | |
let counter = test_scenario::borrow_mut(&mut counter_wrapper); | |
let child: counter::Counter = test_scenario::take_child_object(scenario, counter); | |
counter::delete_child(counter, child); | |
test_scenario::return_shared(scenario, counter_wrapper); | |
}; | |
} | |
#[test] | |
fun test_counter() { | |
let owner = @0xC0FFEE; | |
let user1 = @0xA1; | |
let scenario = &mut test_scenario::begin(&user1); | |
test_scenario::next_tx(scenario, &owner); | |
{ | |
counter::create(test_scenario::ctx(scenario)); | |
}; | |
test_scenario::next_tx(scenario, &user1); | |
{ | |
let counter_wrapper = test_scenario::take_shared<counter::Counter>(scenario); | |
let counter = test_scenario::borrow_mut(&mut counter_wrapper); | |
assert!(counter::owner(counter) == owner, 0); | |
assert!(counter::value(counter) == 0, 1); | |
counter::increment(counter); | |
counter::increment(counter); | |
counter::increment(counter); | |
test_scenario::return_shared(scenario, counter_wrapper); | |
}; | |
test_scenario::next_tx(scenario, &owner); | |
{ | |
let counter_wrapper = test_scenario::take_shared<counter::Counter>(scenario); | |
let counter = test_scenario::borrow_mut(&mut counter_wrapper); | |
assert!(counter::owner(counter) == owner, 0); | |
assert!(counter::value(counter) == 3, 1); | |
counter::set_value(counter, 100, test_scenario::ctx(scenario)); | |
test_scenario::return_shared(scenario, counter_wrapper); | |
}; | |
test_scenario::next_tx(scenario, &user1); | |
{ | |
let counter_wrapper = test_scenario::take_shared<counter::Counter>(scenario); | |
let counter = test_scenario::borrow_mut(&mut counter_wrapper); | |
assert!(counter::owner(counter) == owner, 0); | |
assert!(counter::value(counter) == 100, 1); | |
counter::increment(counter); | |
assert!(counter::value(counter) == 101, 2); | |
test_scenario::return_shared(scenario, counter_wrapper); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment