use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U16, U64, U8},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct BankMetadataRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub bank: Pubkey,
pub placeholder: U64,
pub ticker: Vec<u8>,
pub description: Vec<u8>,
pub data_blob: Vec<u8>,
pub end_description_byte: U16,
pub end_data_blob: U16,
pub end_ticker_byte: U8,
pub bump: U8,
pub pad0: Vec<u8>,
}
impl BankMetadataRow {
pub fn from_parts(
source: crate::accounts::bank_metadata::BankMetadata,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
bank: source.bank.into(),
placeholder: source.placeholder.into(),
ticker: source.ticker.to_vec(),
description: source.description.to_vec(),
data_blob: source.data_blob.to_vec(),
end_description_byte: source.end_description_byte.into(),
end_data_blob: source.end_data_blob.into(),
end_ticker_byte: source.end_ticker_byte.into(),
bump: source.bump.into(),
pad0: source.pad0.to_vec(),
}
}
}
impl TryFrom<BankMetadataRow> for crate::accounts::bank_metadata::BankMetadata {
type Error = carbon_core::error::Error;
fn try_from(source: BankMetadataRow) -> Result<Self, Self::Error> {
Ok(Self {
bank: *source.bank,
placeholder: *source.placeholder,
ticker: source.ticker.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 64 bytes"
.to_string(),
)
})?,
description: source.description.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 128 bytes"
.to_string(),
)
})?,
data_blob: source.data_blob.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 256 bytes"
.to_string(),
)
})?,
end_description_byte: source.end_description_byte.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
end_data_blob: source.end_data_blob.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
end_ticker_byte: source.end_ticker_byte.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
bump: source.bump.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
pad0: source.pad0.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 2 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::bank_metadata::BankMetadata {
fn table() -> &'static str {
"bank_metadata_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"bank",
"placeholder",
"ticker",
"description",
"data_blob",
"end_description_byte",
"end_data_blob",
"end_ticker_byte",
"bump",
"pad0",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for BankMetadataRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO bank_metadata_account (
"bank",
"placeholder",
"ticker",
"description",
"data_blob",
"end_description_byte",
"end_data_blob",
"end_ticker_byte",
"bump",
"pad0",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
)"#,
)
.bind(self.bank)
.bind(&self.placeholder)
.bind(&self.ticker)
.bind(&self.description)
.bind(&self.data_blob)
.bind(self.end_description_byte)
.bind(self.end_data_blob)
.bind(self.end_ticker_byte)
.bind(self.bump)
.bind(&self.pad0)
.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 BankMetadataRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO bank_metadata_account (
"bank",
"placeholder",
"ticker",
"description",
"data_blob",
"end_description_byte",
"end_data_blob",
"end_ticker_byte",
"bump",
"pad0",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"bank" = EXCLUDED."bank",
"placeholder" = EXCLUDED."placeholder",
"ticker" = EXCLUDED."ticker",
"description" = EXCLUDED."description",
"data_blob" = EXCLUDED."data_blob",
"end_description_byte" = EXCLUDED."end_description_byte",
"end_data_blob" = EXCLUDED."end_data_blob",
"end_ticker_byte" = EXCLUDED."end_ticker_byte",
"bump" = EXCLUDED."bump",
"pad0" = EXCLUDED."pad0",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.bank)
.bind(&self.placeholder)
.bind(&self.ticker)
.bind(&self.description)
.bind(&self.data_blob)
.bind(self.end_description_byte)
.bind(self.end_data_blob)
.bind(self.end_ticker_byte)
.bind(self.bump)
.bind(&self.pad0)
.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 BankMetadataRow {
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 bank_metadata_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 BankMetadataRow {
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 bank_metadata_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 BankMetadataMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for BankMetadataMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS bank_metadata_account (
-- Account data
"bank" BYTEA NOT NULL,
"placeholder" NUMERIC(20) NOT NULL,
"ticker" BYTEA NOT NULL,
"description" BYTEA NOT NULL,
"data_blob" BYTEA NOT NULL,
"end_description_byte" INT4 NOT NULL,
"end_data_blob" INT4 NOT NULL,
"end_ticker_byte" INT2 NOT NULL,
"bump" INT2 NOT NULL,
"pad0" 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 bank_metadata_account"#)
.execute(connection)
.await?;
Ok(())
}
}