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