Skip to content

Commit

Permalink
refactor: revisit the whole crate strategy (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega authored Oct 4, 2024
1 parent e9bbbaf commit 73296fd
Show file tree
Hide file tree
Showing 24 changed files with 2,022 additions and 2,952 deletions.
3,067 changes: 498 additions & 2,569 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ resolver = "2"
members = [
"balius",
"examples/basic",
"examples/sundae-stop-loss-strategy",
"examples/ticket-vending-machine",
# "examples/sundae-stop-loss-strategy",
# "examples/ticket-vending-machine",
"examples/minter/offchain",
"balius-sdk",
"balius-runtime",
"balius-macros",
]
11 changes: 11 additions & 0 deletions balius-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "balius-macros"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
quote = "1.0.37"
syn = { version = "2.0.79", features = ["full"] }
38 changes: 38 additions & 0 deletions balius-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemFn};

#[proc_macro_attribute]
pub fn main(_attr: TokenStream, item: TokenStream) -> TokenStream {
// Parse the function
let input = parse_macro_input!(item as ItemFn);
let func_name = &input.sig.ident;
let func_body = &input.block;

// Generate the new function with timing
let output = quote! {
fn #func_name() -> balius_sdk::Worker {
#func_body
}

struct _Main;

impl balius_sdk::wit::Guest for _Main {
fn init(env: balius_sdk::wit::Env) {
let worker = #func_name();
balius_sdk::_internal::global_init_worker(env, worker);
}

fn handle(
id: u32,
evt: balius_sdk::wit::Event,
) -> std::result::Result<balius_sdk::wit::Response, balius_sdk::wit::HandleError> {
balius_sdk::_internal::global_handle_request(id, evt)
}
}

balius_sdk::wit::export!(_Main);
};

output.into()
}
6 changes: 6 additions & 0 deletions balius-runtime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "balius-runtime"
version = "0.1.0"
edition = "2021"

[dependencies]
14 changes: 14 additions & 0 deletions balius-runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: u64, right: u64) -> u64 {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
18 changes: 18 additions & 0 deletions balius-sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "balius-sdk"
version = "0.1.0"
edition = "2021"

[dependencies]
balius-macros = { version = "0.1.0", path = "../balius-macros" }
hex = "0.4.3"
pallas-addresses = { version = "0.30.2", git = "https://github.com/txpipe/pallas.git" }
pallas-codec = { version = "0.30.2", git = "https://github.com/txpipe/pallas.git" }
pallas-crypto = { version = "0.30.2", git = "https://github.com/txpipe/pallas.git" }
pallas-primitives = { version = "0.30.2", git = "https://github.com/txpipe/pallas.git" }
pallas-traverse = { version = "0.30.2", git = "https://github.com/txpipe/pallas.git" }
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
serde_with = "3.9.0"
thiserror = "1.0.64"
wit-bindgen = "0.33.0"
28 changes: 28 additions & 0 deletions balius-sdk/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use balius_sdk::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct FaucetConfig {
validator: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct FaucetRequest {
token: String,
quantity: u64,
recipient: String,
fuel: String,
}

fn on_request(config: Config<FaucetConfig>, params: Params<FaucetRequest>) -> WorkerResult<NewTx> {
todo!()
}

#[balius_sdk::main]
fn balius_main() -> Worker {
Worker::new().with_request_handler("faucet", FnHandler::from(on_request))
}

fn main() {
panic!("not meant to be run directly");
}
53 changes: 53 additions & 0 deletions balius-sdk/src/_internal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::{
collections::HashMap,
sync::{LazyLock, RwLock},
};

use crate::wit;

type ChannelId = u32;

pub trait Handler: Send + Sync + 'static {
fn handle(
&self,
config: wit::Env,
event: wit::Event,
) -> Result<wit::Response, wit::HandleError>;
}

pub(crate) struct Channel {
pub(crate) handler: Box<dyn Handler>,
pub(crate) pattern: wit::balius::app::driver::EventPattern,
}

type ChannelRegistry = HashMap<ChannelId, Channel>;

#[derive(Default)]
pub struct Worker {
pub(crate) channels: ChannelRegistry,
pub(crate) env: Option<wit::Env>,
}

static WORKER: LazyLock<RwLock<Worker>> = LazyLock::new(|| RwLock::new(Worker::default()));

pub fn global_init_worker(env: wit::Env, mut worker: Worker) {
worker.init(env);

for (id, handler) in worker.channels.iter() {
wit::balius::app::driver::register_channel(*id, &handler.pattern);
}

let mut singelton = WORKER.write().unwrap();
*singelton = worker;
}

pub fn global_handle_request(id: u32, evt: wit::Event) -> Result<wit::Response, wit::HandleError> {
let worker = WORKER.read().unwrap();
let channel = worker.channels.get(&id).ok_or(1u32)?;
let env = match &worker.env {
Some(e) => e.clone(),
None => return Err(0),
};

channel.handler.handle(env, evt)
}
26 changes: 26 additions & 0 deletions balius-sdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pub mod wit {
wit_bindgen::generate!({
pub_export_macro: true,
default_bindings_module: "balius_sdk::wit",
path: "../wit",
additional_derives: [PartialEq, Eq, Hash, Clone, serde::Serialize,serde::Deserialize]
});
}

/// Re-export of the macros crate
pub use balius_macros as macros;

/// Macro to mark the main function for the worker
pub use balius_macros::main;

/// Transaction builder artifacts
pub mod txbuilder;

/// Internal functions to be used by the generated code
pub mod _internal;

/// Quality of life features to make the SDK more ergonomic
mod qol;

pub use _internal::Worker;
pub use qol::*;
Loading

0 comments on commit 73296fd

Please sign in to comment.