A Rhai scripting engine plugin providing EVM token denomination helpers and primitive type conversions between alloy-primitives and num_bigint::BigInt.
Working with EVM-based applications requires handling high-precision integers (e.g., 1 ETH =
rhai-evm complements rhai-bigint by providing:
- Denomination Helpers: Ergonomic functions like
ether(),gwei(), andusdc()to handle scaling and precision. - Type Conversions: Lossless conversion from
alloy-primitivestypes (U256,I256) intoBigIntvalues.
- Denomination Constructors:
ether,gwei,wei,usdc,usdt,wbtc, and genericdecimals. - Hashing:
keccak256(string)returns a0x-prefixed hex string of the Keccak-256 digest. - Address Utilities:
is_addressvalidates an address string;to_checksumreturns its EIP-55 checksum form. - Alloy Interop: Convert
U256andI256directly to RhaiDynamicvalues.
cargo add rhai-evm rhai-bigintAdd the following to your Cargo.toml:
[dependencies]
rhai = "1.22.2"
rhai-bigint = "0.1.0"
rhai-evm = "0.1.0"sync: Enablesrhai/syncandrhai-bigint/syncsupport. Turn this on if your Rhai engine requires thread-safe types (e.g., when evaluating scripts across a Tokio thread pool).
Using the plugin requires registering both the BigIntPackage (which provides the math operations) and the EvmPackage (which provides the denomination helpers).
use rhai::{Engine, packages::Package};
use rhai_bigint::BigIntPackage;
use rhai_evm::EvmPackage;
fn main() {
let mut engine = Engine::new();
// Register BOTH packages into the engine
BigIntPackage::new().register_into_engine(&mut engine);
EvmPackage::new().register_into_engine(&mut engine);
let script = r#"
let price = ether(1.5); // 1.5 ETH in Wei
let gas = gwei(30); // 30 Gwei in Wei
let threshold = parse_bigint("1000000000");
price > gas && price > threshold
"#;
let result: bool = engine.eval(script).unwrap();
assert!(result);
}let amount = ether(1.23); // 1230000000000000000
let small = gwei(10); // 10000000000
let stable = usdc(500); // 500000000
let custom = decimals(1.5, 2); // 150let hash = keccak256("hello"); // "0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8"is_address("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045") // true
is_address("not-an-address") // false
let addr = to_checksum("0xd8da6bf26964af9d7eed9e03e53415d37aa96045");
// "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"use alloy_primitives::U256;
use rhai::{Engine, Scope};
use rhai_evm::u256_to_bigint_dynamic;
let mut engine = Engine::new();
let mut scope = Scope::new();
let balance = U256::from(1000000000000000000u128);
scope.push("balance", u256_to_bigint_dynamic(balance));- MIT license (http://opensource.org/licenses/MIT)