forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
M1.move
153 lines (131 loc) · 4.95 KB
/
M1.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
module MyFirstPackage::M1 {
use Sui::ID::VersionedID;
use Sui::TxContext::TxContext;
struct Sword has key, store {
id: VersionedID,
magic: u64,
strength: u64,
}
struct Forge has key, store {
id: VersionedID,
swords_created: u64,
}
// module initializer to be executed when this module is published
fun init(ctx: &mut TxContext) {
use Sui::Transfer;
use Sui::TxContext;
let admin = Forge {
id: TxContext::new_id(ctx),
swords_created: 0,
};
// transfer the forge object to the module/package publisher
// (presumably the game admin)
Transfer::transfer(admin, TxContext::sender(ctx));
}
public fun swords_created(self: &Forge): u64 {
self.swords_created
}
public fun magic(self: &Sword): u64 {
self.magic
}
public fun strength(self: &Sword): u64 {
self.strength
}
public fun sword_create(forge: &mut Forge, magic: u64, strength: u64, recipient: address, ctx: &mut TxContext) {
use Sui::Transfer;
use Sui::TxContext;
// create a sword
let sword = Sword {
id: TxContext::new_id(ctx),
magic: magic,
strength: strength,
};
// transfer the sword
Transfer::transfer(sword, recipient);
forge.swords_created = forge.swords_created + 1;
}
public fun sword_transfer(sword: Sword, recipient: address, _ctx: &mut TxContext) {
use Sui::Transfer;
// transfer the sword
Transfer::transfer(sword, recipient);
}
#[test]
public fun test_module_init() {
use Sui::TestScenario;
// create test address representing game admin
let admin = @0xBABE;
// first transaction to emulate module initialization
let scenario = &mut TestScenario::begin(&admin);
{
init(TestScenario::ctx(scenario));
};
// second transaction to check if the forge has been created
// and has initial value of zero swords created
TestScenario::next_tx(scenario, &admin);
{
// extract the Forge object
let forge = TestScenario::remove_object<Forge>(scenario);
// verify number of created swords
assert!(swords_created(&forge) == 0, 1);
// return the Forge object to the object pool
TestScenario::return_object(scenario, forge)
}
}
#[test]
public fun test_sword_transactions() {
use Sui::TestScenario;
// create test addresses representing users
let admin = @0xBABE;
let initial_owner = @0xCAFE;
let final_owner = @0xFACE;
// first transaction to emulate module initialization
let scenario = &mut TestScenario::begin(&admin);
{
init(TestScenario::ctx(scenario));
};
// second transaction executed by admin to create the sword
TestScenario::next_tx(scenario, &admin);
{
let forge = TestScenario::remove_object<Forge>(scenario);
// create the sword and transfer it to the initial owner
sword_create(&mut forge, 42, 7, initial_owner, TestScenario::ctx(scenario));
TestScenario::return_object(scenario, forge)
};
// third transaction executed by the initial sword owner
TestScenario::next_tx(scenario, &initial_owner);
{
// extract the sword owned by the initial owner
let sword = TestScenario::remove_object<Sword>(scenario);
// transfer the sword to the final owner
sword_transfer(sword, final_owner, TestScenario::ctx(scenario));
};
// fourth transaction executed by the final sword owner
TestScenario::next_tx(scenario, &final_owner);
{
// extract the sword owned by the final owner
let sword = TestScenario::remove_object<Sword>(scenario);
// verify that the sword has expected properties
assert!(magic(&sword) == 42 && strength(&sword) == 7, 1);
// return the sword to the object pool (it cannot be simply "dropped")
TestScenario::return_object(scenario, sword)
}
}
#[test]
public fun test_sword_create() {
use Sui::Transfer;
use Sui::TxContext;
// create a dummy TxContext for testing
let ctx = TxContext::dummy();
// create a sword
let sword = Sword {
id: TxContext::new_id(&mut ctx),
magic: 42,
strength: 7,
};
// check if accessor functions return correct values
assert!(magic(&sword) == 42 && strength(&sword) == 7, 1);
// create a dummy address and transfer the sword
let dummy_address = @0xCAFE;
Transfer::transfer(sword, dummy_address);
}
}