Skip to content

Commit

Permalink
adds a counter example (MystenLabs#1539)
Browse files Browse the repository at this point in the history
  • Loading branch information
damirka authored Apr 22, 2022
1 parent 637f67f commit 8f41b18
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions sui_programmability/examples/basics/sources/Counter.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// 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::ID::VersionedID;
use Sui::TxContext::{Self, TxContext};

/// A shared counter.
struct Counter has key {
id: VersionedID,
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(script) fun create(ctx: &mut TxContext) {
Transfer::share_object(Counter {
id: TxContext::new_id(ctx),
owner: TxContext::sender(ctx),
value: 0
})
}

/// Increment a counter by 1.
public(script) fun increment(counter: &mut Counter, _ctx: &mut TxContext) {
counter.value = counter.value + 1;
}

/// Set value (only runnable by the Counter owner)
public(script) fun set_value(counter: &mut Counter, value: u64, ctx: &mut TxContext) {
assert!(counter.owner == TxContext::sender(ctx), 0);
counter.value = value;
}
}

#[test_only]
module Basics::CounterTest {
use Sui::TestScenario;
use Basics::Counter;

#[test]
public(script) fun test_counter() {
let owner = @0xC0FFEE;
let user1 = @0xA1;

let scenario = &mut TestScenario::begin(&user1);

TestScenario::next_tx(scenario, &owner);
{
Counter::create(TestScenario::ctx(scenario));
};

TestScenario::next_tx(scenario, &user1);
{
let counter = TestScenario::take_object<Counter::Counter>(scenario);

assert!(Counter::owner(&counter) == owner, 0);
assert!(Counter::value(&counter) == 0, 1);

Counter::increment(&mut counter, TestScenario::ctx(scenario));
Counter::increment(&mut counter, TestScenario::ctx(scenario));
Counter::increment(&mut counter, TestScenario::ctx(scenario));
TestScenario::return_object(scenario, counter);
};

TestScenario::next_tx(scenario, &owner);
{
let counter = TestScenario::take_object<Counter::Counter>(scenario);

assert!(Counter::owner(&counter) == owner, 0);
assert!(Counter::value(&counter) == 3, 1);

Counter::set_value(&mut counter, 100, TestScenario::ctx(scenario));

TestScenario::return_object(scenario, counter);
};

TestScenario::next_tx(scenario, &user1);
{
let counter = TestScenario::take_object<Counter::Counter>(scenario);

assert!(Counter::owner(&counter) == owner, 0);
assert!(Counter::value(&counter) == 100, 1);

Counter::increment(&mut counter, TestScenario::ctx(scenario));

assert!(Counter::value(&counter) == 101, 2);

TestScenario::return_object(scenario, counter);
};
}
}

0 comments on commit 8f41b18

Please sign in to comment.