//! This code was AUTOGENERATED using the Codama library.
use {
    carbon_core::graphql::primitives::{U128, U64},
    juniper::GraphQLObject,
    serde_json,
};

#[derive(Debug, Clone, GraphQLObject)]
#[graphql(name = "OpenPositionV2")]
pub struct OpenPositionV2GraphQL {
    pub instruction_metadata: crate::instructions::graphql::InstructionMetadataGraphQL,
    pub tick_lower_index: i32,
    pub tick_upper_index: i32,
    pub tick_array_lower_start_index: i32,
    pub tick_array_upper_start_index: i32,
    pub liquidity: U128,
    pub amount0_max: U64,
    pub amount1_max: U64,
    pub with_metadata: bool,
    pub base_flag: Option<bool>,
    pub accounts: carbon_core::graphql::primitives::Json,
}

impl TryFrom<crate::instructions::postgres::OpenPositionV2Row> for OpenPositionV2GraphQL {
    type Error = carbon_core::error::Error;
    fn try_from(
        row: crate::instructions::postgres::OpenPositionV2Row,
    ) -> Result<Self, Self::Error> {
        Ok(Self {
            instruction_metadata: row.instruction_metadata.into(),
            tick_lower_index: row.tick_lower_index,
            tick_upper_index: row.tick_upper_index,
            tick_array_lower_start_index: row.tick_array_lower_start_index,
            tick_array_upper_start_index: row.tick_array_upper_start_index,
            liquidity: carbon_core::graphql::primitives::U128(*row.liquidity),
            amount0_max: carbon_core::graphql::primitives::U64(*row.amount0_max),
            amount1_max: carbon_core::graphql::primitives::U64(*row.amount1_max),
            with_metadata: row.with_metadata,
            base_flag: row.base_flag,
            accounts: carbon_core::graphql::primitives::Json(
                serde_json::to_value(&row.accounts.0)
                    .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?,
            ),
        })
    }
}
