use {
crate::types::{Key, TokenDelegateRole, TokenState},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct TokenRecordRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub key: sqlx::types::Json<Key>,
pub bump: U8,
pub state: sqlx::types::Json<TokenState>,
pub rule_set_revision: Option<U64>,
pub delegate: Option<Pubkey>,
pub delegate_role: Option<sqlx::types::Json<TokenDelegateRole>>,
pub locked_transfer: Option<Pubkey>,
}
impl TokenRecordRow {
pub fn from_parts(
source: crate::accounts::token_record::TokenRecord,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
key: sqlx::types::Json(source.key),
bump: source.bump.into(),
state: sqlx::types::Json(source.state),
rule_set_revision: source.rule_set_revision.map(|value| value.into()),
delegate: source.delegate.map(|value| value.into()),
delegate_role: source.delegate_role.map(sqlx::types::Json),
locked_transfer: source.locked_transfer.map(|value| value.into()),
}
}
}
impl TryFrom<TokenRecordRow> for crate::accounts::token_record::TokenRecord {
type Error = carbon_core::error::Error;
fn try_from(source: TokenRecordRow) -> Result<Self, Self::Error> {
Ok(Self {
key: source.key.0,
bump: source.bump.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
state: source.state.0,
rule_set_revision: source.rule_set_revision.map(|value| *value),
delegate: source.delegate.map(|value| *value),
delegate_role: source.delegate_role.map(|value| value.0),
locked_transfer: source.locked_transfer.map(|value| *value),
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::token_record::TokenRecord {
fn table() -> &'static str {
"token_record_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"key",
"bump",
"state",
"rule_set_revision",
"delegate",
"delegate_role",
"locked_transfer",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for TokenRecordRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO token_record_account (
"key",
"bump",
"state",
"rule_set_revision",
"delegate",
"delegate_role",
"locked_transfer",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9
)"#,
)
.bind(&self.key)
.bind(self.bump)
.bind(&self.state)
.bind(&self.rule_set_revision)
.bind(self.delegate)
.bind(&self.delegate_role)
.bind(self.locked_transfer)
.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 TokenRecordRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO token_record_account (
"key",
"bump",
"state",
"rule_set_revision",
"delegate",
"delegate_role",
"locked_transfer",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"key" = EXCLUDED."key",
"bump" = EXCLUDED."bump",
"state" = EXCLUDED."state",
"rule_set_revision" = EXCLUDED."rule_set_revision",
"delegate" = EXCLUDED."delegate",
"delegate_role" = EXCLUDED."delegate_role",
"locked_transfer" = EXCLUDED."locked_transfer",
__slot = EXCLUDED.__slot
"#,
)
.bind(&self.key)
.bind(self.bump)
.bind(&self.state)
.bind(&self.rule_set_revision)
.bind(self.delegate)
.bind(&self.delegate_role)
.bind(self.locked_transfer)
.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 TokenRecordRow {
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 token_record_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 TokenRecordRow {
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 token_record_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 TokenRecordMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for TokenRecordMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS token_record_account (
-- Account data
"key" JSONB NOT NULL,
"bump" INT2 NOT NULL,
"state" JSONB NOT NULL,
"rule_set_revision" NUMERIC(20),
"delegate" BYTEA,
"delegate_role" JSONB,
"locked_transfer" BYTEA,
-- 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 token_record_account"#)
.execute(connection)
.await?;
Ok(())
}
}