use carbon_core::{
account::AccountMetadata,
postgres::{metadata::AccountRowMetadata, primitives::Pubkey},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct PositionBundleRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub position_bundle_mint: Pubkey,
pub position_bitmap: Vec<u8>,
}
impl PositionBundleRow {
pub fn from_parts(
source: crate::accounts::position_bundle::PositionBundle,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
position_bundle_mint: source.position_bundle_mint.into(),
position_bitmap: source.position_bitmap.to_vec(),
}
}
}
impl TryFrom<PositionBundleRow> for crate::accounts::position_bundle::PositionBundle {
type Error = carbon_core::error::Error;
fn try_from(source: PositionBundleRow) -> Result<Self, Self::Error> {
Ok(Self {
position_bundle_mint: *source.position_bundle_mint,
position_bitmap: source.position_bitmap.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 32 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::position_bundle::PositionBundle {
fn table() -> &'static str {
"position_bundle_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"position_bundle_mint",
"position_bitmap",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for PositionBundleRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO position_bundle_account (
"position_bundle_mint",
"position_bitmap",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4
)"#,
)
.bind(self.position_bundle_mint)
.bind(&self.position_bitmap)
.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 PositionBundleRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO position_bundle_account (
"position_bundle_mint",
"position_bitmap",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"position_bundle_mint" = EXCLUDED."position_bundle_mint",
"position_bitmap" = EXCLUDED."position_bitmap",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.position_bundle_mint)
.bind(&self.position_bitmap)
.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 PositionBundleRow {
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_bundle_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 PositionBundleRow {
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_bundle_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 PositionBundleMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for PositionBundleMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS position_bundle_account (
-- Account data
"position_bundle_mint" BYTEA NOT NULL,
"position_bitmap" BYTEA 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_bundle_account"#)
.execute(connection)
.await?;
Ok(())
}
}