Skip to content

Commit

Permalink
refactor: create EscrowAccountsError
Browse files Browse the repository at this point in the history
Signed-off-by: Alexis Asseman <[email protected]>
  • Loading branch information
aasseman committed Jan 16, 2024
1 parent 8cf257c commit 223fef3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 68 deletions.
44 changes: 27 additions & 17 deletions common/src/escrow_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@ use anyhow::Result;
use ethers_core::types::U256;
use eventuals::{timer, Eventual, EventualExt};
use serde::Deserialize;
use thiserror::Error;
use tokio::time::sleep;
use tracing::{error, warn};

use crate::prelude::{Query, SubgraphClient};

#[derive(Error, Debug)]
pub enum EscrowAccountsError {
#[error("No signer found for sender {sender}")]
NoSignerFound { sender: Address },
#[error("No balance found for sender {sender}")]
NoBalanceFound { sender: Address },
#[error("No sender found for signer {signer}")]
NoSenderFound { signer: Address },
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct EscrowAccounts {
senders_balances: HashMap<Address, U256>,
Expand All @@ -40,41 +51,40 @@ impl EscrowAccounts {
}
}

pub fn get_signers_for_sender(&self, sender: &Address) -> Result<Vec<Address>> {
pub fn get_signers_for_sender(
&self,
sender: &Address,
) -> Result<Vec<Address>, EscrowAccountsError> {
self.senders_to_signers
.get(sender)
.filter(|signers| !signers.is_empty())
.ok_or(anyhow::format_err!(
"No signers found for sender {}.",
sender
))
.ok_or(EscrowAccountsError::NoSignerFound {
sender: sender.to_owned(),
})
.map(|signers| signers.to_owned())
}

pub fn get_sender_for_signer(&self, signer: &Address) -> Result<Address> {
pub fn get_sender_for_signer(&self, signer: &Address) -> Result<Address, EscrowAccountsError> {
self.signers_to_senders
.get(signer)
.ok_or(anyhow::format_err!(
"Sender not found for receipt signer {}.",
signer
))
.ok_or(EscrowAccountsError::NoSenderFound {
signer: signer.to_owned(),
})
.copied()
}

pub fn get_balance_for_sender(&self, sender: &Address) -> Result<U256> {
pub fn get_balance_for_sender(&self, sender: &Address) -> Result<U256, EscrowAccountsError> {
self.senders_balances
.get(sender)
.ok_or(anyhow::format_err!(
"Balance not found for sender {}.",
sender
))
.ok_or(EscrowAccountsError::NoBalanceFound {
sender: sender.to_owned(),
})
.copied()
}

pub fn get_balance_for_signer(&self, signer: &Address) -> Result<U256> {
pub fn get_balance_for_signer(&self, signer: &Address) -> Result<U256, EscrowAccountsError> {
self.get_sender_for_signer(signer)
.and_then(|sender| self.get_balance_for_sender(&sender))
.map_err(|e| anyhow::format_err!("Could not get balance for signer {}: {}", signer, e))
}

pub fn get_senders(&self) -> HashSet<Address> {
Expand Down
90 changes: 39 additions & 51 deletions tap-agent/src/tap/escrow_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,30 @@ pub struct EscrowAdapter {

#[derive(Debug, Error)]
pub enum AdapterError {
#[error("Error in EscrowAdapter: {error}")]
AdapterError { error: String },
#[error("Could not get escrow accounts from eventual")]
EscrowEventualError { error: String },

#[error("Could not get available escrow for sender")]
AvailableEscrowError(#[from] indexer_common::escrow_accounts::EscrowAccountsError),

#[error("Sender {sender} escrow balance is too large to fit in u128, could not get available escrow.")]
BalanceTooLarge { sender: Address },

#[error("Sender {sender} does not have enough escrow to subtract {fees} from {balance}.")]
NotEnoughEscrow {
sender: Address,
fees: u128,
balance: u128,
},
}

// Conversion from eventuals::error::Closed to AdapterError::EscrowEventualError
impl From<eventuals::error::Closed> for AdapterError {
fn from(e: eventuals::error::Closed) -> Self {
AdapterError::EscrowEventualError {
error: format!("{:?}", e),
}
}
}

impl EscrowAdapter {
Expand All @@ -45,34 +67,16 @@ impl EscrowAdapterTrait for EscrowAdapter {
type AdapterError = AdapterError;

async fn get_available_escrow(&self, sender: Address) -> Result<u128, AdapterError> {
let escrow_accounts =
self.escrow_accounts
.value()
.await
.map_err(|e| AdapterError::AdapterError {
error: format!("Could not get escrow accounts from eventual: {:?}.", e),
})?;

let sender = escrow_accounts
.get_sender_for_signer(&sender)
.map_err(|e| AdapterError::AdapterError {
error: format!("{}", e).to_string(),
})?;
let escrow_accounts = self.escrow_accounts.value().await?;

let balance = escrow_accounts
.get_balance_for_sender(&sender)
.map_err(|e| AdapterError::AdapterError {
error: format!("Could not get available escrow: {}", e).to_string(),
})?
.to_owned();
let balance: u128 = balance.try_into().map_err(|_| AdapterError::AdapterError {
error: format!(
"Sender {} escrow balance is too large to fit in u128, \
could not get available escrow.",
sender
)
.to_string(),
})?;
let sender = escrow_accounts.get_sender_for_signer(&sender)?;

let balance = escrow_accounts.get_balance_for_sender(&sender)?.to_owned();
let balance: u128 = balance
.try_into()
.map_err(|_| AdapterError::BalanceTooLarge {
sender: sender.to_owned(),
})?;

let fees = self
.sender_pending_fees
Expand All @@ -85,35 +89,19 @@ impl EscrowAdapterTrait for EscrowAdapter {
}

async fn subtract_escrow(&self, sender: Address, value: u128) -> Result<(), AdapterError> {
let escrow_accounts =
self.escrow_accounts
.value()
.await
.map_err(|e| AdapterError::AdapterError {
error: format!("Could not get escrow accounts from eventual: {:?}.", e),
})?;
let escrow_accounts = self.escrow_accounts.value().await?;

let current_available_escrow = self.get_available_escrow(sender).await?;

let sender = escrow_accounts
.get_sender_for_signer(&sender)
.map_err(|e| AdapterError::AdapterError {
error: format!(
"Could not get available escrow for receipt signer {}: {}",
sender, e
)
.to_string(),
})?;
let sender = escrow_accounts.get_sender_for_signer(&sender)?;

let mut fees_write = self.sender_pending_fees.write().await;
let fees = fees_write.entry(sender.to_owned()).or_insert(0);
if current_available_escrow < value {
return Err(AdapterError::AdapterError {
error: format!(
"Sender {} does not have enough escrow to subtract {} from {}.",
sender, value, *fees
)
.to_string(),
return Err(AdapterError::NotEnoughEscrow {
sender: sender.to_owned(),
fees: value,
balance: current_available_escrow,
});
}
*fees += value;
Expand Down

0 comments on commit 223fef3

Please sign in to comment.