carbon-zeta-decoder 2.0.0

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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct SpreadAccountRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub authority: Pubkey,
    pub nonce: U8,
    pub balance: U64,
    pub series_expiry: Vec<U64>,
    pub series_expiry_padding: U64,
    pub positions: sqlx::types::Json<Vec<Position>>,
    pub positions_padding: sqlx::types::Json<Vec<Position>>,
    pub asset: sqlx::types::Json<Asset>,
    pub padding: Vec<u8>,
}

impl SpreadAccountRow {
    pub fn from_parts(
        source: crate::accounts::spread_account::SpreadAccount,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            authority: source.authority.into(),
            nonce: source.nonce.into(),
            balance: source.balance.into(),
            series_expiry: source
                .series_expiry
                .into_iter()
                .map(|element| element.into())
                .collect(),
            series_expiry_padding: source.series_expiry_padding.into(),
            positions: sqlx::types::Json(source.positions.to_vec()),
            positions_padding: sqlx::types::Json(source.positions_padding.to_vec()),
            asset: sqlx::types::Json(source.asset),
            padding: source.padding.to_vec(),
        }
    }
}

impl TryFrom<SpreadAccountRow> for crate::accounts::spread_account::SpreadAccount {
    type Error = carbon_core::error::Error;
    fn try_from(source: SpreadAccountRow) -> Result<Self, Self::Error> {
        Ok(Self {
            authority: *source.authority,
            nonce: source.nonce.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            balance: *source.balance,
            series_expiry: source
                .series_expiry
                .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(),
                    )
                })?,
            series_expiry_padding: *source.series_expiry_padding,
            positions: source
                .positions
                .0
                .into_iter()
                .collect::<Vec<_>>()
                .try_into()
                .map_err(|_| {
                    carbon_core::error::Error::Custom(
                        "Failed to convert value from postgres primitive".to_string(),
                    )
                })?,
            positions_padding: source
                .positions_padding
                .0
                .into_iter()
                .collect::<Vec<_>>()
                .try_into()
                .map_err(|_| {
                    carbon_core::error::Error::Custom(
                        "Failed to convert value from postgres primitive".to_string(),
                    )
                })?,
            asset: source.asset.0,
            padding: source.padding.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 262 bytes"
                        .to_string(),
                )
            })?,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::spread_account::SpreadAccount {
    fn table() -> &'static str {
        "spread_account_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "authority",
            "nonce",
            "balance",
            "series_expiry",
            "series_expiry_padding",
            "positions",
            "positions_padding",
            "asset",
            "padding",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for SpreadAccountRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO spread_account_account (
                "authority",
                "nonce",
                "balance",
                "series_expiry",
                "series_expiry_padding",
                "positions",
                "positions_padding",
                "asset",
                "padding",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
            )"#,
        )
        .bind(self.authority)
        .bind(self.nonce)
        .bind(&self.balance)
        .bind(&self.series_expiry)
        .bind(&self.series_expiry_padding)
        .bind(&self.positions)
        .bind(&self.positions_padding)
        .bind(&self.asset)
        .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 SpreadAccountRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO spread_account_account (
                "authority",
                "nonce",
                "balance",
                "series_expiry",
                "series_expiry_padding",
                "positions",
                "positions_padding",
                "asset",
                "padding",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "authority" = EXCLUDED."authority",
                "nonce" = EXCLUDED."nonce",
                "balance" = EXCLUDED."balance",
                "series_expiry" = EXCLUDED."series_expiry",
                "series_expiry_padding" = EXCLUDED."series_expiry_padding",
                "positions" = EXCLUDED."positions",
                "positions_padding" = EXCLUDED."positions_padding",
                "asset" = EXCLUDED."asset",
                "padding" = EXCLUDED."padding",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.authority)
        .bind(self.nonce)
        .bind(&self.balance)
        .bind(&self.series_expiry)
        .bind(&self.series_expiry_padding)
        .bind(&self.positions)
        .bind(&self.positions_padding)
        .bind(&self.asset)
        .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 SpreadAccountRow {
    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 spread_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 SpreadAccountRow {
    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 spread_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 SpreadAccountMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for SpreadAccountMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS spread_account_account (
                -- Account data
                "authority" BYTEA NOT NULL,
                "nonce" INT2 NOT NULL,
                "balance" NUMERIC(20) NOT NULL,
                "series_expiry" NUMERIC(20)[] NOT NULL,
                "series_expiry_padding" NUMERIC(20) NOT NULL,
                "positions" JSONB NOT NULL,
                "positions_padding" JSONB NOT NULL,
                "asset" JSONB NOT NULL,
                "padding" BYTEA 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 spread_account_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}