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