carbon-drift-v2-decoder 2.0.0

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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct ConstituentTargetBaseRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub lp_pool: Pubkey,
    pub bump: U8,
    pub padding: Vec<u8>,
    pub targets: sqlx::types::Json<Vec<TargetsDatum>>,
}

impl ConstituentTargetBaseRow {
    pub fn from_parts(
        source: crate::accounts::constituent_target_base::ConstituentTargetBase,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            lp_pool: source.lp_pool.into(),
            bump: source.bump.into(),
            padding: source.padding.to_vec(),
            targets: sqlx::types::Json(source.targets.to_vec()),
        }
    }
}

impl TryFrom<ConstituentTargetBaseRow>
    for crate::accounts::constituent_target_base::ConstituentTargetBase
{
    type Error = carbon_core::error::Error;
    fn try_from(source: ConstituentTargetBaseRow) -> Result<Self, Self::Error> {
        Ok(Self {
            lp_pool: *source.lp_pool,
            bump: source.bump.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            padding: source.padding.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 3 bytes"
                        .to_string(),
                )
            })?,
            targets: source.targets.0,
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::accounts::constituent_target_base::ConstituentTargetBase
{
    fn table() -> &'static str {
        "constituent_target_base_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey", "__slot", "lp_pool", "bump", "padding", "targets",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for ConstituentTargetBaseRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO constituent_target_base_account (
                "lp_pool",
                "bump",
                "padding",
                "targets",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6
            )"#,
        )
        .bind(self.lp_pool)
        .bind(self.bump)
        .bind(&self.padding)
        .bind(&self.targets)
        .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 ConstituentTargetBaseRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO constituent_target_base_account (
                "lp_pool",
                "bump",
                "padding",
                "targets",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "lp_pool" = EXCLUDED."lp_pool",
                "bump" = EXCLUDED."bump",
                "padding" = EXCLUDED."padding",
                "targets" = EXCLUDED."targets",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.lp_pool)
        .bind(self.bump)
        .bind(&self.padding)
        .bind(&self.targets)
        .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 ConstituentTargetBaseRow {
    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 constituent_target_base_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 ConstituentTargetBaseRow {
    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 constituent_target_base_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 ConstituentTargetBaseMigrationOperation;

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