diff --git a/common/types/src/delegation.rs b/common/types/src/delegation.rs index 37c3eaa329..faadc16ab5 100644 --- a/common/types/src/delegation.rs +++ b/common/types/src/delegation.rs @@ -1,4 +1,5 @@ -use crate::currency::DecCoin; +use crate::currency::{DecCoin, RegisteredCoins}; +use crate::error::TypesError; use mixnet_contract_common::mixnode::DelegationEvent as ContractDelegationEvent; use mixnet_contract_common::mixnode::PendingUndelegate as ContractPendingUndelegate; use mixnet_contract_common::Delegation as MixnetContractDelegation; @@ -22,15 +23,15 @@ pub struct Delegation { impl Delegation { pub fn from_mixnet_contract( delegation: MixnetContractDelegation, - display_amount: DecCoin, - ) -> Self { - Delegation { + reg: &RegisteredCoins, + ) -> Result { + Ok(Delegation { owner: delegation.owner.to_string(), node_identity: delegation.node_identity, - amount: display_amount, + amount: reg.attempt_convert_to_display_dec_coin(delegation.amount.into())?, block_height: delegation.block_height, proxy: delegation.proxy.map(|d| d.to_string()), - } + }) } } @@ -81,20 +82,6 @@ pub struct DelegationResult { amount: Option, } -impl DelegationResult { - pub fn new( - source_address: &str, - target_address: &str, - amount: Option, - ) -> DelegationResult { - DelegationResult { - source_address: source_address.to_string(), - target_address: target_address.to_string(), - amount, - } - } -} - #[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] #[cfg_attr( feature = "generate-ts", @@ -121,23 +108,26 @@ pub struct DelegationEvent { } impl DelegationEvent { - pub fn from_mixnet_contract(event: ContractDelegationEvent, amount: Option) -> Self { - match event { + pub fn from_mixnet_contract( + event: ContractDelegationEvent, + reg: &RegisteredCoins, + ) -> Result { + Ok(match event { ContractDelegationEvent::Delegate(delegation) => DelegationEvent { kind: DelegationEventKind::Delegate, block_height: delegation.block_height, address: delegation.owner.into_string(), node_identity: delegation.node_identity, - amount, + amount: Some(reg.attempt_convert_to_display_dec_coin(delegation.amount.into())?), }, ContractDelegationEvent::Undelegate(pending_undelegate) => DelegationEvent { kind: DelegationEventKind::Undelegate, block_height: pending_undelegate.block_height(), address: pending_undelegate.delegate().into_string(), node_identity: pending_undelegate.mix_identity(), - amount, + amount: None, }, - } + }) } } diff --git a/common/types/src/mixnode.rs b/common/types/src/mixnode.rs index 052f86d199..bae233871c 100644 --- a/common/types/src/mixnode.rs +++ b/common/types/src/mixnode.rs @@ -71,7 +71,6 @@ pub struct MixNodeBond { impl MixNodeBond { pub fn from_mixnet_contract_mixnode_bond( bond: MixnetContractMixNodeBond, - // TODO: other conversion methods should also be using that reg: &RegisteredCoins, ) -> Result { let denom = bond.pledge_amount.denom.clone(); diff --git a/common/types/src/vesting.rs b/common/types/src/vesting.rs index 31f6370072..0c6b6a4c20 100644 --- a/common/types/src/vesting.rs +++ b/common/types/src/vesting.rs @@ -1,8 +1,10 @@ -use crate::currency::DecCoin; -use cosmwasm_std::Timestamp; +use crate::currency::{DecCoin, RegisteredCoins}; +use crate::error::TypesError; use serde::{Deserialize, Serialize}; -use vesting_contract::vesting::Account as VestingAccount; -use vesting_contract::vesting::VestingPeriod as VestingVestingPeriod; +use vesting_contract::vesting::Account as ContractVestingAccount; +use vesting_contract::vesting::VestingPeriod as ContractVestingPeriod; +use vesting_contract_common::OriginalVestingResponse as ContractOriginalVestingResponse; +use vesting_contract_common::PledgeData as ContractPledgeData; #[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))] #[cfg_attr( @@ -16,11 +18,14 @@ pub struct PledgeData { } impl PledgeData { - pub fn new(amount: DecCoin, block_time: Timestamp) -> Self { - PledgeData { - amount, - block_time: block_time.seconds(), - } + pub fn from_vesting_contract( + pledge: ContractPledgeData, + reg: &RegisteredCoins, + ) -> Result { + Ok(PledgeData { + amount: reg.attempt_convert_to_display_dec_coin(pledge.amount.into())?, + block_time: pledge.block_time.seconds(), + }) } } @@ -37,12 +42,15 @@ pub struct OriginalVestingResponse { } impl OriginalVestingResponse { - pub fn new(amount: DecCoin, number_of_periods: usize, period_duration: u64) -> Self { - OriginalVestingResponse { - amount, - number_of_periods, - period_duration, - } + pub fn from_vesting_contract( + res: ContractOriginalVestingResponse, + reg: &RegisteredCoins, + ) -> Result { + Ok(OriginalVestingResponse { + amount: reg.attempt_convert_to_display_dec_coin(res.amount.into())?, + number_of_periods: res.number_of_periods, + period_duration: res.period_duration, + }) } } @@ -61,14 +69,17 @@ pub struct VestingAccountInfo { } impl VestingAccountInfo { - pub fn new(amount: DecCoin, account: VestingAccount) -> Self { - VestingAccountInfo { + pub fn from_vesting_contract( + account: ContractVestingAccount, + reg: &RegisteredCoins, + ) -> Result { + Ok(VestingAccountInfo { owner_address: account.owner_address().to_string(), staking_address: account.staking_address().map(|a| a.to_string()), start_time: account.start_time().seconds(), periods: account.periods().into_iter().map(Into::into).collect(), - amount, - } + amount: reg.attempt_convert_to_display_dec_coin(account.coin.into())?, + }) } } @@ -83,8 +94,8 @@ pub struct VestingPeriod { period_seconds: u64, } -impl From for VestingPeriod { - fn from(period: VestingVestingPeriod) -> Self { +impl From for VestingPeriod { + fn from(period: ContractVestingPeriod) -> Self { Self { start_time: period.start_time, period_seconds: period.period_seconds, diff --git a/contracts/vesting/src/vesting/account/mod.rs b/contracts/vesting/src/vesting/account/mod.rs index b25628deea..5418619cfe 100644 --- a/contracts/vesting/src/vesting/account/mod.rs +++ b/contracts/vesting/src/vesting/account/mod.rs @@ -25,11 +25,11 @@ fn generate_storage_key(storage: &mut dyn Storage) -> Result #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] pub struct Account { - owner_address: Addr, - staking_address: Option, - start_time: Timestamp, - periods: Vec, - coin: Coin, + pub owner_address: Addr, + pub staking_address: Option, + pub start_time: Timestamp, + pub periods: Vec, + pub coin: Coin, storage_key: u32, } diff --git a/nym-wallet/src-tauri/src/operations/mixnet/delegate.rs b/nym-wallet/src-tauri/src/operations/mixnet/delegate.rs index ddfd4b00b7..7bc81283bd 100644 --- a/nym-wallet/src-tauri/src/operations/mixnet/delegate.rs +++ b/nym-wallet/src-tauri/src/operations/mixnet/delegate.rs @@ -20,6 +20,7 @@ pub async fn get_pending_delegation_events( ) -> Result, BackendError> { log::info!(">>> Get pending delegation events"); let guard = state.read().await; + let reg = guard.registered_coins()?; let client = guard.current_client()?; let events = client @@ -29,19 +30,10 @@ pub async fn get_pending_delegation_events( log::info!("<<< {} pending delegation events", events.len()); log::trace!("<<< pending delegation events = {:?}", events); - events + Ok(events .into_iter() - .map(|event| { - if let Some(amount) = event.delegation_amount() { - guard - .attempt_convert_to_display_dec_coin(amount.into()) - .map(Some) - } else { - Ok(None) - } - .map(|amount| DelegationEvent::from_mixnet_contract(event, amount)) - }) - .collect() + .map(|event| DelegationEvent::from_mixnet_contract(event, reg)) + .collect::>()?) } #[tauri::command] @@ -112,6 +104,7 @@ pub async fn get_all_mix_delegations( let guard = state.read().await; let client = guard.current_client()?; + let reg = guard.registered_coins()?; // TODO: add endpoint to validator API to get a single mix node bond let mixnodes = client.validator_api.get_mixnodes().await?; @@ -183,7 +176,7 @@ pub async fn get_all_mix_delegations( let entry = map .entry(d.node_identity.clone()) .or_insert(DelegationWithHistory { - delegation: Delegation::from_mixnet_contract(d, amount.clone()), + delegation: Delegation::from_mixnet_contract(d, reg)?, history: vec![], amount_sum: DecCoin::zero(display_mix_denom), }); diff --git a/nym-wallet/src-tauri/src/operations/vesting/delegate.rs b/nym-wallet/src-tauri/src/operations/vesting/delegate.rs index c9d827646d..e7374b7d31 100644 --- a/nym-wallet/src-tauri/src/operations/vesting/delegate.rs +++ b/nym-wallet/src-tauri/src/operations/vesting/delegate.rs @@ -14,6 +14,7 @@ pub async fn get_pending_vesting_delegation_events( log::info!(">>> Get pending delegations from vesting contract"); let guard = state.read().await; + let reg = guard.registered_coins()?; let client = &guard.current_client()?.nymd; let vesting_contract = client.vesting_contract_address()?; @@ -27,19 +28,10 @@ pub async fn get_pending_vesting_delegation_events( log::info!("<<< {} events", events.len()); log::trace!("<<< {:?}", events); - events + Ok(events .into_iter() - .map(|event| { - if let Some(amount) = event.delegation_amount() { - guard - .attempt_convert_to_display_dec_coin(amount.into()) - .map(Some) - } else { - Ok(None) - } - .map(|amount| DelegationEvent::from_mixnet_contract(event, amount)) - }) - .collect() + .map(|event| DelegationEvent::from_mixnet_contract(event, reg)) + .collect::>()?) } #[tauri::command] diff --git a/nym-wallet/src-tauri/src/operations/vesting/queries.rs b/nym-wallet/src-tauri/src/operations/vesting/queries.rs index f746d686f3..9117d5c86d 100644 --- a/nym-wallet/src-tauri/src/operations/vesting/queries.rs +++ b/nym-wallet/src-tauri/src/operations/vesting/queries.rs @@ -134,6 +134,7 @@ pub async fn original_vesting( ) -> Result { log::info!(">>> Query original vesting"); let guard = state.read().await; + let reg = guard.registered_coins()?; let res = guard .current_client()? @@ -141,11 +142,7 @@ pub async fn original_vesting( .original_vesting(vesting_account_address) .await?; - let res = OriginalVestingResponse::new( - guard.attempt_convert_to_display_dec_coin(res.amount.into())?, - res.number_of_periods, - res.period_duration, - ); + let res = OriginalVestingResponse::from_vesting_contract(res, reg)?; log::info!("<<< {:?}", res); Ok(res) } @@ -204,17 +201,14 @@ pub async fn vesting_get_mixnode_pledge( ) -> Result, BackendError> { log::info!(">>> Query vesting get mixnode pledge"); let guard = state.read().await; + let reg = guard.registered_coins()?; let res = guard .current_client()? .nymd .get_mixnode_pledge(address) .await? - .map(|pledge| { - guard - .attempt_convert_to_display_dec_coin(pledge.amount().into()) - .map(|amount| PledgeData::new(amount, pledge.block_time)) - }) + .map(|pledge| PledgeData::from_vesting_contract(pledge, reg)) .transpose()?; log::info!("<<< {:?}", res); @@ -228,17 +222,14 @@ pub async fn vesting_get_gateway_pledge( ) -> Result, BackendError> { log::info!(">>> Query vesting get gateway pledge"); let guard = state.read().await; + let reg = guard.registered_coins()?; let res = guard .current_client()? .nymd .get_gateway_pledge(address) .await? - .map(|pledge| { - guard - .attempt_convert_to_display_dec_coin(pledge.amount().into()) - .map(|amount| PledgeData::new(amount, pledge.block_time)) - }) + .map(|pledge| PledgeData::from_vesting_contract(pledge, reg)) .transpose()?; log::info!("<<< {:?}", res); @@ -265,13 +256,10 @@ pub async fn get_account_info( ) -> Result { log::info!(">>> Query account info"); let guard = state.read().await; + let res = guard.registered_coins()?; let vesting_account = guard.current_client()?.nymd.get_account(address).await?; - - let res = VestingAccountInfo::new( - guard.attempt_convert_to_display_dec_coin(vesting_account.coin().into())?, - vesting_account, - ); + let res = VestingAccountInfo::from_vesting_contract(vesting_account, res)?; log::info!("<<< {:?}", res); Ok(res)