use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U128},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct ProtocolIfSharesTransferConfigRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub whitelisted_signers: Vec<Pubkey>,
pub max_transfer_per_epoch: U128,
pub current_epoch_transfer: U128,
pub next_epoch_ts: i64,
pub padding: Vec<U128>,
}
impl ProtocolIfSharesTransferConfigRow {
pub fn from_parts(
source: crate::accounts::protocol_if_shares_transfer_config::ProtocolIfSharesTransferConfig,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
whitelisted_signers: source
.whitelisted_signers
.into_iter()
.map(|element| element.into())
.collect(),
max_transfer_per_epoch: source.max_transfer_per_epoch.into(),
current_epoch_transfer: source.current_epoch_transfer.into(),
next_epoch_ts: source.next_epoch_ts,
padding: source
.padding
.into_iter()
.map(|element| element.into())
.collect(),
}
}
}
impl TryFrom<ProtocolIfSharesTransferConfigRow>
for crate::accounts::protocol_if_shares_transfer_config::ProtocolIfSharesTransferConfig
{
type Error = carbon_core::error::Error;
fn try_from(source: ProtocolIfSharesTransferConfigRow) -> Result<Self, Self::Error> {
Ok(Self {
whitelisted_signers: source
.whitelisted_signers
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
max_transfer_per_epoch: *source.max_transfer_per_epoch,
current_epoch_transfer: *source.current_epoch_transfer,
next_epoch_ts: source.next_epoch_ts,
padding: source
.padding
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::protocol_if_shares_transfer_config::ProtocolIfSharesTransferConfig
{
fn table() -> &'static str {
"protocol_if_shares_transfer_config_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"whitelisted_signers",
"max_transfer_per_epoch",
"current_epoch_transfer",
"next_epoch_ts",
"padding",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for ProtocolIfSharesTransferConfigRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO protocol_if_shares_transfer_config_account (
"whitelisted_signers",
"max_transfer_per_epoch",
"current_epoch_transfer",
"next_epoch_ts",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7
)"#,
)
.bind(&self.whitelisted_signers)
.bind(&self.max_transfer_per_epoch)
.bind(&self.current_epoch_transfer)
.bind(self.next_epoch_ts)
.bind(&self.padding)
.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 ProtocolIfSharesTransferConfigRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO protocol_if_shares_transfer_config_account (
"whitelisted_signers",
"max_transfer_per_epoch",
"current_epoch_transfer",
"next_epoch_ts",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"whitelisted_signers" = EXCLUDED."whitelisted_signers",
"max_transfer_per_epoch" = EXCLUDED."max_transfer_per_epoch",
"current_epoch_transfer" = EXCLUDED."current_epoch_transfer",
"next_epoch_ts" = EXCLUDED."next_epoch_ts",
"padding" = EXCLUDED."padding",
__slot = EXCLUDED.__slot
"#,
)
.bind(&self.whitelisted_signers)
.bind(&self.max_transfer_per_epoch)
.bind(&self.current_epoch_transfer)
.bind(self.next_epoch_ts)
.bind(&self.padding)
.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 ProtocolIfSharesTransferConfigRow {
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 protocol_if_shares_transfer_config_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 ProtocolIfSharesTransferConfigRow {
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 protocol_if_shares_transfer_config_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 ProtocolIfSharesTransferConfigMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for ProtocolIfSharesTransferConfigMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS protocol_if_shares_transfer_config_account (
-- Account data
"whitelisted_signers" BYTEA[] NOT NULL,
"max_transfer_per_epoch" NUMERIC(39) NOT NULL,
"current_epoch_transfer" NUMERIC(39) NOT NULL,
"next_epoch_ts" INT8 NOT NULL,
"padding" NUMERIC(39)[] 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 protocol_if_shares_transfer_config_account"#)
.execute(connection)
.await?;
Ok(())
}
}