carbon-zeta-decoder 2.0.0

Zeta Program Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::AnchorDecimal,
    carbon_core::{
        account::AccountMetadata,
        postgres::{
            metadata::AccountRowMetadata,
            primitives::{U16, U8},
        },
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct PerpSyncQueueRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub nonce: U8,
    pub head: U16,
    pub length: U16,
    pub queue: sqlx::types::Json<Vec<AnchorDecimal>>,
}

impl PerpSyncQueueRow {
    pub fn from_parts(
        source: crate::accounts::perp_sync_queue::PerpSyncQueue,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            nonce: source.nonce.into(),
            head: source.head.into(),
            length: source.length.into(),
            queue: sqlx::types::Json(source.queue.to_vec()),
        }
    }
}

impl TryFrom<PerpSyncQueueRow> for crate::accounts::perp_sync_queue::PerpSyncQueue {
    type Error = carbon_core::error::Error;
    fn try_from(source: PerpSyncQueueRow) -> Result<Self, Self::Error> {
        Ok(Self {
            nonce: source.nonce.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            head: source.head.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            length: source.length.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            queue: source
                .queue
                .0
                .into_iter()
                .collect::<Vec<_>>()
                .try_into()
                .map_err(|_| {
                    carbon_core::error::Error::Custom(
                        "Failed to convert value from postgres primitive".to_string(),
                    )
                })?,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::perp_sync_queue::PerpSyncQueue {
    fn table() -> &'static str {
        "perp_sync_queue_account"
    }

    fn columns() -> Vec<&'static str> {
        vec!["__pubkey", "__slot", "nonce", "head", "length", "queue"]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for PerpSyncQueueRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO perp_sync_queue_account (
                "nonce",
                "head",
                "length",
                "queue",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6
            )"#,
        )
        .bind(self.nonce)
        .bind(self.head)
        .bind(self.length)
        .bind(&self.queue)
        .bind(self.account_metadata.pubkey)
        .bind(&self.account_metadata.slot)
        .execute(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(())
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Upsert for PerpSyncQueueRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO perp_sync_queue_account (
                "nonce",
                "head",
                "length",
                "queue",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "nonce" = EXCLUDED."nonce",
                "head" = EXCLUDED."head",
                "length" = EXCLUDED."length",
                "queue" = EXCLUDED."queue",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.nonce)
        .bind(self.head)
        .bind(self.length)
        .bind(&self.queue)
        .bind(self.account_metadata.pubkey)
        .bind(&self.account_metadata.slot)
        .execute(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(())
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Delete for PerpSyncQueueRow {
    type Key = carbon_core::postgres::primitives::Pubkey;

    async fn delete(key: Self::Key, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"DELETE FROM perp_sync_queue_account WHERE
                __pubkey = $1
            "#,
        )
        .bind(key)
        .execute(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(())
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Lookup for PerpSyncQueueRow {
    type Key = carbon_core::postgres::primitives::Pubkey;

    async fn lookup(
        key: Self::Key,
        pool: &sqlx::PgPool,
    ) -> carbon_core::error::CarbonResult<Option<Self>> {
        let row = sqlx::query_as(
            r#"SELECT * FROM perp_sync_queue_account WHERE
                __pubkey = $1
            "#,
        )
        .bind(key)
        .fetch_optional(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(row)
    }
}

pub struct PerpSyncQueueMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for PerpSyncQueueMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS perp_sync_queue_account (
                -- Account data
                "nonce" INT2 NOT NULL,
                "head" INT4 NOT NULL,
                "length" INT4 NOT NULL,
                "queue" JSONB NOT NULL,
                -- Account metadata
                __pubkey BYTEA NOT NULL,
                __slot NUMERIC(20),
                PRIMARY KEY (__pubkey)
            )"#,
        )
        .execute(connection)
        .await?;
        Ok(())
    }

    async fn down(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(r#"DROP TABLE IF EXISTS perp_sync_queue_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}