carbon-sharky-decoder 2.0.0

Sharky Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::{PriceFeedMessage, VerificationLevel},
    carbon_core::{
        account::AccountMetadata,
        postgres::{
            metadata::AccountRowMetadata,
            primitives::{Pubkey, U64},
        },
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct PriceUpdateV2Row {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub write_authority: Pubkey,
    pub verification_level: sqlx::types::Json<VerificationLevel>,
    pub price_message: sqlx::types::Json<PriceFeedMessage>,
    pub posted_slot: U64,
}

impl PriceUpdateV2Row {
    pub fn from_parts(
        source: crate::accounts::price_update_v2::PriceUpdateV2,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            write_authority: source.write_authority.into(),
            verification_level: sqlx::types::Json(source.verification_level),
            price_message: sqlx::types::Json(source.price_message),
            posted_slot: source.posted_slot.into(),
        }
    }
}

impl TryFrom<PriceUpdateV2Row> for crate::accounts::price_update_v2::PriceUpdateV2 {
    type Error = carbon_core::error::Error;
    fn try_from(source: PriceUpdateV2Row) -> Result<Self, Self::Error> {
        Ok(Self {
            write_authority: *source.write_authority,
            verification_level: source.verification_level.0,
            price_message: source.price_message.0,
            posted_slot: *source.posted_slot,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::price_update_v2::PriceUpdateV2 {
    fn table() -> &'static str {
        "price_update_v2_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "write_authority",
            "verification_level",
            "price_message",
            "posted_slot",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for PriceUpdateV2Row {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO price_update_v2_account (
                "write_authority",
                "verification_level",
                "price_message",
                "posted_slot",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6
            )"#,
        )
        .bind(self.write_authority)
        .bind(&self.verification_level)
        .bind(&self.price_message)
        .bind(&self.posted_slot)
        .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 PriceUpdateV2Row {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO price_update_v2_account (
                "write_authority",
                "verification_level",
                "price_message",
                "posted_slot",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "write_authority" = EXCLUDED."write_authority",
                "verification_level" = EXCLUDED."verification_level",
                "price_message" = EXCLUDED."price_message",
                "posted_slot" = EXCLUDED."posted_slot",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.write_authority)
        .bind(&self.verification_level)
        .bind(&self.price_message)
        .bind(&self.posted_slot)
        .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 PriceUpdateV2Row {
    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 price_update_v2_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 PriceUpdateV2Row {
    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 price_update_v2_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 PriceUpdateV2MigrationOperation;

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