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 25, 2024
1 parent 22f484f commit fe0058e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
20 changes: 20 additions & 0 deletions funds-manager/funds-manager-server/src/fee_indexer/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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;

Expand Down Expand Up @@ -46,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 @@ -244,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(|_| ())
}
}
24 changes: 15 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 @@ -4,6 +4,7 @@ use std::collections::HashMap;
use std::str::FromStr;

use aws_sdk_secretsmanager::Client as SecretsManagerClient;
use diesel::IntoSql;

Check failure on line 7 in funds-manager/funds-manager-server/src/fee_indexer/redeem_fees.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `diesel::IntoSql`

error: unused import: `diesel::IntoSql` --> funds-manager/funds-manager-server/src/fee_indexer/redeem_fees.rs:7:5 | 7 | use diesel::IntoSql; | ^^^^^^^^^^^^^^^ | = note: `-D unused-imports` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unused_imports)]`
use ethers::core::rand::thread_rng;
use ethers::signers::LocalWallet;
use ethers::types::TxHash;
Expand Down Expand Up @@ -66,20 +67,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
14 changes: 14 additions & 0 deletions funds-manager/funds-manager-server/src/relayer_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use renegade_api::{
FIND_WALLET_ROUTE, GET_WALLET_ROUTE, REDEEM_NOTE_ROUTE,
},
},
types::ApiWallet,
RENEGADE_AUTH_HEADER_NAME, RENEGADE_SIG_EXPIRATION_HEADER_NAME,
};
use renegade_circuit_types::keychain::SecretSigningKey;
Expand Down Expand Up @@ -85,6 +86,19 @@ impl RelayerClient {
// | Wallet Methods |
// ------------------

/// Get the wallet associated with the given wallet id
pub async fn get_wallet(
&self,
wallet_id: WalletIdentifier,
root_key: &SecretSigningKey,
) -> Result<ApiWallet, FundsManagerError> {
let mut path = GET_WALLET_ROUTE.to_string();
path = path.replace(":wallet_id", &wallet_id.to_string());
let resp: GetWalletResponse = self.get_relayer_with_auth(&path, root_key).await?;

Ok(resp.wallet)
}

/// Check that the relayer has a given wallet, lookup the wallet if not
pub async fn check_wallet_indexed(
&self,
Expand Down

0 comments on commit fe0058e

Please sign in to comment.