Skip to content

Commit

Permalink
V2 (#14)
Browse files Browse the repository at this point in the history
* update bcs support

* Update from_bytes impl from keccak256

* update key type

* update

* update

* Update obelisk.config

* Update examples

* update sigle compontent data gen

* Some nits.

* Some nits.

* Some nits.

* Some nits.

* Add some tests

* Update obelisk.config

* Support world upgrade

* Update comp id

* Add comp names

* Update add_comp

* Update name

* update cli and gen

* Update name

* update world upgrade

* update world upgrade

* update version

* update

* update version

* update

* update version

* update

* update

* update version

* update ci config

* update ci config

* update README

---------

Co-authored-by: web3olalala <[email protected]>
Co-authored-by: henryliu <[email protected]>
  • Loading branch information
3 people authored Sep 25, 2023
1 parent 9c45bd4 commit d53de9c
Show file tree
Hide file tree
Showing 47 changed files with 2,253 additions and 3,127 deletions.
19 changes: 0 additions & 19 deletions .github/workflows/qodana_code_quality.yml

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ Obelisk-Engine is still in its early stages of development, yet the dedicated co
- Typed SDKs

## 🚀 Quick Start
See the [start guide](https://obelisk.build/pack/docs/quick-start) in the document website.
See the [start guide](https://obelisk.build/engine/docs/quick-start) in the document website.


## 🗒️ Documentation

You can find more detailed documentation in [here](https://obelisk.build/pack/docs).
You can find more detailed documentation in [here](https://obelisk.build/engine/docs).

## ❓ Support

Expand Down
2 changes: 1 addition & 1 deletion examples/Move.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[move]
version = 0
manifest_digest = "29EBC8EEB6008703DCFCFC4C49D84765A6C3F555229A11B3917FD950A89CBD15"
manifest_digest = "CDB2CA0F065ECB73601AA770888CB620BD26EEF2D4C702F01BE21AF87435406F"
deps_digest = "112928C94A84031C09CD9B9D1D44B149B73FC0EEA5FA8D8B2D7CA4D91936335A"

dependencies = [
Expand Down
56 changes: 0 additions & 56 deletions examples/sources/codegen/components/counter.move

This file was deleted.

115 changes: 115 additions & 0 deletions examples/sources/codegen/components/multi_column.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
module examples::multi_column_comp {
use std::ascii::{String, string};
use std::option::some;
use std::vector;
use sui::bcs;
use sui::tx_context::TxContext;
use sui::table::{Self, Table};
use examples::entity_key;
use examples::world::{Self, World};

// Systems
friend examples::example_system;

const NAME: vector<u8> = b"multi_column";

public fun id(): address {
entity_key::from_bytes(NAME)
}

// state
// last_update_time
public fun field_types(): vector<String> {
vector[string(b"u64")]
}

struct Field has drop, store {
data: vector<u8>
}

public fun register(world: &mut World, ctx: &mut TxContext) {
world::add_comp<Table<address,Field>>(
world,
NAME,
table::new<address,Field>(ctx)
);
}

public(friend) fun add(world: &mut World, key: address, state: vector<u8>, last_update_time: u64) {
let component = world::get_mut_comp<Table<address,Field>>(world, id());
let data = encode(state, last_update_time);
table::add(component, key, Field { data });
world::emit_add_event(id(), key, data)
}

public(friend) fun remove(world: &mut World, key: address) {
let component = world::get_mut_comp<Table<address,Field>>(world, id());
table::remove(component, key);
world::emit_remove_event(id(), key)
}

public(friend) fun update(world: &mut World, key: address, state: vector<u8>, last_update_time: u64) {
let component = world::get_mut_comp<Table<address, Field>>(world, id());
let field = table::borrow_mut<address, Field>(component, key);
let data = encode(state, last_update_time);
field.data = data;
world::emit_update_event(id(), some(key), data)
}
public(friend) fun update_state(world: &mut World, key: address, state: vector<u8>) {
let component = world::get_mut_comp<Table<address,Field>>(world, id());
let field = table::borrow_mut<address, Field>(component, key);
let (_, last_update_time) = decode(field.data);
let data = encode(state, last_update_time);
field.data = data;
world::emit_update_event(id(), some(key), data)
}

public(friend) fun update_last_update_time(world: &mut World, key: address, last_update_time: u64) {
let component = world::get_mut_comp<Table<address,Field>>(world, id());
let field = table::borrow_mut<address, Field>(component, key);
let (state, _) = decode(field.data);
let data = encode(state, last_update_time);
field.data = data;
world::emit_update_event(id(), some(key), data)
}

public fun get(world: &World, key: address): (vector<u8>,u64) {
let component = world::get_comp<Table<address,Field>>(world, id());
let field = table::borrow<address, Field>(component, key);
decode(field.data)
}

public fun get_state(world: &World, key: address): vector<u8> {
let component = world::get_comp<Table<address,Field>>(world, id());
let field = table::borrow<address, Field>(component, key);
let (state, _) = decode(field.data);
state
}

public fun get_last_update_time(world: &World, key: address): u64 {
let component = world::get_comp<Table<address,Field>>(world, id());
let field = table::borrow<address, Field>(component, key);
let (_, last_update_time) = decode(field.data);
last_update_time
}

public fun contains(world: &World, key: address): bool {
let component = world::get_comp<Table<address,Field>>(world, id());
table::contains<address, Field>(component, key)
}

public fun encode(state: vector<u8>, last_update_time: u64): vector<u8> {
let data = vector::empty<u8>();
vector::append(&mut data, bcs::to_bytes(&state));
vector::append(&mut data, bcs::to_bytes(&last_update_time));
data
}

public fun decode(bytes: vector<u8>): (vector<u8>,u64) {
let data = bcs::new(bytes);
(
bcs::peel_vec_u8(&mut data),
bcs::peel_u64(&mut data)
)
}
}
81 changes: 81 additions & 0 deletions examples/sources/codegen/components/single_column.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
module examples::single_column_comp {
use std::ascii::{String, string};
use std::option::some;
use std::vector;
use sui::bcs;
use sui::tx_context::TxContext;
use sui::table::{Self, Table};
use examples::entity_key;
use examples::world::{Self, World};

// Systems
friend examples::example_system;

const NAME: vector<u8> = b"single_column";

public fun id(): address {
entity_key::from_bytes(NAME)
}

// value
public fun field_types(): vector<String> {
vector[string(b"u64")]
}

struct Field has drop, store {
data: vector<u8>
}

public fun register(world: &mut World, ctx: &mut TxContext) {
world::add_comp<Table<address,Field>>(
world,
NAME,
table::new<address,Field>(ctx)
);
}

public(friend) fun add(world: &mut World, key: address, value: u64) {
let component = world::get_mut_comp<Table<address,Field>>(world, id());
let data = encode(value);
table::add(component, key, Field { data });
world::emit_add_event(id(), key, data)
}

public(friend) fun remove(world: &mut World, key: address) {
let component = world::get_mut_comp<Table<address,Field>>(world, id());
table::remove(component, key);
world::emit_remove_event(id(), key)
}

public(friend) fun update(world: &mut World, key: address, value: u64) {
let component = world::get_mut_comp<Table<address, Field>>(world, id());
let field = table::borrow_mut<address, Field>(component, key);
let data = encode(value);
field.data = data;
world::emit_update_event(id(), some(key), data)
}

public fun get(world: &World, key: address): u64 {
let component = world::get_comp<Table<address,Field>>(world, id());
let field = table::borrow<address, Field>(component, key);
decode(field.data)
}

public fun contains(world: &World, key: address): bool {
let component = world::get_comp<Table<address,Field>>(world, id());
table::contains<address, Field>(component, key)
}

public fun encode(value: u64): vector<u8> {
let data = vector::empty<u8>();
vector::append(&mut data, bcs::to_bytes(&value));
data
}

public fun decode(bytes: vector<u8>): u64 {
let data = bcs::new(bytes);
(
bcs::peel_u64(&mut data)
)
}
}
86 changes: 86 additions & 0 deletions examples/sources/codegen/components/single_struct.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
module examples::single_struct_comp {
use std::ascii::{String, string};
use std::option::none;
use std::vector;
use sui::bcs;
use examples::entity_key;
use examples::world::{Self, World};

// Systems
friend examples::example_system;

const NAME: vector<u8> = b"single_struct";

public fun id(): address {
entity_key::from_bytes(NAME)
}

// admin
// fee
public fun field_types(): vector<String> {
vector[string(b"u64")]
}

struct Field has drop, store {
data: vector<u8>
}

public fun register(world: &mut World) {
world::add_comp<Field>(
world,
NAME,
Field { data: encode(@0x1, 100) }
);
}

public(friend) fun update(world: &mut World, admin: address, fee: u64) {
let data = encode(admin, fee);
world::get_mut_comp<Field>(world, id()).data = data;
world::emit_update_event(id(), none(), data)
}
public(friend) fun update_admin(world: &mut World, admin: address) {
let field = world::get_mut_comp<Field>(world, id());
let (_, fee) = decode(field.data);
field.data = encode(admin, fee);
world::emit_update_event(id(), none(), field.data)
}

public(friend) fun update_fee(world: &mut World, fee: u64) {
let field = world::get_mut_comp<Field>(world, id());
let (admin, _) = decode(field.data);
field.data = encode(admin, fee);
world::emit_update_event(id(), none(), field.data)
}

public fun get(world: &World): (address,u64) {
let data = world::get_comp<Field>(world, id()).data;
decode(data)
}

public fun get_admin(world: &World): address {
let data = world::get_comp<Field>(world, id()).data;
let (admin, _) = decode(data);
admin
}

public fun get_fee(world: &World): u64 {
let data = world::get_comp<Field>(world, id()).data;
let (_, fee) = decode(data);
fee
}

public fun encode(admin: address, fee: u64): vector<u8> {
let data = vector::empty<u8>();
vector::append(&mut data, bcs::to_bytes(&admin));
vector::append(&mut data, bcs::to_bytes(&fee));
data
}

public fun decode(bytes: vector<u8>): (address,u64) {
let data = bcs::new(bytes);
(
bcs::peel_address(&mut data),
bcs::peel_u64(&mut data)
)
}
}
Loading

0 comments on commit d53de9c

Please sign in to comment.