-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: revisit the whole crate strategy (#25)
- Loading branch information
Showing
24 changed files
with
2,022 additions
and
2,952 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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"] } |
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,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() | ||
} |
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,6 @@ | ||
[package] | ||
name = "balius-runtime" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
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,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); | ||
} | ||
} |
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,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" |
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,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"); | ||
} |
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,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) | ||
} |
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,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::*; |
Oops, something went wrong.