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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct RevenueShareRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub authority: Pubkey,
    pub total_referrer_rewards: U64,
    pub total_builder_rewards: U64,
    pub padding: Vec<u8>,
}

impl RevenueShareRow {
    pub fn from_parts(
        source: crate::accounts::revenue_share::RevenueShare,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            authority: source.authority.into(),
            total_referrer_rewards: source.total_referrer_rewards.into(),
            total_builder_rewards: source.total_builder_rewards.into(),
            padding: source.padding.to_vec(),
        }
    }
}

impl TryFrom<RevenueShareRow> for crate::accounts::revenue_share::RevenueShare {
    type Error = carbon_core::error::Error;
    fn try_from(source: RevenueShareRow) -> Result<Self, Self::Error> {
        Ok(Self {
            authority: *source.authority,
            total_referrer_rewards: *source.total_referrer_rewards,
            total_builder_rewards: *source.total_builder_rewards,
            padding: source.padding.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 18 bytes"
                        .to_string(),
                )
            })?,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::revenue_share::RevenueShare {
    fn table() -> &'static str {
        "revenue_share_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "authority",
            "total_referrer_rewards",
            "total_builder_rewards",
            "padding",
        ]
    }
}

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

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