create-dubhe 0.0.19 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/package.json +1 -1
  2. package/template/{nextjs/sui-template/contracts/counter/sources/codegen/schemas/default → 101/sui-template/contracts/counter/sources/codegen}/dapp/metadata.move +6 -6
  3. package/template/101/sui-template/contracts/counter/sources/codegen/dapp/schema.move +112 -0
  4. package/template/{nextjs/sui-template/contracts/counter/sources/codegen/schemas/default → 101/sui-template/contracts/counter/sources/codegen}/dapp/system.move +17 -17
  5. package/template/101/sui-template/contracts/counter/sources/codegen/{events → data}/increment_event.move +0 -6
  6. package/template/101/sui-template/contracts/counter/sources/codegen/errors.move +16 -0
  7. package/template/101/sui-template/contracts/counter/sources/codegen/events.move +23 -0
  8. package/template/101/sui-template/contracts/counter/sources/codegen/genesis.move +20 -0
  9. package/template/101/sui-template/contracts/counter/sources/codegen/schema.move +54 -0
  10. package/template/101/sui-template/contracts/counter/sources/scripts/deploy_hook.move +12 -22
  11. package/template/101/sui-template/contracts/counter/sources/systems/counter.move +10 -13
  12. package/template/101/sui-template/contracts/counter/sources/tests/counter.move +8 -8
  13. package/template/101/sui-template/contracts/counter/sources/tests/init.move +1 -1
  14. package/template/101/sui-template/contracts/dubhe-framework/sources/events.move +33 -0
  15. package/template/101/sui-template/contracts/dubhe-framework/sources/storages/double_map.move +111 -153
  16. package/template/101/sui-template/contracts/dubhe-framework/sources/storages/map.move +87 -141
  17. package/template/{nextjs/sui-template/contracts/dubhe-framework/sources/storages/migration.move → 101/sui-template/contracts/dubhe-framework/sources/storages/storage.move} +3 -3
  18. package/template/101/sui-template/contracts/dubhe-framework/sources/storages/tests.move +82 -334
  19. package/template/101/sui-template/contracts/dubhe-framework/sources/storages/value.move +77 -83
  20. package/template/101/sui-template/dubhe.config.ts +1 -3
  21. package/template/101/sui-template/package.json +6 -4
  22. package/template/101/sui-template/src/chain/config.ts +2 -4
  23. package/template/101/sui-template/src/pages/home/index.tsx +4 -4
  24. package/template/contract/sui-template/contracts/dubhe-framework/sources/events.move +33 -0
  25. package/template/contract/sui-template/contracts/dubhe-framework/sources/storages/double_map.move +111 -153
  26. package/template/contract/sui-template/contracts/dubhe-framework/sources/storages/map.move +87 -141
  27. package/template/{101/sui-template/contracts/dubhe-framework/sources/storages/migration.move → contract/sui-template/contracts/dubhe-framework/sources/storages/storage.move} +3 -3
  28. package/template/contract/sui-template/contracts/dubhe-framework/sources/storages/tests.move +82 -334
  29. package/template/contract/sui-template/contracts/dubhe-framework/sources/storages/value.move +77 -83
  30. package/template/contract/sui-template/package.json +6 -4
  31. package/template/nextjs/sui-template/contracts/counter/Move.toml +1 -1
  32. package/template/{101/sui-template/contracts/counter/sources/codegen/schemas/default → nextjs/sui-template/contracts/counter/sources/codegen}/dapp/metadata.move +6 -6
  33. package/template/nextjs/sui-template/contracts/counter/sources/codegen/dapp/schema.move +112 -0
  34. package/template/{101/sui-template/contracts/counter/sources/codegen/schemas/default → nextjs/sui-template/contracts/counter/sources/codegen}/dapp/system.move +17 -17
  35. package/template/nextjs/sui-template/contracts/counter/sources/codegen/{events → data}/increment_event.move +0 -6
  36. package/template/nextjs/sui-template/contracts/counter/sources/codegen/errors.move +16 -0
  37. package/template/nextjs/sui-template/contracts/counter/sources/codegen/events.move +23 -0
  38. package/template/nextjs/sui-template/contracts/counter/sources/codegen/genesis.move +20 -0
  39. package/template/nextjs/sui-template/contracts/counter/sources/codegen/schema.move +54 -0
  40. package/template/nextjs/sui-template/contracts/counter/sources/scripts/deploy_hook.move +12 -22
  41. package/template/nextjs/sui-template/contracts/counter/sources/systems/counter.move +10 -13
  42. package/template/nextjs/sui-template/contracts/counter/sources/tests/counter.move +8 -8
  43. package/template/nextjs/sui-template/contracts/counter/sources/tests/init.move +1 -1
  44. package/template/nextjs/sui-template/contracts/dubhe-framework/sources/events.move +33 -0
  45. package/template/nextjs/sui-template/contracts/dubhe-framework/sources/storages/double_map.move +111 -153
  46. package/template/nextjs/sui-template/contracts/dubhe-framework/sources/storages/map.move +87 -141
  47. package/template/{contract/sui-template/contracts/dubhe-framework/sources/storages/migration.move → nextjs/sui-template/contracts/dubhe-framework/sources/storages/storage.move} +3 -3
  48. package/template/nextjs/sui-template/contracts/dubhe-framework/sources/storages/tests.move +82 -334
  49. package/template/nextjs/sui-template/contracts/dubhe-framework/sources/storages/value.move +77 -83
  50. package/template/nextjs/sui-template/dubhe.config.ts +1 -3
  51. package/template/nextjs/sui-template/package.json +7 -5
  52. package/template/nextjs/sui-template/src/chain/config.ts +3 -5
  53. package/template/nextjs/sui-template/src/pages/home/index.tsx +4 -4
  54. package/template/101/sui-template/contracts/counter/sources/codegen/errors/invalid_increment_error.move +0 -30
  55. package/template/101/sui-template/contracts/counter/sources/codegen/schemas/counter.move +0 -60
  56. package/template/101/sui-template/contracts/counter/sources/codegen/schemas/default/dapp/schema.move +0 -114
  57. package/template/nextjs/sui-template/contracts/counter/sources/codegen/errors/invalid_increment_error.move +0 -30
  58. package/template/nextjs/sui-template/contracts/counter/sources/codegen/schemas/counter.move +0 -60
  59. package/template/nextjs/sui-template/contracts/counter/sources/codegen/schemas/default/dapp/schema.move +0 -114
@@ -1,195 +1,49 @@
1
1
  #[test_only]
2
2
  module dubhe::storage_tests {
3
- use dubhe::storage_double_map::StorageDoubleMap;
4
- use dubhe::storage_map::StorageMap;
5
3
  use dubhe::storage_double_map;
6
4
  use dubhe::storage_map;
7
- use dubhe::storage_value::StorageValue;
8
5
  use sui::test_scenario;
9
- use sui::transfer::public_share_object;
10
6
  use dubhe::storage_value;
11
7
 
12
8
  public struct TestValue has drop, copy, store {
13
9
  value: u64,
14
10
  }
15
11
 
16
- public struct TestStoreValue has store {
17
- value: u64,
18
- }
19
-
20
- /// Destroy a TestStoreValue`.
21
- public fun destroy_value(value: TestStoreValue) {
22
- let TestStoreValue { value: _ } = value;
23
- }
24
-
25
- public struct TestObject has key, store {
26
- id: UID,
27
- value: StorageValue<TestValue>,
28
- immutable_value: StorageValue<TestStoreValue>,
29
- map: StorageMap<u32, TestValue>,
30
- immutable_map: StorageMap<u32, TestStoreValue>,
31
- double_map: StorageDoubleMap<u32, u32, TestValue>,
32
- immutable_double_map: StorageDoubleMap<u32, u32, TestStoreValue>,
33
- }
34
-
35
- public fun value(self: &mut TestObject): &mut StorageValue<TestValue> {
36
- &mut self.value
37
- }
38
-
39
-
40
- #[test]
41
- public fun test_double_map() {
42
- let mut scenario = test_scenario::begin(@0x0001);
43
- let ctx = test_scenario::ctx(&mut scenario);
44
-
45
- let mut double_map = storage_double_map::new();
46
-
47
- double_map.insert(0, 0, TestValue { value: 0 });
48
- double_map.insert(0, 1, TestValue { value: 1 });
49
- double_map.insert(0, 2, TestValue { value: 2 });
50
-
51
- assert!(double_map.contains_key(0, 0));
52
- assert!(double_map.contains_key(0, 1));
53
- assert!(double_map.contains_key(0, 2));
54
- let (keys1, keys2) = double_map.keys();
55
- assert!(keys1 == vector[0, 0, 0], 0);
56
- assert!(keys2 == vector[0, 1, 2], 0);
57
- assert!(double_map.values() == vector[TestValue { value: 0 }, TestValue { value: 1 }, TestValue { value: 2 }]);
58
- assert!(double_map.length() == 3);
59
- assert!(double_map.borrow(0, 0).value == 0);
60
- assert!(double_map.borrow(0, 1).value == 1);
61
- assert!(double_map.borrow_mut(0, 2).value == 2);
62
- assert!(double_map.get(0, 0).value == 0);
63
- assert!(double_map.get(0, 1).value == 1);
64
- assert!(double_map.get(0, 2).value == 2);
65
- assert!(double_map.try_get(0, 0) == option::some(TestValue { value: 0 }));
66
- assert!(double_map.try_get(0, 1) == option::some(TestValue { value: 1 }));
67
- assert!(double_map.try_get(0, 2) == option::some(TestValue { value: 2 }));
68
- assert!(double_map.try_get(0, 3) == option::none());
69
-
70
- double_map.take(0, 1);
71
-
72
- assert!(double_map.contains_key(0, 0));
73
- assert!(!double_map.contains_key(0, 1));
74
- assert!(double_map.contains_key(0, 2));
75
- let (keys1, keys2) = double_map.keys();
76
- assert!(keys1 == vector[0, 0]);
77
- assert!(keys2 == vector[0, 2]);
78
- assert!(double_map.values() == vector[TestValue { value: 0 }, TestValue { value: 2 }]);
79
- assert!(double_map.length() == 2);
80
- assert!(double_map.borrow(0, 0).value == 0);
81
- assert!(double_map.borrow_mut(0, 2).value == 2);
82
-
83
- double_map.mutate!(0, 0, |value| {
84
- *value = TestValue { value: 2 };
85
- });
86
-
87
- assert!(double_map.contains_key(0, 0));
88
- assert!(!double_map.contains_key(0, 1));
89
- assert!(double_map.contains_key(0, 2));
90
- let (keys1, keys2) = double_map.keys();
91
- assert!(keys1 == vector[0, 0]);
92
- assert!(keys2 == vector[0, 2]);
93
- assert!(double_map.values() == vector[TestValue { value: 2 }, TestValue { value: 2 }]);
94
- assert!(double_map.length() == 2);
95
- assert!(double_map.borrow(0, 0).value == 2);
96
- assert!(double_map.borrow_mut(0, 2).value == 2);
97
-
98
- double_map.remove(0, 0);
99
- assert!(!double_map.contains_key(0,0));
100
- assert!(double_map.length() == 1);
101
- let (keys1, keys2) = double_map.keys();
102
- assert!(keys1 == vector[0]);
103
- assert!(keys2 == vector[2]);
104
- assert!(double_map.values() == vector[TestValue { value: 2 }]);
105
-
106
- double_map.remove(0,3);
107
- assert!(double_map.length() == 1);
108
- let (keys1, keys2) = double_map.keys();
109
- assert!(keys1 == vector[0]);
110
- assert!(keys2 == vector[2]);
111
- assert!(double_map.values() == vector[TestValue { value: 2 }]);
112
-
113
- double_map.set(0,3, TestValue { value: 0 });
114
- assert!(double_map.contains_key(0,3));
115
- assert!(double_map.length() == 2);
116
- let (keys1, keys2) = double_map.keys();
117
- assert!(keys1 == vector[0, 0]);
118
- assert!(keys2 == vector[2, 3]);
119
- assert!(double_map.values() == vector[TestValue { value: 2 }, TestValue { value: 0 }]);
120
- assert!(double_map.get(0,3).value == 0);
121
-
122
- double_map.set(0,3, TestValue { value: 3 });
123
- assert!(double_map.contains_key(0,3));
124
- assert!(double_map.length() == 2);
125
- let (keys1, keys2) = double_map.keys();
126
- assert!(keys1 == vector[0, 0]);
127
- assert!(keys2 == vector[2, 3]);
128
- assert!(double_map.values() == vector[TestValue { value: 2 }, TestValue { value: 3 }]);
129
- assert!(double_map.get(0,3).value == 3);
130
-
131
- public_share_object(TestObject {
132
- id: object::new(ctx),
133
- value: storage_value::new(),
134
- immutable_value: storage_value::new(),
135
- map: storage_map::new(),
136
- immutable_map: storage_map::new(),
137
- double_map,
138
- immutable_double_map: storage_double_map::new(),
139
- });
140
-
141
- scenario.end();
142
- }
143
-
144
12
  #[test]
145
13
  public fun test_value() {
146
14
  let mut scenario = test_scenario::begin(@0x0001);
147
15
  let ctx = test_scenario::ctx(&mut scenario);
148
16
 
149
- let mut value = storage_value::new();
17
+ let mut value = storage_value::new(b"value", ctx);
150
18
  value.set(TestValue { value: 1 });
19
+ assert!(value.contains() == true);
151
20
  assert!(value.get() == TestValue { value: 1 });
152
- assert!(*value.borrow() == TestValue { value: 1 });
153
- assert!(*value.borrow_mut() == TestValue { value: 1 });
21
+ assert!(value[] == TestValue { value: 1 });
154
22
 
155
23
  value.set(TestValue { value: 2 });
156
24
  assert!(value.get() == TestValue { value: 2 });
157
-
158
- value.mutate!(|value| {
159
- *value = TestValue { value: 3 };
160
- });
25
+ value.set(TestValue { value: 3 });
161
26
  assert!(value.get() == TestValue { value: 3 });
27
+ assert!(value.try_get() == option::some(TestValue { value: 3 }));
28
+ assert!(value.is_empty() == false);
162
29
 
163
- value.set(TestValue { value: 4 });
164
- assert!(value.get() == TestValue { value: 4 });
165
-
30
+ value.remove();
31
+ assert!(value.contains() == false);
32
+ assert!(value.try_get() == option::none<TestValue>());
33
+ assert!(value.is_empty() == true);
166
34
 
167
- let mut immutable_value = storage_value::new();
168
- immutable_value.put(TestStoreValue { value: 1 });
169
- assert!(immutable_value.borrow().value == 1);
170
-
171
- immutable_value.mutate!(|store_value| {
172
- store_value.value = 2;
173
- });
174
- assert!(immutable_value.borrow().value == 2);
175
- assert!(immutable_value.borrow_mut().value == 2);
176
-
177
- immutable_value.mutate!(|store_value| {
178
- store_value.value = 3;
179
- });
180
- assert!(immutable_value.borrow().value == 3);
181
- assert!(immutable_value.borrow_mut().value == 3);
182
-
183
- public_share_object(TestObject {
184
- id: object::new(ctx),
185
- value,
186
- immutable_value,
187
- map: storage_map::new(),
188
- immutable_map: storage_map::new(),
189
- double_map: storage_double_map::new(),
190
- immutable_double_map: storage_double_map::new(),
35
+ value.set(TestValue { value: 4 });
36
+ assert!(value.contains() == true);
37
+ assert!(value.try_remove() == option::some(TestValue { value: 4 }));
38
+ assert!(value.try_remove() == option::none<TestValue>());
39
+ assert!(value.contains() == false);
40
+
41
+ let x: u64 = 0;
42
+ x.range_do!(1000, |x| {
43
+ value.set(TestValue { value: x });
191
44
  });
192
45
 
46
+ value.drop();
193
47
  scenario.end();
194
48
  }
195
49
 
@@ -198,201 +52,95 @@ module dubhe::storage_tests {
198
52
  let mut scenario = test_scenario::begin(@0x0001);
199
53
  let ctx = test_scenario::ctx(&mut scenario);
200
54
 
201
- let mut map = storage_map::new();
202
- map.insert(0, TestValue { value: 0 });
203
- map.insert(1, TestValue { value: 1 });
204
- map.insert(2, TestValue { value: 2 });
55
+ let mut map = storage_map::new(b"TestValueMap", ctx);
56
+ assert!(map.is_empty() == true);
57
+ assert!(map.length() == 0);
58
+ map.set(0, TestValue { value: 0 });
59
+ map.set(1, TestValue { value: 1 });
60
+ map.set(2, TestValue { value: 2 });
205
61
 
206
- assert!(map.contains_key(0));
207
- assert!(map.contains_key(1));
208
- assert!(map.contains_key(2));
209
- assert!(map.keys() == vector[0, 1, 2]);
210
- assert!(map.values() == vector[TestValue { value: 0 }, TestValue { value: 1 }, TestValue { value: 2 }]);
211
- assert!(map.length() == 3);
212
- assert!(map.borrow(0).value == 0);
213
- assert!(map.borrow(1).value == 1);
214
- assert!(map.borrow_mut(2).value == 2);
215
- assert!(map.get(0).value == 0);
216
- assert!(map.get(1).value == 1);
217
- assert!(map.get(2).value == 2);
62
+ assert!(map[0] == TestValue { value: 0 });
63
+ assert!(map.get(0) == TestValue { value: 0 });
218
64
  assert!(map.try_get(0) == option::some(TestValue { value: 0 }));
65
+ assert!(map[1] == TestValue { value: 1 });
66
+ assert!(map.get(1) == TestValue { value: 1 });
219
67
  assert!(map.try_get(1) == option::some(TestValue { value: 1 }));
68
+ assert!(map[2] == TestValue { value: 2 });
69
+ assert!(map.get(2) == TestValue { value: 2 });
220
70
  assert!(map.try_get(2) == option::some(TestValue { value: 2 }));
221
71
  assert!(map.try_get(3) == option::none());
72
+ assert!(map.contains(0) == true);
73
+ assert!(map.contains(1) == true);
74
+ assert!(map.contains(2) == true);
75
+ assert!(map.contains(3) == false);
76
+ assert!(map.is_empty() == false);
77
+ assert!(map.length() == 3);
222
78
 
223
- map.take(1);
224
-
225
- assert!(map.contains_key(0));
226
- assert!(!map.contains_key(1));
227
- assert!(map.contains_key(2));
228
- assert!(map.keys() == vector[0, 2]);
229
- assert!(map.values() == vector[TestValue { value: 0 }, TestValue { value: 2 }]);
230
- assert!(map.length() == 2);
231
- assert!(map.borrow(0).value == 0);
232
- assert!(map.borrow_mut(2).value == 2);
233
-
234
-
235
- map.mutate!(0, |store_value| {
236
- store_value.value = 2;
237
- });
238
- assert!(map.contains_key(0));
239
- assert!(!map.contains_key(1));
240
- assert!(map.contains_key(2));
241
- assert!(map.keys() == vector[0, 2]);
242
- assert!(map.values() == vector[TestValue { value: 2 }, TestValue { value: 2 }]);
79
+ map.remove(1);
80
+ assert!(map.try_get(1) == option::none());
81
+ assert!(map.contains(0) == true);
82
+ assert!(map.contains(1) == false);
83
+ assert!(map.contains(2) == true);
243
84
  assert!(map.length() == 2);
244
- assert!(map.borrow(0).value == 2);
245
- assert!(map.borrow_mut(2).value == 2);
246
85
 
247
- map.remove(0);
248
- assert!(!map.contains_key(0));
86
+ assert!(map.try_remove(2) == option::some(TestValue { value: 2 }));
87
+ assert!(map.contains(2) == false);
249
88
  assert!(map.length() == 1);
250
- assert!(map.keys() == vector[2]);
251
- assert!(map.values() == vector[TestValue { value: 2 }]);
252
89
 
253
- map.remove(3);
254
- assert!(map.length() == 1);
255
- assert!(map.keys() == vector[2]);
256
- assert!(map.values() == vector[TestValue { value: 2 }]);
257
-
258
- map.set(3, TestValue { value: 0 });
259
- assert!(map.contains_key(3));
260
- assert!(map.length() == 2);
261
- assert!(map.keys() == vector[2, 3]);
262
- assert!(map.values() == vector[TestValue { value: 2 }, TestValue { value: 0 }]);
263
- assert!(map.get(3).value == 0);
264
-
265
- map.set(3, TestValue { value: 3 });
266
- assert!(map.contains_key(3));
267
- assert!(map.length() == 2);
268
- assert!(map.keys() == vector[2, 3]);
269
- assert!(map.values() == vector[TestValue { value: 2 }, TestValue { value: 3 }]);
270
- assert!(map.get(3).value == 3);
271
-
272
- public_share_object(TestObject {
273
- id: object::new(ctx),
274
- value: storage_value::new(),
275
- immutable_value: storage_value::new(),
276
- map,
277
- immutable_map: storage_map::new(),
278
- double_map: storage_double_map::new(),
279
- immutable_double_map: storage_double_map::new(),
90
+ let x: u32 = 1000;
91
+ x.range_do!(2000, |x| {
92
+ map.set(x, TestValue { value: 2 });
280
93
  });
281
94
 
95
+ map.drop();
282
96
  scenario.end();
283
97
  }
284
98
 
285
99
  #[test]
286
- public fun test_immutable_map() {
100
+ public fun test_double_map() {
287
101
  let mut scenario = test_scenario::begin(@0x0001);
288
102
  let ctx = test_scenario::ctx(&mut scenario);
289
103
 
290
- let mut immutable_map = storage_map::new();
291
- immutable_map.insert(0, TestStoreValue { value: 0 });
292
- immutable_map.insert(1, TestStoreValue { value: 1 });
293
- immutable_map.insert(2, TestStoreValue { value: 2 });
104
+ let mut double_map = storage_double_map::new(b"TestValueDoubleMap", ctx);
294
105
 
295
- assert!(immutable_map.contains_key(0));
296
- assert!(immutable_map.contains_key(1));
297
- assert!(immutable_map.contains_key(2));
298
- assert!(immutable_map.keys() == vector[0, 1, 2]);
299
- assert!(immutable_map.length() == 3);
300
- assert!(immutable_map.borrow(0).value == 0);
301
- assert!(immutable_map.borrow(1).value == 1);
302
- assert!(immutable_map.borrow_mut(2).value == 2);
106
+ double_map.set(0, 0, TestValue { value: 0 });
107
+ double_map.set(0, 0, TestValue { value: 0 });
108
+ double_map.set(0, 1, TestValue { value: 1 });
109
+ double_map.set(0, 2, TestValue { value: 2 });
303
110
 
304
- let detele_store_value = immutable_map.take(1);
305
- destroy_value(detele_store_value);
306
- assert!(immutable_map.contains_key(0));
307
- assert!(!immutable_map.contains_key(1));
308
- assert!(immutable_map.contains_key(2));
309
- assert!(immutable_map.keys() == vector[0, 2]);
310
- assert!(immutable_map.length() == 2);
311
- assert!(immutable_map.borrow(0).value == 0);
312
- assert!(immutable_map.borrow_mut(2).value == 2);
111
+ assert!(double_map.contains(0, 0));
112
+ assert!(double_map.contains(0, 1));
113
+ assert!(double_map.contains(0, 2));
114
+ assert!(double_map.length() == 3);
115
+ assert!(double_map.is_empty() == false);
313
116
 
117
+ assert!(double_map.get(0, 0) == TestValue { value: 0 });
118
+ assert!(double_map[0, 0] == TestValue { value: 0 });
119
+ assert!(double_map.try_get(0, 0) == option::some(TestValue { value: 0 }));
120
+ assert!(double_map.get(0, 1) == TestValue { value: 1 });
121
+ assert!(double_map[0, 1] == TestValue { value: 1 });
122
+ assert!(double_map.try_get(0, 1) == option::some(TestValue { value: 1 }));
123
+ assert!(double_map.get(0, 2) == TestValue { value: 2 });
124
+ assert!(double_map[0, 2] == TestValue { value: 2 });
125
+ assert!(double_map.try_get(0, 2) == option::some(TestValue { value: 2 }));
314
126
 
315
- immutable_map.mutate!(0, |store_value| {
316
- store_value.value = 2;
317
- });
318
- assert!(immutable_map.contains_key(0));
319
- assert!(!immutable_map.contains_key(1));
320
- assert!(immutable_map.contains_key(2));
321
- assert!(immutable_map.keys() == vector[0, 2]);
322
- assert!(immutable_map.length() == 2);
323
- assert!(immutable_map.borrow(0).value == 2);
324
- assert!(immutable_map.borrow_mut(2).value == 2);
127
+ double_map.remove(0, 1);
128
+ assert!(double_map.try_get(0, 1) == option::none());
129
+ assert!(double_map.contains(0, 1) == false);
130
+ assert!(double_map.length() == 2);
131
+
132
+ assert!(double_map.try_remove(0, 2) == option::some(TestValue { value: 2 }));
133
+ assert!(double_map.try_get(0, 2) == option::none());
134
+ assert!(double_map.contains(0, 2) == false);
135
+ assert!(double_map.length() == 1);
325
136
 
326
- public_share_object(TestObject {
327
- id: object::new(ctx),
328
- value: storage_value::new(),
329
- immutable_value: storage_value::new(),
330
- map: storage_map::new(),
331
- immutable_map,
332
- double_map: storage_double_map::new(),
333
- immutable_double_map: storage_double_map::new(),
137
+ let x: u32 = 1000;
138
+ x.range_do!(1000, |x| {
139
+ double_map.set(x, x, TestValue { value: 2 });
334
140
  });
335
141
 
142
+ double_map.drop();
336
143
  scenario.end();
337
144
  }
338
145
 
339
- // #[test]
340
- // public fun test_immutable_map2() {
341
- // let mut scenario = test_scenario::begin(@0x0001);
342
- // let ctx = test_scenario::ctx(&mut scenario);
343
- //
344
- // let mut immutable_map = storage_immutable_map::new(ctx);
345
- // immutable_map.set(0, TestValue { value: 1 });
346
- //
347
- // // immutable_map.mutate!(|store_value| {
348
- // // store_value.value = 2;
349
- // // });
350
- // // assert!(immutable_map.get().value == 2);
351
- // // assert!(immutable_map.borrow_mut().value == 2);
352
- // //
353
- // // immutable_map.mutate!(|store_value| {
354
- // // store_value.value = 3;
355
- // // });
356
- // // assert!(immutable_map.get().value == 3);
357
- // // assert!(immutable_map.borrow_mut().value == 3);
358
- //
359
- // public_share_object(TestObject2 {
360
- // id: object::new(ctx),
361
- // value: storage_value::new(TestValue { value: 1 }),
362
- // immutable_value: storage_value::new(TestStoreValue { value: 1 }),
363
- // immutable_map,
364
- // });
365
- //
366
- // scenario.end();
367
- // }
368
-
369
- // #[test]
370
- // public fun test_object() {
371
- // let sender = @0x0001;
372
- // let mut scenario_val = test_scenario::begin(sender);
373
- // let scenario = &mut scenario_val;
374
- //
375
- // let obj = TestObject {
376
- // id: object::new(test_scenario::ctx(scenario)),
377
- // value: storage_value::empty(),
378
- // };
379
- // public_share_object(obj);
380
- //
381
- // test_scenario::next_tx(scenario, sender);
382
- //
383
- // let mut obj = test_scenario::take_shared<TestObject>(scenario);
384
- //
385
- // obj.value.set(TestValue { value: 1 });
386
- // assert!(obj.value.contains());
387
- // assert!(!obj.value.is_empty());
388
- // assert!(obj.value.get() == TestValue { value: 1 });
389
- //
390
- // obj.value.set(TestValue { value: 2 });
391
- // assert!(obj.value.contains());
392
- // assert!(!obj.value.is_empty());
393
- // assert!(obj.value.get() == TestValue { value: 2 });
394
- //
395
- // test_scenario::return_shared<TestObject>(obj);
396
- // test_scenario::end(scenario_val);
397
- // }
398
146
  }
@@ -1,97 +1,91 @@
1
- module dubhe::storage_value {
2
-
3
- /// This key does not exist in the map
4
- const EValueDoesNotExist: u64 = 0;
5
-
6
- /// This key does not exist in the map
7
- const EValueAlreadyExist: u64 = 1;
8
-
9
- /// An entry in the value
10
- public struct Entry<Value: store> has copy, drop, store {
11
- value: Value,
12
- }
13
-
14
- /// A storable handler for values in general. Is used in the `StorageValue`
15
- public struct StorageValue<Value: store> has store {
16
- contents: vector<Entry<Value>>,
17
- }
18
-
19
- /// Creates a new, empty StorageValue
20
- public fun new<Value: store>(): StorageValue<Value> {
21
- StorageValue { contents: vector[] }
22
- }
23
-
24
- /// Gets the value of the StorageValue `self: &StorageValue<Value>`.
25
- public fun borrow<Value: store>(self: &StorageValue<Value>): &Value {
26
- assert!(self.contains(), EValueDoesNotExist);
27
- &self.contents[0].value
28
- }
29
-
30
- /// Gets the value of the StorageValue `self: &mut StorageValue<Value>`.
31
- public fun borrow_mut<Value: store>(self: &mut StorageValue<Value>): &mut Value {
32
- assert!(self.contains(), EValueDoesNotExist);
33
- &mut self.contents[0].value
34
- }
35
-
36
- /// Update the `value` of the `StorageValue`.
37
- public macro fun mutate<$Value: store>($self: &mut StorageValue<$Value>, $f: |&mut $Value|) {
38
- let self = $self;
39
- $f(borrow_mut(self));
40
- }
1
+ module dubhe::storage_value;
2
+
3
+ use std::ascii::{String, string};
4
+ use sui::dynamic_field as field;
5
+ use dubhe::storage_event;
6
+ use std::option::some;
7
+ use std::option::none;
8
+
9
+ public struct StorageValue<phantom V: copy + drop + store> has key, store {
10
+ /// the ID of this Storage
11
+ id: UID,
12
+ // the name of the Storage
13
+ name: String,
14
+ /// the number of key-value pairs in the Storage
15
+ size: u64,
16
+ }
41
17
 
42
- /// Return true if `self` contains_key an entry for `key`, false otherwise
43
- public fun contains<V: store>(self: &StorageValue<V>): bool {
44
- self.contents.length() == 1
18
+ /// Creates a new, empty table
19
+ public fun new<V: copy + drop + store>(name: vector<u8>, ctx: &mut TxContext): StorageValue<V> {
20
+ StorageValue {
21
+ id: object::new(ctx),
22
+ name: string(name),
23
+ size: 0,
45
24
  }
25
+ }
46
26
 
47
- /// Remove the entry `key` |-> `value` from self. Aborts if `key` is not bound in `self`.
48
- public fun take<V: store>(self: &mut StorageValue<V>): V {
49
- assert!(self.contains(), EValueDoesNotExist);
50
- let Entry { value } = self.contents.remove(0);
51
- value
52
- }
27
+ /// Adds a key-value pair to the table `table: &mut Table<K, V>`
28
+ public fun set<V: copy + drop + store>(table: &mut StorageValue<V>, v: V) {
29
+ if (table.contains()) {
30
+ field::remove<u8, V>(&mut table.id, 0);
31
+ };
32
+ field::add<u8, V>(&mut table.id, 0, v);
33
+ storage_event::emit_set_record<u8, u8, V>(table.name, none(), none(), some(v));
34
+ }
53
35
 
54
- /// Put the `value` of the `StorageValue`.
55
- public fun put<V: store>(self: &mut StorageValue<V>, value: V) {
56
- assert!(!self.contains(), EValueAlreadyExist);
57
- self.contents.push_back(Entry { value });
36
+ /// Immutable borrows the value associated with the key in the table `table: &Table<K, V>`.
37
+ public fun try_get<V: copy + drop + store>(table: &StorageValue<V>): Option<V> {
38
+ if (table.contains()) {
39
+ option::some(table[])
40
+ } else {
41
+ option::none()
58
42
  }
43
+ }
59
44
 
45
+ #[syntax(index)]
46
+ /// Immutable borrows the value associated with the key in the table `table: &Table<K, V>`.
47
+ /// Aborts with `sui::dynamic_field::EFieldDoesNotExist` if the table does not have an entry with
48
+ /// that key `k: K`.
49
+ public fun get<V: copy + drop + store>(table: &StorageValue<V>): &V {
50
+ field::borrow<u8, V>(&table.id, 0)
51
+ }
60
52
 
61
- // ======================================= Value: drop + copy + store =======================================
62
-
63
- /// Set the `value` of the `StorageValue`.
64
- public fun set<V: copy + drop + store>(self: &mut StorageValue<V>, value: V) {
65
- if (self.contains()) {
66
- *self.borrow_mut() = value;
67
- } else {
68
- self.contents.push_back(Entry { value });
69
- }
53
+ /// Removes the key-value pair in the table `table: &mut Table<K, V>` and returns the value.
54
+ /// Aborts with `sui::dynamic_field::EFieldDoesNotExist` if the table does not have an entry with
55
+ /// that key `k: K`.
56
+ public fun remove<V: copy + drop + store>(table: &mut StorageValue<V>) {
57
+ if (table.contains()) {
58
+ field::remove<u8, V>(&mut table.id, 0);
59
+ storage_event::emit_remove_record<u8, u8>(table.name, none(), none());
70
60
  }
61
+ }
71
62
 
72
- /// Get the `value` of the `StorageValue`.
73
- public fun get<V: copy + drop + store>(self: &StorageValue<V>): V {
74
- assert!(self.contains(), EValueDoesNotExist);
75
- self.contents[0].value
63
+ /// Removes the key-value pair in the table `table: &mut Table<K, V>` and returns the value.
64
+ /// Aborts with `sui::dynamic_field::EFieldDoesNotExist` if the table does not have an entry with
65
+ /// that key `k: K`.
66
+ public fun try_remove<V: copy + drop + store>(table: &mut StorageValue<V>): Option<V> {
67
+ if (table.contains()) {
68
+ let v = field::remove<u8, V>(&mut table.id, 0);
69
+ storage_event::emit_remove_record<u8, u8>(table.name, none(), none());
70
+ option::some(v)
71
+ } else {
72
+ option::none()
76
73
  }
74
+ }
77
75
 
78
- /// Safely try borrow a value bound to `key` in `self`.
79
- /// Return Some(V) if the value exists, None otherwise.
80
- /// Only works for a "copyable" value as references cannot be stored in `vector`.
81
- public fun try_get<V: copy + drop + store>(self: &StorageValue<V>): Option<V> {
82
- if (self.contains()) {
83
- option::some(self.contents[0].value)
84
- } else {
85
- option::none()
86
- }
87
- }
76
+ /// Returns true iff there is a value associated with the key `k: K` in table `table: &Table<K, V>`
77
+ public fun contains<V: copy + drop + store>(table: &StorageValue<V>): bool {
78
+ field::exists_with_type<u8, V>(&table.id, 0)
79
+ }
88
80
 
89
- /// Remove the entry `key` |-> `value` from self. Aborts if `key` is not bound in `self`.
90
- public fun remove<V: copy + drop + store>(self: &mut StorageValue<V>) {
91
- if (self.contains()) {
92
- self.contents.remove(0);
93
- }
94
- }
81
+ /// Returns true iff the table is empty (if `length` returns `0`)
82
+ public fun is_empty<V: copy + drop + store>(table: &StorageValue<V>): bool {
83
+ !field::exists_with_type<u8, V>(&table.id, 0)
84
+ }
95
85
 
96
- // ============================================================================================
86
+ /// Drop a possibly non-empty table.
87
+ /// Usable only if the value type `V` has the `drop` ability
88
+ public fun drop<V: copy + drop + store>(table: StorageValue<V>) {
89
+ let StorageValue { id, name: _, size: _ } = table;
90
+ id.delete()
97
91
  }
@@ -4,9 +4,7 @@ export const dubheConfig = {
4
4
  name: 'counter',
5
5
  description: 'counter contract',
6
6
  schemas: {
7
- counter: {
8
- value: 'StorageValue<u32>',
9
- },
7
+ value: 'StorageValue<u32>',
10
8
  },
11
9
  events: {
12
10
  Increment: { value: 'u32' },