Utility native <> printable functions
This commit is contained in:
@@ -4,11 +4,11 @@
|
||||
)]
|
||||
|
||||
use bip39::Mnemonic;
|
||||
use cosmos_sdk::{AccountId, Coin, Decimal};
|
||||
use error::BackendError;
|
||||
use std::collections::HashMap;
|
||||
use std::str::FromStr;
|
||||
use validator_client::nymd::{NymdClient, SigningNymdClient};
|
||||
use cosmos_sdk::AccountId;
|
||||
|
||||
mod config;
|
||||
mod error;
|
||||
@@ -31,6 +31,76 @@ macro_rules! format_err {
|
||||
};
|
||||
}
|
||||
|
||||
fn printable_coin(coin: Option<Coin>) -> Result<String, String> {
|
||||
if let Some(coin) = coin {
|
||||
let amount = match native_to_printable(&coin.amount.to_string()) {
|
||||
Ok(amount) => amount,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let ticker = if coin.denom.to_string().starts_with("u") {
|
||||
coin.denom.to_string()[1..].to_uppercase()
|
||||
} else {
|
||||
coin.denom.to_string().to_uppercase()
|
||||
};
|
||||
Ok(format!("{} {}", amount, ticker))
|
||||
} else {
|
||||
Ok("0".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn printable_balance(balance: Option<Vec<Coin>>) -> Result<String, String> {
|
||||
if let Some(balance) = balance {
|
||||
if !balance.is_empty() {
|
||||
return Ok(
|
||||
balance
|
||||
.into_iter()
|
||||
.map(|coin| match printable_coin(Some(coin)) {
|
||||
Ok(native) => native,
|
||||
Err(e) => e,
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join(", "),
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok("-".to_string())
|
||||
}
|
||||
|
||||
// converts display amount, such as "12.0346" to its native token representation,
|
||||
// with 6 fractional digits. So in that case it would result in "12034600"
|
||||
// Basically does the same job as `displayAmountToNative` but without the requirement
|
||||
// of having the coinMap
|
||||
#[tauri::command]
|
||||
fn printable_balance_to_native(amount: &str) -> Result<String, String> {
|
||||
match amount.parse::<f64>() {
|
||||
Ok(f) => match Decimal::from_str(&(f * 1_000_000.).to_string()) {
|
||||
Ok(amount) => Ok(amount.to_string()),
|
||||
Err(e) => Err(format_err!(format!(
|
||||
"Could not convert `{}` to Decimal",
|
||||
amount
|
||||
))),
|
||||
},
|
||||
Err(e) => Err(format_err!(format!(
|
||||
"Could not convert `{}` to f64",
|
||||
amount
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn native_to_printable(native_value: &str) -> Result<String, String> {
|
||||
match Decimal::from_str(native_value) {
|
||||
Ok(decimal) => Ok(format!(
|
||||
"{}",
|
||||
decimal.to_string().parse::<f64>().unwrap() / 1_000_000.
|
||||
)),
|
||||
Err(e) => Err(format_err!(format!(
|
||||
"Could not convert `{}` to Decimal",
|
||||
native_value
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn connect_with_mnemonic(
|
||||
mnemonic: String,
|
||||
@@ -54,10 +124,7 @@ async fn connect_with_mnemonic(
|
||||
Err(e) => format_err!(e),
|
||||
},
|
||||
);
|
||||
ret.insert(
|
||||
"client_address",
|
||||
client.address().to_string()
|
||||
);
|
||||
ret.insert("client_address", client.address().to_string());
|
||||
ret.insert(
|
||||
"denom",
|
||||
match client.denom() {
|
||||
@@ -82,6 +149,7 @@ async fn get_balance(
|
||||
let mut balance = HashMap::new();
|
||||
balance.insert("amount", coin.amount.to_string());
|
||||
balance.insert("denom", coin.denom.to_string());
|
||||
balance.insert("printableBalance", printable_coin(Some(coin))?);
|
||||
Ok(balance)
|
||||
}
|
||||
Ok(None) => Err(format!(
|
||||
@@ -109,7 +177,12 @@ fn _connect_with_mnemonic(mnemonic: Mnemonic, config: &Config) -> NymdClient<Sig
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.manage(Arc::new(RwLock::new(State::default())))
|
||||
.invoke_handler(tauri::generate_handler![connect_with_mnemonic, get_balance])
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
connect_with_mnemonic,
|
||||
get_balance,
|
||||
printable_balance_to_native,
|
||||
native_to_printable
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import React, { useState } from 'react'
|
||||
import React, { useState } from "react";
|
||||
import {
|
||||
Button,
|
||||
Checkbox,
|
||||
@@ -9,20 +9,44 @@ import {
|
||||
ListItem,
|
||||
TextField,
|
||||
Theme,
|
||||
} from '@material-ui/core'
|
||||
import { useTheme } from '@material-ui/styles'
|
||||
import { DocEntry } from './DocEntry'
|
||||
} from "@material-ui/core";
|
||||
import { useTheme } from "@material-ui/styles";
|
||||
import { DocEntry } from "./DocEntry";
|
||||
|
||||
export const ApiList = () => {
|
||||
const [advancedShown, setAdvancedShown] = React.useState(false)
|
||||
const [advancedShown, setAdvancedShown] = React.useState(false);
|
||||
|
||||
const theme: Theme = useTheme()
|
||||
const theme: Theme = useTheme();
|
||||
|
||||
return (
|
||||
<List>
|
||||
<ListItem><DocEntry function={{ name: 'connect_with_mnemonic', args: [{ name: 'mnemonic', type: 'str' }] }} /></ListItem>
|
||||
<ListItem><DocEntry function={{ name: 'get_balance', args: [] }} /></ListItem>
|
||||
<ListItem>
|
||||
<DocEntry
|
||||
function={{
|
||||
name: "connect_with_mnemonic",
|
||||
args: [{ name: "mnemonic", type: "str" }],
|
||||
}}
|
||||
/>
|
||||
</ListItem>
|
||||
<ListItem>
|
||||
<DocEntry function={{ name: "get_balance", args: [] }} />
|
||||
</ListItem>
|
||||
<ListItem>
|
||||
<DocEntry
|
||||
function={{
|
||||
name: "printable_balance_to_native",
|
||||
args: [{ name: "amount", type: "str" }],
|
||||
}}
|
||||
/>
|
||||
</ListItem>
|
||||
<ListItem>
|
||||
<DocEntry
|
||||
function={{
|
||||
name: "native_to_printable",
|
||||
args: [{ name: "nativeValue", type: "str" }],
|
||||
}}
|
||||
/>
|
||||
</ListItem>
|
||||
</List>
|
||||
|
||||
)
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
+5860
-6989
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user