Skip to content

Commit

Permalink
delete zero balances
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Coats committed Mar 21, 2024
1 parent 925d142 commit 8115acb
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/db/mongodb/collections/analytics/address_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,24 @@ pub struct DistributionStat {
impl AddressBalanceCollection {
/// Insert a balance for an address.
pub async fn insert_balance(&self, address: &Address, balance: u64) -> Result<(), DbError> {
self.update_one(
doc! { "_id": AddressDto::from(address) },
doc! { "$set": { "balance": balance.to_string() } },
UpdateOptions::builder().upsert(true).build(),
)
.await?;
if balance == 0 {
self.delete_balance(address).await?;
} else {
self.update_one(
doc! { "_id": AddressDto::from(address) },
doc! { "$set": { "balance": balance.to_string() } },
UpdateOptions::builder().upsert(true).build(),
)
.await?;
}
Ok(())
}

/// Delete a balance for an address.
pub async fn delete_balance(&self, address: &Address) -> Result<(), DbError> {
self.collection
.delete_one(doc! { "_id": AddressDto::from(address) }, None)
.await?;
Ok(())
}

Expand Down

0 comments on commit 8115acb

Please sign in to comment.