carbon-zeta-decoder 2.0.0

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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct CrossOpenOrdersMapRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub user_key: Pubkey,
    pub subaccount_index: U8,
}

impl CrossOpenOrdersMapRow {
    pub fn from_parts(
        source: crate::accounts::cross_open_orders_map::CrossOpenOrdersMap,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            user_key: source.user_key.into(),
            subaccount_index: source.subaccount_index.into(),
        }
    }
}

impl TryFrom<CrossOpenOrdersMapRow> for crate::accounts::cross_open_orders_map::CrossOpenOrdersMap {
    type Error = carbon_core::error::Error;
    fn try_from(source: CrossOpenOrdersMapRow) -> Result<Self, Self::Error> {
        Ok(Self {
            user_key: *source.user_key,
            subaccount_index: source.subaccount_index.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::cross_open_orders_map::CrossOpenOrdersMap
{
    fn table() -> &'static str {
        "cross_open_orders_map_account"
    }

    fn columns() -> Vec<&'static str> {
        vec!["__pubkey", "__slot", "user_key", "subaccount_index"]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for CrossOpenOrdersMapRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO cross_open_orders_map_account (
                "user_key",
                "subaccount_index",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4
            )"#,
        )
        .bind(self.user_key)
        .bind(self.subaccount_index)
        .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 CrossOpenOrdersMapRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO cross_open_orders_map_account (
                "user_key",
                "subaccount_index",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "user_key" = EXCLUDED."user_key",
                "subaccount_index" = EXCLUDED."subaccount_index",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.user_key)
        .bind(self.subaccount_index)
        .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 CrossOpenOrdersMapRow {
    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 cross_open_orders_map_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 CrossOpenOrdersMapRow {
    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 cross_open_orders_map_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 CrossOpenOrdersMapMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for CrossOpenOrdersMapMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS cross_open_orders_map_account (
                -- Account data
                "user_key" BYTEA NOT NULL,
                "subaccount_index" INT2 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 cross_open_orders_map_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}