carbon-bonkswap-decoder 2.0.0

Carbon BonkSwap Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::Token,
    carbon_core::{
        instruction::InstructionMetadata,
        postgres::{metadata::InstructionRowMetadata, primitives::U64},
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct AddSupplyRow {
    #[sqlx(flatten)]
    pub instruction_metadata: InstructionRowMetadata,
    pub supply_marco: sqlx::types::Json<Token>,
    pub supply_project_first: sqlx::types::Json<Token>,
    pub supply_project_second: sqlx::types::Json<Token>,
    pub duration: U64,
    #[sqlx(rename = "__accounts")]
    pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}

impl AddSupplyRow {
    pub fn from_parts(
        source: crate::instructions::add_supply::AddSupply,
        metadata: InstructionMetadata,
        accounts: Vec<solana_instruction::AccountMeta>,
    ) -> Self {
        Self {
            instruction_metadata: metadata.into(),
            supply_marco: sqlx::types::Json(source.supply_marco),
            supply_project_first: sqlx::types::Json(source.supply_project_first),
            supply_project_second: sqlx::types::Json(source.supply_project_second),
            duration: source.duration.into(),
            accounts: sqlx::types::Json(accounts),
        }
    }
}

impl TryFrom<AddSupplyRow> for crate::instructions::add_supply::AddSupply {
    type Error = carbon_core::error::Error;
    fn try_from(source: AddSupplyRow) -> Result<Self, Self::Error> {
        Ok(Self {
            supply_marco: source.supply_marco.0,
            supply_project_first: source.supply_project_first.0,
            supply_project_second: source.supply_project_second.0,
            duration: *source.duration,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::instructions::add_supply::AddSupply {
    fn table() -> &'static str {
        "add_supply_instruction"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__signature",
            "__instruction_index",
            "__stack_height",
            "__slot",
            "supply_marco",
            "supply_project_first",
            "supply_project_second",
            "duration",
            "__accounts",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for AddSupplyRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO add_supply_instruction (
                "supply_marco",
                "supply_project_first",
                "supply_project_second",
                "duration",
                __signature, __instruction_index, __stack_height, __slot, __accounts
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9
            )"#,
        )
        .bind(&self.supply_marco)
        .bind(&self.supply_project_first)
        .bind(&self.supply_project_second)
        .bind(&self.duration)
        .bind(&self.instruction_metadata.signature)
        .bind(self.instruction_metadata.instruction_index)
        .bind(self.instruction_metadata.stack_height)
        .bind(&self.instruction_metadata.slot)
        .bind(&self.accounts)
        .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 AddSupplyRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO add_supply_instruction (
                "supply_marco",
                "supply_project_first",
                "supply_project_second",
                "duration",
                __signature, __instruction_index, __stack_height, __slot, __accounts
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9
            ) ON CONFLICT (
                __signature, __instruction_index, __stack_height
            ) DO UPDATE SET
                "supply_marco" = EXCLUDED."supply_marco",
                "supply_project_first" = EXCLUDED."supply_project_first",
                "supply_project_second" = EXCLUDED."supply_project_second",
                "duration" = EXCLUDED."duration",
                __instruction_index = EXCLUDED.__instruction_index,
                __stack_height = EXCLUDED.__stack_height,
                __slot = EXCLUDED.__slot,
                __accounts = EXCLUDED.__accounts
            "#,
        )
        .bind(&self.supply_marco)
        .bind(&self.supply_project_first)
        .bind(&self.supply_project_second)
        .bind(&self.duration)
        .bind(&self.instruction_metadata.signature)
        .bind(self.instruction_metadata.instruction_index)
        .bind(self.instruction_metadata.stack_height)
        .bind(&self.instruction_metadata.slot)
        .bind(&self.accounts)
        .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 AddSupplyRow {
    type Key = (
        String,
        carbon_core::postgres::primitives::U32,
        carbon_core::postgres::primitives::U32,
    );

    async fn delete(key: Self::Key, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"DELETE FROM add_supply_instruction WHERE
                __signature = $1 AND __instruction_index = $2 AND __stack_height = $3
            "#,
        )
        .bind(key.0)
        .bind(key.1)
        .bind(key.2)
        .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 AddSupplyRow {
    type Key = (
        String,
        carbon_core::postgres::primitives::U32,
        carbon_core::postgres::primitives::U32,
    );

    async fn lookup(
        key: Self::Key,
        pool: &sqlx::PgPool,
    ) -> carbon_core::error::CarbonResult<Option<Self>> {
        let row = sqlx::query_as(
            r#"SELECT * FROM add_supply_instruction WHERE
                __signature = $1 AND __instruction_index = $2 AND __stack_height = $3
            "#,
        )
        .bind(key.0)
        .bind(key.1)
        .bind(key.2)
        .fetch_optional(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(row)
    }
}

pub struct AddSupplyMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for AddSupplyMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS add_supply_instruction (
                -- Instruction data
                "supply_marco" JSONB NOT NULL,
                "supply_project_first" JSONB NOT NULL,
                "supply_project_second" JSONB NOT NULL,
                "duration" NUMERIC(20) NOT NULL,
                -- Instruction metadata
                __signature TEXT NOT NULL,
                __instruction_index BIGINT NOT NULL,
                __stack_height BIGINT NOT NULL,
                __slot NUMERIC(20),
                __accounts JSONB NOT NULL,
                PRIMARY KEY (__signature, __instruction_index, __stack_height)
            )"#,
        )
        .execute(connection)
        .await?;
        Ok(())
    }

    async fn down(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(r#"DROP TABLE IF EXISTS add_supply_instruction"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}