nym_vesting_contract_common/
lib.rs

1// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
2// SPDX-License-Identifier: Apache-2.0
3
4#![warn(clippy::expect_used)]
5#![warn(clippy::unwrap_used)]
6
7use cosmwasm_schema::cw_serde;
8use cosmwasm_std::{Addr, Coin};
9use nym_mixnet_contract_common::NodeId;
10
11pub mod account;
12pub mod error;
13pub mod events;
14pub mod messages;
15pub mod types;
16
17pub use account::Account;
18pub use error::VestingContractError;
19pub use messages::{ExecuteMsg, InitMsg, MigrateMsg, QueryMsg};
20pub use types::*;
21
22/// Details about the original vesting specification used when the account was created.
23#[cw_serde]
24pub struct OriginalVestingResponse {
25    /// The original amount that was used for the creation of this vesting account
26    pub amount: Coin,
27
28    /// The number of vesting periods that the account was created with
29    pub number_of_periods: usize,
30
31    /// Duration of each vesting period in seconds
32    pub period_duration: u64,
33}
34
35impl OriginalVestingResponse {
36    pub fn amount(&self) -> Coin {
37        self.amount.clone()
38    }
39
40    pub fn number_of_periods(&self) -> usize {
41        self.number_of_periods
42    }
43
44    pub fn period_duration(&self) -> u64 {
45        self.period_duration
46    }
47
48    pub fn new(amount: Coin, number_of_periods: usize, period_duration: u64) -> Self {
49        Self {
50            amount,
51            number_of_periods,
52            period_duration,
53        }
54    }
55}
56
57/// Response containing timestamps of all delegations made towards particular mixnode by given vesting account.
58#[cw_serde]
59pub struct DelegationTimesResponse {
60    /// Address of this account's owner
61    pub owner: Addr,
62
63    /// Id associated with this account
64    pub account_id: u32,
65
66    /// Id of the mixnode towards which the delegation was made
67    pub mix_id: NodeId,
68
69    /// All timestamps where a delegation was made
70    pub delegation_timestamps: Vec<u64>,
71}
72
73/// Response containing paged list of all vesting delegations made using vesting coins.
74#[cw_serde]
75pub struct AllDelegationsResponse {
76    /// The actual vesting delegations made.
77    pub delegations: Vec<VestingDelegation>,
78
79    /// Field indicating paging information for the following queries if the caller wishes to get further entries.
80    pub start_next_after: Option<(u32, NodeId, u64)>,
81}
82
83/// Basic information regarding particular vesting account alongside the amount of vesting coins.
84#[cw_serde]
85pub struct AccountVestingCoins {
86    /// Id associated with this account
87    pub account_id: u32,
88
89    /// Address of this account's owner
90    pub owner: Addr,
91
92    /// Coins that are still vesting belonging to this account.
93    pub still_vesting: Coin,
94}
95
96/// Response containing vesting coins held in this contract
97#[cw_serde]
98pub struct VestingCoinsResponse {
99    /// The actual accounts, and their vesting coins, returned by the query.
100    pub accounts: Vec<AccountVestingCoins>,
101
102    /// Field indicating paging information for the following queries if the caller wishes to get further entries.
103    pub start_next_after: Option<Addr>,
104}
105
106/// Basic information regarding particular vesting account
107#[cw_serde]
108pub struct BaseVestingAccountInfo {
109    /// Id associated with this account
110    pub account_id: u32,
111
112    /// Address of this account's owner
113    pub owner: Addr,
114    // TODO: should this particular query/response expose anything else?
115}
116
117/// Response containing basic vesting account information
118#[cw_serde]
119pub struct AccountsResponse {
120    /// The actual accounts returned by the query.
121    pub accounts: Vec<BaseVestingAccountInfo>,
122
123    /// Field indicating paging information for the following queries if the caller wishes to get further entries.
124    pub start_next_after: Option<Addr>,
125}
126
127#[cfg(test)]
128mod test {
129    use cosmwasm_std::Uint128;
130    use nym_contracts_common::Percent;
131    use std::str::FromStr;
132
133    use crate::PledgeCap;
134
135    #[test]
136    fn test_pledge_cap_from_str() {
137        assert_eq!(
138            PledgeCap::from_str("0.1").unwrap(),
139            PledgeCap::Percent(Percent::from_percentage_value(10).unwrap())
140        );
141        assert_eq!(
142            PledgeCap::from_str("0,1").unwrap(),
143            PledgeCap::Percent(Percent::from_percentage_value(10).unwrap())
144        );
145        assert_eq!(
146            PledgeCap::from_str("100_000_000_000").unwrap(),
147            PledgeCap::Absolute(Uint128::new(100_000_000_000))
148        );
149        assert_eq!(
150            PledgeCap::from_str("100000000000").unwrap(),
151            PledgeCap::Absolute(Uint128::new(100_000_000_000))
152        );
153    }
154}