use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U8},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct StateRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub admin: Pubkey,
pub program_authority: Pubkey,
pub bump: U8,
pub nonce: U8,
}
impl StateRow {
pub fn from_parts(source: crate::accounts::state::State, metadata: AccountMetadata) -> Self {
Self {
account_metadata: metadata.into(),
admin: source.admin.into(),
program_authority: source.program_authority.into(),
bump: source.bump.into(),
nonce: source.nonce.into(),
}
}
}
impl TryFrom<StateRow> for crate::accounts::state::State {
type Error = carbon_core::error::Error;
fn try_from(source: StateRow) -> Result<Self, Self::Error> {
Ok(Self {
admin: *source.admin,
program_authority: *source.program_authority,
bump: source.bump.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
nonce: source.nonce.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::state::State {
fn table() -> &'static str {
"state_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"admin",
"program_authority",
"bump",
"nonce",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for StateRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO state_account (
"admin",
"program_authority",
"bump",
"nonce",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6
)"#,
)
.bind(self.admin)
.bind(self.program_authority)
.bind(self.bump)
.bind(self.nonce)
.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 StateRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO state_account (
"admin",
"program_authority",
"bump",
"nonce",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"admin" = EXCLUDED."admin",
"program_authority" = EXCLUDED."program_authority",
"bump" = EXCLUDED."bump",
"nonce" = EXCLUDED."nonce",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.admin)
.bind(self.program_authority)
.bind(self.bump)
.bind(self.nonce)
.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 StateRow {
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 state_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 StateRow {
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 state_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 StateMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for StateMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS state_account (
-- Account data
"admin" BYTEA NOT NULL,
"program_authority" BYTEA NOT NULL,
"bump" INT2 NOT NULL,
"nonce" 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 state_account"#)
.execute(connection)
.await?;
Ok(())
}
}