Merge branch 'tauri-wallet' into tauri-wallet-frontend

rust updates
This commit is contained in:
fmtabbara
2021-09-03 12:13:28 +01:00
4 changed files with 242 additions and 4 deletions
+2 -1
View File
@@ -6,8 +6,9 @@ use cosmwasm_std::{coin, Addr, Coin};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use ts_rs::TS;
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema)]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema, TS)]
pub struct Gateway {
pub host: String,
pub mix_port: u16,
+154 -3
View File
@@ -10,7 +10,7 @@ use cosmos_sdk::Denom as CosmosDenom;
use cosmos_sdk::{AccountId, Decimal};
use cosmwasm_std::Coin as CosmWasmCoin;
use error::BackendError;
use mixnet_contract::MixNode;
use mixnet_contract::{Gateway, MixNode};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
@@ -310,6 +310,149 @@ async fn bond_mixnode(
}
}
#[tauri::command]
async fn delegate_to_mixnode(
identity: String,
amount: Coin,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> 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<RwLock<State>>>,
) -> 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<RwLock<State>>>,
) -> 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<RwLock<State>>>,
) -> 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,
bond: Coin,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<(), String> {
let r_state = state.read().await;
let bond: CosmWasmCoin = match bond.try_into() {
Ok(b) => b,
Err(e) => return Err(format_err!(e)),
};
if let Some(client) = &r_state.signing_client {
match client.bond_gateway(gateway, 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 unbond_gateway(state: tauri::State<'_, Arc<RwLock<State>>>) -> Result<(), String> {
let r_state = state.read().await;
if let Some(client) = &r_state.signing_client {
match client.unbond_gateway().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 send(address: &str, amount: Coin, memo: String, state: tauri::State<'_, Arc<RwLock<State>>>,) -> Result<(), String> {
let address = match AccountId::from_str(address) {
Ok(addy) => addy,
Err(e) => return Err(format_err!(e))
};
let amount: CosmosCoin = match amount.try_into() {
Ok(b) => b,
Err(e) => return Err(format_err!(e)),
};
let r_state = state.read().await;
if let Some(client) = &r_state.signing_client {
match client.send(&address, vec!(amount), memo).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",
))
}
}
fn _connect_with_mnemonic(mnemonic: Mnemonic, config: &Config) -> NymdClient<SigningNymdClient> {
match NymdClient::connect_with_mnemonic(
config.get_nymd_validator_url().unwrap(),
@@ -332,7 +475,14 @@ fn main() {
owns_gateway,
owns_mixnode,
bond_mixnode,
unbond_mixnode
unbond_mixnode,
bond_gateway,
unbond_gateway,
delegate_to_mixnode,
undelegate_from_mixnode,
delegate_to_gateway,
undelegate_from_gateway,
send
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
@@ -341,5 +491,6 @@ fn main() {
export! {
MixNode => "../src/types/rust/mixnode.ts",
Coin => "../src/types/rust/coin.ts",
Balance => "../src/types/rust/balance.ts"
Balance => "../src/types/rust/balance.ts",
Gateway => "../src/types/rust/gateway.ts"
}
@@ -59,6 +59,83 @@ export const ApiList = () => {
}}
/>
</ListItem>
<ListItem>
<DocEntry
function={{
name: "unbond_mixnode",
args: [],
}}
/>
</ListItem>
<ListItem>
<DocEntry
function={{
name: "bond_gateway",
args: [
{ name: "gateway", type: "object" },
{ name: "bond", type: "object" },
],
}}
/>
</ListItem>
<ListItem>
<DocEntry
function={{
name: "unbond_gateway",
args: [],
}}
/>
</ListItem>
<ListItem>
<DocEntry
function={{
name: "delegate_to_mixnode",
args: [
{ name: "identity", type: "str" },
{ name: "amount", type: "object" },
],
}}
/>
</ListItem>
<ListItem>
<DocEntry
function={{
name: "undelegate_from_mixnode",
args: [{ name: "identity", type: "str" }],
}}
/>
</ListItem>
<ListItem>
<DocEntry
function={{
name: "delegate_to_gateway",
args: [
{ name: "identity", type: "str" },
{ name: "amount", type: "object" },
],
}}
/>
</ListItem>
<ListItem>
<DocEntry
function={{
name: "undelegate_from_gateway",
args: [{ name: "identity", type: "str" }],
}}
/>
</ListItem>
<ListItem>
<DocEntry
function={{
name: "send",
args: [
{ name: "address", type: "str" },
{ name: "amount", type: "object" },
{ name: "memo", type: "str" },
],
}}
/>
</ListItem>
</List>
)
}
+9
View File
@@ -0,0 +1,9 @@
export interface Gateway {
host: string;
mix_port: number;
clients_port: number;
location: string;
sphinx_key: string;
identity_key: string;
version: string;
}