use {
crate::types::graphql::{AnchorDecimalGraphQL, MarginAccountTypeGraphQL, ProductLedgerGraphQL},
carbon_core::graphql::primitives::{Pubkey, I64, U64, U8},
juniper::GraphQLObject,
};
#[derive(Debug, Clone, GraphQLObject)]
#[graphql(name = "CrossMarginAccount")]
pub struct CrossMarginAccountGraphQL {
pub account_metadata: crate::accounts::graphql::AccountMetadataGraphQL,
pub authority: Pubkey,
pub delegated_pubkey: Pubkey,
pub balance: U64,
pub subaccount_index: U8,
pub nonce: U8,
pub force_cancel_flag: bool,
pub account_type: MarginAccountTypeGraphQL,
pub open_orders_nonces: Vec<U8>,
pub open_orders_nonces_padding: Vec<U8>,
pub rebalance_amount: I64,
pub last_funding_deltas: Vec<AnchorDecimalGraphQL>,
pub last_funding_deltas_padding: Vec<AnchorDecimalGraphQL>,
pub product_ledgers: Vec<ProductLedgerGraphQL>,
pub product_ledgers_padding: Vec<ProductLedgerGraphQL>,
pub padding: Vec<U8>,
}
impl TryFrom<crate::accounts::postgres::CrossMarginAccountRow> for CrossMarginAccountGraphQL {
type Error = carbon_core::error::Error;
fn try_from(
row: crate::accounts::postgres::CrossMarginAccountRow,
) -> Result<Self, Self::Error> {
Ok(Self {
account_metadata: row.account_metadata.into(),
authority: carbon_core::graphql::primitives::Pubkey(row.authority.0),
delegated_pubkey: carbon_core::graphql::primitives::Pubkey(row.delegated_pubkey.0),
balance: carbon_core::graphql::primitives::U64(*row.balance),
subaccount_index: carbon_core::graphql::primitives::U8((*row.subaccount_index) as u8),
nonce: carbon_core::graphql::primitives::U8((*row.nonce) as u8),
force_cancel_flag: row.force_cancel_flag,
account_type: row.account_type.0.into(),
open_orders_nonces: row
.open_orders_nonces
.into_iter()
.map(carbon_core::graphql::primitives::U8)
.collect(),
open_orders_nonces_padding: row
.open_orders_nonces_padding
.into_iter()
.map(carbon_core::graphql::primitives::U8)
.collect(),
rebalance_amount: carbon_core::graphql::primitives::I64(row.rebalance_amount),
last_funding_deltas: row
.last_funding_deltas
.0
.into_iter()
.map(|item| item.into())
.collect(),
last_funding_deltas_padding: row
.last_funding_deltas_padding
.0
.into_iter()
.map(|item| item.into())
.collect(),
product_ledgers: row
.product_ledgers
.0
.into_iter()
.map(|item| item.into())
.collect(),
product_ledgers_padding: row
.product_ledgers_padding
.0
.into_iter()
.map(|item| item.into())
.collect(),
padding: row
.padding
.into_iter()
.map(carbon_core::graphql::primitives::U8)
.collect(),
})
}
}