From fb43e917cb2ea70bd9a025451ddd97415969b2f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Wed, 25 Sep 2024 10:32:40 +0100 Subject: [PATCH] updated ts_rs and derived it for more types --- Cargo.lock | 16 ++---- Cargo.toml | 2 +- .../mixnet-contract/src/nym_node.rs | 5 ++ .../mixnet-contract/src/reward_params.rs | 7 +++ common/types/src/currency.rs | 38 +++---------- common/types/src/fees.rs | 38 +++---------- nym-api/nym-api-requests/src/models.rs | 54 +++++++++++++++++++ nym-api/nym-api-requests/src/pagination.rs | 10 ++++ nym-wallet/nym-wallet-types/Cargo.toml | 2 +- nym-wallet/src-tauri/Cargo.toml | 2 +- tools/ts-rs-cli/src/main.rs | 30 +++++++++-- 11 files changed, 124 insertions(+), 80 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 11ed1c4400..77221ef262 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,12 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "Inflector" -version = "0.11.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" - [[package]] name = "accessory" version = "1.3.1" @@ -9680,10 +9674,11 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" [[package]] name = "ts-rs" -version = "7.1.1" +version = "10.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc2cae1fc5d05d47aa24b64f9a4f7cba24cdc9187a2084dd97ac57bef5eccae6" +checksum = "3a2f31991cee3dce1ca4f929a8a04fdd11fd8801aac0f2030b0fa8a0a3fef6b9" dependencies = [ + "lazy_static", "thiserror", "ts-rs-macros", ] @@ -9705,11 +9700,10 @@ dependencies = [ [[package]] name = "ts-rs-macros" -version = "7.1.1" +version = "10.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73f7f9b821696963053a89a7bd8b292dc34420aea8294d7b225274d488f3ec92" +checksum = "0ea0b99e8ec44abd6f94a18f28f7934437809dd062820797c52401298116f70e" dependencies = [ - "Inflector", "proc-macro2", "quote", "syn 2.0.66", diff --git a/Cargo.toml b/Cargo.toml index 81804b89da..04e5f5c8e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -327,7 +327,7 @@ tracing = "0.1.37" tracing-opentelemetry = "0.19.0" tracing-subscriber = "0.3.16" tracing-tree = "0.2.2" -ts-rs = "7.0.0" +ts-rs = "10.0.0" tungstenite = { version = "0.20.1", default-features = false } url = "2.5" utoipa = "4.2" diff --git a/common/cosmwasm-smart-contracts/mixnet-contract/src/nym_node.rs b/common/cosmwasm-smart-contracts/mixnet-contract/src/nym_node.rs index fa5db3b39c..9453375dd3 100644 --- a/common/cosmwasm-smart-contracts/mixnet-contract/src/nym_node.rs +++ b/common/cosmwasm-smart-contracts/mixnet-contract/src/nym_node.rs @@ -12,6 +12,11 @@ use std::fmt::{Display, Formatter}; #[cw_serde] #[derive(PartialOrd, Copy, Hash, Eq)] #[repr(u8)] +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/Role.ts") +)] pub enum Role { #[serde(rename = "eg", alias = "entry", alias = "entry_gateway")] EntryGateway = 0, diff --git a/common/cosmwasm-smart-contracts/mixnet-contract/src/reward_params.rs b/common/cosmwasm-smart-contracts/mixnet-contract/src/reward_params.rs index da6e2d3dae..03cd89bd04 100644 --- a/common/cosmwasm-smart-contracts/mixnet-contract/src/reward_params.rs +++ b/common/cosmwasm-smart-contracts/mixnet-contract/src/reward_params.rs @@ -303,14 +303,21 @@ impl RewardedSetParams { } /// Parameters used for rewarding particular node. +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/NodeRewardingParameters.ts") +)] #[cw_serde] #[derive(Copy)] pub struct NodeRewardingParameters { /// Performance of the particular node in the current epoch. + #[cfg_attr(feature = "generate-ts", ts(type = "string"))] pub performance: Performance, /// Amount of work performed by this node in the current epoch /// also known as 'omega' in the paper + #[cfg_attr(feature = "generate-ts", ts(type = "string"))] pub work_factor: WorkFactor, } diff --git a/common/types/src/currency.rs b/common/types/src/currency.rs index 26cda1a2db..5cbe901cc8 100644 --- a/common/types/src/currency.rs +++ b/common/types/src/currency.rs @@ -7,13 +7,9 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use std::cmp::Ordering; use std::collections::HashMap; - use std::fmt::{Display, Formatter}; use strum::{Display, EnumString, VariantNames}; -#[cfg(feature = "generate-ts")] -use ts_rs::{Dependency, TS}; - #[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] #[cfg_attr( feature = "generate-ts", @@ -248,40 +244,20 @@ impl From for CoinMetadata { // tries to semi-replicate cosmos-sdk's DecCoin for being able to handle tokens with decimal amounts // https://github.com/cosmos/cosmos-sdk/blob/v0.45.4/types/dec_coin.go #[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, JsonSchema)] +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/DecCoin.ts") +)] pub struct DecCoin { + #[cfg_attr(feature = "generate-ts", ts(as = "CurrencyDenom"))] pub denom: Denom, // Decimal is already serialized to string and using string in its schema, so lets also go straight to string for ts_rs // todo: is `Decimal` the correct type to use? Do we want to depend on cosmwasm_std here? + #[cfg_attr(feature = "generate-ts", ts(type = "string"))] pub amount: Decimal, } -// I had to implement it manually to correctly set dependencies -#[cfg(feature = "generate-ts")] -impl TS for DecCoin { - const EXPORT_TO: Option<&'static str> = Some("ts-packages/types/src/types/rust/DecCoin.ts"); - - fn decl() -> String { - format!("type {} = {};", Self::name(), Self::inline()) - } - - fn name() -> String { - "DecCoin".into() - } - - fn inline() -> String { - "{ denom: CurrencyDenom, amount: string }".into() - } - - fn dependencies() -> Vec { - vec![Dependency::from_ty::() - .expect("TS was incorrectly defined on `CurrencyDenom`")] - } - - fn transparent() -> bool { - false - } -} - impl DecCoin { pub fn new_base>(amount: impl Into, denom: S) -> Self { DecCoin { diff --git a/common/types/src/fees.rs b/common/types/src/fees.rs index cd98be9d40..c27b19fef5 100644 --- a/common/types/src/fees.rs +++ b/common/types/src/fees.rs @@ -2,13 +2,16 @@ use crate::currency::DecCoin; use nym_validator_client::nyxd::Fee; use serde::{Deserialize, Serialize}; -#[cfg(feature = "generate-ts")] -use ts_rs::{Dependency, TS}; - #[derive(Debug, Clone, Serialize, Deserialize)] +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/FeeDetails.ts") +)] pub struct FeeDetails { // expected to be used by the wallet in order to display detailed fee information to the user pub amount: Option, + #[cfg_attr(feature = "generate-ts", ts(as = "ts_type_helpers::Fee"))] pub fee: Fee, } @@ -18,35 +21,6 @@ impl FeeDetails { } } -#[cfg(feature = "generate-ts")] -impl TS for FeeDetails { - const EXPORT_TO: Option<&'static str> = Some("ts-packages/types/src/types/rust/FeeDetails.ts"); - - fn decl() -> String { - format!("type {} = {};", Self::name(), Self::inline()) - } - - fn name() -> String { - "FeeDetails".into() - } - - fn inline() -> String { - "{ amount: DecCoin | null, fee: Fee }".into() - } - - fn dependencies() -> Vec { - vec![ - Dependency::from_ty::().expect("TS was incorrectly defined on `DecCoin`"), - Dependency::from_ty::() - .expect("TS was incorrectly defined on `ts_type_helpers::Fee`"), - ] - } - - fn transparent() -> bool { - false - } -} - // this should really be sealed and NEVER EVER used as "normal" types, // but due to our typescript requirements, we have to expose it to generate // the types... diff --git a/nym-api/nym-api-requests/src/models.rs b/nym-api/nym-api-requests/src/models.rs index aaec49854e..44279666e2 100644 --- a/nym-api/nym-api-requests/src/models.rs +++ b/nym-api/nym-api-requests/src/models.rs @@ -130,12 +130,23 @@ pub struct NodePerformance { // nym-api shouldn't be calculating apy or stake saturation for you. // it should just return its own metrics (performance) and then you can do with it as you wish #[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema)] +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/NodeAnnotation.ts") +)] pub struct NodeAnnotation { + #[cfg_attr(feature = "generate-ts", ts(type = "string"))] pub last_24h_performance: Performance, pub current_role: Option, } #[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, ToSchema)] +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/AnnotationResponse.ts") +)] pub struct AnnotationResponse { #[schema(value_type = u32)] pub node_id: NodeId, @@ -143,6 +154,11 @@ pub struct AnnotationResponse { } #[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, ToSchema)] +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/NodePerformanceResponse.ts") +)] pub struct NodePerformanceResponse { #[schema(value_type = u32)] pub node_id: NodeId, @@ -150,11 +166,17 @@ pub struct NodePerformanceResponse { } #[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, ToSchema)] +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/NodeDatePerformanceResponse.ts") +)] pub struct NodeDatePerformanceResponse { #[schema(value_type = u32)] pub node_id: NodeId, #[schema(value_type = String, example = "1970-01-01")] #[schemars(with = "String")] + #[cfg_attr(feature = "generate-ts", ts(type = "string"))] pub date: Date, pub performance: Option, } @@ -490,6 +512,11 @@ pub struct GatewayStatusReportResponse { } #[derive(Serialize, Deserialize, schemars::JsonSchema, ToSchema)] +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/PerformanceHistoryResponse.ts") +)] pub struct PerformanceHistoryResponse { #[schema(value_type = u32)] pub node_id: NodeId, @@ -497,6 +524,11 @@ pub struct PerformanceHistoryResponse { } #[derive(Serialize, Deserialize, schemars::JsonSchema, ToSchema)] +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/UptimeHistoryResponse.ts") +)] pub struct UptimeHistoryResponse { #[schema(value_type = u32)] pub node_id: NodeId, @@ -504,18 +536,30 @@ pub struct UptimeHistoryResponse { } #[derive(Clone, Serialize, Deserialize, schemars::JsonSchema, ToSchema)] +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/HistoricalUptimeResponse.ts") +)] pub struct HistoricalUptimeResponse { #[schema(value_type = String, example = "1970-01-01")] #[schemars(with = "String")] + #[cfg_attr(feature = "generate-ts", ts(type = "string"))] pub date: Date, pub uptime: Uptime, } #[derive(Clone, Serialize, Deserialize, schemars::JsonSchema, ToSchema)] +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/HistoricalPerformanceResponse.ts") +)] pub struct HistoricalPerformanceResponse { #[schema(value_type = String, example = "1970-01-01")] #[schemars(with = "String")] + #[cfg_attr(feature = "generate-ts", ts(type = "string"))] pub date: Date, pub performance: f64, @@ -745,6 +789,11 @@ impl NymNodeDescription { #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, schemars::JsonSchema, ToSchema)] #[serde(rename_all = "snake_case")] +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/DescribedNodeType.ts") +)] pub enum DescribedNodeType { LegacyMixnode, LegacyGateway, @@ -752,6 +801,11 @@ pub enum DescribedNodeType { } #[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, schemars::JsonSchema, ToSchema)] +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/DeclaredRoles.ts") +)] pub struct DeclaredRoles { pub mixnode: bool, pub entry: bool, diff --git a/nym-api/nym-api-requests/src/pagination.rs b/nym-api/nym-api-requests/src/pagination.rs index 2d82c826ae..9a36caa988 100644 --- a/nym-api/nym-api-requests/src/pagination.rs +++ b/nym-api/nym-api-requests/src/pagination.rs @@ -6,6 +6,11 @@ use serde::{Deserialize, Serialize}; use utoipa::ToSchema; #[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, ToSchema)] +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/Pagination.ts") +)] pub struct Pagination { pub total: usize, pub page: u32, @@ -13,6 +18,11 @@ pub struct Pagination { } #[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, ToSchema)] +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/PaginatedResponse.ts") +)] pub struct PaginatedResponse { pub pagination: Pagination, pub data: Vec, diff --git a/nym-wallet/nym-wallet-types/Cargo.toml b/nym-wallet/nym-wallet-types/Cargo.toml index 9bfd3717b9..720027178b 100644 --- a/nym-wallet/nym-wallet-types/Cargo.toml +++ b/nym-wallet/nym-wallet-types/Cargo.toml @@ -10,7 +10,7 @@ hex-literal = "0.3.3" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" strum = { version = "0.23", features = ["derive"] } -ts-rs = "7.0.0" +ts-rs = "10.0.0" cosmwasm-std = "1.4.3" cosmrs = "=0.15.0" diff --git a/nym-wallet/src-tauri/Cargo.toml b/nym-wallet/src-tauri/Cargo.toml index 61eee10d85..88032e5efb 100644 --- a/nym-wallet/src-tauri/Cargo.toml +++ b/nym-wallet/src-tauri/Cargo.toml @@ -67,7 +67,7 @@ nym-store-cipher = { path = "../../common/store-cipher", features = ["json"] } nym-crypto = { path = "../../common/crypto", features = ["rand"] } rand_chacha = "0.3" tempfile = "3.3.0" -ts-rs = "7.0.0" +ts-rs = "10.0.0" [features] default = ["custom-protocol"] diff --git a/tools/ts-rs-cli/src/main.rs b/tools/ts-rs-cli/src/main.rs index 8a7720664f..a876faf637 100644 --- a/tools/ts-rs-cli/src/main.rs +++ b/tools/ts-rs-cli/src/main.rs @@ -1,7 +1,14 @@ use nym_api_requests::models::{ - GatewayCoreStatusResponse, InclusionProbabilityResponse, MixnodeCoreStatusResponse, - MixnodeStatus, MixnodeStatusResponse, RewardEstimationResponse, SelectionChance, - StakeSaturationResponse, + AnnotationResponse, DeclaredRoles, DescribedNodeType, GatewayCoreStatusResponse, + HistoricalPerformanceResponse, HistoricalUptimeResponse, InclusionProbabilityResponse, + MixnodeCoreStatusResponse, MixnodeStatus, MixnodeStatusResponse, NodeAnnotation, + NodeDatePerformanceResponse, NodePerformanceResponse, PerformanceHistoryResponse, + RewardEstimationResponse, SelectionChance, StakeSaturationResponse, UptimeHistoryResponse, +}; +use nym_api_requests::pagination::{PaginatedResponse, Pagination}; +use nym_mixnet_contract_common::nym_node::{NodeConfigUpdate, Role}; +use nym_mixnet_contract_common::reward_params::{ + ActiveSetUpdate, NodeRewardingParameters, RewardedSetParams, }; use nym_mixnet_contract_common::rewarding::RewardEstimate; use nym_mixnet_contract_common::{ @@ -70,11 +77,16 @@ fn main() { do_export!(MixNode); do_export!(MixNodeConfigUpdate); do_export!(RewardingParams); + do_export!(RewardedSetParams); + do_export!(NodeRewardingParameters); + do_export!(ActiveSetUpdate); do_export!(UnbondedMixnode); do_export!(RewardEstimate); do_export!(ContractInterval); do_export!(NymNode); do_export!(PendingNodeChanges); + do_export!(NodeConfigUpdate); + do_export!(Role); // common/types/src do_export!(Account); @@ -133,6 +145,18 @@ fn main() { do_export!(SelectionChance); do_export!(StakeSaturationResponse); do_export!(RewardEstimationResponse); + do_export!(NodeAnnotation); + do_export!(AnnotationResponse); + do_export!(NodePerformanceResponse); + do_export!(NodeDatePerformanceResponse); + do_export!(PerformanceHistoryResponse); + do_export!(UptimeHistoryResponse); + do_export!(HistoricalUptimeResponse); + do_export!(HistoricalPerformanceResponse); + do_export!(DescribedNodeType); + do_export!(DeclaredRoles); + do_export!(PaginatedResponse); + do_export!(Pagination); // nym-wallet do_export!(AppEnv);