Skip to content

Commit

Permalink
fix: typescript generate types (nymtech#1660)
Browse files Browse the repository at this point in the history
  • Loading branch information
doums authored Oct 5, 2022
1 parent 8a3351b commit 7a3253e
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions common/cosmwasm-smart-contracts/mixnet-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ schemars = "0.8"
thiserror = "1.0"
contracts-common = { path = "../contracts-common" }
serde_json = "1.0.0"
humantime-serde = "1.1.1"

# TO CHECK WHETHER STILL NEEDED:
log = "0.4.14"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,23 @@ pub(crate) mod string_rfc3339_offset_date_time {
}
}

#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
feature = "generate-ts",
ts(export_to = "ts-packages/types/src/types/rust/Interval.ts")
)]
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, Serialize)]
pub struct Interval {
id: IntervalId,
epochs_in_interval: u32,

// TODO add a better TS type generation
#[cfg_attr(feature = "generate-ts", ts(type = "string"))]
#[serde(with = "string_rfc3339_offset_date_time")]
current_epoch_start: OffsetDateTime,
current_epoch_id: EpochId,
#[cfg_attr(feature = "generate-ts", ts(type = "string"))]
#[serde(with = "humantime_serde")]
epoch_length: Duration,
total_elapsed_epochs: EpochId,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ pub struct UnbondedMixnode {
#[cfg_attr(feature = "generate-ts", ts(type = "string | null"))]
pub proxy: Option<Addr>,

#[cfg_attr(feature = "generate-ts", ts(type = "number"))]
pub unbonding_height: u64,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@ use serde::{Deserialize, Serialize};
pub mod helpers;
pub mod simulator;

#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
feature = "generate-ts",
ts(export_to = "ts-packages/types/src/types/rust/RewardEstimate.ts")
)]
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, JsonSchema, PartialEq, Eq)]
pub struct RewardEstimate {
#[cfg_attr(feature = "generate-ts", ts(type = "string"))]
pub total_node_reward: Decimal,

// note that operator reward includes the operating_cost,
// i.e. say total_node_reward was `1nym` and operating_cost was `2nym`
// in that case the operator reward would still be `1nym` as opposed to 0
#[cfg_attr(feature = "generate-ts", ts(type = "string"))]
pub operator: Decimal,
#[cfg_attr(feature = "generate-ts", ts(type = "string"))]
pub delegates: Decimal,
#[cfg_attr(feature = "generate-ts", ts(type = "string"))]
pub operating_cost: Decimal,
}

Expand Down
11 changes: 8 additions & 3 deletions tools/ts-rs-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use mixnet_contract_common::rewarding::RewardEstimate;
use mixnet_contract_common::{
IntervalRewardParams, IntervalRewardingParamsUpdate, MixNode, MixNodeConfigUpdate,
RewardedSetNodeStatus, RewardingParams, UnbondedMixnode,
Interval as ContractInterval, IntervalRewardParams, IntervalRewardingParamsUpdate, MixNode,
MixNodeConfigUpdate, RewardedSetNodeStatus, RewardingParams, UnbondedMixnode,
};
use nym_types::account::{Account, AccountEntry, AccountWithMnemonic, Balance};
use nym_types::currency::{CurrencyDenom, DecCoin};
Expand Down Expand Up @@ -28,7 +29,8 @@ use std::path::Path;
use ts_rs::TS;
use validator_api_requests::models::{
GatewayCoreStatusResponse, InclusionProbabilityResponse, MixnodeCoreStatusResponse,
MixnodeStatus, MixnodeStatusResponse, SelectionChance, StakeSaturationResponse,
MixnodeStatus, MixnodeStatusResponse, RewardEstimationResponse, SelectionChance,
StakeSaturationResponse,
};
use vesting_contract_common::Period;
use walkdir::WalkDir;
Expand Down Expand Up @@ -65,6 +67,8 @@ fn main() {
do_export!(RewardingParams);
do_export!(RewardedSetNodeStatus);
do_export!(UnbondedMixnode);
do_export!(RewardEstimate);
do_export!(ContractInterval);

// common/types/src
do_export!(Account);
Expand Down Expand Up @@ -116,6 +120,7 @@ fn main() {
do_export!(MixnodeStatusResponse);
do_export!(SelectionChance);
do_export!(StakeSaturationResponse);
do_export!(RewardEstimationResponse);

// nym-wallet
do_export!(AppEnv);
Expand Down
3 changes: 1 addition & 2 deletions ts-packages/types/src/types/rust/DelegationEventKind.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

export type DelegationEventKind = "Delegate" | "Undelegate";
export type DelegationEventKind = 'Delegate' | 'Undelegate';
8 changes: 8 additions & 0 deletions ts-packages/types/src/types/rust/Interval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface Interval {
id: number;
epochs_in_interval: number;
current_epoch_start: string;
current_epoch_id: number;
epoch_length: string;
total_elapsed_epochs: number;
}
4 changes: 2 additions & 2 deletions ts-packages/types/src/types/rust/RewardEstimate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type RewardEstimate = {
export interface RewardEstimate {
total_node_reward: string;
operator: string;
delegates: string;
operating_cost: string;
};
}
11 changes: 6 additions & 5 deletions ts-packages/types/src/types/rust/RewardEstimationResponse.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { RewardEstimate } from './RewardEstimate';
import { RewardingParams } from './RewardingParams';
import type { Interval } from './Interval';
import type { RewardEstimate } from './RewardEstimate';
import type { RewardingParams } from './RewardingParams';

export type RewardEstimationResponse = {
export interface RewardEstimationResponse {
estimation: RewardEstimate;
reward_params: RewardingParams;
// epoch: Interval;
epoch: Interval;
as_at: number;
};
}
8 changes: 6 additions & 2 deletions ts-packages/types/src/types/rust/UnbondedMixnode.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@

export interface UnbondedMixnode { identity_key: string, owner: string, proxy: string | null, unbonding_height: bigint, }
export interface UnbondedMixnode {
identity_key: string;
owner: string;
proxy: string | null;
unbonding_height: number;
}
7 changes: 6 additions & 1 deletion validator-api/validator-api-requests/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,17 @@ pub struct ComputeRewardEstParam {
pub total_delegation: Option<u64>,
}

#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
feature = "generate-ts",
ts(export_to = "ts-packages/types/src/types/rust/RewardEstimationResponse.ts")
)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema)]
pub struct RewardEstimationResponse {
pub estimation: RewardEstimate,

pub reward_params: RewardingParams,
pub epoch: Interval,
#[cfg_attr(feature = "generate-ts", ts(type = "number"))]
pub as_at: i64,
}

Expand Down

0 comments on commit 7a3253e

Please sign in to comment.