More type-restrictive exported denom type

This commit is contained in:
Jędrzej Stuczyński
2022-06-09 11:48:53 +01:00
parent 7c9fbc9c73
commit 7ffc8b9999
6 changed files with 67 additions and 17 deletions
+61 -6
View File
@@ -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<DenomDetails> 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<Dependency> {
vec![Dependency::from_ty::<CurrencyDenom>()
.expect("TS was incorrectly defined on `CurrencyDenom`")]
}
fn transparent() -> bool {
false
}
}
impl DecCoin {
pub fn new_base<S: Into<String>>(amount: impl Into<Uint128>, denom: S) -> Self {
DecCoin {
+2 -1
View File
@@ -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);
@@ -1,7 +0,0 @@
import type { CurrencyDenom } from './CurrencyDenom';
import type { MajorAmountString } from './CurrencyStringMajorAmount';
export interface MajorCurrencyAmount {
amount: MajorAmountString;
denom: CurrencyDenom;
}
@@ -1 +1,2 @@
export type CurrencyDenom = 'NYM' | 'NYMT' | 'NYX' | 'NYXT';
export type CurrencyDenom = "nym" | "nymt" | "nyx" | "nyxt";
@@ -1 +0,0 @@
export type MajorAmountString = string;
+2 -1
View File
@@ -1,2 +1,3 @@
import type { CurrencyDenom } from "./CurrencyDenom";
export interface DecCoin { denom: string, amount: string, }
export type DecCoin = { denom: CurrencyDenom, amount: string };