Skip to content

Commit

Permalink
funds-manager: migrations: Add hot_wallets table
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut committed Jul 27, 2024
1 parent 63e4e1e commit efc4639
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion funds-manager/funds-manager-server/src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub struct Metadata {

/// A metadata entry for a wallet managed by the indexer
#[derive(Clone, Queryable, Selectable, Insertable)]
#[diesel(table_name = crate::db::schema::wallets)]
#[diesel(table_name = crate::db::schema::renegade_wallets)]
#[diesel(check_for_backend(diesel::pg::Pg))]
#[allow(missing_docs, clippy::missing_docs_in_private_items)]
pub struct WalletMetadata {
Expand Down
17 changes: 15 additions & 2 deletions funds-manager/funds-manager-server/src/db/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ diesel::table! {
}
}

diesel::table! {
hot_wallets (id) {
id -> Uuid,
secret_id -> Text,
vault -> Text,
}
}

diesel::table! {
indexing_metadata (key) {
key -> Text,
Expand All @@ -20,11 +28,16 @@ diesel::table! {
}

diesel::table! {
wallets (id) {
renegade_wallets (id) {
id -> Uuid,
mints -> Array<Nullable<Text>>,
secret_id -> Text,
}
}

diesel::allow_tables_to_appear_in_same_query!(fees, indexing_metadata, wallets,);
diesel::allow_tables_to_appear_in_same_query!(
fees,
hot_wallets,
indexing_metadata,
renegade_wallets,
);
16 changes: 9 additions & 7 deletions funds-manager/funds-manager-server/src/fee_indexer/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ use crate::db::schema::{
indexing_metadata::dsl::{
indexing_metadata as metadata_table, key as metadata_key, value as metadata_value,
},
wallets::dsl::{id as wallet_id_col, mints as managed_mints_col, wallets as wallet_table},
renegade_wallets::dsl::{
id as wallet_id_col, mints as managed_mints_col, renegade_wallets as renegade_wallet_table,
},
};
use crate::error::FundsManagerError;
use crate::Indexer;
Expand Down Expand Up @@ -214,7 +216,7 @@ impl Indexer {
&mut self,
wallet_id: &Uuid,
) -> Result<WalletMetadata, FundsManagerError> {
wallet_table
renegade_wallet_table
.filter(wallet_id_col.eq(wallet_id))
.first::<WalletMetadata>(&mut self.db_conn)
.await
Expand All @@ -225,7 +227,7 @@ impl Indexer {
pub(crate) async fn get_all_wallets(
&mut self,
) -> Result<Vec<WalletMetadata>, FundsManagerError> {
let wallets = wallet_table
let wallets = renegade_wallet_table
.load::<WalletMetadata>(&mut self.db_conn)
.await
.map_err(|e| FundsManagerError::db(format!("failed to load wallets: {}", e)))?;
Expand All @@ -239,7 +241,7 @@ impl Indexer {
&mut self,
mint: &str,
) -> Result<Option<WalletMetadata>, FundsManagerError> {
let wallets: Vec<WalletMetadata> = wallet_table
let wallets: Vec<WalletMetadata> = renegade_wallet_table
.filter(managed_mints_col.contains(vec![mint]))
.load::<WalletMetadata>(&mut self.db_conn)
.await
Expand All @@ -253,7 +255,7 @@ impl Indexer {
&mut self,
) -> Result<Option<WalletMetadata>, FundsManagerError> {
let n_mints = coalesce(array_length(managed_mints_col, 1 /* dim */), 0);
let wallets = wallet_table
let wallets = renegade_wallet_table
.filter(n_mints.lt(MAX_BALANCES as i32))
.load::<WalletMetadata>(&mut self.db_conn)
.await
Expand All @@ -267,7 +269,7 @@ impl Indexer {
&mut self,
wallet: WalletMetadata,
) -> Result<(), FundsManagerError> {
diesel::insert_into(wallet_table)
diesel::insert_into(renegade_wallet_table)
.values(vec![wallet])
.execute(&mut self.db_conn)
.await
Expand All @@ -281,7 +283,7 @@ impl Indexer {
wallet_id: &WalletIdentifier,
mint: &str,
) -> Result<(), FundsManagerError> {
diesel::update(wallet_table.find(wallet_id))
diesel::update(renegade_wallet_table.find(wallet_id))
.set(managed_mints_col.eq(array_append(managed_mints_col, mint)))
.execute(&mut self.db_conn)
.await
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Drop the hot wallets table
DROP TABLE hot_wallets;
ALTER TABLE renegade_wallets RENAME TO wallets;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Create a table for storing hot wallets
CREATE TABLE hot_wallets(
id UUID PRIMARY KEY,
secret_id TEXT NOT NULL, -- The AWS Secrets Manager secret that holds the hot wallet's key
vault TEXT NOT NULL -- The fireblocks vault that the hot wallet writes back to
);

--- Rename the wallets table to renegade_wallets for clarity
ALTER TABLE wallets RENAME TO renegade_wallets;

0 comments on commit efc4639

Please sign in to comment.