From 01a430588340e12b4e8371a803294fddea97dd29 Mon Sep 17 00:00:00 2001 From: Drazen Urch Date: Fri, 3 Sep 2021 07:33:11 +0200 Subject: [PATCH] Delegation --- tauri-wallet/src-tauri/src/main.rs | 86 ++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/tauri-wallet/src-tauri/src/main.rs b/tauri-wallet/src-tauri/src/main.rs index e1bdd12199..558ec2bd19 100644 --- a/tauri-wallet/src-tauri/src/main.rs +++ b/tauri-wallet/src-tauri/src/main.rs @@ -310,6 +310,88 @@ async fn bond_mixnode( } } +#[tauri::command] +async fn delegate_to_mixnode( + identity: String, + amount: Coin, + state: tauri::State<'_, Arc>>, +) -> Result<(), String> { + let r_state = state.read().await; + let bond: CosmWasmCoin = match amount.try_into() { + Ok(b) => b, + Err(e) => return Err(format_err!(e)), + }; + if let Some(client) = &r_state.signing_client { + match client.delegate_to_mixnode(identity, bond).await { + Ok(_result) => Ok(()), + Err(e) => Err(format_err!(e)), + } + } else { + Err(String::from( + "Client has not been initialized yet, connect with mnemonic to initialize", + )) + } +} + +#[tauri::command] +async fn undelegate_from_mixnode( + identity: String, + state: tauri::State<'_, Arc>>, +) -> Result<(), String> { + let r_state = state.read().await; + if let Some(client) = &r_state.signing_client { + match client.remove_mixnode_delegation(identity).await { + Ok(_result) => Ok(()), + Err(e) => Err(format_err!(e)), + } + } else { + Err(String::from( + "Client has not been initialized yet, connect with mnemonic to initialize", + )) + } +} + +#[tauri::command] +async fn delegate_to_gateway( + identity: String, + amount: Coin, + state: tauri::State<'_, Arc>>, +) -> Result<(), String> { + let r_state = state.read().await; + let bond: CosmWasmCoin = match amount.try_into() { + Ok(b) => b, + Err(e) => return Err(format_err!(e)), + }; + if let Some(client) = &r_state.signing_client { + match client.delegate_to_gateway(identity, bond).await { + Ok(_result) => Ok(()), + Err(e) => Err(format_err!(e)), + } + } else { + Err(String::from( + "Client has not been initialized yet, connect with mnemonic to initialize", + )) + } +} + +#[tauri::command] +async fn undelegate_from_gateway( + identity: String, + state: tauri::State<'_, Arc>>, +) -> Result<(), String> { + let r_state = state.read().await; + if let Some(client) = &r_state.signing_client { + match client.remove_gateway_delegation(identity).await { + Ok(_result) => Ok(()), + Err(e) => Err(format_err!(e)), + } + } else { + Err(String::from( + "Client has not been initialized yet, connect with mnemonic to initialize", + )) + } +} + #[tauri::command] async fn bond_gateway( gateway: Gateway, @@ -373,6 +455,10 @@ fn main() { unbond_mixnode, bond_gateway, unbond_gateway, + delegate_to_mixnode, + undelegate_from_mixnode, + delegate_to_gateway, + undelegate_from_gateway ]) .run(tauri::generate_context!()) .expect("error while running tauri application");