forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds a counter example (MystenLabs#1539)
- Loading branch information
Showing
1 changed file
with
106 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
sui_programmability/examples/basics/sources/Counter.move
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
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); | ||
}; | ||
} | ||
} |