Further replacements of MajorCurrencyAmount into DecCoin

This commit is contained in:
Jędrzej Stuczyński
2022-06-06 10:33:41 +01:00
parent 81a206f6c0
commit b248f2e4b5
8 changed files with 107 additions and 85 deletions
@@ -4,6 +4,7 @@
use crate::nymd::Coin;
use cosmrs::tx;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
pub mod gas_price;
@@ -15,6 +16,12 @@ pub enum Fee {
Auto(Option<f32>),
}
impl Display for Fee {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
todo!()
}
}
impl Fee {
pub fn try_get_manual_amount(&self) -> Option<Vec<Coin>> {
match self {
+3
View File
@@ -34,6 +34,7 @@ pub type Denom = String;
#[serde(rename_all = "UPPERCASE")]
#[strum(serialize_all = "UPPERCASE")]
// TODO: this shouldn't be an enum...
#[deprecated]
pub enum CurrencyDenom {
#[strum(ascii_case_insensitive)]
Nym,
@@ -72,6 +73,7 @@ impl TryFrom<CosmosDenom> for CurrencyDenom {
ts(export_to = "ts-packages/types/src/types/rust/CurrencyStringMajorAmount.ts")
)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[deprecated]
pub struct MajorAmountString(String); // see https://github.com/Aleph-Alpha/ts-rs/issues/51 for exporting type aliases
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
@@ -80,6 +82,7 @@ pub struct MajorAmountString(String); // see https://github.com/Aleph-Alpha/ts-r
ts(export_to = "ts-packages/types/src/types/rust/Currency.ts")
)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[deprecated]
pub struct MajorCurrencyAmount {
pub amount: MajorAmountString,
pub denom: CurrencyDenom,
-3
View File
@@ -1,9 +1,6 @@
use crate::currency::MajorCurrencyAmount;
use crate::error::TypesError;
use cosmrs::tx::Gas as CosmrsGas;
use serde::{Deserialize, Serialize};
use validator_client::nymd::cosmwasm_client::types::GasInfo as ValidatorClientGasInfo;
use validator_client::nymd::GasPrice;
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
@@ -110,6 +110,7 @@ pub async fn update_mixnode(
fee: Option<Fee>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<TransactionExecuteResult, BackendError> {
let guard = state.read().await;
let fee_amount = guard.convert_tx_fee(fee.as_ref());
log::info!(
">>> Update mixnode: profit_margin_percent = {}, fee {:?}",
@@ -96,7 +96,7 @@ struct DelegationWithHistory {
pub async fn get_all_mix_delegations(
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<Vec<DelegationWithEverything>, BackendError> {
todo!("deal with this later :)")
todo!("deal with this later :)");
log::info!(">>> Get all mixnode delegations");
//
@@ -341,7 +341,7 @@ pub async fn get_delegator_rewards(
pub async fn get_delegation_summary(
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<DelegationsSummaryResponse, BackendError> {
todo!("also deal with later : )")
todo!("also deal with later : )");
log::info!(">>> Get delegation summary");
// let denom_minor = state.read().await.current_network().denom();
@@ -3,7 +3,7 @@ use crate::nymd_client;
use crate::state::State;
use crate::{Gateway, MixNode};
use nym_types::currency::MajorCurrencyAmount;
use nym_types::currency::DecCoin;
use nym_types::transaction::TransactionExecuteResult;
use std::sync::Arc;
use tokio::sync::RwLock;
@@ -12,28 +12,31 @@ use validator_client::nymd::{Fee, VestingSigningClient};
#[tauri::command]
pub async fn vesting_bond_gateway(
gateway: Gateway,
pledge: MajorCurrencyAmount,
pledge: DecCoin,
owner_signature: String,
fee: Option<Fee>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<TransactionExecuteResult, BackendError> {
let denom_minor = state.read().await.current_network().denom();
let pledge_minor = pledge.clone().into();
let guard = state.read().await;
let pledge_base = guard.attempt_convert_to_base_coin(pledge.clone())?;
let fee_amount = guard.convert_tx_fee(fee.as_ref());
log::info!(
">>> Bond gateway with locked tokens: identity_key = {}, pledge = {}, pledge_minor = {}, fee = {:?}",
gateway.identity_key,
pledge,
pledge_minor,
fee,
);
let res = nymd_client!(state)
.vesting_bond_gateway(gateway, &owner_signature, pledge_minor, fee)
">>> Bond gateway with locked tokens: identity_key = {}, pledge_display = {}, pledge_base = {}, fee = {:?}",
gateway.identity_key,
pledge,
pledge_base,
fee,
);
let res = guard
.current_client()?
.nymd
.vesting_bond_gateway(gateway, &owner_signature, pledge_base, fee)
.await?;
log::info!("<<< tx hash = {}", res.transaction_hash);
log::trace!("<<< {:?}", res);
Ok(TransactionExecuteResult::from_execute_result(
res,
denom_minor.as_ref(),
res, fee_amount,
)?)
}
@@ -42,7 +45,8 @@ pub async fn vesting_unbond_gateway(
fee: Option<Fee>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<TransactionExecuteResult, BackendError> {
let denom_minor = state.read().await.current_network().denom();
let guard = state.read().await;
let fee_amount = guard.convert_tx_fee(fee.as_ref());
log::info!(
">>> Unbond gateway bonded with locked tokens, fee = {:?}",
fee
@@ -51,8 +55,7 @@ pub async fn vesting_unbond_gateway(
log::info!("<<< tx hash = {}", res.transaction_hash);
log::trace!("<<< {:?}", res);
Ok(TransactionExecuteResult::from_execute_result(
res,
denom_minor.as_ref(),
res, fee_amount,
)?)
}
@@ -60,27 +63,30 @@ pub async fn vesting_unbond_gateway(
pub async fn vesting_bond_mixnode(
mixnode: MixNode,
owner_signature: String,
pledge: MajorCurrencyAmount,
pledge: DecCoin,
fee: Option<Fee>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<TransactionExecuteResult, BackendError> {
let denom_minor = state.read().await.current_network().denom();
let pledge_minor = pledge.clone().into();
let guard = state.read().await;
let pledge_base = guard.attempt_convert_to_base_coin(pledge.clone())?;
let fee_amount = guard.convert_tx_fee(fee.as_ref());
log::info!(
">>> Bond mixnode with locked tokens: identity_key = {}, pledge = {}, pledge_minor = {}, fee = {:?}",
">>> Bond mixnode with locked tokens: identity_key = {}, pledge_display = {}, pledge_base = {}, fee = {:?}",
mixnode.identity_key,
pledge,
pledge_minor,
pledge_base,
fee
);
let res = nymd_client!(state)
.vesting_bond_mixnode(mixnode, &owner_signature, pledge_minor, fee)
let res = guard
.current_client()?
.nymd
.vesting_bond_mixnode(mixnode, &owner_signature, pledge_base, fee)
.await?;
log::info!("<<< tx hash = {}", res.transaction_hash);
log::trace!("<<< {:?}", res);
Ok(TransactionExecuteResult::from_execute_result(
res,
denom_minor.as_ref(),
res, fee_amount,
)?)
}
@@ -89,42 +95,49 @@ pub async fn vesting_unbond_mixnode(
fee: Option<Fee>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<TransactionExecuteResult, BackendError> {
let denom_minor = state.read().await.current_network().denom();
let guard = state.read().await;
let fee_amount = guard.convert_tx_fee(fee.as_ref());
log::info!(
">>> Unbond mixnode bonded with locked tokens, fee = {:?}",
fee
);
let res = nymd_client!(state).vesting_unbond_mixnode(fee).await?;
let res = guard
.current_client()?
.nymd
.vesting_unbond_mixnode(fee)
.await?;
log::info!("<<< tx hash = {}", res.transaction_hash);
log::trace!("<<< {:?}", res);
Ok(TransactionExecuteResult::from_execute_result(
res,
denom_minor.as_ref(),
res, fee_amount,
)?)
}
#[tauri::command]
pub async fn withdraw_vested_coins(
amount: MajorCurrencyAmount,
amount: DecCoin,
fee: Option<Fee>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<TransactionExecuteResult, BackendError> {
let denom_minor = state.read().await.current_network().denom();
let amount_minor = amount.clone().into();
let guard = state.read().await;
let amount_base = guard.attempt_convert_to_base_coin(amount.clone())?;
let fee_amount = guard.convert_tx_fee(fee.as_ref());
log::info!(
">>> Withdraw vested liquid coins: amount = {}, amount_minor = {}, fee = {:?}",
">>> Withdraw vested liquid coins: amount_base = {}, amount_base = {}, fee = {:?}",
amount,
amount_minor,
amount_base,
fee
);
let res = nymd_client!(state)
.withdraw_vested_coins(amount_minor, fee)
let res = guard
.current_client()?
.nymd
.withdraw_vested_coins(amount_base, fee)
.await?;
log::info!("<<< tx hash = {}", res.transaction_hash);
log::trace!("<<< {:?}", res);
Ok(TransactionExecuteResult::from_execute_result(
res,
denom_minor.as_ref(),
res, fee_amount,
)?)
}
@@ -134,19 +147,21 @@ pub async fn vesting_update_mixnode(
fee: Option<Fee>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<TransactionExecuteResult, BackendError> {
let denom_minor = state.read().await.current_network().denom();
let guard = state.read().await;
let fee_amount = guard.convert_tx_fee(fee.as_ref());
log::info!(
">>> Update mixnode bonded with locked tokens: profit_margin_percent = {}, fee = {:?}",
profit_margin_percent,
fee,
);
let res = nymd_client!(state)
let res = guard
.current_client()?
.nymd
.vesting_update_mixnode_config(profit_margin_percent, fee)
.await?;
log::info!("<<< tx hash = {}", res.transaction_hash);
log::trace!("<<< {:?}", res);
Ok(TransactionExecuteResult::from_execute_result(
res,
denom_minor.as_ref(),
res, fee_amount,
)?)
}
@@ -1,15 +1,12 @@
use std::sync::Arc;
use tokio::sync::RwLock;
use nym_types::currency::MajorCurrencyAmount;
use nym_types::delegation::{from_contract_delegation_events, DelegationEvent};
use nym_types::transaction::TransactionExecuteResult;
use validator_client::nymd::{Fee, VestingSigningClient};
use crate::error::BackendError;
use crate::nymd_client;
use crate::state::State;
use nym_types::currency::DecCoin;
use nym_types::delegation::{from_contract_delegation_events, DelegationEvent};
use nym_types::transaction::TransactionExecuteResult;
use std::sync::Arc;
use tokio::sync::RwLock;
use validator_client::nymd::{Fee, VestingSigningClient};
#[tauri::command]
pub async fn get_pending_vesting_delegation_events(
@@ -40,27 +37,30 @@ pub async fn get_pending_vesting_delegation_events(
#[tauri::command]
pub async fn vesting_delegate_to_mixnode(
identity: &str,
amount: MajorCurrencyAmount,
amount: DecCoin,
fee: Option<Fee>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<TransactionExecuteResult, BackendError> {
let denom_minor = state.read().await.current_network().denom();
let delegation = amount.clone().into();
let guard = state.read().await;
let delegation = guard.attempt_convert_to_base_coin(amount.clone())?;
let fee_amount = guard.convert_tx_fee(fee.as_ref());
log::info!(
">>> Delegate to mixnode with locked tokens: identity_key = {}, amount = {}, minor_amount = {}, fee = {:?}",
">>> Delegate to mixnode with locked tokens: identity_key = {}, amount_display = {}, amount_base = {}, fee = {:?}",
identity,
amount,
delegation,
fee
);
let res = nymd_client!(state)
let res = guard
.current_client()?
.nymd
.vesting_delegate_to_mixnode(identity, delegation, fee)
.await?;
log::info!("<<< tx hash = {}", res.transaction_hash);
log::trace!("<<< {:?}", res);
Ok(TransactionExecuteResult::from_execute_result(
res,
denom_minor.as_ref(),
res, fee_amount,
)?)
}
@@ -70,19 +70,21 @@ pub async fn vesting_undelegate_from_mixnode(
fee: Option<Fee>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<TransactionExecuteResult, BackendError> {
let denom_minor = state.read().await.current_network().denom();
let guard = state.read().await;
let fee_amount = guard.convert_tx_fee(fee.as_ref());
log::info!(
">>> Undelegate from mixnode delegated with locked tokens: identity_key = {}, fee = {:?}",
identity,
fee,
);
let res = nymd_client!(state)
let res = guard
.current_client()?
.nymd
.vesting_undelegate_from_mixnode(identity, fee)
.await?;
log::info!("<<< tx hash = {}", res.transaction_hash);
log::trace!("<<< {:?}", res);
Ok(TransactionExecuteResult::from_execute_result(
res,
denom_minor.as_ref(),
res, fee_amount,
)?)
}
@@ -1,23 +1,20 @@
use std::sync::Arc;
use cosmwasm_std::Timestamp;
use tokio::sync::RwLock;
use nym_types::currency::MajorCurrencyAmount;
use nym_types::vesting::VestingAccountInfo;
use nym_types::vesting::{OriginalVestingResponse, PledgeData};
use validator_client::nymd::VestingQueryClient;
use vesting_contract_common::Period;
use crate::error::BackendError;
use crate::nymd_client;
use crate::state::State;
use cosmwasm_std::Timestamp;
use nym_types::currency::DecCoin;
use nym_types::vesting::VestingAccountInfo;
use nym_types::vesting::{OriginalVestingResponse, PledgeData};
use std::sync::Arc;
use tokio::sync::RwLock;
use validator_client::nymd::VestingQueryClient;
use vesting_contract_common::Period;
#[tauri::command]
pub async fn locked_coins(
block_time: Option<u64>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<MajorCurrencyAmount, BackendError> {
) -> Result<DecCoin, BackendError> {
log::info!(">>> Query locked coins");
let res = nymd_client!(state)
.locked_coins(
@@ -34,7 +31,7 @@ pub async fn locked_coins(
pub async fn spendable_coins(
block_time: Option<u64>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<MajorCurrencyAmount, BackendError> {
) -> Result<DecCoin, BackendError> {
log::info!(">>> Query spendable coins");
let res = nymd_client!(state)
.spendable_coins(
@@ -52,7 +49,7 @@ pub async fn vested_coins(
vesting_account_address: &str,
block_time: Option<u64>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<MajorCurrencyAmount, BackendError> {
) -> Result<DecCoin, BackendError> {
log::info!(">>> Query vested coins");
let res = nymd_client!(state)
.vested_coins(
@@ -70,7 +67,7 @@ pub async fn vesting_coins(
vesting_account_address: &str,
block_time: Option<u64>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<MajorCurrencyAmount, BackendError> {
) -> Result<DecCoin, BackendError> {
log::info!(">>> Query vesting coins");
let res = nymd_client!(state)
.vesting_coins(
@@ -130,7 +127,7 @@ pub async fn delegated_free(
vesting_account_address: &str,
block_time: Option<u64>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<MajorCurrencyAmount, BackendError> {
) -> Result<DecCoin, BackendError> {
log::info!(">>> Query delegated free");
let res = nymd_client!(state)
.delegated_free(
@@ -149,7 +146,7 @@ pub async fn delegated_vesting(
block_time: Option<u64>,
vesting_account_address: &str,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<MajorCurrencyAmount, BackendError> {
) -> Result<DecCoin, BackendError> {
log::info!(">>> Query delegated vesting");
let res = nymd_client!(state)
.delegated_vesting(