Skip to content

Commit 0ad0e5c

Browse files
authored
Merge pull request #503 from PrimeIntellect-ai/fix/eth-formatting
fix(worker): cleanup eth formatting
2 parents 20380a5 + 9c5133d commit 0ad0e5c

File tree

3 files changed

+29
-42
lines changed

3 files changed

+29
-42
lines changed

crates/dev-utils/examples/transfer_eth.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use alloy::{
2-
network::TransactionBuilder, primitives::Address, primitives::U256, providers::Provider,
3-
rpc::types::TransactionRequest,
2+
network::TransactionBuilder, primitives::utils::format_ether, primitives::Address,
3+
primitives::U256, providers::Provider, rpc::types::TransactionRequest,
44
};
55
use clap::Parser;
66
use eyre::Result;
@@ -49,13 +49,13 @@ async fn main() -> Result<()> {
4949

5050
println!(
5151
"Sender's ETH balance before transaction: {} ETH",
52-
balance_before / U256::from(10u64.pow(18))
52+
format_ether(balance_before)
5353
);
5454

5555
let balance_after = wallet.provider.get_balance(to).await?;
5656
println!(
5757
"Receiver's ETH balance after transaction: {} ETH",
58-
alloy::primitives::utils::format_ether(balance_after)
58+
format_ether(balance_after)
5959
);
6060

6161
Ok(())

crates/worker/src/cli/command.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::services::discovery::DiscoveryService;
1515
use crate::services::discovery_updater::DiscoveryUpdater;
1616
use crate::state::system_state::SystemState;
1717
use crate::TaskHandles;
18+
use alloy::primitives::utils::format_ether;
1819
use alloy::primitives::U256;
1920
use alloy::signers::local::PrivateKeySigner;
2021
use alloy::signers::Signer;
@@ -528,10 +529,7 @@ pub async fn execute_command(
528529
std::process::exit(1);
529530
}
530531
};
531-
Console::info(
532-
"Required stake",
533-
&format!("{}", required_stake / U256::from(10u128.pow(18))),
534-
);
532+
Console::info("Required stake", &format_ether(required_stake).to_string());
535533

536534
if let Err(e) = provider_ops
537535
.retry_register_provider(
@@ -595,8 +593,8 @@ pub async fn execute_command(
595593
"Provider stake is less than required stake",
596594
&format!(
597595
"Required: {} tokens, Current: {} tokens",
598-
required_stake / U256::from(10u128.pow(18)),
599-
provider_stake / U256::from(10u128.pow(18))
596+
format_ether(required_stake),
597+
format_ether(provider_stake)
600598
),
601599
);
602600

@@ -885,7 +883,7 @@ pub async fn execute_command(
885883
.await
886884
.unwrap();
887885

888-
let format_balance = format!("{}", provider_balance / U256::from(10u128.pow(18)));
886+
let format_balance = format_ether(provider_balance).to_string();
889887

890888
println!("Provider balance: {format_balance}");
891889
Ok(())

crates/worker/src/operations/provider.rs

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::console::Console;
2+
use alloy::primitives::utils::format_ether;
23
use alloy::primitives::{Address, U256};
34
use log::error;
45
use shared::web3::contracts::core::builder::Contracts;
@@ -69,20 +70,20 @@ impl ProviderOperations {
6970
match stake_manager.get_stake(provider_address).await {
7071
Ok(stake) => {
7172
if first_check || stake != last_stake {
72-
Console::info("🔄 Chain Sync - Provider stake", &format!("{}", stake / U256::from(10u128.pow(18))));
73+
Console::info("🔄 Chain Sync - Provider stake", &format_ether(stake));
7374
if !first_check {
7475
if stake < last_stake {
7576
Console::warning(&format!("Stake decreased - possible slashing detected: From {} to {}",
76-
last_stake / U256::from(10u128.pow(18)),
77-
stake / U256::from(10u128.pow(18))
77+
format_ether(last_stake),
78+
format_ether(stake)
7879
));
7980
if stake == U256::ZERO {
8081
Console::warning("Stake is 0 - you might have to restart the node to increase your stake (if you still have balance left)");
8182
}
8283
} else {
8384
Console::info("🔄 Chain Sync - Stake changed", &format!("From {} to {}",
84-
last_stake / U256::from(10u128.pow(18)),
85-
stake / U256::from(10u128.pow(18))
85+
format_ether(last_stake),
86+
format_ether(stake)
8687
));
8788
}
8889
}
@@ -100,11 +101,11 @@ impl ProviderOperations {
100101
match contracts.ai_token.balance_of(provider_address).await {
101102
Ok(balance) => {
102103
if first_check || balance != last_balance {
103-
Console::info("🔄 Chain Sync - Balance", &format!("{}", balance / U256::from(10u128.pow(18))));
104+
Console::info("🔄 Chain Sync - Balance", &format_ether(balance));
104105
if !first_check {
105106
Console::info("🔄 Chain Sync - Balance changed", &format!("From {} to {}",
106-
last_balance / U256::from(10u128.pow(18)),
107-
balance / U256::from(10u128.pow(18))
107+
format_ether(last_balance),
108+
format_ether(balance)
108109
));
109110
}
110111
last_balance = balance;
@@ -222,24 +223,21 @@ impl ProviderOperations {
222223
let provider_exists = self.check_provider_exists().await?;
223224

224225
if !provider_exists {
225-
Console::info(
226-
"Balance",
227-
&format!("{}", balance / U256::from(10u128.pow(18))),
228-
);
226+
Console::info("Balance", &format_ether(balance));
229227
Console::info(
230228
"ETH Balance",
231-
&format!("{:.6} ETH", { f64::from(eth_balance) / 10f64.powf(18.0) }),
229+
&format!("{} ETH", format_ether(U256::from(eth_balance))),
232230
);
233231
if balance < stake {
234232
Console::user_error(&format!(
235233
"Insufficient balance for stake: {}",
236-
stake / U256::from(10u128.pow(18))
234+
format_ether(stake)
237235
));
238236
return Err(ProviderError::InsufficientBalance);
239237
}
240238
if !self.prompt_user_confirmation(&format!(
241239
"Do you want to approve staking {}?",
242-
stake.to_string().parse::<f64>().unwrap_or(0.0) / 10f64.powf(18.0)
240+
format_ether(stake)
243241
)) {
244242
Console::info("Operation cancelled by user", "Staking approval declined");
245243
return Err(ProviderError::UserCancelled);
@@ -273,24 +271,21 @@ impl ProviderOperations {
273271
let provider_exists = self.check_provider_exists().await?;
274272

275273
if !provider_exists {
276-
Console::info(
277-
"Balance",
278-
&format!("{}", balance / U256::from(10u128.pow(18))),
279-
);
274+
Console::info("Balance", &format_ether(balance));
280275
Console::info(
281276
"ETH Balance",
282-
&format!("{:.6} ETH", { f64::from(eth_balance) / 10f64.powf(18.0) }),
277+
&format!("{} ETH", format_ether(U256::from(eth_balance))),
283278
);
284279
if balance < stake {
285280
Console::user_error(&format!(
286281
"Insufficient balance for stake: {}",
287-
stake / U256::from(10u128.pow(18))
282+
format_ether(stake)
288283
));
289284
return Err(ProviderError::InsufficientBalance);
290285
}
291286
if !self.prompt_user_confirmation(&format!(
292287
"Do you want to approve staking {}?",
293-
stake.to_string().parse::<f64>().unwrap_or(0.0) / 10f64.powf(18.0)
288+
format_ether(stake)
294289
)) {
295290
Console::info("Operation cancelled by user", "Staking approval declined");
296291
return Err(ProviderError::UserCancelled);
@@ -347,14 +342,8 @@ impl ProviderOperations {
347342
.await
348343
.map_err(|_| ProviderError::Other)?;
349344

350-
Console::info(
351-
"Current Balance",
352-
&format!("{}", balance / U256::from(10u128.pow(18))),
353-
);
354-
Console::info(
355-
"Additional stake amount",
356-
&format!("{}", additional_stake / U256::from(10u128.pow(18))),
357-
);
345+
Console::info("Current Balance", &format_ether(balance));
346+
Console::info("Additional stake amount", &format_ether(additional_stake));
358347

359348
if balance < additional_stake {
360349
Console::user_error("Insufficient balance for stake increase");
@@ -363,7 +352,7 @@ impl ProviderOperations {
363352

364353
if !self.prompt_user_confirmation(&format!(
365354
"Do you want to approve staking {} additional funds?",
366-
additional_stake.to_string().parse::<f64>().unwrap_or(0.0) / 10f64.powf(18.0)
355+
format_ether(additional_stake)
367356
)) {
368357
Console::info("Operation cancelled by user", "Staking approval declined");
369358
return Err(ProviderError::UserCancelled);

0 commit comments

Comments
 (0)