Skip to content

Commit

Permalink
Merge pull request #994 from rainlanguage/2024-11-13-cleanup-wasm
Browse files Browse the repository at this point in the history
cleanup wasm
  • Loading branch information
hardyjosh authored Nov 22, 2024
2 parents db828aa + a7be041 commit 4a6cf55
Show file tree
Hide file tree
Showing 30 changed files with 313 additions and 474 deletions.
84 changes: 11 additions & 73 deletions crates/bindings/src/js_api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::wasm_traits::prelude::*;
use crate::IOrderBookV4::{
takeOrders2Call, EvaluableV3 as MainEvaluableV3, OrderV3 as MainOrderV3, Quote as MainQuote,
SignedContextV1 as MainSignedContextV1, TakeOrderConfigV3 as MainTakeOrderConfigV3,
Expand All @@ -12,15 +13,7 @@ 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,
};

// 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 +22,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 +32,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 +40,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 +50,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 +60,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 +71,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 +82,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 +371,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 crate::impl_all_wasm_traits;

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;
113 changes: 113 additions & 0 deletions crates/bindings/src/wasm_traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
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,
};
}

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

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

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

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

#[macro_export]
macro_rules! impl_all_wasm_traits {
($type_name:path) => {
$crate::impl_main_wasm_traits!($type_name);
$crate::impl_complementary_wasm_traits!($type_name);
};
}
24 changes: 2 additions & 22 deletions crates/common/src/dotrain_order/calldata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,16 @@ use alloy::{
use rain_orderbook_app_settings::{deployment::Deployment, orderbook::Orderbook};
use std::{collections::HashMap, str::FromStr, sync::Arc};

#[cfg(target_family = "wasm")]
use rain_orderbook_bindings::impl_wasm_traits;
#[cfg(target_family = "wasm")]
use serde_wasm_bindgen::{from_value, to_value};
#[cfg(target_family = "wasm")]
use tsify::Tsify;
#[cfg(target_family = "wasm")]
use wasm_bindgen::convert::{
js_value_vector_from_abi, js_value_vector_into_abi, FromWasmAbi, IntoWasmAbi,
LongRefFromWasmAbi, RefFromWasmAbi, TryFromJsValue, VectorFromWasmAbi, VectorIntoWasmAbi,
};
#[cfg(target_family = "wasm")]
use wasm_bindgen::describe::{inform, WasmDescribe, WasmDescribeVector, VECTOR};
#[cfg(target_family = "wasm")]
use wasm_bindgen::{JsValue, UnwrapThrowExt};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[cfg_attr(
target_family = "wasm",
derive(Tsify),
tsify(into_wasm_abi, from_wasm_abi)
)]
#[cfg_attr(target_family = "wasm", derive(Tsify))]
pub struct ApprovalCalldata {
#[cfg_attr(target_family = "wasm", tsify(type = "string"))]
token: Address,
#[cfg_attr(target_family = "wasm", tsify(type = "string"))]
calldata: Bytes,
}
#[cfg(target_family = "wasm")]
impl_wasm_traits!(ApprovalCalldata);
impl_all_wasm_traits!(ApprovalCalldata);

impl DotrainOrder {
fn get_deployment(
Expand Down
2 changes: 1 addition & 1 deletion crates/common/src/dotrain_order/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::dotrain_order::{DotrainOrder, DotrainOrderError};
pub use rain_metadata::types::authoring::v2::*;
use rain_orderbook_app_settings::config_source::ConfigSource;
#[cfg(target_family = "wasm")]
use wasm_bindgen::prelude::*;
use rain_orderbook_bindings::wasm_traits::prelude::*;

#[cfg_attr(target_family = "wasm", wasm_bindgen)]
impl DotrainOrder {
Expand Down
4 changes: 2 additions & 2 deletions crates/common/src/dotrain_order/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ use rain_orderbook_app_settings::{
merge::MergeError,
Config, ParseConfigSourceError,
};
#[cfg(target_family = "wasm")]
use rain_orderbook_bindings::{impl_all_wasm_traits, wasm_traits::prelude::*};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use thiserror::Error;
use typeshare::typeshare;
#[cfg(target_family = "wasm")]
use wasm_bindgen::prelude::*;

pub mod calldata;
pub mod filter;
Expand Down
26 changes: 4 additions & 22 deletions crates/common/src/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,21 @@ use alloy_ethers_typecast::{
};
use rain_error_decoding::{AbiDecodeFailedErrors, AbiDecodedErrorType};
use rain_orderbook_bindings::IERC20::{decimalsCall, nameCall, symbolCall};
#[cfg(target_family = "wasm")]
use rain_orderbook_bindings::{impl_all_wasm_traits, wasm_traits::prelude::*};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use url::Url;

#[cfg(target_family = "wasm")]
use rain_orderbook_bindings::impl_wasm_traits;
#[cfg(target_family = "wasm")]
use serde_wasm_bindgen::{from_value, to_value};
#[cfg(target_family = "wasm")]
use tsify::Tsify;
#[cfg(target_family = "wasm")]
use wasm_bindgen::convert::{
js_value_vector_from_abi, js_value_vector_into_abi, FromWasmAbi, IntoWasmAbi,
LongRefFromWasmAbi, RefFromWasmAbi, TryFromJsValue, VectorFromWasmAbi, VectorIntoWasmAbi,
};
#[cfg(target_family = "wasm")]
use wasm_bindgen::describe::{inform, WasmDescribe, WasmDescribeVector, VECTOR};
#[cfg(target_family = "wasm")]
use wasm_bindgen::{JsValue, UnwrapThrowExt};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(
target_family = "wasm",
derive(Tsify),
tsify(into_wasm_abi, from_wasm_abi)
)]
#[cfg_attr(target_family = "wasm", derive(Tsify))]
pub struct TokenInfo {
pub decimals: u8,
pub name: String,
pub symbol: String,
}
#[cfg(target_family = "wasm")]
impl_wasm_traits!(TokenInfo);
impl_all_wasm_traits!(TokenInfo);

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ERC20 {
Expand Down
3 changes: 1 addition & 2 deletions crates/js_api/src/gui/deposits.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use super::*;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct TokenDeposit {
pub token: String,
pub amount: String,
#[tsify(type = "string")]
pub address: Address,
}
impl_wasm_traits!(TokenDeposit);
impl_all_wasm_traits!(TokenDeposit);

#[wasm_bindgen]
impl DotrainOrderGui {
Expand Down
Loading

0 comments on commit 4a6cf55

Please sign in to comment.