carbon-drift-v2-decoder 2.0.0

Drift v2 Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use carbon_core::{
    account::AccountMetadata,
    postgres::{
        metadata::AccountRowMetadata,
        primitives::{Pubkey, U128, U32},
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct FuelOverflowRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub authority: Pubkey,
    pub fuel_insurance: U128,
    pub fuel_deposits: U128,
    pub fuel_borrows: U128,
    pub fuel_positions: U128,
    pub fuel_taker: U128,
    pub fuel_maker: U128,
    pub last_fuel_sweep_ts: U32,
    pub last_reset_ts: U32,
    pub padding: Vec<U128>,
}

impl FuelOverflowRow {
    pub fn from_parts(
        source: crate::accounts::fuel_overflow::FuelOverflow,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            authority: source.authority.into(),
            fuel_insurance: source.fuel_insurance.into(),
            fuel_deposits: source.fuel_deposits.into(),
            fuel_borrows: source.fuel_borrows.into(),
            fuel_positions: source.fuel_positions.into(),
            fuel_taker: source.fuel_taker.into(),
            fuel_maker: source.fuel_maker.into(),
            last_fuel_sweep_ts: source.last_fuel_sweep_ts.into(),
            last_reset_ts: source.last_reset_ts.into(),
            padding: source
                .padding
                .into_iter()
                .map(|element| element.into())
                .collect(),
        }
    }
}

impl TryFrom<FuelOverflowRow> for crate::accounts::fuel_overflow::FuelOverflow {
    type Error = carbon_core::error::Error;
    fn try_from(source: FuelOverflowRow) -> Result<Self, Self::Error> {
        Ok(Self {
            authority: *source.authority,
            fuel_insurance: *source.fuel_insurance,
            fuel_deposits: *source.fuel_deposits,
            fuel_borrows: *source.fuel_borrows,
            fuel_positions: *source.fuel_positions,
            fuel_taker: *source.fuel_taker,
            fuel_maker: *source.fuel_maker,
            last_fuel_sweep_ts: source.last_fuel_sweep_ts.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            last_reset_ts: source.last_reset_ts.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            padding: source
                .padding
                .into_iter()
                .map(|element| Ok(*element))
                .collect::<Result<Vec<_>, carbon_core::error::Error>>()
                .map_err(|_| {
                    carbon_core::error::Error::Custom(
                        "Failed to collect array elements".to_string(),
                    )
                })?
                .try_into()
                .map_err(|_| {
                    carbon_core::error::Error::Custom(
                        "Failed to convert array element to primitive".to_string(),
                    )
                })?,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::fuel_overflow::FuelOverflow {
    fn table() -> &'static str {
        "fuel_overflow_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "authority",
            "fuel_insurance",
            "fuel_deposits",
            "fuel_borrows",
            "fuel_positions",
            "fuel_taker",
            "fuel_maker",
            "last_fuel_sweep_ts",
            "last_reset_ts",
            "padding",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for FuelOverflowRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO fuel_overflow_account (
                "authority",
                "fuel_insurance",
                "fuel_deposits",
                "fuel_borrows",
                "fuel_positions",
                "fuel_taker",
                "fuel_maker",
                "last_fuel_sweep_ts",
                "last_reset_ts",
                "padding",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
            )"#,
        )
        .bind(self.authority)
        .bind(&self.fuel_insurance)
        .bind(&self.fuel_deposits)
        .bind(&self.fuel_borrows)
        .bind(&self.fuel_positions)
        .bind(&self.fuel_taker)
        .bind(&self.fuel_maker)
        .bind(self.last_fuel_sweep_ts)
        .bind(self.last_reset_ts)
        .bind(&self.padding)
        .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 FuelOverflowRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO fuel_overflow_account (
                "authority",
                "fuel_insurance",
                "fuel_deposits",
                "fuel_borrows",
                "fuel_positions",
                "fuel_taker",
                "fuel_maker",
                "last_fuel_sweep_ts",
                "last_reset_ts",
                "padding",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "authority" = EXCLUDED."authority",
                "fuel_insurance" = EXCLUDED."fuel_insurance",
                "fuel_deposits" = EXCLUDED."fuel_deposits",
                "fuel_borrows" = EXCLUDED."fuel_borrows",
                "fuel_positions" = EXCLUDED."fuel_positions",
                "fuel_taker" = EXCLUDED."fuel_taker",
                "fuel_maker" = EXCLUDED."fuel_maker",
                "last_fuel_sweep_ts" = EXCLUDED."last_fuel_sweep_ts",
                "last_reset_ts" = EXCLUDED."last_reset_ts",
                "padding" = EXCLUDED."padding",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.authority)
        .bind(&self.fuel_insurance)
        .bind(&self.fuel_deposits)
        .bind(&self.fuel_borrows)
        .bind(&self.fuel_positions)
        .bind(&self.fuel_taker)
        .bind(&self.fuel_maker)
        .bind(self.last_fuel_sweep_ts)
        .bind(self.last_reset_ts)
        .bind(&self.padding)
        .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 FuelOverflowRow {
    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 fuel_overflow_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 FuelOverflowRow {
    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 fuel_overflow_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 FuelOverflowMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for FuelOverflowMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS fuel_overflow_account (
                -- Account data
                "authority" BYTEA NOT NULL,
                "fuel_insurance" NUMERIC(39) NOT NULL,
                "fuel_deposits" NUMERIC(39) NOT NULL,
                "fuel_borrows" NUMERIC(39) NOT NULL,
                "fuel_positions" NUMERIC(39) NOT NULL,
                "fuel_taker" NUMERIC(39) NOT NULL,
                "fuel_maker" NUMERIC(39) NOT NULL,
                "last_fuel_sweep_ts" INT8 NOT NULL,
                "last_reset_ts" INT8 NOT NULL,
                "padding" NUMERIC(39)[] 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 fuel_overflow_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}