carbon-zeta-decoder 2.0.0

Zeta Program Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use carbon_core::{
    account::AccountMetadata,
    postgres::{
        metadata::AccountRowMetadata,
        primitives::{U64, U8},
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct SocializedLossAccountRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub nonce: U8,
    pub overbankrupt_amount: U64,
}

impl SocializedLossAccountRow {
    pub fn from_parts(
        source: crate::accounts::socialized_loss_account::SocializedLossAccount,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            nonce: source.nonce.into(),
            overbankrupt_amount: source.overbankrupt_amount.into(),
        }
    }
}

impl TryFrom<SocializedLossAccountRow>
    for crate::accounts::socialized_loss_account::SocializedLossAccount
{
    type Error = carbon_core::error::Error;
    fn try_from(source: SocializedLossAccountRow) -> Result<Self, Self::Error> {
        Ok(Self {
            nonce: source.nonce.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            overbankrupt_amount: *source.overbankrupt_amount,
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::accounts::socialized_loss_account::SocializedLossAccount
{
    fn table() -> &'static str {
        "socialized_loss_account_account"
    }

    fn columns() -> Vec<&'static str> {
        vec!["__pubkey", "__slot", "nonce", "overbankrupt_amount"]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for SocializedLossAccountRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO socialized_loss_account_account (
                "nonce",
                "overbankrupt_amount",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4
            )"#,
        )
        .bind(self.nonce)
        .bind(&self.overbankrupt_amount)
        .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 SocializedLossAccountRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO socialized_loss_account_account (
                "nonce",
                "overbankrupt_amount",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "nonce" = EXCLUDED."nonce",
                "overbankrupt_amount" = EXCLUDED."overbankrupt_amount",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.nonce)
        .bind(&self.overbankrupt_amount)
        .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 SocializedLossAccountRow {
    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 socialized_loss_account_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 SocializedLossAccountRow {
    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 socialized_loss_account_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 SocializedLossAccountMigrationOperation;

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