Skip to content

Commit

Permalink
chore: attend pr review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
guidiaz committed Aug 20, 2024
1 parent 0a983af commit d7b2da6
Showing 1 changed file with 88 additions and 110 deletions.
198 changes: 88 additions & 110 deletions bridges/centralized-ethereum/src/actors/watch_dog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&eth_jsonrpc_url, eth_account).await {
Ok(Some(eth_balance)) => {
let eth_balance: f64 = eth_balance.to_string().parse().unwrap_or_default();
//Some(Unit::Wei(&eth_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;
Expand Down Expand Up @@ -248,7 +216,7 @@ impl WatchDog {
async fn check_eth_account_balance(
eth_jsonrpc_url: &str,
eth_account: H160,
) -> Result<Option<U256>, String> {
) -> Result<Option<f64>, String> {
let web3_http = web3::transports::Http::new(eth_jsonrpc_url)
.map_err(|_e| "evm-disconnect".to_string())
.unwrap();
Expand All @@ -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),
}
}
Expand All @@ -282,83 +253,90 @@ async fn check_wit_connection_status(wit_jsonrpc_socket: &str) -> Result<(), Str
}
}

async fn fetch_wit_account(wit_client: &Addr<JsonRpcClient>) -> Result<Option<String>, String> {
async fn fetch_wit_info (
wit_client: &Addr<JsonRpcClient>,
wit_utxos_min_threshold: u64,
) -> Result<(Option<String>, Option<f64>, Option<u64>), 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::<String>(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<JsonRpcClient>,
wit_account: &str,
) -> Result<Option<f64>, 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<JsonRpcClient>,
wit_account: &str,
threshold: u64,
) -> Result<Option<u64>, 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))
}

0 comments on commit d7b2da6

Please sign in to comment.