From 7ffc8b9999d63d8fa847537fdb45945bcbcd7872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Thu, 9 Jun 2022 11:48:53 +0100 Subject: [PATCH] More type-restrictive exported denom type --- common/types/src/currency.rs | 67 +++++++++++++++++-- tools/ts-rs-cli/src/main.rs | 3 +- ts-packages/types/src/types/rust/Currency.ts | 7 -- .../types/src/types/rust/CurrencyDenom.ts | 3 +- .../types/rust/CurrencyStringMajorAmount.ts | 1 - ts-packages/types/src/types/rust/DecCoin.ts | 3 +- 6 files changed, 67 insertions(+), 17 deletions(-) delete mode 100644 ts-packages/types/src/types/rust/Currency.ts delete mode 100644 ts-packages/types/src/types/rust/CurrencyStringMajorAmount.ts diff --git a/common/types/src/currency.rs b/common/types/src/currency.rs index abe79d8b1e..183510193f 100644 --- a/common/types/src/currency.rs +++ b/common/types/src/currency.rs @@ -9,8 +9,42 @@ use std::cmp::Ordering; use std::collections::HashMap; use std::convert::TryFrom; use std::fmt::{Display, Formatter}; +use strum::{Display, EnumString, EnumVariantNames}; use validator_client::nymd::Coin; +#[cfg(feature = "generate-ts")] +use ts_rs::{Dependency, TS}; + +#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] +#[cfg_attr( + feature = "generate-ts", + ts(export_to = "ts-packages/types/src/types/rust/CurrencyDenom.ts") +)] +#[cfg_attr(feature = "generate-ts", ts(rename_all = "lowercase"))] +#[derive( + Display, + Serialize, + Deserialize, + Clone, + Debug, + EnumString, + EnumVariantNames, + PartialEq, + JsonSchema, +)] +#[serde(rename_all = "lowercase")] +#[strum(serialize_all = "lowercase")] +pub enum CurrencyDenom { + #[strum(ascii_case_insensitive)] + Nym, + #[strum(ascii_case_insensitive)] + Nymt, + #[strum(ascii_case_insensitive)] + Nyx, + #[strum(ascii_case_insensitive)] + Nyxt, +} + pub type Denom = String; #[derive(Debug, Default)] @@ -149,20 +183,41 @@ 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 -#[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") -)] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] pub struct DecCoin { 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/tools/ts-rs-cli/src/main.rs b/tools/ts-rs-cli/src/main.rs index d251e61631..c24e3d6487 100644 --- a/tools/ts-rs-cli/src/main.rs +++ b/tools/ts-rs-cli/src/main.rs @@ -4,7 +4,7 @@ use walkdir::WalkDir; use mixnet_contract_common::mixnode::RewardedSetNodeStatus; use nym_types::account::{Account, AccountEntry, AccountWithMnemonic, Balance}; -use nym_types::currency::DecCoin; +use nym_types::currency::{CurrencyDenom, DecCoin}; use nym_types::delegation::{ Delegation, DelegationEvent, DelegationEventKind, DelegationRecord, DelegationResult, DelegationWithEverything, DelegationsSummaryResponse, PendingUndelegate, @@ -71,6 +71,7 @@ fn main() { do_export!(GasInfo); do_export!(Gateway); do_export!(GatewayBond); + do_export!(CurrencyDenom); do_export!(DecCoin); do_export!(MixNode); do_export!(MixNodeBond); diff --git a/ts-packages/types/src/types/rust/Currency.ts b/ts-packages/types/src/types/rust/Currency.ts deleted file mode 100644 index f0c6d57fbb..0000000000 --- a/ts-packages/types/src/types/rust/Currency.ts +++ /dev/null @@ -1,7 +0,0 @@ -import type { CurrencyDenom } from './CurrencyDenom'; -import type { MajorAmountString } from './CurrencyStringMajorAmount'; - -export interface MajorCurrencyAmount { - amount: MajorAmountString; - denom: CurrencyDenom; -} diff --git a/ts-packages/types/src/types/rust/CurrencyDenom.ts b/ts-packages/types/src/types/rust/CurrencyDenom.ts index d6a45dc3d2..e55231ccc0 100644 --- a/ts-packages/types/src/types/rust/CurrencyDenom.ts +++ b/ts-packages/types/src/types/rust/CurrencyDenom.ts @@ -1 +1,2 @@ -export type CurrencyDenom = 'NYM' | 'NYMT' | 'NYX' | 'NYXT'; + +export type CurrencyDenom = "nym" | "nymt" | "nyx" | "nyxt"; \ No newline at end of file diff --git a/ts-packages/types/src/types/rust/CurrencyStringMajorAmount.ts b/ts-packages/types/src/types/rust/CurrencyStringMajorAmount.ts deleted file mode 100644 index 9863028330..0000000000 --- a/ts-packages/types/src/types/rust/CurrencyStringMajorAmount.ts +++ /dev/null @@ -1 +0,0 @@ -export type MajorAmountString = string; diff --git a/ts-packages/types/src/types/rust/DecCoin.ts b/ts-packages/types/src/types/rust/DecCoin.ts index ea89bdbee5..d3751d56f8 100644 --- a/ts-packages/types/src/types/rust/DecCoin.ts +++ b/ts-packages/types/src/types/rust/DecCoin.ts @@ -1,2 +1,3 @@ +import type { CurrencyDenom } from "./CurrencyDenom"; -export interface DecCoin { denom: string, amount: string, } \ No newline at end of file +export type DecCoin = { denom: CurrencyDenom, amount: string }; \ No newline at end of file