Skip to content

Commit

Permalink
Update client to handle whole coins
Browse files Browse the repository at this point in the history
  • Loading branch information
jkilpatr committed Feb 13, 2021
1 parent 9c1df07 commit bdaa93b
Showing 1 changed file with 44 additions and 8 deletions.
52 changes: 44 additions & 8 deletions orchestrator/client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,29 @@ use deep_space::address::Address as CosmosAddress;
use deep_space::{coin::Coin, private_key::PrivateKey as CosmosPrivateKey};
use docopt::Docopt;
use ethereum_peggy::send_to_cosmos::send_to_cosmos;
use std::time::Duration;
use std::{time::Duration, u128};
use url::Url;
use web30::client::Web3;

const TIMEOUT: Duration = Duration::from_secs(60);

pub fn one_eth() -> f64 {
1000000000000000000f64
}

/// TODO revisit this for higher precision while
/// still representing the number to the user as a float
pub fn fraction_eth_to_wei(num: f64) -> Uint256 {
let mut res = num;
// in order to avoid floating point rounding issues we
// multiply only by 10 each time. this reduces the rounding
// errors enough to be ignored
for _ in 0..18 {
res *= 10f64
}
(res as u128).into()
}

#[derive(Debug, Deserialize)]
struct Args {
flag_cosmos_phrase: String,
Expand All @@ -35,7 +52,7 @@ struct Args {
flag_ethereum_rpc: String,
flag_contract_address: String,
flag_fees: String,
flag_amount: String,
flag_amount: f64,
flag_cosmos_destination: String,
flag_erc20_address: String,
flag_eth_destination: String,
Expand All @@ -57,11 +74,11 @@ lazy_static! {
--fees=<denom> The Cosmos Denom in which to pay Cosmos chain fees
--contract-address=<addr> The Ethereum contract address for Peggy, this is temporary
--erc20-address=<addr> An erc20 address to send funds
--amount=<amount> The amount of tokens to send
--amount=<amount> The amount of tokens to send, for example 1.5DAI
--cosmos-destination=<dest> A cosmos address to send tokens to
--eth-destination=<dest> A cosmos address to send tokens to
About:
Althea Peggy client software, moves tokens from Ethereum to Cosmos and back
Althea Gravity client software, moves tokens from Ethereum to Cosmos and back
Written By: {}
Version {}",
env!("CARGO_PKG_NAME"),
Expand All @@ -81,7 +98,7 @@ async fn main() {
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());

let amount: Uint256 = args.flag_amount.parse().unwrap();
let amount = fraction_eth_to_wei(args.flag_amount);
let erc20_address: EthAddress = args
.flag_erc20_address
.parse()
Expand Down Expand Up @@ -109,7 +126,10 @@ async fn main() {
};
let eth_dest: EthAddress = args.flag_eth_destination.parse().unwrap();

println!("Locking funds into the batch pool");
println!(
"Locking {} / {} into the batch pool",
args.flag_amount, erc20_address
);
send_to_eth(cosmos_key, eth_dest, amount, bridge_fee, &contact)
.await
.expect("Failed to Send to ETH");
Expand All @@ -136,8 +156,8 @@ async fn main() {
let ethereum_public_key = ethereum_key.to_public_key().unwrap();

println!(
"Sending to Cosmos from {} to {} with amount {}",
ethereum_public_key, cosmos_dest, amount
"Sending {} / {} to Cosmos from {} to {} with amount",
args.flag_amount, contract_address, ethereum_public_key, cosmos_dest
);
// we send some erc20 tokens to the peggy contract to register a deposit
let tx_id = send_to_cosmos(
Expand All @@ -155,3 +175,19 @@ async fn main() {
println!("Send to Cosmos txid: {:#066x}", tx_id);
}
}

#[test]
fn even_f32_rounding() {
let one_eth: Uint256 = 1000000000000000000u128.into();
let one_point_five_eth: Uint256 = 1500000000000000000u128.into();
let one_point_one_five_eth: Uint256 = 1150000000000000000u128.into();
let a_high_precision_number: Uint256 = 1150100000000000000u128.into();
let res = fraction_eth_to_wei(1f64);
assert_eq!(one_eth, res);
let res = fraction_eth_to_wei(1.5f64);
assert_eq!(one_point_five_eth, res);
let res = fraction_eth_to_wei(1.15f64);
assert_eq!(one_point_one_five_eth, res);
let res = fraction_eth_to_wei(1.1501f64);
assert_eq!(a_high_precision_number, res);
}

0 comments on commit bdaa93b

Please sign in to comment.