use {
crate::types::LpPosition,
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct LpPositionAccountRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub authority: Pubkey,
pub pool: Pubkey,
pub status: U64,
pub lp_position: sqlx::types::Json<LpPosition>,
}
impl LpPositionAccountRow {
pub fn from_parts(
source: crate::accounts::lp_position_account::LpPositionAccount,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
authority: source.authority.into(),
pool: source.pool.into(),
status: source.status.into(),
lp_position: sqlx::types::Json(source.lp_position),
}
}
}
impl TryFrom<LpPositionAccountRow> for crate::accounts::lp_position_account::LpPositionAccount {
type Error = carbon_core::error::Error;
fn try_from(source: LpPositionAccountRow) -> Result<Self, Self::Error> {
Ok(Self {
authority: *source.authority,
pool: *source.pool,
status: *source.status,
lp_position: source.lp_position.0,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::lp_position_account::LpPositionAccount
{
fn table() -> &'static str {
"lp_position_account_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"authority",
"pool",
"status",
"lp_position",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for LpPositionAccountRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO lp_position_account_account (
"authority",
"pool",
"status",
"lp_position",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6
)"#,
)
.bind(self.authority)
.bind(self.pool)
.bind(&self.status)
.bind(&self.lp_position)
.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 LpPositionAccountRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO lp_position_account_account (
"authority",
"pool",
"status",
"lp_position",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"authority" = EXCLUDED."authority",
"pool" = EXCLUDED."pool",
"status" = EXCLUDED."status",
"lp_position" = EXCLUDED."lp_position",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.authority)
.bind(self.pool)
.bind(&self.status)
.bind(&self.lp_position)
.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 LpPositionAccountRow {
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 lp_position_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 LpPositionAccountRow {
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 lp_position_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 LpPositionAccountMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for LpPositionAccountMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS lp_position_account_account (
-- Account data
"authority" BYTEA NOT NULL,
"pool" BYTEA NOT NULL,
"status" NUMERIC(20) NOT NULL,
"lp_position" 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 lp_position_account_account"#)
.execute(connection)
.await?;
Ok(())
}
}