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