carbon-drift-v2-decoder 2.0.0

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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct HighLeverageModeConfigRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub max_users: U32,
    pub current_users: U32,
    pub reduce_only: U8,
    pub padding1: Vec<u8>,
    pub current_maintenance_users: U32,
    pub padding2: Vec<u8>,
}

impl HighLeverageModeConfigRow {
    pub fn from_parts(
        source: crate::accounts::high_leverage_mode_config::HighLeverageModeConfig,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            max_users: source.max_users.into(),
            current_users: source.current_users.into(),
            reduce_only: source.reduce_only.into(),
            padding1: source.padding1.to_vec(),
            current_maintenance_users: source.current_maintenance_users.into(),
            padding2: source.padding2.to_vec(),
        }
    }
}

impl TryFrom<HighLeverageModeConfigRow>
    for crate::accounts::high_leverage_mode_config::HighLeverageModeConfig
{
    type Error = carbon_core::error::Error;
    fn try_from(source: HighLeverageModeConfigRow) -> Result<Self, Self::Error> {
        Ok(Self {
            max_users: source.max_users.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            current_users: source.current_users.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            reduce_only: source.reduce_only.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            padding1: source.padding1.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 3 bytes"
                        .to_string(),
                )
            })?,
            current_maintenance_users: source.current_maintenance_users.try_into().map_err(
                |_| {
                    carbon_core::error::Error::Custom(
                        "Failed to convert value from postgres primitive".to_string(),
                    )
                },
            )?,
            padding2: source.padding2.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 24 bytes"
                        .to_string(),
                )
            })?,
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::accounts::high_leverage_mode_config::HighLeverageModeConfig
{
    fn table() -> &'static str {
        "high_leverage_mode_config_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "max_users",
            "current_users",
            "reduce_only",
            "padding1",
            "current_maintenance_users",
            "padding2",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for HighLeverageModeConfigRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO high_leverage_mode_config_account (
                "max_users",
                "current_users",
                "reduce_only",
                "padding1",
                "current_maintenance_users",
                "padding2",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8
            )"#,
        )
        .bind(self.max_users)
        .bind(self.current_users)
        .bind(self.reduce_only)
        .bind(&self.padding1)
        .bind(self.current_maintenance_users)
        .bind(&self.padding2)
        .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 HighLeverageModeConfigRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO high_leverage_mode_config_account (
                "max_users",
                "current_users",
                "reduce_only",
                "padding1",
                "current_maintenance_users",
                "padding2",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "max_users" = EXCLUDED."max_users",
                "current_users" = EXCLUDED."current_users",
                "reduce_only" = EXCLUDED."reduce_only",
                "padding1" = EXCLUDED."padding1",
                "current_maintenance_users" = EXCLUDED."current_maintenance_users",
                "padding2" = EXCLUDED."padding2",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.max_users)
        .bind(self.current_users)
        .bind(self.reduce_only)
        .bind(&self.padding1)
        .bind(self.current_maintenance_users)
        .bind(&self.padding2)
        .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 HighLeverageModeConfigRow {
    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 high_leverage_mode_config_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 HighLeverageModeConfigRow {
    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 high_leverage_mode_config_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 HighLeverageModeConfigMigrationOperation;

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