More unification for conversion methods
This commit is contained in:
@@ -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<Self, TypesError> {
|
||||
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<DecCoin>,
|
||||
}
|
||||
|
||||
impl DelegationResult {
|
||||
pub fn new(
|
||||
source_address: &str,
|
||||
target_address: &str,
|
||||
amount: Option<DecCoin>,
|
||||
) -> 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<DecCoin>) -> Self {
|
||||
match event {
|
||||
pub fn from_mixnet_contract(
|
||||
event: ContractDelegationEvent,
|
||||
reg: &RegisteredCoins,
|
||||
) -> Result<Self, TypesError> {
|
||||
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,
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<MixNodeBond, TypesError> {
|
||||
let denom = bond.pledge_amount.denom.clone();
|
||||
|
||||
+32
-21
@@ -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<Self, TypesError> {
|
||||
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<Self, TypesError> {
|
||||
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<Self, TypesError> {
|
||||
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<VestingVestingPeriod> for VestingPeriod {
|
||||
fn from(period: VestingVestingPeriod) -> Self {
|
||||
impl From<ContractVestingPeriod> for VestingPeriod {
|
||||
fn from(period: ContractVestingPeriod) -> Self {
|
||||
Self {
|
||||
start_time: period.start_time,
|
||||
period_seconds: period.period_seconds,
|
||||
|
||||
@@ -25,11 +25,11 @@ fn generate_storage_key(storage: &mut dyn Storage) -> Result<u32, ContractError>
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
|
||||
pub struct Account {
|
||||
owner_address: Addr,
|
||||
staking_address: Option<Addr>,
|
||||
start_time: Timestamp,
|
||||
periods: Vec<VestingPeriod>,
|
||||
coin: Coin,
|
||||
pub owner_address: Addr,
|
||||
pub staking_address: Option<Addr>,
|
||||
pub start_time: Timestamp,
|
||||
pub periods: Vec<VestingPeriod>,
|
||||
pub coin: Coin,
|
||||
storage_key: u32,
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ pub async fn get_pending_delegation_events(
|
||||
) -> Result<Vec<DelegationEvent>, 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::<Result<_, _>>()?)
|
||||
}
|
||||
|
||||
#[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),
|
||||
});
|
||||
|
||||
@@ -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::<Result<_, _>>()?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
|
||||
@@ -134,6 +134,7 @@ pub async fn original_vesting(
|
||||
) -> Result<OriginalVestingResponse, BackendError> {
|
||||
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<Option<PledgeData>, 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<Option<PledgeData>, 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<VestingAccountInfo, BackendError> {
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user