Skip to content

Commit

Permalink
funds-manager: migrations: Add gas_wallets table
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut committed Aug 2, 2024
1 parent 9b9c770 commit 0d4dd07
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
64 changes: 64 additions & 0 deletions funds-manager/funds-manager-server/src/db/models.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Self, Self::Err> {
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<String>,
pub status: String,
pub created_at: SystemTime,
}
11 changes: 11 additions & 0 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,16 @@ diesel::table! {
}
}

diesel::table! {
gas_wallets (id) {
id -> Uuid,
address -> Text,
peer_id -> Nullable<Text>,
status -> Text,
created_at -> Timestamp,
}
}

diesel::table! {
hot_wallets (id) {
id -> Uuid,
Expand Down Expand Up @@ -39,6 +49,7 @@ diesel::table! {

diesel::allow_tables_to_appear_in_same_query!(
fees,
gas_wallets,
hot_wallets,
indexing_metadata,
renegade_wallets,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Drop the gas_wallets table
DROP TABLE IF EXISTS gas_wallets;
Original file line number Diff line number Diff line change
@@ -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()
);

0 comments on commit 0d4dd07

Please sign in to comment.