nym_types/
vesting.rs

1use crate::currency::{DecCoin, RegisteredCoins};
2use crate::error::TypesError;
3use nym_vesting_contract_common::account::Account as ContractVestingAccount;
4use nym_vesting_contract_common::types::VestingPeriod as ContractVestingPeriod;
5use nym_vesting_contract_common::OriginalVestingResponse as ContractOriginalVestingResponse;
6use nym_vesting_contract_common::PledgeData as ContractPledgeData;
7use serde::{Deserialize, Serialize};
8
9#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
10#[cfg_attr(
11    feature = "generate-ts",
12    ts(export, export_to = "ts-packages/types/src/types/rust/PledgeData.ts")
13)]
14#[derive(Serialize, Deserialize, Debug)]
15pub struct PledgeData {
16    pub amount: DecCoin,
17    pub block_time: u64,
18}
19
20impl PledgeData {
21    pub fn from_vesting_contract(
22        pledge: ContractPledgeData,
23        reg: &RegisteredCoins,
24    ) -> Result<Self, TypesError> {
25        Ok(PledgeData {
26            amount: reg.attempt_convert_to_display_dec_coin(pledge.amount.into())?,
27            block_time: pledge.block_time.seconds(),
28        })
29    }
30}
31
32#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
33#[cfg_attr(
34    feature = "generate-ts",
35    ts(
36        export,
37        export_to = "ts-packages/types/src/types/rust/OriginalVestingResponse.ts"
38    )
39)]
40#[derive(Serialize, Deserialize, Debug)]
41pub struct OriginalVestingResponse {
42    amount: DecCoin,
43    number_of_periods: usize,
44    period_duration: u64,
45}
46
47impl OriginalVestingResponse {
48    pub fn from_vesting_contract(
49        res: ContractOriginalVestingResponse,
50        reg: &RegisteredCoins,
51    ) -> Result<Self, TypesError> {
52        Ok(OriginalVestingResponse {
53            amount: reg.attempt_convert_to_display_dec_coin(res.amount.into())?,
54            number_of_periods: res.number_of_periods,
55            period_duration: res.period_duration,
56        })
57    }
58}
59
60#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
61#[cfg_attr(
62    feature = "generate-ts",
63    ts(
64        export,
65        export_to = "ts-packages/types/src/types/rust/VestingAccountInfo.ts"
66    )
67)]
68#[derive(Serialize, Deserialize, Debug)]
69pub struct VestingAccountInfo {
70    owner_address: String,
71    staking_address: Option<String>,
72    start_time: u64,
73    periods: Vec<VestingPeriod>,
74    amount: DecCoin,
75}
76
77impl VestingAccountInfo {
78    pub fn from_vesting_contract(
79        account: ContractVestingAccount,
80        reg: &RegisteredCoins,
81    ) -> Result<Self, TypesError> {
82        Ok(VestingAccountInfo {
83            owner_address: account.owner_address().to_string(),
84            staking_address: account.staking_address().map(|a| a.to_string()),
85            start_time: account.start_time().seconds(),
86            periods: account.periods().into_iter().map(Into::into).collect(),
87            amount: reg.attempt_convert_to_display_dec_coin(account.coin.into())?,
88        })
89    }
90}
91
92#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
93#[cfg_attr(
94    feature = "generate-ts",
95    ts(
96        export,
97        export_to = "ts-packages/types/src/types/rust/VestingPeriod.ts"
98    )
99)]
100#[derive(Serialize, Deserialize, Debug)]
101pub struct VestingPeriod {
102    start_time: u64,
103    period_seconds: u64,
104}
105
106impl From<ContractVestingPeriod> for VestingPeriod {
107    fn from(period: ContractVestingPeriod) -> Self {
108        Self {
109            start_time: period.start_time,
110            period_seconds: period.period_seconds,
111        }
112    }
113}