carbon-virtuals-decoder 1.0.0

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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct VirtualsPoolRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub creator: Pubkey,
    pub mint: Pubkey,
    pub virtual_y: U64,
    pub graduation_x: U64,
    pub state: sqlx::types::Json<PoolState>,
    pub bump: U8,
}

impl VirtualsPoolRow {
    pub fn from_parts(
        source: crate::accounts::virtuals_pool::VirtualsPool,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            creator: source.creator.into(),
            mint: source.mint.into(),
            virtual_y: source.virtual_y.into(),
            graduation_x: source.graduation_x.into(),
            state: sqlx::types::Json(source.state),
            bump: source.bump.into(),
        }
    }
}

impl TryFrom<VirtualsPoolRow> for crate::accounts::virtuals_pool::VirtualsPool {
    type Error = carbon_core::error::Error;
    fn try_from(source: VirtualsPoolRow) -> Result<Self, Self::Error> {
        Ok(Self {
            creator: *source.creator,
            mint: *source.mint,
            virtual_y: *source.virtual_y,
            graduation_x: *source.graduation_x,
            state: source.state.0,
            bump: source.bump.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::virtuals_pool::VirtualsPool {
    fn table() -> &'static str {
        "virtuals_pool_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "creator",
            "mint",
            "virtual_y",
            "graduation_x",
            "state",
            "bump",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for VirtualsPoolRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO virtuals_pool_account (
                "creator",
                "mint",
                "virtual_y",
                "graduation_x",
                "state",
                "bump",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8
            )"#,
        )
        .bind(self.creator)
        .bind(self.mint)
        .bind(&self.virtual_y)
        .bind(&self.graduation_x)
        .bind(&self.state)
        .bind(self.bump)
        .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 VirtualsPoolRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO virtuals_pool_account (
                "creator",
                "mint",
                "virtual_y",
                "graduation_x",
                "state",
                "bump",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "creator" = EXCLUDED."creator",
                "mint" = EXCLUDED."mint",
                "virtual_y" = EXCLUDED."virtual_y",
                "graduation_x" = EXCLUDED."graduation_x",
                "state" = EXCLUDED."state",
                "bump" = EXCLUDED."bump",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.creator)
        .bind(self.mint)
        .bind(&self.virtual_y)
        .bind(&self.graduation_x)
        .bind(&self.state)
        .bind(self.bump)
        .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 VirtualsPoolRow {
    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 virtuals_pool_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 VirtualsPoolRow {
    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 virtuals_pool_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 VirtualsPoolMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for VirtualsPoolMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS virtuals_pool_account (
                -- Account data
                "creator" BYTEA NOT NULL,
                "mint" BYTEA NOT NULL,
                "virtual_y" NUMERIC(20) NOT NULL,
                "graduation_x" NUMERIC(20) NOT NULL,
                "state" JSONB NOT NULL,
                "bump" INT2 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 virtuals_pool_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}