Skip to content

Instantly share code, notes, and snippets.

@toonsevrin
Last active May 25, 2018 13:58
Show Gist options
  • Save toonsevrin/bd9732341b0ab585058e152e38231885 to your computer and use it in GitHub Desktop.
Save toonsevrin/bd9732341b0ab585058e152e38231885 to your computer and use it in GitHub Desktop.
//Instead of having users write the entrypoint like this:
apply(code: u64, action: u64): void {
//notice how the action matching has to be done manually
if (action == fromString("create")) {
this.on_create(Create.from_ds(this.get_ds()));//this.get_ds is a wrapper for read_action_data, an environment function that passes the serialized parameters of the contract call (as a bytestream).
}
}
class Create implements ISerializable {
constructor(user : u64, game : u64, num_rows : u32, num_cols : u32, seed : u32) {
this.user = user;
this.game = game;
this.num_rows = num_rows;
this.num_cols = num_cols;
this.seed = seed;
}
//notice how the bytestream parsing has to be done manually
static from_ds(ds : DataStream) : Create {
return new Create(
ds.read<u64>(),
ds.read<u64>(),
ds.read<u32>(),
ds.read<u32>(),
ds.read<u32>()
);
}
//They should simply be able to do something like this, and get the backend to do the .apply and from_ds plumbing:
@Expose("create")
function on_create(user: u64, game: u64, num_rows: u32, num_cols: u32, seed: u32){
//function body
}
//Or if decorators are a problem
Contract.register(on_create);
//Or if reflection is a problem:
Contract.register(on_create, ["u64", "u64", "u32", "u32", "u32"]);
//that final one is probably the most realistic for the current state of typescript, but it has the worsed UX and is more error prone.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment