nomy-data-models 0.35.6

Data model definitions for Nomy wallet analysis data processing
Documentation
#![allow(clippy::too_many_arguments, unused_imports, non_camel_case_types)]
//! Position model definition.
//!
//! This file is generated automatically from the Python SQLAlchemy model.
//! Do not edit this file manually.

use serde::{Deserialize, Serialize};

// Standard imports that may be needed
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde_json::Value as JsonValue;
use uuid::Uuid;

// Additional imports specific to this model
use crate::enums::MarketType;
use crate::enums::PositionDirection;
use crate::enums::PositionStatus;

/// Model for tracking trading positions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Position {
    pub position_id: Uuid,
    pub chain_id: i32,
    pub exchange: String,
    pub market_type: String,
    pub position_direction: String,
    pub wallet_address: String,
    pub token_symbol_pair: String,
    pub token_address_pair: Option<String>,
    pub base_token_symbol: String,
    pub base_token_address: Option<String>,
    pub quote_token_symbol: String,
    pub quote_token_address: Option<String>,
    pub status: String,
    pub current_base_amount: Decimal,
    pub original_base_amount: Decimal,
    pub current_avg_entry_price: Decimal,
    pub avg_entry_price: Decimal,
    pub avg_exit_price: Decimal,
    pub cost_basis: Decimal,
    pub realized_pnl: Decimal,
    pub realized_pnl_usd: Decimal,
    pub realized_roi: Option<Decimal>,
    pub leverage: Option<Decimal>,
    pub opened_at: DateTime<Utc>,
    pub closed_at: Option<DateTime<Utc>>,
    pub fee_json: Option<JsonValue>,
    pub id: Uuid,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub created_by: Option<String>,
    pub updated_by: Option<String>,
}

impl Position {
    /// Create a new Position.
    pub fn new(
        position_id: Uuid,
        chain_id: i32,
        exchange: String,
        market_type: String,
        position_direction: String,
        wallet_address: String,
        token_symbol_pair: String,
        token_address_pair: String,
        base_token_symbol: String,
        base_token_address: String,
        quote_token_symbol: String,
        quote_token_address: String,
        status: String,
        current_base_amount: Decimal,
        original_base_amount: Decimal,
        current_avg_entry_price: Decimal,
        avg_entry_price: Decimal,
        avg_exit_price: Decimal,
        cost_basis: Decimal,
        realized_pnl: Decimal,
        realized_pnl_usd: Decimal,
        realized_roi: Decimal,
        leverage: Decimal,
        opened_at: DateTime<Utc>,
        closed_at: DateTime<Utc>,
        fee_json: JsonValue,
        id: Uuid,
        created_at: DateTime<Utc>,
        updated_at: DateTime<Utc>,
        created_by: String,
        updated_by: String,
    ) -> Self {
        Self {
            position_id,
            chain_id,
            exchange,
            market_type,
            position_direction,
            wallet_address,
            token_symbol_pair,
            token_address_pair: Some(token_address_pair),
            base_token_symbol,
            base_token_address: Some(base_token_address),
            quote_token_symbol,
            quote_token_address: Some(quote_token_address),
            status,
            current_base_amount,
            original_base_amount,
            current_avg_entry_price,
            avg_entry_price,
            avg_exit_price,
            cost_basis,
            realized_pnl,
            realized_pnl_usd,
            realized_roi: Some(realized_roi),
            leverage: Some(leverage),
            opened_at,
            closed_at: Some(closed_at),
            fee_json: Some(fee_json),
            id,
            created_at,
            updated_at,
            created_by: Some(created_by),
            updated_by: Some(updated_by),
        }
    }

    /// Convert to a JSON string.
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string(self)
    }

    /// Convert from a JSON string.
    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
        serde_json::from_str(json)
    }

    /// Convert to a dictionary-like structure.
    pub fn to_dict(&self) -> serde_json::Map<String, serde_json::Value> {
        let json = serde_json::to_value(self).unwrap_or(serde_json::Value::Null);
        if let serde_json::Value::Object(map) = json {
            map
        } else {
            serde_json::Map::new()
        }
    }
}