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::U64},
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct PythLazerOracleRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub price: i64,
    pub publish_time: U64,
    pub posted_slot: U64,
    pub exponent: i32,
    pub padding: Vec<u8>,
    pub conf: U64,
}

impl PythLazerOracleRow {
    pub fn from_parts(
        source: crate::accounts::pyth_lazer_oracle::PythLazerOracle,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            price: source.price,
            publish_time: source.publish_time.into(),
            posted_slot: source.posted_slot.into(),
            exponent: source.exponent,
            padding: source.padding.to_vec(),
            conf: source.conf.into(),
        }
    }
}

impl TryFrom<PythLazerOracleRow> for crate::accounts::pyth_lazer_oracle::PythLazerOracle {
    type Error = carbon_core::error::Error;
    fn try_from(source: PythLazerOracleRow) -> Result<Self, Self::Error> {
        Ok(Self {
            price: source.price,
            publish_time: *source.publish_time,
            posted_slot: *source.posted_slot,
            exponent: source.exponent,
            padding: source.padding.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 4 bytes"
                        .to_string(),
                )
            })?,
            conf: *source.conf,
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::accounts::pyth_lazer_oracle::PythLazerOracle
{
    fn table() -> &'static str {
        "pyth_lazer_oracle_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "price",
            "publish_time",
            "posted_slot",
            "exponent",
            "padding",
            "conf",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for PythLazerOracleRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO pyth_lazer_oracle_account (
                "price",
                "publish_time",
                "posted_slot",
                "exponent",
                "padding",
                "conf",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8
            )"#,
        )
        .bind(self.price)
        .bind(&self.publish_time)
        .bind(&self.posted_slot)
        .bind(self.exponent)
        .bind(&self.padding)
        .bind(&self.conf)
        .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 PythLazerOracleRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO pyth_lazer_oracle_account (
                "price",
                "publish_time",
                "posted_slot",
                "exponent",
                "padding",
                "conf",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "price" = EXCLUDED."price",
                "publish_time" = EXCLUDED."publish_time",
                "posted_slot" = EXCLUDED."posted_slot",
                "exponent" = EXCLUDED."exponent",
                "padding" = EXCLUDED."padding",
                "conf" = EXCLUDED."conf",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.price)
        .bind(&self.publish_time)
        .bind(&self.posted_slot)
        .bind(self.exponent)
        .bind(&self.padding)
        .bind(&self.conf)
        .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 PythLazerOracleRow {
    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 pyth_lazer_oracle_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 PythLazerOracleRow {
    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 pyth_lazer_oracle_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 PythLazerOracleMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for PythLazerOracleMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS pyth_lazer_oracle_account (
                -- Account data
                "price" INT8 NOT NULL,
                "publish_time" NUMERIC(20) NOT NULL,
                "posted_slot" NUMERIC(20) NOT NULL,
                "exponent" INT4 NOT NULL,
                "padding" BYTEA NOT NULL,
                "conf" NUMERIC(20) 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 pyth_lazer_oracle_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}