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