carbon-orca-whirlpool-decoder 1.0.0

Orca Whirlpool Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::DynamicTick,
    carbon_core::{
        account::AccountMetadata,
        postgres::{
            metadata::AccountRowMetadata,
            primitives::{Pubkey, U128},
        },
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct DynamicTickArrayRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub start_tick_index: i32,
    pub whirlpool: Pubkey,
    pub tick_bitmap: U128,
    pub ticks: sqlx::types::Json<Vec<DynamicTick>>,
}

impl DynamicTickArrayRow {
    pub fn from_parts(
        source: crate::accounts::dynamic_tick_array::DynamicTickArray,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            start_tick_index: source.start_tick_index,
            whirlpool: source.whirlpool.into(),
            tick_bitmap: source.tick_bitmap.into(),
            ticks: sqlx::types::Json(source.ticks.to_vec()),
        }
    }
}

impl TryFrom<DynamicTickArrayRow> for crate::accounts::dynamic_tick_array::DynamicTickArray {
    type Error = carbon_core::error::Error;
    fn try_from(source: DynamicTickArrayRow) -> Result<Self, Self::Error> {
        Ok(Self {
            start_tick_index: source.start_tick_index,
            whirlpool: *source.whirlpool,
            tick_bitmap: *source.tick_bitmap,
            ticks: source
                .ticks
                .0
                .into_iter()
                .collect::<Vec<_>>()
                .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::dynamic_tick_array::DynamicTickArray
{
    fn table() -> &'static str {
        "dynamic_tick_array_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "start_tick_index",
            "whirlpool",
            "tick_bitmap",
            "ticks",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for DynamicTickArrayRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO dynamic_tick_array_account (
                "start_tick_index",
                "whirlpool",
                "tick_bitmap",
                "ticks",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6
            )"#,
        )
        .bind(self.start_tick_index)
        .bind(self.whirlpool)
        .bind(&self.tick_bitmap)
        .bind(&self.ticks)
        .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 DynamicTickArrayRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO dynamic_tick_array_account (
                "start_tick_index",
                "whirlpool",
                "tick_bitmap",
                "ticks",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "start_tick_index" = EXCLUDED."start_tick_index",
                "whirlpool" = EXCLUDED."whirlpool",
                "tick_bitmap" = EXCLUDED."tick_bitmap",
                "ticks" = EXCLUDED."ticks",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.start_tick_index)
        .bind(self.whirlpool)
        .bind(&self.tick_bitmap)
        .bind(&self.ticks)
        .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 DynamicTickArrayRow {
    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 dynamic_tick_array_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 DynamicTickArrayRow {
    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 dynamic_tick_array_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 DynamicTickArrayMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for DynamicTickArrayMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS dynamic_tick_array_account (
                -- Account data
                "start_tick_index" INT4 NOT NULL,
                "whirlpool" BYTEA NOT NULL,
                "tick_bitmap" NUMERIC(39) NOT NULL,
                "ticks" 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 dynamic_tick_array_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}