Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Nov 13, 2024
1 parent ea82353 commit bbc9507
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 220 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 11 additions & 72 deletions crates/bindings/src/js_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@ use alloy::{
sol_types::SolCall,
};
use serde::{Deserialize, Serialize};
use serde_wasm_bindgen::{from_value, to_value};
use std::str::FromStr;
use tsify::Tsify;
use wasm_bindgen::{
convert::*,
describe::{inform, WasmDescribe, WasmDescribeVector, VECTOR},
prelude::*,
JsValue, UnwrapThrowExt,
};
use wasm_bindgen::prelude::*;

// a serializer fn for serializing Vec<u8> as Uint8Array for js
fn bytes_serilializer<S: serde::Serializer>(val: &[u8], serializer: S) -> Result<S::Ok, S::Error> {
Expand All @@ -29,7 +23,6 @@ fn bytes_serilializer<S: serde::Serializer>(val: &[u8], serializer: S) -> Result

#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, Default, Tsify)]
#[serde(rename_all = "camelCase")]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct EvaluableV3 {
pub interpreter: String,
pub store: String,
Expand All @@ -40,7 +33,6 @@ pub struct EvaluableV3 {

#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, Default, Tsify)]
#[serde(rename_all = "camelCase")]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct IO {
pub token: String,
pub decimals: u8,
Expand All @@ -49,7 +41,6 @@ pub struct IO {

#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, Default, Tsify)]
#[serde(rename_all = "camelCase")]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct OrderV3 {
pub owner: String,
pub evaluable: EvaluableV3,
Expand All @@ -60,7 +51,6 @@ pub struct OrderV3 {

#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, Default, Tsify)]
#[serde(rename_all = "camelCase")]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct SignedContextV1 {
pub signer: String,
pub context: Vec<String>,
Expand All @@ -71,7 +61,6 @@ pub struct SignedContextV1 {

#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, Default, Tsify)]
#[serde(rename_all = "camelCase")]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct Quote {
pub order: OrderV3,
#[serde(rename = "inputIOIndex")]
Expand All @@ -83,7 +72,6 @@ pub struct Quote {

#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, Default, Tsify)]
#[serde(rename_all = "camelCase")]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct TakeOrderConfigV3 {
order: OrderV3,
#[serde(rename = "inputIOIndex")]
Expand All @@ -95,7 +83,6 @@ pub struct TakeOrderConfigV3 {

#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, Default, Tsify)]
#[serde(rename_all = "camelCase")]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct TakeOrdersConfigV3 {
minimum_input: String,
maximum_input: String,
Expand Down Expand Up @@ -385,65 +372,17 @@ impl From<MainTakeOrdersConfigV3> for TakeOrdersConfigV3 {
}
}

#[macro_export]
macro_rules! impl_wasm_traits {
($struct_name:ident) => {
impl RefFromWasmAbi for $struct_name {
type Abi = <JsValue as RefFromWasmAbi>::Abi;
type Anchor = Box<$struct_name>;
unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor {
Box::new($struct_name::from_abi(js))
}
}
impl LongRefFromWasmAbi for $struct_name {
type Abi = <JsValue as RefFromWasmAbi>::Abi;
type Anchor = Box<$struct_name>;
unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor {
Box::new($struct_name::from_abi(js))
}
}
impl VectorIntoWasmAbi for $struct_name {
type Abi = <Box<[JsValue]> as IntoWasmAbi>::Abi;
fn vector_into_abi(vector: Box<[Self]>) -> Self::Abi {
js_value_vector_into_abi(vector)
}
}
impl VectorFromWasmAbi for $struct_name {
type Abi = <Box<[JsValue]> as IntoWasmAbi>::Abi;
unsafe fn vector_from_abi(js: Self::Abi) -> Box<[Self]> {
js_value_vector_from_abi(js)
}
}
impl WasmDescribeVector for $struct_name {
fn describe_vector() {
inform(VECTOR);
$struct_name::describe();
}
}
impl From<$struct_name> for JsValue {
fn from(value: $struct_name) -> Self {
let mut err = "".to_string();
to_value(&value)
.inspect_err(|e| err.push_str(&e.to_string()))
.expect_throw(&err)
}
}
impl TryFromJsValue for $struct_name {
type Error = serde_wasm_bindgen::Error;
fn try_from_js_value(value: JsValue) -> Result<Self, Self::Error> {
from_value(value)
}
}
};
}
mod impls {
use super::super::{impl_all_wasm_traits, wasm_traits::prelude::*};

impl_wasm_traits!(IO);
impl_wasm_traits!(Quote);
impl_wasm_traits!(OrderV3);
impl_wasm_traits!(EvaluableV3);
impl_wasm_traits!(SignedContextV1);
impl_wasm_traits!(TakeOrderConfigV3);
impl_wasm_traits!(TakeOrdersConfigV3);
impl_all_wasm_traits!(super::IO);
impl_all_wasm_traits!(super::Quote);
impl_all_wasm_traits!(super::OrderV3);
impl_all_wasm_traits!(super::EvaluableV3);
impl_all_wasm_traits!(super::SignedContextV1);
impl_all_wasm_traits!(super::TakeOrderConfigV3);
impl_all_wasm_traits!(super::TakeOrdersConfigV3);
}

#[cfg(test)]
mod tests {
Expand Down
3 changes: 3 additions & 0 deletions crates/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ sol!(

#[cfg(target_family = "wasm")]
pub mod js_api;

#[cfg(target_family = "wasm")]
pub mod wasm_traits;
204 changes: 204 additions & 0 deletions crates/bindings/src/wasm_traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
pub mod prelude {
pub use serde_wasm_bindgen::{from_value, to_value};
pub use tsify::Tsify;
pub use wasm_bindgen::{
convert::*,
describe::{inform, WasmDescribe, WasmDescribeVector, VECTOR},
prelude::*,
JsValue, UnwrapThrowExt,
};
}

pub use prelude::*;

#[macro_export]
macro_rules! impl_main_wasm_traits {
($struct_name:path) => {
impl WasmDescribe for $struct_name {
#[inline]
fn describe() {
<Self as Tsify>::JsType::describe()
}
}
impl IntoWasmAbi for $struct_name {
type Abi = <<Self as Tsify>::JsType as IntoWasmAbi>::Abi;

#[inline]
fn into_abi(self) -> Self::Abi {
let mut err = "".to_string();
self.into_js()
.inspect_err(|e| err.push_str(&e.to_string()))
.expect_throw(&err)
.into_abi()
}
}
impl OptionIntoWasmAbi for $struct_name {
#[inline]
fn none() -> Self::Abi {
<<Self as Tsify>::JsType as OptionIntoWasmAbi>::none()
}
}
impl FromWasmAbi for $struct_name {
type Abi = <<Self as Tsify>::JsType as FromWasmAbi>::Abi;

#[inline]
unsafe fn from_abi(js: Self::Abi) -> Self {
let mut err = "".to_string();
Self::from_js(<Self as Tsify>::JsType::from_abi(js))
.inspect_err(|e| err.push_str(&e.to_string()))
.expect_throw(&err)
}
}
impl OptionFromWasmAbi for $struct_name {
#[inline]
fn is_none(js: &Self::Abi) -> bool {
<<Self as Tsify>::JsType as OptionFromWasmAbi>::is_none(js)
}
}
};
}

#[macro_export]
macro_rules! impl_complementary_wasm_traits {
($struct_name:path) => {
impl RefFromWasmAbi for $struct_name {
type Abi = <JsValue as RefFromWasmAbi>::Abi;
type Anchor = Box<$struct_name>;
unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor {
Box::new(<$struct_name>::from_abi(js))
}
}
impl LongRefFromWasmAbi for $struct_name {
type Abi = <JsValue as RefFromWasmAbi>::Abi;
type Anchor = Box<$struct_name>;
unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor {
Box::new(<$struct_name>::from_abi(js))
}
}
impl VectorIntoWasmAbi for $struct_name {
type Abi = <Box<[JsValue]> as IntoWasmAbi>::Abi;
fn vector_into_abi(vector: Box<[Self]>) -> Self::Abi {
js_value_vector_into_abi(vector)
}
}
impl VectorFromWasmAbi for $struct_name {
type Abi = <Box<[JsValue]> as IntoWasmAbi>::Abi;
unsafe fn vector_from_abi(js: Self::Abi) -> Box<[Self]> {
js_value_vector_from_abi(js)
}
}
impl WasmDescribeVector for $struct_name {
fn describe_vector() {
inform(VECTOR);
<$struct_name>::describe();
}
}
impl From<$struct_name> for JsValue {
fn from(value: $struct_name) -> Self {
let mut err = "".to_string();
to_value(&value)
.inspect_err(|e| err.push_str(&e.to_string()))
.expect_throw(&err)
}
}
impl TryFromJsValue for $struct_name {
type Error = serde_wasm_bindgen::Error;
fn try_from_js_value(value: JsValue) -> Result<Self, Self::Error> {
from_value(value)
}
}
};
}

#[macro_export]
macro_rules! impl_all_wasm_traits {
($struct_name:path) => {
impl WasmDescribe for $struct_name {
#[inline]
fn describe() {
<Self as Tsify>::JsType::describe()
}
}
impl IntoWasmAbi for $struct_name {
type Abi = <<Self as Tsify>::JsType as IntoWasmAbi>::Abi;

#[inline]
fn into_abi(self) -> Self::Abi {
let mut err = "".to_string();
self.into_js()
.inspect_err(|e| err.push_str(&e.to_string()))
.expect_throw(&err)
.into_abi()
}
}
impl OptionIntoWasmAbi for $struct_name {
#[inline]
fn none() -> Self::Abi {
<<Self as Tsify>::JsType as OptionIntoWasmAbi>::none()
}
}
impl FromWasmAbi for $struct_name {
type Abi = <<Self as Tsify>::JsType as FromWasmAbi>::Abi;

#[inline]
unsafe fn from_abi(js: Self::Abi) -> Self {
let mut err = "".to_string();
Self::from_js(<Self as Tsify>::JsType::from_abi(js))
.inspect_err(|e| err.push_str(&e.to_string()))
.expect_throw(&err)
}
}
impl OptionFromWasmAbi for $struct_name {
#[inline]
fn is_none(js: &Self::Abi) -> bool {
<<Self as Tsify>::JsType as OptionFromWasmAbi>::is_none(js)
}
}
impl RefFromWasmAbi for $struct_name {
type Abi = <JsValue as RefFromWasmAbi>::Abi;
type Anchor = Box<$struct_name>;
unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor {
Box::new(<$struct_name>::from_abi(js))
}
}
impl LongRefFromWasmAbi for $struct_name {
type Abi = <JsValue as RefFromWasmAbi>::Abi;
type Anchor = Box<$struct_name>;
unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor {
Box::new(<$struct_name>::from_abi(js))
}
}
impl VectorIntoWasmAbi for $struct_name {
type Abi = <Box<[JsValue]> as IntoWasmAbi>::Abi;
fn vector_into_abi(vector: Box<[Self]>) -> Self::Abi {
js_value_vector_into_abi(vector)
}
}
impl VectorFromWasmAbi for $struct_name {
type Abi = <Box<[JsValue]> as IntoWasmAbi>::Abi;
unsafe fn vector_from_abi(js: Self::Abi) -> Box<[Self]> {
js_value_vector_from_abi(js)
}
}
impl WasmDescribeVector for $struct_name {
fn describe_vector() {
inform(VECTOR);
<$struct_name>::describe();
}
}
impl From<$struct_name> for JsValue {
fn from(value: $struct_name) -> Self {
let mut err = "".to_string();
to_value(&value)
.inspect_err(|e| err.push_str(&e.to_string()))
.expect_throw(&err)
}
}
impl TryFromJsValue for $struct_name {
type Error = serde_wasm_bindgen::Error;
fn try_from_js_value(value: JsValue) -> Result<Self, Self::Error> {
from_value(value)
}
}
};
}
Loading

0 comments on commit bbc9507

Please sign in to comment.