carbon-jupiter-dca-decoder 2.0.0

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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct DcaRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub user: Pubkey,
    pub input_mint: Pubkey,
    pub output_mint: Pubkey,
    pub idx: U64,
    pub next_cycle_at: i64,
    pub in_deposited: U64,
    pub in_withdrawn: U64,
    pub out_withdrawn: U64,
    pub in_used: U64,
    pub out_received: U64,
    pub in_amount_per_cycle: U64,
    pub cycle_frequency: i64,
    pub next_cycle_amount_left: U64,
    pub in_account: Pubkey,
    pub out_account: Pubkey,
    pub min_out_amount: U64,
    pub max_out_amount: U64,
    pub keeper_in_balance_before_borrow: U64,
    pub dca_out_balance_before_swap: U64,
    pub created_at: i64,
    pub bump: U8,
}

impl DcaRow {
    pub fn from_parts(source: crate::accounts::dca::Dca, metadata: AccountMetadata) -> Self {
        Self {
            account_metadata: metadata.into(),
            user: source.user.into(),
            input_mint: source.input_mint.into(),
            output_mint: source.output_mint.into(),
            idx: source.idx.into(),
            next_cycle_at: source.next_cycle_at,
            in_deposited: source.in_deposited.into(),
            in_withdrawn: source.in_withdrawn.into(),
            out_withdrawn: source.out_withdrawn.into(),
            in_used: source.in_used.into(),
            out_received: source.out_received.into(),
            in_amount_per_cycle: source.in_amount_per_cycle.into(),
            cycle_frequency: source.cycle_frequency,
            next_cycle_amount_left: source.next_cycle_amount_left.into(),
            in_account: source.in_account.into(),
            out_account: source.out_account.into(),
            min_out_amount: source.min_out_amount.into(),
            max_out_amount: source.max_out_amount.into(),
            keeper_in_balance_before_borrow: source.keeper_in_balance_before_borrow.into(),
            dca_out_balance_before_swap: source.dca_out_balance_before_swap.into(),
            created_at: source.created_at,
            bump: source.bump.into(),
        }
    }
}

impl TryFrom<DcaRow> for crate::accounts::dca::Dca {
    type Error = carbon_core::error::Error;
    fn try_from(source: DcaRow) -> Result<Self, Self::Error> {
        Ok(Self {
            user: *source.user,
            input_mint: *source.input_mint,
            output_mint: *source.output_mint,
            idx: *source.idx,
            next_cycle_at: source.next_cycle_at,
            in_deposited: *source.in_deposited,
            in_withdrawn: *source.in_withdrawn,
            out_withdrawn: *source.out_withdrawn,
            in_used: *source.in_used,
            out_received: *source.out_received,
            in_amount_per_cycle: *source.in_amount_per_cycle,
            cycle_frequency: source.cycle_frequency,
            next_cycle_amount_left: *source.next_cycle_amount_left,
            in_account: *source.in_account,
            out_account: *source.out_account,
            min_out_amount: *source.min_out_amount,
            max_out_amount: *source.max_out_amount,
            keeper_in_balance_before_borrow: *source.keeper_in_balance_before_borrow,
            dca_out_balance_before_swap: *source.dca_out_balance_before_swap,
            created_at: source.created_at,
            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::dca::Dca {
    fn table() -> &'static str {
        "dca_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "user",
            "input_mint",
            "output_mint",
            "idx",
            "next_cycle_at",
            "in_deposited",
            "in_withdrawn",
            "out_withdrawn",
            "in_used",
            "out_received",
            "in_amount_per_cycle",
            "cycle_frequency",
            "next_cycle_amount_left",
            "in_account",
            "out_account",
            "min_out_amount",
            "max_out_amount",
            "keeper_in_balance_before_borrow",
            "dca_out_balance_before_swap",
            "created_at",
            "bump",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for DcaRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(r#"
            INSERT INTO dca_account (
                "user",
                "input_mint",
                "output_mint",
                "idx",
                "next_cycle_at",
                "in_deposited",
                "in_withdrawn",
                "out_withdrawn",
                "in_used",
                "out_received",
                "in_amount_per_cycle",
                "cycle_frequency",
                "next_cycle_amount_left",
                "in_account",
                "out_account",
                "min_out_amount",
                "max_out_amount",
                "keeper_in_balance_before_borrow",
                "dca_out_balance_before_swap",
                "created_at",
                "bump",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23
            )"#)
        .bind(self.user)
        .bind(self.input_mint)
        .bind(self.output_mint)
        .bind(&self.idx)
        .bind(self.next_cycle_at)
        .bind(&self.in_deposited)
        .bind(&self.in_withdrawn)
        .bind(&self.out_withdrawn)
        .bind(&self.in_used)
        .bind(&self.out_received)
        .bind(&self.in_amount_per_cycle)
        .bind(self.cycle_frequency)
        .bind(&self.next_cycle_amount_left)
        .bind(self.in_account)
        .bind(self.out_account)
        .bind(&self.min_out_amount)
        .bind(&self.max_out_amount)
        .bind(&self.keeper_in_balance_before_borrow)
        .bind(&self.dca_out_balance_before_swap)
        .bind(self.created_at)
        .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 DcaRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(r#"INSERT INTO dca_account (
                "user",
                "input_mint",
                "output_mint",
                "idx",
                "next_cycle_at",
                "in_deposited",
                "in_withdrawn",
                "out_withdrawn",
                "in_used",
                "out_received",
                "in_amount_per_cycle",
                "cycle_frequency",
                "next_cycle_amount_left",
                "in_account",
                "out_account",
                "min_out_amount",
                "max_out_amount",
                "keeper_in_balance_before_borrow",
                "dca_out_balance_before_swap",
                "created_at",
                "bump",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "user" = EXCLUDED."user",
                "input_mint" = EXCLUDED."input_mint",
                "output_mint" = EXCLUDED."output_mint",
                "idx" = EXCLUDED."idx",
                "next_cycle_at" = EXCLUDED."next_cycle_at",
                "in_deposited" = EXCLUDED."in_deposited",
                "in_withdrawn" = EXCLUDED."in_withdrawn",
                "out_withdrawn" = EXCLUDED."out_withdrawn",
                "in_used" = EXCLUDED."in_used",
                "out_received" = EXCLUDED."out_received",
                "in_amount_per_cycle" = EXCLUDED."in_amount_per_cycle",
                "cycle_frequency" = EXCLUDED."cycle_frequency",
                "next_cycle_amount_left" = EXCLUDED."next_cycle_amount_left",
                "in_account" = EXCLUDED."in_account",
                "out_account" = EXCLUDED."out_account",
                "min_out_amount" = EXCLUDED."min_out_amount",
                "max_out_amount" = EXCLUDED."max_out_amount",
                "keeper_in_balance_before_borrow" = EXCLUDED."keeper_in_balance_before_borrow",
                "dca_out_balance_before_swap" = EXCLUDED."dca_out_balance_before_swap",
                "created_at" = EXCLUDED."created_at",
                "bump" = EXCLUDED."bump",
                __slot = EXCLUDED.__slot
            "#)
        .bind(self.user)
        .bind(self.input_mint)
        .bind(self.output_mint)
        .bind(&self.idx)
        .bind(self.next_cycle_at)
        .bind(&self.in_deposited)
        .bind(&self.in_withdrawn)
        .bind(&self.out_withdrawn)
        .bind(&self.in_used)
        .bind(&self.out_received)
        .bind(&self.in_amount_per_cycle)
        .bind(self.cycle_frequency)
        .bind(&self.next_cycle_amount_left)
        .bind(self.in_account)
        .bind(self.out_account)
        .bind(&self.min_out_amount)
        .bind(&self.max_out_amount)
        .bind(&self.keeper_in_balance_before_borrow)
        .bind(&self.dca_out_balance_before_swap)
        .bind(self.created_at)
        .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 DcaRow {
    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 dca_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 DcaRow {
    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 dca_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 DcaMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for DcaMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS dca_account (
                -- Account data
                "user" BYTEA NOT NULL,
                "input_mint" BYTEA NOT NULL,
                "output_mint" BYTEA NOT NULL,
                "idx" NUMERIC(20) NOT NULL,
                "next_cycle_at" INT8 NOT NULL,
                "in_deposited" NUMERIC(20) NOT NULL,
                "in_withdrawn" NUMERIC(20) NOT NULL,
                "out_withdrawn" NUMERIC(20) NOT NULL,
                "in_used" NUMERIC(20) NOT NULL,
                "out_received" NUMERIC(20) NOT NULL,
                "in_amount_per_cycle" NUMERIC(20) NOT NULL,
                "cycle_frequency" INT8 NOT NULL,
                "next_cycle_amount_left" NUMERIC(20) NOT NULL,
                "in_account" BYTEA NOT NULL,
                "out_account" BYTEA NOT NULL,
                "min_out_amount" NUMERIC(20) NOT NULL,
                "max_out_amount" NUMERIC(20) NOT NULL,
                "keeper_in_balance_before_borrow" NUMERIC(20) NOT NULL,
                "dca_out_balance_before_swap" NUMERIC(20) NOT NULL,
                "created_at" INT8 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 dca_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}