use {
crate::types::PositionRewardInfo,
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U128, U64},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct PositionRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub whirlpool: Pubkey,
pub position_mint: Pubkey,
pub liquidity: U128,
pub tick_lower_index: i32,
pub tick_upper_index: i32,
pub fee_growth_checkpoint_a: U128,
pub fee_owed_a: U64,
pub fee_growth_checkpoint_b: U128,
pub fee_owed_b: U64,
pub reward_infos: sqlx::types::Json<Vec<PositionRewardInfo>>,
}
impl PositionRow {
pub fn from_parts(
source: crate::accounts::position::Position,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
whirlpool: source.whirlpool.into(),
position_mint: source.position_mint.into(),
liquidity: source.liquidity.into(),
tick_lower_index: source.tick_lower_index,
tick_upper_index: source.tick_upper_index,
fee_growth_checkpoint_a: source.fee_growth_checkpoint_a.into(),
fee_owed_a: source.fee_owed_a.into(),
fee_growth_checkpoint_b: source.fee_growth_checkpoint_b.into(),
fee_owed_b: source.fee_owed_b.into(),
reward_infos: sqlx::types::Json(source.reward_infos.to_vec()),
}
}
}
impl TryFrom<PositionRow> for crate::accounts::position::Position {
type Error = carbon_core::error::Error;
fn try_from(source: PositionRow) -> Result<Self, Self::Error> {
Ok(Self {
whirlpool: *source.whirlpool,
position_mint: *source.position_mint,
liquidity: *source.liquidity,
tick_lower_index: source.tick_lower_index,
tick_upper_index: source.tick_upper_index,
fee_growth_checkpoint_a: *source.fee_growth_checkpoint_a,
fee_owed_a: *source.fee_owed_a,
fee_growth_checkpoint_b: *source.fee_growth_checkpoint_b,
fee_owed_b: *source.fee_owed_b,
reward_infos: source
.reward_infos
.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::position::Position {
fn table() -> &'static str {
"position_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"whirlpool",
"position_mint",
"liquidity",
"tick_lower_index",
"tick_upper_index",
"fee_growth_checkpoint_a",
"fee_owed_a",
"fee_growth_checkpoint_b",
"fee_owed_b",
"reward_infos",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for PositionRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO position_account (
"whirlpool",
"position_mint",
"liquidity",
"tick_lower_index",
"tick_upper_index",
"fee_growth_checkpoint_a",
"fee_owed_a",
"fee_growth_checkpoint_b",
"fee_owed_b",
"reward_infos",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
)"#,
)
.bind(self.whirlpool)
.bind(self.position_mint)
.bind(&self.liquidity)
.bind(self.tick_lower_index)
.bind(self.tick_upper_index)
.bind(&self.fee_growth_checkpoint_a)
.bind(&self.fee_owed_a)
.bind(&self.fee_growth_checkpoint_b)
.bind(&self.fee_owed_b)
.bind(&self.reward_infos)
.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 PositionRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO position_account (
"whirlpool",
"position_mint",
"liquidity",
"tick_lower_index",
"tick_upper_index",
"fee_growth_checkpoint_a",
"fee_owed_a",
"fee_growth_checkpoint_b",
"fee_owed_b",
"reward_infos",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"whirlpool" = EXCLUDED."whirlpool",
"position_mint" = EXCLUDED."position_mint",
"liquidity" = EXCLUDED."liquidity",
"tick_lower_index" = EXCLUDED."tick_lower_index",
"tick_upper_index" = EXCLUDED."tick_upper_index",
"fee_growth_checkpoint_a" = EXCLUDED."fee_growth_checkpoint_a",
"fee_owed_a" = EXCLUDED."fee_owed_a",
"fee_growth_checkpoint_b" = EXCLUDED."fee_growth_checkpoint_b",
"fee_owed_b" = EXCLUDED."fee_owed_b",
"reward_infos" = EXCLUDED."reward_infos",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.whirlpool)
.bind(self.position_mint)
.bind(&self.liquidity)
.bind(self.tick_lower_index)
.bind(self.tick_upper_index)
.bind(&self.fee_growth_checkpoint_a)
.bind(&self.fee_owed_a)
.bind(&self.fee_growth_checkpoint_b)
.bind(&self.fee_owed_b)
.bind(&self.reward_infos)
.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 PositionRow {
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 position_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 PositionRow {
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 position_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 PositionMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for PositionMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS position_account (
-- Account data
"whirlpool" BYTEA NOT NULL,
"position_mint" BYTEA NOT NULL,
"liquidity" NUMERIC(39) NOT NULL,
"tick_lower_index" INT4 NOT NULL,
"tick_upper_index" INT4 NOT NULL,
"fee_growth_checkpoint_a" NUMERIC(39) NOT NULL,
"fee_owed_a" NUMERIC(20) NOT NULL,
"fee_growth_checkpoint_b" NUMERIC(39) NOT NULL,
"fee_owed_b" NUMERIC(20) NOT NULL,
"reward_infos" 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 position_account"#)
.execute(connection)
.await?;
Ok(())
}
}