diff --git a/crates/dev-utils/examples/transfer_eth.rs b/crates/dev-utils/examples/transfer_eth.rs index 1dd68f60..ed45d5a7 100644 --- a/crates/dev-utils/examples/transfer_eth.rs +++ b/crates/dev-utils/examples/transfer_eth.rs @@ -1,6 +1,6 @@ use alloy::{ - network::TransactionBuilder, primitives::Address, primitives::U256, providers::Provider, - rpc::types::TransactionRequest, + network::TransactionBuilder, primitives::utils::format_ether, primitives::Address, + primitives::U256, providers::Provider, rpc::types::TransactionRequest, }; use clap::Parser; use eyre::Result; @@ -49,13 +49,13 @@ async fn main() -> Result<()> { println!( "Sender's ETH balance before transaction: {} ETH", - balance_before / U256::from(10u64.pow(18)) + format_ether(balance_before) ); let balance_after = wallet.provider.get_balance(to).await?; println!( "Receiver's ETH balance after transaction: {} ETH", - alloy::primitives::utils::format_ether(balance_after) + format_ether(balance_after) ); Ok(()) diff --git a/crates/worker/src/cli/command.rs b/crates/worker/src/cli/command.rs index 7ed1b3a3..78161d0e 100644 --- a/crates/worker/src/cli/command.rs +++ b/crates/worker/src/cli/command.rs @@ -15,6 +15,7 @@ use crate::services::discovery::DiscoveryService; use crate::services::discovery_updater::DiscoveryUpdater; use crate::state::system_state::SystemState; use crate::TaskHandles; +use alloy::primitives::utils::format_ether; use alloy::primitives::U256; use alloy::signers::local::PrivateKeySigner; use alloy::signers::Signer; @@ -528,10 +529,7 @@ pub async fn execute_command( std::process::exit(1); } }; - Console::info( - "Required stake", - &format!("{}", required_stake / U256::from(10u128.pow(18))), - ); + Console::info("Required stake", &format_ether(required_stake).to_string()); if let Err(e) = provider_ops .retry_register_provider( @@ -595,8 +593,8 @@ pub async fn execute_command( "Provider stake is less than required stake", &format!( "Required: {} tokens, Current: {} tokens", - required_stake / U256::from(10u128.pow(18)), - provider_stake / U256::from(10u128.pow(18)) + format_ether(required_stake), + format_ether(provider_stake) ), ); @@ -885,7 +883,7 @@ pub async fn execute_command( .await .unwrap(); - let format_balance = format!("{}", provider_balance / U256::from(10u128.pow(18))); + let format_balance = format_ether(provider_balance).to_string(); println!("Provider balance: {format_balance}"); Ok(()) diff --git a/crates/worker/src/operations/provider.rs b/crates/worker/src/operations/provider.rs index 2ee489a0..2e87b921 100644 --- a/crates/worker/src/operations/provider.rs +++ b/crates/worker/src/operations/provider.rs @@ -1,4 +1,5 @@ use crate::console::Console; +use alloy::primitives::utils::format_ether; use alloy::primitives::{Address, U256}; use log::error; use shared::web3::contracts::core::builder::Contracts; @@ -69,20 +70,20 @@ impl ProviderOperations { match stake_manager.get_stake(provider_address).await { Ok(stake) => { if first_check || stake != last_stake { - Console::info("🔄 Chain Sync - Provider stake", &format!("{}", stake / U256::from(10u128.pow(18)))); + Console::info("🔄 Chain Sync - Provider stake", &format_ether(stake)); if !first_check { if stake < last_stake { Console::warning(&format!("Stake decreased - possible slashing detected: From {} to {}", - last_stake / U256::from(10u128.pow(18)), - stake / U256::from(10u128.pow(18)) + format_ether(last_stake), + format_ether(stake) )); if stake == U256::ZERO { Console::warning("Stake is 0 - you might have to restart the node to increase your stake (if you still have balance left)"); } } else { Console::info("🔄 Chain Sync - Stake changed", &format!("From {} to {}", - last_stake / U256::from(10u128.pow(18)), - stake / U256::from(10u128.pow(18)) + format_ether(last_stake), + format_ether(stake) )); } } @@ -100,11 +101,11 @@ impl ProviderOperations { match contracts.ai_token.balance_of(provider_address).await { Ok(balance) => { if first_check || balance != last_balance { - Console::info("🔄 Chain Sync - Balance", &format!("{}", balance / U256::from(10u128.pow(18)))); + Console::info("🔄 Chain Sync - Balance", &format_ether(balance)); if !first_check { Console::info("🔄 Chain Sync - Balance changed", &format!("From {} to {}", - last_balance / U256::from(10u128.pow(18)), - balance / U256::from(10u128.pow(18)) + format_ether(last_balance), + format_ether(balance) )); } last_balance = balance; @@ -222,24 +223,21 @@ impl ProviderOperations { let provider_exists = self.check_provider_exists().await?; if !provider_exists { - Console::info( - "Balance", - &format!("{}", balance / U256::from(10u128.pow(18))), - ); + Console::info("Balance", &format_ether(balance)); Console::info( "ETH Balance", - &format!("{:.6} ETH", { f64::from(eth_balance) / 10f64.powf(18.0) }), + &format!("{} ETH", format_ether(U256::from(eth_balance))), ); if balance < stake { Console::user_error(&format!( "Insufficient balance for stake: {}", - stake / U256::from(10u128.pow(18)) + format_ether(stake) )); return Err(ProviderError::InsufficientBalance); } if !self.prompt_user_confirmation(&format!( "Do you want to approve staking {}?", - stake.to_string().parse::().unwrap_or(0.0) / 10f64.powf(18.0) + format_ether(stake) )) { Console::info("Operation cancelled by user", "Staking approval declined"); return Err(ProviderError::UserCancelled); @@ -273,24 +271,21 @@ impl ProviderOperations { let provider_exists = self.check_provider_exists().await?; if !provider_exists { - Console::info( - "Balance", - &format!("{}", balance / U256::from(10u128.pow(18))), - ); + Console::info("Balance", &format_ether(balance)); Console::info( "ETH Balance", - &format!("{:.6} ETH", { f64::from(eth_balance) / 10f64.powf(18.0) }), + &format!("{} ETH", format_ether(U256::from(eth_balance))), ); if balance < stake { Console::user_error(&format!( "Insufficient balance for stake: {}", - stake / U256::from(10u128.pow(18)) + format_ether(stake) )); return Err(ProviderError::InsufficientBalance); } if !self.prompt_user_confirmation(&format!( "Do you want to approve staking {}?", - stake.to_string().parse::().unwrap_or(0.0) / 10f64.powf(18.0) + format_ether(stake) )) { Console::info("Operation cancelled by user", "Staking approval declined"); return Err(ProviderError::UserCancelled); @@ -347,14 +342,8 @@ impl ProviderOperations { .await .map_err(|_| ProviderError::Other)?; - Console::info( - "Current Balance", - &format!("{}", balance / U256::from(10u128.pow(18))), - ); - Console::info( - "Additional stake amount", - &format!("{}", additional_stake / U256::from(10u128.pow(18))), - ); + Console::info("Current Balance", &format_ether(balance)); + Console::info("Additional stake amount", &format_ether(additional_stake)); if balance < additional_stake { Console::user_error("Insufficient balance for stake increase"); @@ -363,7 +352,7 @@ impl ProviderOperations { if !self.prompt_user_confirmation(&format!( "Do you want to approve staking {} additional funds?", - additional_stake.to_string().parse::().unwrap_or(0.0) / 10f64.powf(18.0) + format_ether(additional_stake) )) { Console::info("Operation cancelled by user", "Staking approval declined"); return Err(ProviderError::UserCancelled);