Move format_ether into transaction_request.rs
This commit is contained in:
parent
a7e10cebbd
commit
1d0ccb1c30
@ -42,17 +42,33 @@ pub struct TransactionRequest {
|
||||
pub nonce: Option<U256>,
|
||||
}
|
||||
|
||||
pub fn format_ether(i: U256) -> String {
|
||||
let mut string = format!("{}", i);
|
||||
let idx = string.len() as isize - 18;
|
||||
if idx <= 0 {
|
||||
let mut prefix = String::from("0.");
|
||||
for _ in 0..idx.abs() {
|
||||
prefix.push('0');
|
||||
}
|
||||
string = prefix + &string;
|
||||
} else {
|
||||
string.insert(idx as usize, '.');
|
||||
}
|
||||
String::from(string.trim_right_matches('0')
|
||||
.trim_right_matches('.'))
|
||||
}
|
||||
|
||||
impl fmt::Display for TransactionRequest {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let eth = self.value.unwrap_or(U256::from(0));
|
||||
match self.to {
|
||||
Some(ref to) => write!(f, "{} Ether from {:?} to {:?}",
|
||||
eth.format_ether(),
|
||||
self.from,
|
||||
to),
|
||||
format_ether(eth),
|
||||
self.from,
|
||||
to),
|
||||
None => write!(f, "{} Ether from {:?}",
|
||||
eth.format_ether(),
|
||||
self.from),
|
||||
format_ether(eth),
|
||||
self.from),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -208,4 +224,15 @@ mod tests {
|
||||
|
||||
assert!(deserialized.is_err(), "Should be error because to is empty");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_ether() {
|
||||
assert_eq!(&format_ether(U256::from(1000000000000000000u64)), "1");
|
||||
assert_eq!(&format_ether(U256::from(500000000000000000u64)), "0.5");
|
||||
assert_eq!(&format_ether(U256::from(50000000000000000u64)), "0.05");
|
||||
assert_eq!(&format_ether(U256::from(5000000000000000u64)), "0.005");
|
||||
assert_eq!(&format_ether(U256::from(2000000000000000000u64)), "2");
|
||||
assert_eq!(&format_ether(U256::from(2500000000000000000u64)), "2.5");
|
||||
assert_eq!(&format_ether(U256::from(10000000000000000000u64)), "10");
|
||||
}
|
||||
}
|
||||
|
@ -48,19 +48,6 @@ macro_rules! impl_uint {
|
||||
}
|
||||
}
|
||||
|
||||
impl $name {
|
||||
/// Human readable formatting
|
||||
pub fn format_ether(&self) -> String {
|
||||
let divisor = $other::from(10u64.pow(18));
|
||||
let ether = self.0 / divisor;
|
||||
let rest = self.0 - ether * divisor;
|
||||
let string = format!("{}.{}", ether, rest);
|
||||
string.trim_right_matches('0')
|
||||
.trim_right_matches('.')
|
||||
.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for $name {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
|
@ -8,7 +8,7 @@ extern crate rpassword;
|
||||
extern crate parity_rpc_client as client;
|
||||
|
||||
use rpc::v1::types::{U256, ConfirmationRequest};
|
||||
use client::signer::SignerRpc;
|
||||
use client::signer_client::SignerRpc;
|
||||
use std::io::{Write, BufRead, BufReader, stdout, stdin};
|
||||
use std::path::PathBuf;
|
||||
use std::fs::File;
|
||||
|
Loading…
Reference in New Issue
Block a user