From b76802e6eb83fc14f40bdbbe30622fc8e57fee36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Thu, 18 Jul 2024 16:16:19 +0100 Subject: [PATCH] exposed tauri operations for vesting migrations --- nym-wallet/src-tauri/Cargo.toml | 2 +- nym-wallet/src-tauri/src/error.rs | 3 + nym-wallet/src-tauri/src/main.rs | 2 + .../src/operations/vesting/migrate.rs | 93 +++++++++++++++++++ .../src-tauri/src/operations/vesting/mod.rs | 1 + 5 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 nym-wallet/src-tauri/src/operations/vesting/migrate.rs diff --git a/nym-wallet/src-tauri/Cargo.toml b/nym-wallet/src-tauri/Cargo.toml index 75a4046264..8708665bd2 100644 --- a/nym-wallet/src-tauri/Cargo.toml +++ b/nym-wallet/src-tauri/Cargo.toml @@ -8,7 +8,7 @@ repository = "" default-run = "nym_wallet" edition = "2021" build = "src/build.rs" -rust-version = "1.58" +rust-version = "1.76" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/nym-wallet/src-tauri/src/error.rs b/nym-wallet/src-tauri/src/error.rs index e968f269d4..9d879c706e 100644 --- a/nym-wallet/src-tauri/src/error.rs +++ b/nym-wallet/src-tauri/src/error.rs @@ -155,6 +155,9 @@ pub enum BackendError { #[error("This command ({name}) has been removed. Please try to use {alternative} instead.")] RemovedCommand { name: String, alternative: String }, + + #[error("there aren't any vesting delegations to migrate")] + NoVestingDelegations, } impl Serialize for BackendError { diff --git a/nym-wallet/src-tauri/src/main.rs b/nym-wallet/src-tauri/src/main.rs index 148dcded2b..623ab63e45 100644 --- a/nym-wallet/src-tauri/src/main.rs +++ b/nym-wallet/src-tauri/src/main.rs @@ -129,6 +129,8 @@ fn main() { vesting::bond::withdraw_vested_coins, vesting::delegate::vesting_delegate_to_mixnode, vesting::delegate::vesting_undelegate_from_mixnode, + vesting::migrate::migrate_vested_mixnode, + vesting::migrate::migrate_vested_delegations, vesting::queries::get_account_info, vesting::queries::get_current_vesting_period, vesting::queries::locked_coins, diff --git a/nym-wallet/src-tauri/src/operations/vesting/migrate.rs b/nym-wallet/src-tauri/src/operations/vesting/migrate.rs new file mode 100644 index 0000000000..e5624cfbf4 --- /dev/null +++ b/nym-wallet/src-tauri/src/operations/vesting/migrate.rs @@ -0,0 +1,93 @@ +// Copyright 2024 - Nym Technologies SA +// SPDX-License-Identifier: Apache-2.0 + +use crate::error::BackendError; +use crate::nyxd_client; +use crate::state::WalletState; +use nym_mixnet_contract_common::ExecuteMsg; +use nym_types::transaction::TransactionExecuteResult; +use nym_validator_client::nyxd::contract_traits::{ + MixnetSigningClient, NymContractsProvider, PagedMixnetQueryClient, +}; +use nym_validator_client::nyxd::Fee; + +#[tauri::command] +pub async fn migrate_vested_mixnode( + fee: Option, + state: tauri::State<'_, WalletState>, +) -> Result { + let guard = state.read().await; + let fee_amount = guard.convert_tx_fee(fee.as_ref()); + log::info!(">>> migrate vested mixnode, fee = {fee:?}"); + + let res = nyxd_client!(state).migrate_vested_mixnode(fee).await?; + log::info!("<<< tx hash = {}", res.transaction_hash); + log::trace!("<<< {:?}", res); + Ok(TransactionExecuteResult::from_execute_result( + res, fee_amount, + )?) +} + +#[tauri::command] +pub async fn migrate_vested_delegations( + state: tauri::State<'_, WalletState>, +) -> Result { + log::info!(">>> migrate vested delegations"); + + let guard = state.read().await; + let client = guard.current_client()?; + + let address = client.nyxd.address(); + let mixnet_contract = client + .nyxd + .mixnet_contract_address() + .expect("unavailable mixnet contract address"); + + log::info!(" >>> Get delegations"); + let delegations = client + .nyxd + .get_all_delegator_delegations(&address) + .await + .inspect_err(|err| { + log::error!(" <<< Failed to get delegations. Error: {}", err); + })?; + log::info!(" <<< {} delegations", delegations.len()); + + let vesting_delegations = delegations + .into_iter() + .filter(|d| d.proxy.is_some()) + .collect::>(); + + log::info!(" <<< {} vesting delegations", vesting_delegations.len()); + + if vesting_delegations.is_empty() { + return Err(BackendError::NoVestingDelegations); + } + + let mut migrate_msgs = Vec::new(); + for delegation in &vesting_delegations { + migrate_msgs.push(( + ExecuteMsg::MigrateVestedDelegation { + mix_id: delegation.mix_id, + }, + Vec::new(), + )); + } + + let res = client + .nyxd + .execute_multiple( + mixnet_contract, + migrate_msgs, + None, + format!( + "migrating {} vesting delegations", + vesting_delegations.len() + ), + ) + .await?; + + log::info!("<<< tx hash = {}", res.transaction_hash); + log::trace!("<<< {:?}", res); + Ok(TransactionExecuteResult::from_execute_result(res, None)?) +} diff --git a/nym-wallet/src-tauri/src/operations/vesting/mod.rs b/nym-wallet/src-tauri/src/operations/vesting/mod.rs index fb80d4ed43..7ed3d0f0dc 100644 --- a/nym-wallet/src-tauri/src/operations/vesting/mod.rs +++ b/nym-wallet/src-tauri/src/operations/vesting/mod.rs @@ -1,4 +1,5 @@ pub mod bond; pub mod delegate; +pub mod migrate; pub mod queries; pub mod rewards;