create-dubhe 0.0.4 → 0.0.5

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 (157) hide show
  1. package/dist/index.mjs +44 -42
  2. package/package.json +1 -1
  3. package/template/101/rooch-template/.dockerignore +7 -0
  4. package/template/101/rooch-template/.eslintrc.json +3 -0
  5. package/template/101/rooch-template/.prettierrc +8 -0
  6. package/template/101/rooch-template/Dockerfile +52 -0
  7. package/template/101/rooch-template/LICENSE +674 -0
  8. package/template/101/rooch-template/README.md +14 -0
  9. package/template/101/rooch-template/contracts/counter/Move.toml +14 -0
  10. package/template/101/rooch-template/contracts/counter/sources/counter.move +48 -0
  11. package/template/101/rooch-template/dubhe.config.ts +14 -0
  12. package/template/101/rooch-template/jest.config.ts +17 -0
  13. package/template/101/rooch-template/next-env.d.ts +5 -0
  14. package/template/101/rooch-template/next-i18next.config.js +6 -0
  15. package/template/101/rooch-template/next.config.js +15 -0
  16. package/template/101/rooch-template/package.json +72 -0
  17. package/template/101/rooch-template/postcss.config.js +6 -0
  18. package/template/101/rooch-template/public/discord.svg +9 -0
  19. package/template/101/rooch-template/public/fail.svg +12 -0
  20. package/template/101/rooch-template/public/favicon.ico +0 -0
  21. package/template/101/rooch-template/public/locales/de/common.json +8 -0
  22. package/template/101/rooch-template/public/locales/de/footer.json +3 -0
  23. package/template/101/rooch-template/public/locales/de/second-page.json +5 -0
  24. package/template/101/rooch-template/public/locales/en/common.json +8 -0
  25. package/template/101/rooch-template/public/locales/en/footer.json +3 -0
  26. package/template/101/rooch-template/public/locales/en/second-page.json +5 -0
  27. package/template/101/rooch-template/public/locales/zn/common.json +8 -0
  28. package/template/101/rooch-template/public/locales/zn/footer.json +3 -0
  29. package/template/101/rooch-template/public/locales/zn/second-page.json +5 -0
  30. package/template/101/rooch-template/public/logo.svg +1 -0
  31. package/template/101/rooch-template/public/medium.svg +9 -0
  32. package/template/101/rooch-template/public/successful.svg +11 -0
  33. package/template/101/rooch-template/public/telegram.svg +9 -0
  34. package/template/101/rooch-template/public/twitter.svg +9 -0
  35. package/template/101/rooch-template/scripts/checkBalance.ts +37 -0
  36. package/template/101/rooch-template/scripts/generateAccount.ts +55 -0
  37. package/template/101/rooch-template/scripts/storeConfig.ts +52 -0
  38. package/template/101/rooch-template/scripts/waitNode.ts +9 -0
  39. package/template/101/rooch-template/src/chain/config.ts +5 -0
  40. package/template/101/rooch-template/src/css/font-awesome.css +2337 -0
  41. package/template/101/rooch-template/src/css/font-awesome.min.css +4 -0
  42. package/template/101/rooch-template/src/fonts/FontAwesome.otf +0 -0
  43. package/template/101/rooch-template/src/fonts/fontawesome-webfont.eot +0 -0
  44. package/template/101/rooch-template/src/fonts/fontawesome-webfont.svg +2671 -0
  45. package/template/101/rooch-template/src/fonts/fontawesome-webfont.ttf +0 -0
  46. package/template/101/rooch-template/src/fonts/fontawesome-webfont.woff +0 -0
  47. package/template/101/rooch-template/src/fonts/fontawesome-webfont.woff2 +0 -0
  48. package/template/101/rooch-template/src/jotai/index.tsx +12 -0
  49. package/template/101/rooch-template/src/pages/_app.tsx +9 -0
  50. package/template/101/rooch-template/src/pages/home/index.tsx +82 -0
  51. package/template/101/rooch-template/src/pages/index.tsx +14 -0
  52. package/template/101/rooch-template/tailwind.config.js +56 -0
  53. package/template/101/rooch-template/tsconfig.json +25 -0
  54. package/template/101/sui-template/contracts/counter/Move.toml +3 -3
  55. package/template/101/sui-template/contracts/counter/sources/codegen/schema_hub.move +60 -0
  56. package/template/101/sui-template/contracts/counter/sources/codegen/schemas/counter.move +17 -15
  57. package/template/101/sui-template/contracts/counter/sources/script/deploy_hook.move +37 -19
  58. package/template/101/sui-template/contracts/counter/sources/script/migrate.move +8 -0
  59. package/template/101/sui-template/localnet/dubhe-framework/Move.toml +6 -16
  60. package/template/{nextjs/sui-template/localnet/dubhe-framework/sources/schemas/dapps/dapps.move → 101/sui-template/localnet/dubhe-framework/sources/core/dapps/schema.move} +12 -2
  61. package/template/101/sui-template/localnet/dubhe-framework/sources/core/dapps/system.move +75 -0
  62. package/template/101/sui-template/localnet/dubhe-framework/sources/core/dapps/tests.move +52 -0
  63. package/template/101/sui-template/localnet/dubhe-framework/sources/core/root/schema.move +33 -0
  64. package/template/101/sui-template/localnet/dubhe-framework/sources/core/root/system.move +24 -0
  65. package/template/101/sui-template/localnet/dubhe-framework/sources/core/root/tests.move +39 -0
  66. package/template/101/sui-template/localnet/dubhe-framework/sources/frames/utils/type_info.move +29 -0
  67. package/template/101/sui-template/localnet/dubhe-framework/sources/storages/double_map.move +0 -3
  68. package/template/101/sui-template/localnet/dubhe-framework/sources/storages/map.move +0 -3
  69. package/template/101/sui-template/localnet/dubhe-framework/sources/storages/migrate.move +19 -0
  70. package/template/101/sui-template/localnet/dubhe-framework/sources/storages/value.move +0 -3
  71. package/template/101/sui-template/package.json +19 -27
  72. package/template/101/sui-template/scripts/deployment/testnet/deploy.ts +2 -2
  73. package/template/101/sui-template/src/chain/config.ts +3 -3
  74. package/template/cocos/sui-template/assets/lib/dubhe.js +21 -1
  75. package/template/cocos/sui-template/contracts/counter/Move.toml +1 -1
  76. package/template/cocos/sui-template/package.json +3 -3
  77. package/template/contract/sui-template/contracts/counter/Move.toml +13 -0
  78. package/template/contract/sui-template/contracts/counter/sources/codegen/dapp_key.move +16 -0
  79. package/template/contract/sui-template/contracts/counter/sources/codegen/schema_hub.move +60 -0
  80. package/template/contract/sui-template/contracts/counter/sources/codegen/schemas/counter.move +60 -0
  81. package/template/contract/sui-template/contracts/counter/sources/script/deploy_hook.move +85 -0
  82. package/template/contract/sui-template/contracts/counter/sources/script/migrate.move +8 -0
  83. package/template/contract/sui-template/dubhe.config.ts +14 -0
  84. package/template/contract/sui-template/localnet/dubhe-framework/Move.toml +29 -0
  85. package/template/{101/sui-template/localnet/dubhe-framework/sources/schemas/dapps/dapps.move → contract/sui-template/localnet/dubhe-framework/sources/core/dapps/schema.move} +12 -2
  86. package/template/contract/sui-template/localnet/dubhe-framework/sources/core/dapps/system.move +75 -0
  87. package/template/contract/sui-template/localnet/dubhe-framework/sources/core/dapps/tests.move +52 -0
  88. package/template/contract/sui-template/localnet/dubhe-framework/sources/core/root/schema.move +33 -0
  89. package/template/contract/sui-template/localnet/dubhe-framework/sources/core/root/system.move +24 -0
  90. package/template/contract/sui-template/localnet/dubhe-framework/sources/core/root/tests.move +39 -0
  91. package/template/contract/sui-template/localnet/dubhe-framework/sources/frames/utils/type_info.move +29 -0
  92. package/template/contract/sui-template/localnet/dubhe-framework/sources/storages/double_map.move +153 -0
  93. package/template/contract/sui-template/localnet/dubhe-framework/sources/storages/map.move +156 -0
  94. package/template/contract/sui-template/localnet/dubhe-framework/sources/storages/migrate.move +19 -0
  95. package/template/contract/sui-template/localnet/dubhe-framework/sources/storages/value.move +97 -0
  96. package/template/contract/sui-template/localnet/dubhe-framework/tests/init.move +6 -0
  97. package/template/contract/sui-template/localnet/dubhe-framework/tests/obelisk_framework_tests.move +19 -0
  98. package/template/contract/sui-template/package.json +28 -0
  99. package/template/contract/sui-template/scripts/checkBalance.ts +40 -0
  100. package/template/contract/sui-template/scripts/deployment/common.ts +45 -0
  101. package/template/contract/sui-template/scripts/deployment/localnet/deploy.ts +42 -0
  102. package/template/contract/sui-template/scripts/deployment/testnet/deploy.ts +48 -0
  103. package/template/contract/sui-template/scripts/deployment/types.ts +13 -0
  104. package/template/contract/sui-template/scripts/framework/common.ts +17 -0
  105. package/template/contract/sui-template/scripts/framework/deploy.ts +199 -0
  106. package/template/contract/sui-template/scripts/framework/parse-history.ts +76 -0
  107. package/template/contract/sui-template/scripts/framework/types.ts +8 -0
  108. package/template/contract/sui-template/scripts/generateAccount.ts +37 -0
  109. package/template/contract/sui-template/tsconfig.json +35 -0
  110. package/template/nextjs/sui-template/contracts/counter/Move.toml +3 -3
  111. package/template/nextjs/sui-template/contracts/counter/sources/codegen/schema_hub.move +60 -0
  112. package/template/nextjs/sui-template/contracts/counter/sources/codegen/schemas/counter.move +17 -15
  113. package/template/nextjs/sui-template/contracts/counter/sources/script/deploy_hook.move +37 -19
  114. package/template/nextjs/sui-template/contracts/counter/sources/script/migrate.move +8 -0
  115. package/template/nextjs/sui-template/contracts/counter/sources/systems/counter.move +11 -0
  116. package/template/nextjs/sui-template/dubhe.config.ts +2 -2
  117. package/template/nextjs/sui-template/localnet/dubhe-framework/Move.toml +6 -16
  118. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/core/dapps/metadata.move +96 -0
  119. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/core/dapps/schema.move +83 -0
  120. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/core/dapps/system.move +75 -0
  121. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/core/dapps/tests.move +52 -0
  122. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/core/root/schema.move +33 -0
  123. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/core/root/system.move +24 -0
  124. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/core/root/tests.move +39 -0
  125. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/frames/utils/type_info.move +29 -0
  126. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/storages/double_map.move +0 -3
  127. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/storages/map.move +0 -3
  128. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/storages/migrate.move +19 -0
  129. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/storages/tests.move +398 -0
  130. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/storages/value.move +0 -3
  131. package/template/nextjs/sui-template/package.json +19 -27
  132. package/template/nextjs/sui-template/scripts/deployment/testnet/deploy.ts +2 -2
  133. package/template/nextjs/sui-template/src/chain/config.ts +2 -2
  134. package/template/101/sui-template/localnet/dubhe-framework/README.md +0 -18
  135. package/template/101/sui-template/localnet/dubhe-framework/sources/access_control.move +0 -35
  136. package/template/101/sui-template/localnet/dubhe-framework/sources/events.move +0 -23
  137. package/template/101/sui-template/localnet/dubhe-framework/sources/resource_id.move +0 -22
  138. package/template/101/sui-template/localnet/dubhe-framework/sources/resource_types.move +0 -18
  139. package/template/101/sui-template/localnet/dubhe-framework/sources/schema.move +0 -36
  140. package/template/101/sui-template/localnet/dubhe-framework/sources/systems/dapps.move +0 -90
  141. package/template/101/sui-template/localnet/dubhe-framework/sources/world.move +0 -81
  142. package/template/101/sui-template/localnet/dubhe-framework/tests/dapp.move +0 -16
  143. package/template/nextjs/sui-template/localnet/dubhe-framework/README.md +0 -18
  144. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/access_control.move +0 -35
  145. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/events.move +0 -23
  146. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/resource_id.move +0 -22
  147. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/resource_types.move +0 -18
  148. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/schema.move +0 -36
  149. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/systems/dapps.move +0 -90
  150. package/template/nextjs/sui-template/localnet/dubhe-framework/sources/world.move +0 -81
  151. package/template/nextjs/sui-template/localnet/dubhe-framework/tests/dapp.move +0 -16
  152. /package/template/101/sui-template/contracts/counter/sources/{system → systems}/counter.move +0 -0
  153. /package/template/101/sui-template/localnet/dubhe-framework/sources/{schemas → core}/dapps/metadata.move +0 -0
  154. /package/template/101/sui-template/localnet/dubhe-framework/{tests/storage.move → sources/storages/tests.move} +0 -0
  155. /package/template/{nextjs/sui-template/contracts/counter/sources/system → contract/sui-template/contracts/counter/sources/systems}/counter.move +0 -0
  156. /package/template/{nextjs/sui-template/localnet/dubhe-framework/sources/schemas → contract/sui-template/localnet/dubhe-framework/sources/core}/dapps/metadata.move +0 -0
  157. /package/template/{nextjs/sui-template/localnet/dubhe-framework/tests/storage.move → contract/sui-template/localnet/dubhe-framework/sources/storages/tests.move} +0 -0
@@ -0,0 +1,75 @@
1
+ module dubhe::dapps_system {
2
+ use std::ascii::String;
3
+ use std::ascii;
4
+ use dubhe::root_schema::Root;
5
+ use dubhe::dapp_metadata;
6
+ use sui::clock::Clock;
7
+ use dubhe::dapps_schema::Dapps;
8
+ use dubhe::root_system;
9
+
10
+ public entry fun register<UpgradeCap: key>(
11
+ dapps: &mut Dapps,
12
+ upgrade_cap: &UpgradeCap,
13
+ name: String,
14
+ description: String,
15
+ clock: &Clock,
16
+ ctx: &mut TxContext
17
+ ) {
18
+
19
+ let dapp_id = object::id_address<UpgradeCap>(upgrade_cap);
20
+ assert!(!dapps.borrow_metadata().contains_key(dapp_id), 0);
21
+
22
+ dapps.borrow_mut_metadata().set(
23
+ dapp_id,
24
+ dapp_metadata::new(
25
+ name,
26
+ description,
27
+ ascii::string(b""),
28
+ ascii::string(b""),
29
+ clock.timestamp_ms(),
30
+ vector[]
31
+ )
32
+ );
33
+ dapps.borrow_mut_admin().set(dapp_id, ctx.sender());
34
+ dapps.borrow_mut_version().set(dapp_id, 0);
35
+ dapps.borrow_mut_safe_mode().set(dapp_id, false);
36
+ }
37
+
38
+ public entry fun set_metadata<UpgradeCap: key>(
39
+ dapps: &mut Dapps,
40
+ upgrade_cap: &UpgradeCap,
41
+ name: String,
42
+ description: String,
43
+ icon_url: String,
44
+ website_url: String,
45
+ partners: vector<String>
46
+ ) {
47
+ let dapp_id = object::id_address<UpgradeCap>(upgrade_cap);
48
+ assert!(dapps.borrow_metadata().contains_key(dapp_id), 0);
49
+ let created_at = dapps.borrow_mut_metadata().take(dapp_id).get_created_at();
50
+ dapps.borrow_mut_metadata().set(dapp_id, dapp_metadata::new(name, description, icon_url, website_url, created_at, partners));
51
+ }
52
+
53
+ public entry fun transfer_ownership<UpgradeCap: key>(
54
+ dapps: &mut Dapps,
55
+ upgrade_cap: &UpgradeCap,
56
+ new_admin: address,
57
+ ctx: &mut TxContext
58
+ ) {
59
+ let dapp_id = object::id_address<UpgradeCap>(upgrade_cap);
60
+ assert!(dapps.borrow_admin().get(dapp_id) == ctx.sender(), 0);
61
+ dapps.borrow_mut_admin().set(dapp_id, new_admin);
62
+ }
63
+
64
+ public entry fun add_verification(dapps: &mut Dapps, root: &Root, dapp_id: address, ctx: &mut TxContext) {
65
+ root_system::ensure_root(root, ctx);
66
+ assert!(dapps.borrow_metadata().contains_key(dapp_id), 0);
67
+ dapps.borrow_mut_verified().set(dapp_id, true);
68
+ }
69
+
70
+ public entry fun remove_verification(dapps: &mut Dapps, root: &Root, dapp_id: address, ctx: &mut TxContext) {
71
+ root_system::ensure_root(root, ctx);
72
+ assert!(dapps.borrow_metadata().contains_key(dapp_id), 0);
73
+ dapps.borrow_mut_verified().remove(dapp_id);
74
+ }
75
+ }
@@ -0,0 +1,52 @@
1
+ #[test_only]
2
+ module dubhe::dapps_tests {
3
+ use dubhe::dapps_schema::Dapps;
4
+ use dubhe::dapps_schema;
5
+ use dubhe::dapps_system;
6
+ use std::ascii::string;
7
+ use std::debug;
8
+ use sui::clock;
9
+ use sui::test_scenario;
10
+ use sui::package;
11
+ use sui::package::UpgradeCap;
12
+
13
+ public struct DappKey has drop {}
14
+
15
+ #[test]
16
+ public fun dapps_register() {
17
+ let mut scenario = test_scenario::begin(@0xA);
18
+ {
19
+ let ctx = test_scenario::ctx(&mut scenario);
20
+ dapps_schema::init_dapps_for_testing(ctx);
21
+ test_scenario::next_tx(&mut scenario,@0xA);
22
+ };
23
+
24
+ let mut dapps = test_scenario::take_shared<Dapps>(&scenario);
25
+
26
+ let upgrade_cap = package::test_publish(@0x42.to_id(), scenario.ctx());
27
+ debug::print(&object::id_address<UpgradeCap>(&upgrade_cap));
28
+
29
+ let clock = clock::create_for_testing(test_scenario::ctx(&mut scenario));
30
+ dapps_system::register<UpgradeCap>(
31
+ &mut dapps,
32
+ &upgrade_cap,
33
+ string(b"DappKey"),
34
+ string(b"DappKey"),
35
+ &clock,
36
+ test_scenario::ctx(&mut scenario)
37
+ );
38
+
39
+ test_scenario::next_tx(&mut scenario,@0xA);
40
+
41
+ let dapp_id = object::id_address<UpgradeCap>(&upgrade_cap);
42
+ assert!(dapps.borrow_version().get(dapp_id) == 0, 0);
43
+ assert!(dapps.borrow_metadata().contains_key(dapp_id));
44
+ assert!(dapps.borrow_admin().get(dapp_id) == test_scenario::ctx(&mut scenario).sender(), 0);
45
+ assert!(dapps.borrow_safe_mode().get(dapp_id) == false, 0);
46
+
47
+ clock::destroy_for_testing(clock);
48
+ test_scenario::return_shared<Dapps>(dapps);
49
+ upgrade_cap.make_immutable();
50
+ scenario.end();
51
+ }
52
+ }
@@ -0,0 +1,33 @@
1
+ module dubhe::root_schema {
2
+ use dubhe::storage_value;
3
+ use dubhe::storage_value::StorageValue;
4
+ use sui::transfer::public_share_object;
5
+
6
+ public struct Root has key, store {
7
+ id: UID,
8
+ key: StorageValue<address>,
9
+ }
10
+
11
+
12
+ public(package) fun borrow_mut_key(self: &mut Root): &mut StorageValue<address> {
13
+ &mut self.key
14
+ }
15
+
16
+ public fun borrow_key(self: &Root): &StorageValue<address> {
17
+ &self.key
18
+ }
19
+
20
+ fun init(ctx: &mut TxContext) {
21
+ let mut key = storage_value::new();
22
+ key.set(ctx.sender());
23
+ public_share_object(Root {
24
+ id: object::new(ctx),
25
+ key,
26
+ });
27
+ }
28
+
29
+ #[test_only]
30
+ public fun init_root_for_testing(ctx: &mut TxContext){
31
+ init(ctx)
32
+ }
33
+ }
@@ -0,0 +1,24 @@
1
+ module dubhe::root_system {
2
+ use dubhe::root_schema::Root;
3
+
4
+ public entry fun set_key(
5
+ root: &mut Root,
6
+ key: address,
7
+ ctx: &mut TxContext
8
+ ) {
9
+ ensure_root(root, ctx);
10
+ root.borrow_mut_key().set(key);
11
+ }
12
+
13
+ public entry fun remove_key(
14
+ root: &mut Root,
15
+ ctx: &mut TxContext
16
+ ) {
17
+ ensure_root(root, ctx);
18
+ root.borrow_mut_key().remove();
19
+ }
20
+
21
+ public fun ensure_root(root: &Root, ctx: &TxContext) {
22
+ assert!(root.borrow_key().get() == ctx.sender(), 0);
23
+ }
24
+ }
@@ -0,0 +1,39 @@
1
+ #[test_only]
2
+ module dubhe::root_tests {
3
+ use dubhe::root_schema::Root;
4
+ use dubhe::root_schema;
5
+ use dubhe::root_system;
6
+ use sui::test_scenario;
7
+
8
+ public struct USDT has drop {}
9
+
10
+ #[test]
11
+ public fun root_key() {
12
+ let mut scenario = test_scenario::begin(@0xA);
13
+ {
14
+ let ctx = test_scenario::ctx(&mut scenario);
15
+ root_schema::init_root_for_testing(ctx);
16
+ test_scenario::next_tx(&mut scenario,@0xA);
17
+ };
18
+
19
+ let mut root = test_scenario::take_shared<Root>(&scenario);
20
+ assert!(root.borrow_key().get() == @0xA, 0);
21
+
22
+ {
23
+ let ctx = test_scenario::ctx(&mut scenario);
24
+ root_system::set_key(&mut root, @0xB, ctx);
25
+ test_scenario::next_tx(&mut scenario,@0xB);
26
+ };
27
+ assert!(root.borrow_key().get() == @0xB, 0);
28
+
29
+ {
30
+ let ctx = test_scenario::ctx(&mut scenario);
31
+ root_system::remove_key(&mut root, ctx);
32
+ test_scenario::next_tx(&mut scenario,@0xC);
33
+ };
34
+ assert!(root.borrow_key().try_get() == option::none<address>(), 0);
35
+
36
+ test_scenario::return_shared<Root>(root);
37
+ scenario.end();
38
+ }
39
+ }
@@ -0,0 +1,29 @@
1
+ module dubhe::type_info {
2
+ use std::ascii::String;
3
+ use std::ascii::string;
4
+ use sui::address;
5
+ use std::type_name::TypeName;
6
+
7
+ public fun parse_type_name(type_name: TypeName): (address, String, String, String) {
8
+ let pending_parse_str = type_name.into_string();
9
+ let delimiter = string(b"::");
10
+
11
+ // TBD: this can probably be hard-coded as all hex addrs are 64 bytes
12
+ let package_delimiter_index = pending_parse_str.index_of(&delimiter);
13
+ let package_addr = pending_parse_str.substring(0, package_delimiter_index);
14
+ let package_id = address::from_ascii_bytes(package_addr.as_bytes());
15
+
16
+ let tail = pending_parse_str.substring(package_delimiter_index + 2, pending_parse_str.length());
17
+
18
+ let module_delimiter_index = tail.index_of(&delimiter);
19
+ let module_name = tail.substring(0, module_delimiter_index);
20
+
21
+ let struct_name_with_type = tail.substring(module_delimiter_index + 2, tail.length());
22
+
23
+ let delimiter = string(b"<");
24
+ let struct_name_delimiter_index = struct_name_with_type.index_of(&delimiter);
25
+ let struct_name = struct_name_with_type.substring(0, struct_name_delimiter_index);
26
+
27
+ (package_id, module_name, struct_name, struct_name_with_type)
28
+ }
29
+ }
@@ -0,0 +1,153 @@
1
+ #[allow(unused_variable)]
2
+ module dubhe::storage_double_map {
3
+
4
+ // This key does not exist in the map
5
+ const EKeyDoesNotExist: u64 = 0;
6
+
7
+ /// This key already exists in the map
8
+ const EKeyAlreadyExists: u64 = 1;
9
+
10
+ // An entry in the map
11
+ public struct Entry<K1: copy + drop + store, K2: copy + drop + store, V: store> has copy, drop, store {
12
+ key1: K1,
13
+ key2: K2,
14
+ value: V,
15
+ }
16
+
17
+ // A map data structure backed by a vector. The map is guaranteed not to contain duplicate keys, but entries
18
+ public struct StorageDoubleMap<K1: copy + drop + store, K2: copy + drop + store, V: store> has store {
19
+ contents: vector<Entry<K1, K2, V>>,
20
+ }
21
+
22
+ // Create an empty `StorageDoubleMap`
23
+ public fun new<K1: copy + drop + store, K2: copy + drop + store, V: store>(): StorageDoubleMap<K1, K2,V> {
24
+ StorageDoubleMap { contents: vector[] }
25
+ }
26
+
27
+ // Return true if `self` contains_key an entry for `key`, false otherwise
28
+ public fun contains_key<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &StorageDoubleMap<K1, K2, V>, key1: K1, key2: K2): bool {
29
+ get_idx_opt(self, key1, key2).is_some()
30
+ }
31
+
32
+ // Return the number of entries in `self`
33
+ public fun length<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &StorageDoubleMap<K1, K2, V>): u64 {
34
+ self.contents.length()
35
+ }
36
+
37
+ // Get a reference to the value bound to `key` in `self`.
38
+ // Aborts if `key` is not bound in `self`.
39
+ public fun borrow<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &StorageDoubleMap<K1, K2, V>, key1: K1, key2: K2): &V {
40
+ let idx = self.get_idx(key1, key2);
41
+ let entry = &self.contents[idx];
42
+ &entry.value
43
+ }
44
+
45
+ // Get a mutable reference to the value bound to `key` in `self`.
46
+ // Aborts if `key` is not bound in `self`.
47
+ public fun borrow_mut<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &mut StorageDoubleMap<K1, K2, V>, key1: K1, key2: K2): &mut V {
48
+ let idx = self.get_idx(key1, key2);
49
+ let entry = &mut self.contents[idx];
50
+ &mut entry.value
51
+ }
52
+
53
+ // Remove the entry `key` |-> `value` from self. Aborts if `key` is not bound in `self`.
54
+ public fun take<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &mut StorageDoubleMap<K1, K2,V>, key1: K1, key2: K2): V {
55
+ let idx = self.get_idx(key1, key2);
56
+ let Entry { key1, key2, value } = self.contents.remove(idx);
57
+ value
58
+ }
59
+
60
+ public macro fun mutate<$K1: copy + drop, $K2: copy + drop, $V: copy + drop>(
61
+ $self: &mut StorageDoubleMap<$K1, $K2, $V>,
62
+ $key1: $K1,
63
+ $key2: $K2,
64
+ $f: |&mut $V|
65
+ ) {
66
+ let self = $self;
67
+ let key1 = $key1;
68
+ let key2 = $key2;
69
+ $f(borrow_mut(self, key1, key2));
70
+ }
71
+
72
+ // Returns a list of keys in the map.
73
+ // Do not assume any particular ordering.
74
+ public fun keys<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &StorageDoubleMap<K1, K2, V>): (vector<K1>, vector<K2>) {
75
+ let mut i = 0;
76
+ let n = self.contents.length();
77
+ let mut keys1 = vector[];
78
+ let mut keys2 = vector[];
79
+ while (i < n) {
80
+ let entry = self.contents.borrow(i);
81
+ keys1.push_back(entry.key1);
82
+ keys2.push_back(entry.key2);
83
+ i = i + 1;
84
+ };
85
+ (keys1, keys2)
86
+ }
87
+
88
+ // Find the index of `key` in `self`. Return `None` if `key` is not in `self`.
89
+ // Note that map entries are stored in insertion order, *not* sorted by key.
90
+ public fun get_idx_opt<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &StorageDoubleMap<K1, K2, V>, key1: K1, key2: K2): Option<u64> {
91
+ self.contents.find_index!(|entry| { entry.key1 == key1 && entry.key2 == key2 })
92
+ }
93
+
94
+ // Find the index of `key` in `self`. Aborts if `key` is not in `self`.
95
+ // Note that map entries are stored in insertion order, *not* sorted by key.
96
+ public fun get_idx<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &StorageDoubleMap<K1, K2, V>, key1: K1, key2: K2): u64 {
97
+ let idx_opt = self.get_idx_opt(key1, key2);
98
+ assert!(idx_opt.is_some(), EKeyDoesNotExist);
99
+ idx_opt.destroy_some()
100
+ }
101
+
102
+ /// Insert the entry `key` |-> `value` into `self`.
103
+ /// Aborts if `key` is already bound in `self`.
104
+ public fun insert<K1: copy + drop + store, K2: copy + drop + store, V: store>(self: &mut StorageDoubleMap<K1, K2, V>, key1: K1, key2: K2, value: V) {
105
+ assert!(!self.contains_key(key1, key2), EKeyAlreadyExists);
106
+ self.contents.push_back(Entry { key1, key2, value })
107
+ }
108
+
109
+ // =======================================Value: drop + copy + store=======================================
110
+
111
+ // Insert the entry `key` |-> `value` into `self`.
112
+ public fun set<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(self: &mut StorageDoubleMap<K1, K2,V>, key1: K1, key2: K2, value: V) {
113
+ let idx = self.get_idx_opt(key1, key2);
114
+ if (idx.is_some()) {
115
+ self.contents[idx.destroy_some()].value = value;
116
+ } else {
117
+ self.contents.push_back(Entry { key1, key2, value })
118
+ }
119
+ }
120
+
121
+ // Get a reference to the value bound to `key` in `self`.
122
+ // Aborts if `key` is not bound in `self`.
123
+ public fun get<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(self: &StorageDoubleMap<K1, K2, V>, key1: K1, key2: K2): V {
124
+ let idx = self.get_idx(key1, key2);
125
+ let entry = &self.contents[idx];
126
+ entry.value
127
+ }
128
+
129
+ // Safely try borrow a value bound to `key` in `self`.
130
+ // Return Some(V) if the value exists, None otherwise.
131
+ // Only works for a "copyable" value as references cannot be stored in `vector`.
132
+ public fun try_get<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(self: &StorageDoubleMap<K1, K2, V>, key1: K1, key2: K2): Option<V> {
133
+ if (self.contains_key(key1, key2)) {
134
+ option::some(get(self, key1, key2))
135
+ } else {
136
+ option::none()
137
+ }
138
+ }
139
+
140
+ // Remove the entry `key` |-> `value` from self.
141
+ public fun remove<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(self: &mut StorageDoubleMap<K1, K2,V>, key1: K1, key2: K2) {
142
+ let idx = self.get_idx_opt(key1, key2);
143
+ if (idx.is_some()) {
144
+ self.contents.remove(idx.destroy_some());
145
+ }
146
+ }
147
+
148
+ // Returns a list of values in the map.
149
+ // Do not assume any particular ordering.
150
+ public fun values<K1: copy + drop + store, K2: copy + drop + store, V: copy + drop + store>(self: &StorageDoubleMap<K1, K2, V>): vector<V> {
151
+ self.contents.map!(|entry| entry.value)
152
+ }
153
+ }
@@ -0,0 +1,156 @@
1
+ #[allow(unused_variable)]
2
+ module dubhe::storage_map {
3
+
4
+ /// This key already exists in the map
5
+ const EKeyAlreadyExists: u64 = 0;
6
+
7
+ /// This key does not exist in the map
8
+ const EKeyDoesNotExist: u64 = 1;
9
+
10
+ /// An entry in the map
11
+ public struct Entry<K: copy + drop + store, V: store> has copy, drop, store {
12
+ key: K,
13
+ value: V,
14
+ }
15
+
16
+ /// A map data structure backed by a vector. The map is guaranteed not to contain duplicate keys, but entries
17
+ /// are *not* sorted by key--entries are included in insertion order.
18
+ /// All operations are O(N) in the size of the map--the intention of this data structure is only to provide
19
+ /// the convenience of programming against a map API.
20
+ /// Large maps should use handwritten parent/child relationships instead.
21
+ /// Maps that need sorted iteration rather than insertion order iteration should also be handwritten.
22
+ public struct StorageMap<K: copy + drop + store, V: store> has store {
23
+ contents: vector<Entry<K, V>>,
24
+ }
25
+
26
+ /// Create an empty `StorageMap`
27
+ public fun new<K: copy + drop + store, V: store>(): StorageMap<K, V> {
28
+ StorageMap { contents: vector[] }
29
+ }
30
+
31
+ /// Return true if `self` contains_key an entry for `key`, false otherwise
32
+ public fun contains_key<K: copy + drop + store, V: store>(self: &StorageMap<K, V>, key: K): bool {
33
+ get_idx_opt(self, key).is_some()
34
+ }
35
+
36
+ /// Return the number of entries in `self`
37
+ public fun length<K: copy + drop + store, V: store>(self: &StorageMap<K,V>): u64 {
38
+ self.contents.length()
39
+ }
40
+
41
+ /// Get a reference to the value bound to `key` in `self`.
42
+ /// Aborts if `key` is not bound in `self`.
43
+ public fun borrow<K: copy + drop + store, V: store>(self: &StorageMap<K,V>, key: K): &V {
44
+ let idx = self.get_idx(key);
45
+ let entry = &self.contents[idx];
46
+ &entry.value
47
+ }
48
+
49
+
50
+ /// Get a mutable reference to the value bound to `key` in `self`.
51
+ /// Aborts if `key` is not bound in `self`.
52
+ public fun borrow_mut<K: copy + drop + store, V: store>(self: &mut StorageMap<K,V>, key: K): &mut V {
53
+ let idx = self.get_idx(key);
54
+ let entry = &mut self.contents[idx];
55
+ &mut entry.value
56
+ }
57
+
58
+ /// Remove the entry `key` |-> `value` from self. Aborts if `key` is not bound in `self`.
59
+ public fun take<K: copy + drop + store, V: store>(self: &mut StorageMap<K,V>, key: K): V {
60
+ let idx = self.get_idx(key);
61
+ let Entry { key, value } = self.contents.remove(idx);
62
+ value
63
+ }
64
+
65
+ public macro fun mutate<$K: copy + drop + store, $V: store>(
66
+ $self: &mut StorageMap<$K, $V>,
67
+ $key: $K,
68
+ $f: |&mut $V|
69
+ ) {
70
+ let self = $self;
71
+ let key = $key;
72
+ $f(borrow_mut(self, key));
73
+ }
74
+
75
+ /// Returns a list of keys in the map.
76
+ /// Do not assume any particular ordering.
77
+ public fun keys<K: copy + drop + store, V: store>(self: &StorageMap<K, V>): vector<K> {
78
+ let mut i = 0;
79
+ let n = self.contents.length();
80
+ let mut keys = vector[];
81
+ while (i < n) {
82
+ let entry = self.contents.borrow(i);
83
+ keys.push_back(entry.key);
84
+ i = i + 1;
85
+ };
86
+ keys
87
+ }
88
+
89
+ /// Find the index of `key` in `self`. Return `None` if `key` is not in `self`.
90
+ /// Note that map entries are stored in insertion order, *not* sorted by key.
91
+ public fun get_idx_opt<K: copy + drop + store, V: store>(self: &StorageMap<K,V>, key: K): Option<u64> {
92
+ self.contents.find_index!(|entry| { entry.key == key})
93
+ }
94
+
95
+ /// Find the index of `key` in `self`. Aborts if `key` is not in `self`.
96
+ /// Note that map entries are stored in insertion order, *not* sorted by key.
97
+ public fun get_idx<K: copy + drop + store, V: store>(self: &StorageMap<K,V>, key: K): u64 {
98
+ let idx_opt = self.get_idx_opt(key);
99
+ assert!(idx_opt.is_some(), EKeyDoesNotExist);
100
+ idx_opt.destroy_some()
101
+ }
102
+
103
+ /// Insert the entry `key` |-> `value` into `self`.
104
+ /// Aborts if `key` is already bound in `self`.
105
+ public fun insert<K: copy + drop + store, V: store>(self: &mut StorageMap<K,V>, key: K, value: V) {
106
+ assert!(!self.contains_key(key), EKeyAlreadyExists);
107
+ self.contents.push_back(Entry { key, value })
108
+ }
109
+
110
+ // =======================================Value: drop + copy + store=======================================
111
+
112
+ /// Adds a key-value pair to the self `self: &mut StorageMap<K, V>`
113
+ /// Aborts with `sui::dynamic_field::EFieldAlreadyExists` if the self already has an entry with
114
+ /// that key `k: K`.
115
+ public fun set<K: copy + drop + store, V: copy + drop + store>(self: &mut StorageMap<K, V>, key: K, value: V) {
116
+ let idx = self.get_idx_opt(key);
117
+ if (idx.is_some()) {
118
+ self.contents[idx.destroy_some()].value = value;
119
+ } else {
120
+ self.contents.push_back(Entry { key, value })
121
+ }
122
+ }
123
+
124
+ /// Get a reference to the value bound to `key` in `self`.
125
+ /// Aborts if `key` is not bound in `self`.
126
+ public fun get<K: copy + drop + store, V: copy + drop + store>(self: &StorageMap<K,V>, key: K): V {
127
+ let idx = self.get_idx(key);
128
+ let entry = &self.contents[idx];
129
+ entry.value
130
+ }
131
+
132
+ /// Safely try borrow a value bound to `key` in `self`.
133
+ /// Return Some(V) if the value exists, None otherwise.
134
+ /// Only works for a "copyable" value as references cannot be stored in `vector`.
135
+ public fun try_get<K: copy + drop + store, V: copy + drop + store>(self: &StorageMap<K,V>, key: K): Option<V> {
136
+ if (self.contains_key(key)) {
137
+ option::some(get(self, key))
138
+ } else {
139
+ option::none()
140
+ }
141
+ }
142
+
143
+ /// Remove the entry `key` |-> `value` from self. Aborts if `key` is not bound in `self`.
144
+ public fun remove<K: copy + drop + store, V: copy + drop + store>(self: &mut StorageMap<K,V>, key: K) {
145
+ let idx = self.get_idx_opt(key);
146
+ if (idx.is_some()) {
147
+ self.contents.remove(idx.destroy_some());
148
+ }
149
+ }
150
+
151
+ // Returns a list of values in the map.
152
+ // Do not assume any particular ordering.
153
+ public fun values<K: copy + drop + store, V: copy + drop + store>(self: &StorageMap<K, V>): vector<V> {
154
+ self.contents.map!(|entry| entry.value)
155
+ }
156
+ }
@@ -0,0 +1,19 @@
1
+ module dubhe::storage_migrate {
2
+ use sui::dynamic_field as df;
3
+
4
+ public fun add_field<StorageType: store>(uid: &mut UID, field_name: vector<u8>, storagr_type: StorageType) {
5
+ df::add(uid, field_name, storagr_type);
6
+ }
7
+
8
+ public fun borrow_field<StorageType: store>(uid: &UID, field_name: vector<u8>): &StorageType {
9
+ df::borrow(uid, field_name)
10
+ }
11
+
12
+ public fun borrow_mut_field<StorageType: store>(uid: &mut UID, field_name: vector<u8>): &mut StorageType {
13
+ df::borrow_mut(uid, field_name)
14
+ }
15
+
16
+ public fun field_exists(uid: &UID, field_name: vector<u8>): bool {
17
+ df::exists_(uid, field_name)
18
+ }
19
+ }
@@ -0,0 +1,97 @@
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
+ }
41
+
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
45
+ }
46
+
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
+ }
53
+
54
+ /// Set 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 });
58
+ }
59
+
60
+
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
+ }
70
+ }
71
+
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
76
+ }
77
+
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
+ }
88
+
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
+ }
95
+
96
+ // ============================================================================================
97
+ }