-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client commands: cosmos-to-eth and eth-to-cosmos should use amount ve…
…rbatim (#132) * gravity-client {cosmos-to-eth,eth-to-cosmos} commands should _not_ manipulate the amount provided by the user * burn off duplicate fraction_to_exponent
- Loading branch information
Showing
4 changed files
with
16 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,3 @@ | ||
use clarity::Address as EthAddress; | ||
use clarity::Uint256; | ||
use std::{time::Duration, u128}; | ||
use web30::{client::Web3, jsonrpc::error::Web3Error}; | ||
use std::{time::Duration}; | ||
|
||
pub const TIMEOUT: Duration = Duration::from_secs(60); | ||
|
||
/// TODO revisit this for higher precision while | ||
/// still representing the number to the user as a float | ||
/// this takes a number like 0.37 eth and turns it into wei | ||
/// or any erc20 with arbitrary decimals | ||
pub fn fraction_to_exponent(num: f64, exponent: u8) -> 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..exponent { | ||
res *= 10f64 | ||
} | ||
(res as u128).into() | ||
} | ||
|
||
pub async fn get_erc20_decimals( | ||
web3: &Web3, | ||
erc20: EthAddress, | ||
caller_address: EthAddress, | ||
) -> Result<Uint256, Web3Error> { | ||
let decimals = web3 | ||
.contract_call(erc20, "decimals()", &[], caller_address, None) | ||
.await?; | ||
|
||
Ok(Uint256::from_bytes_be(match decimals.get(0..32) { | ||
Some(val) => val, | ||
None => { | ||
return Err(Web3Error::ContractCallError( | ||
"Bad response from ERC20 decimals".to_string(), | ||
)) | ||
} | ||
})) | ||
} | ||
pub const TIMEOUT: Duration = Duration::from_secs(60); |