gleeth/wei
Convert between human-readable values (ETH, gwei, integers) and the
0x-prefixed hex strings used by the Ethereum JSON-RPC wire format.
All RPC methods in gleeth accept and return hex strings. This module bridges the gap so users don’t have to construct hex manually.
Examples
let assert Ok(hex) = wei.from_ether("1.0")
// hex = "0xde0b6b3a7640000"
let assert Ok(eth) = wei.to_ether("0xde0b6b3a7640000")
// eth = "1.0"
Values
pub fn from_ether(ether: String) -> Result(String, String)
Convert an ether amount string to a wei hex string. Handles integers (“1”, “100”) and decimals (“1.5”, “0.001”).
Examples
wei.from_ether("1.0") // -> Ok("0xde0b6b3a7640000")
wei.from_ether("0.5") // -> Ok("0x6f05b59d3b20000")
wei.from_ether("0") // -> Ok("0x0")
pub fn from_gwei(gwei: String) -> Result(String, String)
Convert a gwei amount string to a wei hex string.
Examples
wei.from_gwei("1.0") // -> Ok("0x3b9aca00")
wei.from_gwei("20") // -> Ok("0x4a817c800")
pub fn from_int(value: Int) -> String
Convert a decimal integer to a hex string.
Examples
wei.from_int(21000) // -> "0x5208"
wei.from_int(0) // -> "0x0"
pub fn to_ether(wei_hex: String) -> Result(String, String)
Convert a wei hex string to an ether decimal string.
Examples
wei.to_ether("0xde0b6b3a7640000") // -> Ok("1.0")
wei.to_ether("0x0") // -> Ok("0.0")
pub fn to_gwei(wei_hex: String) -> Result(String, String)
Convert a wei hex string to a gwei decimal string.
Examples
wei.to_gwei("0x3b9aca00") // -> Ok("1.0")