From d7b2da626ed067fed494456d96f0f0eca59386b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20D=C3=ADaz?= Date: Tue, 20 Aug 2024 12:46:54 +0200 Subject: [PATCH] chore: attend pr review comments --- .../src/actors/watch_dog.rs | 198 ++++++++---------- 1 file changed, 88 insertions(+), 110 deletions(-) diff --git a/bridges/centralized-ethereum/src/actors/watch_dog.rs b/bridges/centralized-ethereum/src/actors/watch_dog.rs index 1c6633148..708439a07 100644 --- a/bridges/centralized-ethereum/src/actors/watch_dog.rs +++ b/bridges/centralized-ethereum/src/actors/watch_dog.rs @@ -119,59 +119,27 @@ impl WatchDog { if let Err(err) = check_wit_connection_status(&wit_jsonrpc_socket).await { status = err; } - let wit_client = JsonRpcClient::start(&wit_jsonrpc_socket) - .expect("cannot start JSON/WIT connection"); - let wit_account = match fetch_wit_account(&wit_client).await { - Ok(pkh) => pkh, + let wit_client = match JsonRpcClient::start(&wit_jsonrpc_socket) { + Ok(client) => client, + Err(_) => return (None, None), + }; + let (wit_account, wit_balance, wit_utxos_above_threshold) = match fetch_wit_info( + &wit_client, + wit_utxo_min_value_threshold + ).await { + Ok((wit_account, wit_balance, wit_utxos_above_threshold)) => { + (wit_account, wit_balance, wit_utxos_above_threshold) + } Err(err) => { if status.eq("up-and-running") { status = err; } - None - } - }; - - let wit_balance = match wit_account.clone() { - Some(pkh) => match fetch_wit_account_balance(&wit_client, pkh.as_str()).await { - Ok(wit_balance) => wit_balance, - Err(err) => { - if status.eq("up-and-running") { - status = err; - } - None - } - }, - None => None, - }; - - let wit_utxos_above_threshold = match wit_account.clone() { - Some(pkh) => { - match fetch_wit_account_count_utxos_above( - &wit_client, - pkh.as_str(), - wit_utxo_min_value_threshold, - ) - .await - { - Ok(wit_utxos_above_threshold) => wit_utxos_above_threshold, - Err(err) => { - if status.eq("up-and-running") { - status = err; - } - None - } - } + (None, None, None) } - None => None, }; let eth_balance = match check_eth_account_balance(ð_jsonrpc_url, eth_account).await { - Ok(Some(eth_balance)) => { - let eth_balance: f64 = eth_balance.to_string().parse().unwrap_or_default(); - //Some(Unit::Wei(ð_balance.to_string()).to_eth_str().unwrap_or_default()), - Some(eth_balance / 1000000000000000000.0) - } - Ok(None) => None, + Ok(eth_balance) => eth_balance, Err(err) => { if status.eq("up-and-running") { status = err; @@ -248,7 +216,7 @@ impl WatchDog { async fn check_eth_account_balance( eth_jsonrpc_url: &str, eth_account: H160, -) -> Result, String> { +) -> Result, String> { let web3_http = web3::transports::Http::new(eth_jsonrpc_url) .map_err(|_e| "evm-disconnect".to_string()) .unwrap(); @@ -258,7 +226,10 @@ async fn check_eth_account_balance( Ok(syncing) => match syncing { web3::types::SyncState::NotSyncing => { match web3.eth().balance(eth_account, None).await { - Ok(balance) => Ok(Some(balance)), + Ok(eth_balance) => { + let eth_balance: f64 = eth_balance.to_string().parse().unwrap_or_default(); + Ok(Some(eth_balance / 1000000000000000000.0)) + } _ => Ok(None), } } @@ -282,83 +253,90 @@ async fn check_wit_connection_status(wit_jsonrpc_socket: &str) -> Result<(), Str } } -async fn fetch_wit_account(wit_client: &Addr) -> Result, String> { +async fn fetch_wit_info ( + wit_client: &Addr, + wit_utxos_min_threshold: u64, +) -> Result<(Option, Option, Option), String> { let req = jsonrpc::Request::method("getPkh").timeout(Duration::from_secs(5)); let res = wit_client.send(req).await; - match res { + let wit_account = match res { Ok(Ok(res)) => match serde_json::from_value::(res) { - Ok(pkh) => Ok(Some(pkh)), - Err(_) => Ok(None), + Ok(pkh) => Some(pkh), + Err(_) => None, }, - Ok(Err(_)) => Ok(None), - Err(_) => Err("wit-errors-getPkh".to_string()), - } -} - -async fn fetch_wit_account_balance( - wit_client: &Addr, - wit_account: &str, -) -> Result, String> { - let req = jsonrpc::Request::method("getBalance") - .timeout(Duration::from_secs(5)) - .params(vec![wit_account, "true"]) - .expect("getBalance wrong params"); - - let res = wit_client.send(req).await; - let res = match res { - Ok(res) => res, + Ok(Err(_)) => None, Err(_) => { - return Err("wit-errors-getBalance".to_string()); + return Err("wit-errors-getPkh".to_string()); } }; - match res { - Ok(value) => match value.get("total") { - Some(value) => match value.as_f64() { - Some(value) => Ok(Some(value / 1000000000.0)), - None => Ok(None), - }, - None => Ok(None), - }, - Err(_) => Err("wit-errors-getBalance".to_string()), - } -} - -async fn fetch_wit_account_count_utxos_above( - wit_client: &Addr, - wit_account: &str, - threshold: u64, -) -> Result, String> { - let req = jsonrpc::Request::method("getUtxoInfo") - .timeout(Duration::from_secs(5)) - .params(wit_account) - .expect("getUtxoInfo wrong params"); - - let res = wit_client.send(req).await; - let res = match res { - Ok(res) => res, - Err(_) => { - return Err("wit-errors-getUtxoInfo".to_string()); + let wit_account_balance = match wit_account.clone() { + Some(wit_account) => { + let req = jsonrpc::Request::method("getBalance") + .timeout(Duration::from_secs(5)) + .params(wit_account) + .expect("getBalance wrong params"); + let res = wit_client.send(req).await; + let res = match res { + Ok(res) => res, + Err(_) => { + return Err("wit-errors-getBalance".to_string()); + } + }; + match res { + Ok(value) => match value.get("total") { + Some(value) => match value.as_f64() { + Some(value) => Some(value / 1000000000.0), + None => None, + }, + None => None, + }, + Err(_) => { + return Err("wit-errors-getBalance".to_string()); + } + } } + None => None, }; - match res { - Ok(utxo_info) => { - if let Some(utxos) = utxo_info["utxos"].as_array() { - let mut counter: u64 = u64::default(); - for utxo in utxos { - if let Some(value) = utxo["value"].as_u64() { - if value >= threshold { - counter += 1; + let wit_utxos_above_threshold = match wit_account.clone() { + Some(wit_account) => { + let req = jsonrpc::Request::method("getUtxoInfo") + .timeout(Duration::from_secs(5)) + .params(wit_account) + .expect("getUtxoInfo wrong params"); + let res = wit_client.send(req).await; + let res = match res { + Ok(res) => res, + Err(_) => { + return Err("wit-errors-getUtxoInfo".to_string()); + } + }; + match res { + Ok(utxo_info) => { + if let Some(utxos) = utxo_info["utxos"].as_array() { + let mut counter: u64 = u64::default(); + for utxo in utxos { + if let Some(value) = utxo["value"].as_u64() { + if value >= wit_utxos_min_threshold { + counter += 1; + } + } } + + Some(counter) + } else { + None } } - - Ok(Some(counter)) - } else { - Ok(None) + Err(_) => { + return Err("wit-errors-getUtxoInfo".to_string()); + } } } - Err(_) => Err("wit-errors-getUtxoInfo".to_string()), - } + None => None, + }; + + + Ok((wit_account, wit_account_balance, wit_utxos_above_threshold)) }