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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct ReferrerNameRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub authority: Pubkey,
    pub user: Pubkey,
    pub user_stats: Pubkey,
    pub name: Vec<u8>,
}

impl ReferrerNameRow {
    pub fn from_parts(
        source: crate::accounts::referrer_name::ReferrerName,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            authority: source.authority.into(),
            user: source.user.into(),
            user_stats: source.user_stats.into(),
            name: source.name.to_vec(),
        }
    }
}

impl TryFrom<ReferrerNameRow> for crate::accounts::referrer_name::ReferrerName {
    type Error = carbon_core::error::Error;
    fn try_from(source: ReferrerNameRow) -> Result<Self, Self::Error> {
        Ok(Self {
            authority: *source.authority,
            user: *source.user,
            user_stats: *source.user_stats,
            name: source.name.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 32 bytes"
                        .to_string(),
                )
            })?,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::referrer_name::ReferrerName {
    fn table() -> &'static str {
        "referrer_name_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "authority",
            "user",
            "user_stats",
            "name",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for ReferrerNameRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO referrer_name_account (
                "authority",
                "user",
                "user_stats",
                "name",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6
            )"#,
        )
        .bind(self.authority)
        .bind(self.user)
        .bind(self.user_stats)
        .bind(&self.name)
        .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 ReferrerNameRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO referrer_name_account (
                "authority",
                "user",
                "user_stats",
                "name",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "authority" = EXCLUDED."authority",
                "user" = EXCLUDED."user",
                "user_stats" = EXCLUDED."user_stats",
                "name" = EXCLUDED."name",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.authority)
        .bind(self.user)
        .bind(self.user_stats)
        .bind(&self.name)
        .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 ReferrerNameRow {
    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 referrer_name_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 ReferrerNameRow {
    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 referrer_name_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 ReferrerNameMigrationOperation;

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