Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

funds-manager: migrations: Add gas_wallets table #26

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
);
Loading