Skip to content

Commit

Permalink
funds-manager: fee-indexer: Fix misc bugs with fee indexing + redemption
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut committed Jul 26, 2024
1 parent bb7df6f commit 7c5743c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Indexer {
let note = match maybe_note {
Some(note) => note,
None => {
info!("note not found, skipping");
info!("not the note receiver, skipping...");
return Ok(());
},
};
Expand Down
39 changes: 33 additions & 6 deletions funds-manager/funds-manager-server/src/fee_indexer/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ use std::collections::HashMap;
use bigdecimal::BigDecimal;
use diesel::deserialize::Queryable;
use diesel::deserialize::QueryableByName;
use diesel::result::Error as DieselError;
use diesel::sql_function;
use diesel::sql_query;
use diesel::sql_types::SingleValue;
use diesel::sql_types::{Array, Integer, Nullable, Numeric, Text};
use diesel::PgArrayExpressionMethods;
use diesel::{ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use renegade_common::types::wallet::WalletIdentifier;
use renegade_constants::MAX_BALANCES;
use tracing::warn;

use crate::db::models::WalletMetadata;
use crate::db::models::{Metadata, NewFee};
Expand Down Expand Up @@ -44,6 +47,11 @@ sql_function! {
fn coalesce<T: SingleValue>(x: Nullable<T>, y: T) -> T;
}

sql_function! {
/// Append an element to an array
fn array_append<T: SingleValue>(arr: Array<T>, elem: T) -> Array<T>;
}

// ---------------
// | Query Types |
// ---------------
Expand Down Expand Up @@ -111,12 +119,17 @@ impl Indexer {

/// Insert a fee into the fees table
pub(crate) async fn insert_fee(&mut self, fee: NewFee) -> Result<(), FundsManagerError> {
diesel::insert_into(fees_table)
.values(vec![fee])
.execute(&mut self.db_conn)
.await
.map_err(|e| FundsManagerError::db(format!("failed to insert fee: {e}")))
.map(|_| ())
match diesel::insert_into(fees_table).values(vec![fee]).execute(&mut self.db_conn).await {
Ok(_) => Ok(()),
Err(DieselError::DatabaseError(
diesel::result::DatabaseErrorKind::UniqueViolation,
_,
)) => {
warn!("Fee already exists in the database, skipping insertion...",);
Ok(())
},
Err(e) => Err(FundsManagerError::db(format!("failed to insert fee: {e}"))),
}
}

/// Get all mints that have unredeemed fees
Expand Down Expand Up @@ -237,4 +250,18 @@ impl Indexer {
.map_err(|_| FundsManagerError::db("failed to insert wallet"))
.map(|_| ())
}

/// Add a new mint to a wallet's managed mints
pub(crate) async fn add_mint_to_wallet(
&mut self,
wallet_id: &WalletIdentifier,
mint: &str,
) -> Result<(), FundsManagerError> {
diesel::update(wallet_table.find(wallet_id))
.set(managed_mints_col.eq(array_append(managed_mints_col, mint)))
.execute(&mut self.db_conn)
.await
.map_err(|_| FundsManagerError::db("failed to add mint to wallet"))
.map(|_| ())
}
}
23 changes: 14 additions & 9 deletions funds-manager/funds-manager-server/src/fee_indexer/redeem_fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,25 @@ impl Indexer {
&mut self,
mint: &str,
) -> Result<WalletMetadata, FundsManagerError> {
// Find a wallet with an existing balance
let maybe_wallet = self.get_wallet_for_mint(mint).await?;
let maybe_wallet = if maybe_wallet.is_none() {
self.find_wallet_with_empty_balance().await?
} else {
maybe_wallet
};
if let Some(wallet) = maybe_wallet {
return Ok(wallet);
}

match maybe_wallet {
Some(wallet) => Ok(wallet),
// Otherwise find a wallet with an empty balance slot, create a new one if no
// such wallet exists
let maybe_wallet = self.find_wallet_with_empty_balance().await?;
let wallet = match maybe_wallet {
Some(wallet) => wallet,
None => {
info!("creating new wallet for {mint}");
self.create_new_wallet().await
self.create_new_wallet().await?
},
}
};

self.add_mint_to_wallet(&wallet.id, mint).await?;
Ok(wallet)
}

/// Create a new wallet for managing a given mint
Expand Down

0 comments on commit 7c5743c

Please sign in to comment.