From 70454f9eb4926c9bb585b52049e043a595c82797 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Wed, 19 Jun 2024 11:15:49 +0800 Subject: [PATCH] fix vault state --- json/src/vault.rs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/json/src/vault.rs b/json/src/vault.rs index 3ef51fa..057dee1 100644 --- a/json/src/vault.rs +++ b/json/src/vault.rs @@ -1,4 +1,7 @@ use std::collections::HashMap; +use serde::{ + Deserialize, Serialize, Serializer, +}; #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] @@ -14,8 +17,7 @@ pub struct UpdateVault { loan_scheme_id: Option, } -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "UPPERCASE")] +#[derive(Debug, Deserialize)] pub enum VaultState { Unknown, Active, @@ -24,18 +26,25 @@ pub enum VaultState { MayLiquidate, } -impl std::fmt::Display for VaultState { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - match self { - VaultState::Unknown => write!(f, "Unknown"), - VaultState::Active => write!(f, "Active"), - VaultState::InLiquidation => write!(f, "InLiquidation"), - VaultState::Frozen => write!(f, "Frozen"), - VaultState::MayLiquidate => write!(f, "MayLiquidate"), +impl Serialize for VaultState { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + match *self { + VaultState::Unknown => serializer.serialize_str("UNKNOWN"), + VaultState::Active => serializer.serialize_str("ACTIVE"), + VaultState::InLiquidation => serializer.serialize_str("IN_LIQUIDATION"), + VaultState::Frozen => serializer.serialize_str("FROZEN"), + VaultState::MayLiquidate => serializer.serialize_str("MAY_LIQUIDATE"), } } } +impl VaultState { + fn in_liquidation() -> Self { VaultState::InLiquidation } +} + #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Vault { @@ -67,7 +76,8 @@ pub struct VaultLiquidation { pub vault_id: String, pub loan_scheme_id: String, pub owner_address: String, - pub state: String, // VaultState::InLiquidation + #[serde(default = "VaultState::in_liquidation")] + pub state: VaultState, pub liquidation_height: u64, pub liquidation_penalty: f64, pub batch_count: usize,