-
Notifications
You must be signed in to change notification settings - Fork 11.3k
/
Copy pathObjectBasics.move
66 lines (54 loc) · 1.85 KB
/
ObjectBasics.move
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/// Test CTURD object basics (create, transfer, update, read, delete)
module Sui::ObjectBasics {
use Sui::Event;
use Sui::ID::{Self, VersionedID};
use Sui::TxContext::{Self, TxContext};
use Sui::Transfer;
struct Object has key, store {
id: VersionedID,
value: u64,
}
struct Wrapper has key {
id: VersionedID,
o: Object
}
struct NewValueEvent has copy, drop {
new_value: u64
}
public fun create(value: u64, recipient: address, ctx: &mut TxContext) {
Transfer::transfer(
Object { id: TxContext::new_id(ctx), value },
recipient
)
}
public fun transfer(o: Object, recipient: address, _ctx: &mut TxContext) {
Transfer::transfer(o, recipient)
}
public fun freeze_object(o: Object, _ctx: &mut TxContext) {
Transfer::freeze_object(o)
}
public fun transfer_to_object(o: Object, owner: &mut Object, _ctx: &mut TxContext) {
Transfer::transfer_to_object(o, owner)
}
public fun set_value(o: &mut Object, value: u64, _ctx: &mut TxContext) {
o.value = value;
}
// test that reading o2 and updating o1 works
public fun update(o1: &mut Object, o2: &Object, _ctx: &mut TxContext) {
o1.value = o2.value;
// emit an event so the world can see the new value
Event::emit(NewValueEvent { new_value: o2.value })
}
public fun delete(o: Object, _ctx: &mut TxContext) {
let Object { id, value: _ } = o;
ID::delete(id);
}
public fun wrap(o: Object, ctx: &mut TxContext) {
Transfer::transfer(Wrapper { id: TxContext::new_id(ctx), o }, TxContext::sender(ctx))
}
public fun unwrap(w: Wrapper, ctx: &mut TxContext) {
let Wrapper { id, o } = w;
ID::delete(id);
Transfer::transfer(o, TxContext::sender(ctx))
}
}