-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
9c45bd4
commit d53de9c
Showing
47 changed files
with
2,253 additions
and
3,127 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} | ||
} |
Oops, something went wrong.