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

#[derive(Debug, Clone, GraphQLObject)]
#[graphql(name = "CreateAmmConfig")]
pub struct CreateAmmConfigGraphQL {
    pub instruction_metadata: crate::instructions::graphql::InstructionMetadataGraphQL,
    pub index: i32,
    pub tick_spacing: i32,
    pub trade_fee_rate: U32,
    pub protocol_fee_rate: U32,
    pub fund_fee_rate: U32,
    pub accounts: carbon_core::graphql::primitives::Json,
}

impl TryFrom<crate::instructions::postgres::CreateAmmConfigRow> for CreateAmmConfigGraphQL {
    type Error = carbon_core::error::Error;
    fn try_from(
        row: crate::instructions::postgres::CreateAmmConfigRow,
    ) -> Result<Self, Self::Error> {
        Ok(Self {
            instruction_metadata: row.instruction_metadata.into(),
            index: *row.index,
            tick_spacing: *row.tick_spacing,
            trade_fee_rate: carbon_core::graphql::primitives::U32((*row.trade_fee_rate) as u32),
            protocol_fee_rate: carbon_core::graphql::primitives::U32(
                (*row.protocol_fee_rate) as u32,
            ),
            fund_fee_rate: carbon_core::graphql::primitives::U32((*row.fund_fee_rate) as u32),
            accounts: carbon_core::graphql::primitives::Json(
                serde_json::to_value(&row.accounts.0)
                    .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?,
            ),
        })
    }
}
