From 1d0ccb1c30364927d2df82eebdefc9bfefccbcd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20Str=C3=B6m?= Date: Tue, 18 Oct 2016 19:13:43 +0200 Subject: [PATCH] Move format_ether into transaction_request.rs --- rpc/src/v1/types/transaction_request.rs | 37 +++++++++++++++++++++---- rpc/src/v1/types/uint.rs | 13 --------- rpc_cli/src/lib.rs | 2 +- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/rpc/src/v1/types/transaction_request.rs b/rpc/src/v1/types/transaction_request.rs index b71902043..492f2354d 100644 --- a/rpc/src/v1/types/transaction_request.rs +++ b/rpc/src/v1/types/transaction_request.rs @@ -42,17 +42,33 @@ pub struct TransactionRequest { pub nonce: Option, } +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"); + } } diff --git a/rpc/src/v1/types/uint.rs b/rpc/src/v1/types/uint.rs index b92498994..60aae8f1d 100644 --- a/rpc/src/v1/types/uint.rs +++ b/rpc/src/v1/types/uint.rs @@ -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) diff --git a/rpc_cli/src/lib.rs b/rpc_cli/src/lib.rs index 0fd90e333..7461e8abd 100644 --- a/rpc_cli/src/lib.rs +++ b/rpc_cli/src/lib.rs @@ -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;