use {
carbon_core::graphql::primitives::{U128, U64},
juniper::GraphQLObject,
serde_json,
};
#[derive(Debug, Clone, GraphQLObject)]
#[graphql(name = "Swap")]
pub struct SwapGraphQL {
pub instruction_metadata: crate::instructions::graphql::InstructionMetadataGraphQL,
pub amount: U64,
pub other_amount_threshold: U64,
pub sqrt_price_limit: U128,
pub amount_specified_is_input: bool,
pub a_to_b: bool,
pub accounts: carbon_core::graphql::primitives::Json,
}
impl TryFrom<crate::instructions::postgres::SwapRow> for SwapGraphQL {
type Error = carbon_core::error::Error;
fn try_from(row: crate::instructions::postgres::SwapRow) -> Result<Self, Self::Error> {
Ok(Self {
instruction_metadata: row.instruction_metadata.into(),
amount: carbon_core::graphql::primitives::U64(*row.amount),
other_amount_threshold: carbon_core::graphql::primitives::U64(
*row.other_amount_threshold,
),
sqrt_price_limit: carbon_core::graphql::primitives::U128(*row.sqrt_price_limit),
amount_specified_is_input: row.amount_specified_is_input,
a_to_b: row.a_to_b,
accounts: carbon_core::graphql::primitives::Json(
serde_json::to_value(&row.accounts.0)
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?,
),
})
}
}