diff --git a/funds-manager/funds-manager-server/src/db/models.rs b/funds-manager/funds-manager-server/src/db/models.rs index ffcd650..1c02267 100644 --- a/funds-manager/funds-manager-server/src/db/models.rs +++ b/funds-manager/funds-manager-server/src/db/models.rs @@ -1,6 +1,8 @@ #![allow(missing_docs)] #![allow(trivial_bounds)] +use std::{fmt::Display, str::FromStr, time::SystemTime}; + use bigdecimal::BigDecimal; use diesel::prelude::*; use num_bigint::BigInt; @@ -100,3 +102,65 @@ impl HotWallet { HotWallet { id: Uuid::new_v4(), secret_id, vault, address, internal_wallet_id } } } + +/// The status of a gas wallet +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub enum GasWalletStatus { + /// The gas wallet is active + Active, + /// Marked as inactive but not yet transitioned to inactive + Pending, + /// The gas wallet is inactive + Inactive, +} + +impl Display for GasWalletStatus { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + GasWalletStatus::Active => write!(f, "active"), + GasWalletStatus::Pending => write!(f, "pending"), + GasWalletStatus::Inactive => write!(f, "inactive"), + } + } +} + +impl FromStr for GasWalletStatus { + type Err = String; + + fn from_str(s: &str) -> Result { + match s { + "active" => Ok(GasWalletStatus::Active), + "pending" => Ok(GasWalletStatus::Pending), + "inactive" => Ok(GasWalletStatus::Inactive), + _ => Err(format!("Invalid gas wallet status: {s}")), + } + } +} + +impl GasWalletStatus { + /// Get the state resulting from marking the gas wallet as active + pub fn transition_active(&self) -> Self { + GasWalletStatus::Active + } + + /// Get the state resulting from marking the gas wallet as inactive + pub fn transition_inactive(&self) -> Self { + match self { + GasWalletStatus::Active => GasWalletStatus::Pending, + GasWalletStatus::Pending => GasWalletStatus::Inactive, + GasWalletStatus::Inactive => GasWalletStatus::Inactive, + } + } +} + +/// A gas wallet's metadata +#[derive(Clone, Queryable, Selectable, Insertable)] +#[diesel(table_name = crate::db::schema::gas_wallets)] +#[diesel(check_for_backend(diesel::pg::Pg))] +pub struct GasWallet { + pub id: Uuid, + pub address: String, + pub peer_id: Option, + pub status: String, + pub created_at: SystemTime, +} diff --git a/funds-manager/funds-manager-server/src/db/schema.rs b/funds-manager/funds-manager-server/src/db/schema.rs index b1b86dd..bc2dc52 100644 --- a/funds-manager/funds-manager-server/src/db/schema.rs +++ b/funds-manager/funds-manager-server/src/db/schema.rs @@ -12,6 +12,16 @@ diesel::table! { } } +diesel::table! { + gas_wallets (id) { + id -> Uuid, + address -> Text, + peer_id -> Nullable, + status -> Text, + created_at -> Timestamp, + } +} + diesel::table! { hot_wallets (id) { id -> Uuid, @@ -39,6 +49,7 @@ diesel::table! { diesel::allow_tables_to_appear_in_same_query!( fees, + gas_wallets, hot_wallets, indexing_metadata, renegade_wallets, diff --git a/funds-manager/migrations/2024-08-01-214330_add_gas_wallets_table/down.sql b/funds-manager/migrations/2024-08-01-214330_add_gas_wallets_table/down.sql new file mode 100644 index 0000000..a5072f3 --- /dev/null +++ b/funds-manager/migrations/2024-08-01-214330_add_gas_wallets_table/down.sql @@ -0,0 +1,2 @@ +-- Drop the gas_wallets table +DROP TABLE IF EXISTS gas_wallets; \ No newline at end of file diff --git a/funds-manager/migrations/2024-08-01-214330_add_gas_wallets_table/up.sql b/funds-manager/migrations/2024-08-01-214330_add_gas_wallets_table/up.sql new file mode 100644 index 0000000..17fea4e --- /dev/null +++ b/funds-manager/migrations/2024-08-01-214330_add_gas_wallets_table/up.sql @@ -0,0 +1,8 @@ +-- Create a table to store gas wallets +CREATE TABLE gas_wallets ( + id UUID PRIMARY KEY, + address TEXT NOT NULL UNIQUE, + peer_id TEXT, + status TEXT NOT NULL DEFAULT 'inactive', + created_at TIMESTAMP NOT NULL DEFAULT NOW() +); \ No newline at end of file