@0xobelisk/sui-common 0.5.9 → 0.5.11

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.
@@ -1,52 +0,0 @@
1
- import { ObeliskConfig } from "../../types";
2
- import { formatAndWriteMove } from "../formatAndWrite";
3
-
4
- export function generateEntityKey(config: ObeliskConfig, srcPrefix: string) {
5
- let code = `module ${config.name}::entity_key {
6
- use sui::hash::keccak256;
7
- use sui::address;
8
- use sui::bcs;
9
-
10
- public fun from_object<T: key + store>(object: &T): address {
11
- object::id_address(object)
12
- }
13
-
14
- public fun from_bytes(bytes: vector<u8>): address {
15
- address::from_bytes(keccak256(&bytes))
16
- }
17
-
18
- public fun from_u256(x: u256): address {
19
- address::from_u256(x)
20
- }
21
-
22
- public fun from_address_with_seed(addr: address, seed: vector<u8>): address {
23
- let mut data = address::to_bytes(addr);
24
- vector::append(&mut data, seed);
25
- from_bytes(data)
26
- }
27
-
28
- public fun from_address_with_u256(addr: address, x: u256): address {
29
- let mut data = address::to_bytes(addr);
30
- vector::append(&mut data, bcs::to_bytes<u256>(&x));
31
- from_bytes(data)
32
- }
33
-
34
- public fun from_object_with_seed<T: key + store>(object: &T, seed: vector<u8>): address {
35
- let mut data = address::to_bytes(object::id_address(object));
36
- vector::append(&mut data, seed);
37
- from_bytes(data)
38
- }
39
-
40
- public fun from_object_with_u256<T: key + store>(object: &T, x: u256): address {
41
- let mut data = address::to_bytes(object::id_address(object));
42
- vector::append(&mut data, bcs::to_bytes<u256>(&x));
43
- from_bytes(data)
44
- }
45
- }
46
- `;
47
- formatAndWriteMove(
48
- code,
49
- `${srcPrefix}/contracts/${config.name}/sources/entity_key.move`,
50
- "formatAndWriteMove"
51
- );
52
- }
@@ -1,159 +0,0 @@
1
- import { formatAndWriteMove } from "../formatAndWrite";
2
-
3
- export function generateEps(
4
- projectName: string,
5
- srcPrefix: string,
6
- version?: number
7
- ) {
8
- generateWorld(projectName, srcPrefix, version);
9
- generateEvents(projectName, srcPrefix);
10
- }
11
-
12
- function generateWorld(
13
- projectName: string,
14
- srcPrefix: string,
15
- version?: number
16
- ) {
17
- if (version === undefined) {
18
- version = 1;
19
- }
20
-
21
- let code = `module ${projectName}::world {
22
- use std::ascii::{String, string};
23
- use std::vector;
24
- use sui::tx_context::TxContext;
25
- use sui::bag::{Self, Bag};
26
- use sui::object::{Self, UID, ID};
27
-
28
- const VERSION: u64 = ${version};
29
-
30
- /// Schema does not exist
31
- const ESchemaDoesNotExist: u64 = 0;
32
- /// Schema already exists
33
- const ESchemaAlreadyExists: u64 = 1;
34
- /// Not the right admin for this world
35
- const ENotAdmin: u64 = 2;
36
- /// Migration is not an upgrade
37
- const ENotUpgrade: u64 = 3;
38
- /// Calling functions from the wrong package version
39
- const EWrongVersion: u64 = 4;
40
-
41
- struct AdminCap has key, store {
42
- id: UID,
43
- }
44
-
45
- struct World has key, store {
46
- id: UID,
47
- /// Name of the world
48
- name: String,
49
- /// Description of the world
50
- description: String,
51
- /// Schemas of the world
52
- schemas: Bag,
53
- /// Schema names of the world
54
- schema_names: vector<String>,
55
- /// admin of the world
56
- admin: ID,
57
- /// Version of the world
58
- version: u64
59
- }
60
-
61
- public fun create(name: String, description: String, ctx: &mut TxContext): (World, AdminCap) {
62
- let admin_cap = AdminCap {
63
- id: object::new(ctx),
64
- };
65
- let _obelisk_world = World {
66
- id: object::new(ctx),
67
- name,
68
- description,
69
- schemas: bag::new(ctx),
70
- schema_names: vector::empty(),
71
- admin: object::id(&admin_cap),
72
- version: VERSION
73
- };
74
- (_obelisk_world, admin_cap)
75
- }
76
-
77
- public fun get_admin(_obelisk_world: &World): ID {
78
- _obelisk_world.admin
79
- }
80
-
81
- public fun info(_obelisk_world: &World): (String, String, u64) {
82
- (_obelisk_world.name, _obelisk_world.description, _obelisk_world.version)
83
- }
84
-
85
- public fun schema_names(_obelisk_world: &World): vector<String> {
86
- _obelisk_world.schema_names
87
- }
88
-
89
- public fun get_schema<T : store>(_obelisk_world: &World, _obelisk_schema_id: vector<u8>): &T {
90
- assert!(_obelisk_world.version == VERSION, EWrongVersion);
91
- assert!(bag::contains(&_obelisk_world.schemas, _obelisk_schema_id), ESchemaDoesNotExist);
92
- bag::borrow<vector<u8>, T>(&_obelisk_world.schemas, _obelisk_schema_id)
93
- }
94
-
95
- public fun get_mut_schema<T : store>(_obelisk_world: &mut World, _obelisk_schema_id: vector<u8>): &mut T {
96
- assert!(_obelisk_world.version == VERSION, EWrongVersion);
97
- assert!(bag::contains(&_obelisk_world.schemas, _obelisk_schema_id), ESchemaDoesNotExist);
98
- bag::borrow_mut<vector<u8>, T>(&mut _obelisk_world.schemas, _obelisk_schema_id)
99
- }
100
-
101
- public fun add_schema<T : store>(_obelisk_world: &mut World, _obelisk_schema_id: vector<u8>, schema: T, admin_cap: &AdminCap){
102
- assert!(_obelisk_world.admin == object::id(admin_cap), ENotAdmin);
103
- assert!(_obelisk_world.version == VERSION, EWrongVersion);
104
- assert!(!bag::contains(&_obelisk_world.schemas, _obelisk_schema_id), ESchemaAlreadyExists);
105
- vector::push_back(&mut _obelisk_world.schema_names, string(_obelisk_schema_id));
106
- bag::add<vector<u8>,T>(&mut _obelisk_world.schemas, _obelisk_schema_id, schema);
107
- }
108
-
109
- public fun contains(_obelisk_world: &mut World, _obelisk_schema_id: vector<u8>): bool {
110
- assert!(_obelisk_world.version == VERSION, EWrongVersion);
111
- bag::contains(&mut _obelisk_world.schemas, _obelisk_schema_id)
112
- }
113
-
114
- entry fun migrate(_obelisk_world: &mut World, admin_cap: &AdminCap) {
115
- assert!(_obelisk_world.admin == object::id(admin_cap), ENotAdmin);
116
- assert!(_obelisk_world.version < VERSION, ENotUpgrade);
117
- _obelisk_world.version = VERSION;
118
- }
119
- }
120
- `;
121
- formatAndWriteMove(
122
- code,
123
- `${srcPrefix}/contracts/${projectName}/sources/codegen/eps/world.move`,
124
- "formatAndWriteMove"
125
- );
126
- }
127
-
128
- function generateEvents(projectName: string, srcPrefix: string) {
129
- let code = `module ${projectName}::events {
130
- use sui::event;
131
- use std::option::Option;
132
-
133
- struct SchemaSetRecord<T: copy + drop> has copy, drop {
134
- _obelisk_schema_id: vector<u8>,
135
- _obelisk_schema_type: u8, // obelisk_schema_type_enum => { 0: common, 1: singleton, 2: ephemeral }
136
- _obelisk_entity_key: Option<address>,
137
- data: T
138
- }
139
-
140
- struct SchemaRemoveRecord has copy, drop {
141
- _obelisk_schema_id: vector<u8>,
142
- _obelisk_entity_key: address
143
- }
144
-
145
- public fun emit_set<T: copy + drop>(_obelisk_schema_id: vector<u8>, _obelisk_schema_type: u8, _obelisk_entity_key: Option<address>, data: T) {
146
- event::emit(SchemaSetRecord { _obelisk_schema_id, _obelisk_schema_type, _obelisk_entity_key, data})
147
- }
148
-
149
- public fun emit_remove(_obelisk_schema_id: vector<u8>, _obelisk_entity_key: address) {
150
- event::emit(SchemaRemoveRecord { _obelisk_schema_id, _obelisk_entity_key })
151
- }
152
- }
153
- `;
154
- formatAndWriteMove(
155
- code,
156
- `${srcPrefix}/contracts/${projectName}/sources/codegen/eps/events.move`,
157
- "formatAndWriteMove"
158
- );
159
- }
@@ -1,45 +0,0 @@
1
- import { ObeliskConfig } from "../../types";
2
- import { formatAndWriteMove } from "../formatAndWrite";
3
- import {
4
- getRegisterSchema,
5
- getUseSchema,
6
- capitalizeFirstLetter,
7
- } from "./common";
8
-
9
- export function generateInit(config: ObeliskConfig, srcPrefix: string) {
10
- let code = `#[allow(lint(share_owned))]
11
-
12
- module ${config.name}::init {
13
- use std::ascii::string;
14
- use ${config.name}::app_key::AppKey;
15
- use obelisk::access_control;
16
- use obelisk::world;
17
- ${getUseSchema(config.name, config.schemas).join("\n")}
18
-
19
- fun init(ctx: &mut TxContext) {
20
- let (mut _obelisk_world, admin_cap) = world::create(string(b"${capitalizeFirstLetter(
21
- config.name
22
- )}"), string(b"${capitalizeFirstLetter(config.description)}"),ctx);
23
-
24
- // Authorize this application to access protected features of the World.
25
- access_control::authorize_app<AppKey>(&admin_cap, &mut _obelisk_world);
26
-
27
- // Add Schema
28
- ${getRegisterSchema(config.schemas).join("\n")}
29
-
30
- transfer::public_share_object(_obelisk_world);
31
- transfer::public_transfer(admin_cap, tx_context::sender(ctx));
32
- }
33
-
34
- #[test_only]
35
- public fun init_world_for_testing(ctx: &mut TxContext){
36
- init(ctx)
37
- }
38
- }
39
- `;
40
- formatAndWriteMove(
41
- code,
42
- `${srcPrefix}/contracts/${config.name}/sources/codegen/init.move`,
43
- "formatAndWriteMove"
44
- );
45
- }