carbon-pump-swap-decoder 1.0.0

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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct SharingConfigRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub bump: U8,
    pub version: U8,
    pub status: sqlx::types::Json<ConfigStatus>,
    pub mint: Pubkey,
    pub admin: Pubkey,
    pub admin_revoked: bool,
    pub shareholders: sqlx::types::Json<Vec<Shareholder>>,
}

impl SharingConfigRow {
    pub fn from_parts(
        source: crate::accounts::sharing_config::SharingConfig,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            bump: source.bump.into(),
            version: source.version.into(),
            status: sqlx::types::Json(source.status),
            mint: source.mint.into(),
            admin: source.admin.into(),
            admin_revoked: source.admin_revoked,
            shareholders: sqlx::types::Json(source.shareholders.to_vec()),
        }
    }
}

impl TryFrom<SharingConfigRow> for crate::accounts::sharing_config::SharingConfig {
    type Error = carbon_core::error::Error;
    fn try_from(source: SharingConfigRow) -> Result<Self, Self::Error> {
        Ok(Self {
            bump: source.bump.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            version: source.version.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            status: source.status.0,
            mint: *source.mint,
            admin: *source.admin,
            admin_revoked: source.admin_revoked,
            shareholders: source.shareholders.0,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::sharing_config::SharingConfig {
    fn table() -> &'static str {
        "sharing_config_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "bump",
            "version",
            "status",
            "mint",
            "admin",
            "admin_revoked",
            "shareholders",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for SharingConfigRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO sharing_config_account (
                "bump",
                "version",
                "status",
                "mint",
                "admin",
                "admin_revoked",
                "shareholders",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9
            )"#,
        )
        .bind(self.bump)
        .bind(self.version)
        .bind(&self.status)
        .bind(self.mint)
        .bind(self.admin)
        .bind(self.admin_revoked)
        .bind(&self.shareholders)
        .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 SharingConfigRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO sharing_config_account (
                "bump",
                "version",
                "status",
                "mint",
                "admin",
                "admin_revoked",
                "shareholders",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "bump" = EXCLUDED."bump",
                "version" = EXCLUDED."version",
                "status" = EXCLUDED."status",
                "mint" = EXCLUDED."mint",
                "admin" = EXCLUDED."admin",
                "admin_revoked" = EXCLUDED."admin_revoked",
                "shareholders" = EXCLUDED."shareholders",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.bump)
        .bind(self.version)
        .bind(&self.status)
        .bind(self.mint)
        .bind(self.admin)
        .bind(self.admin_revoked)
        .bind(&self.shareholders)
        .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 SharingConfigRow {
    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 sharing_config_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 SharingConfigRow {
    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 sharing_config_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 SharingConfigMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for SharingConfigMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS sharing_config_account (
                -- Account data
                "bump" INT2 NOT NULL,
                "version" INT2 NOT NULL,
                "status" JSONB NOT NULL,
                "mint" BYTEA NOT NULL,
                "admin" BYTEA NOT NULL,
                "admin_revoked" BOOLEAN NOT NULL,
                "shareholders" 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 sharing_config_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}