use {
crate::types::{SpotPosition, UserStatus},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U16, U64},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct MinimalUserRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub authority: Pubkey,
pub delegate: Pubkey,
pub name: Vec<u8>,
pub spot_positions: sqlx::types::Json<Vec<SpotPosition>>,
pub padding1: Vec<U64>,
pub padding2: Vec<U64>,
pub padding3: Vec<U64>,
pub padding4: Vec<U64>,
pub padding5: Vec<U64>,
pub padding6: Vec<U64>,
pub padding7: Vec<U16>,
pub sub_account_id: U16,
pub status: sqlx::types::Json<UserStatus>,
pub padding8: Vec<u8>,
}
impl MinimalUserRow {
pub fn from_parts(
source: crate::accounts::minimal_user::MinimalUser,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
authority: source.authority.into(),
delegate: source.delegate.into(),
name: source.name.to_vec(),
spot_positions: sqlx::types::Json(source.spot_positions.to_vec()),
padding1: source
.padding1
.into_iter()
.map(|element| element.into())
.collect(),
padding2: source
.padding2
.into_iter()
.map(|element| element.into())
.collect(),
padding3: source
.padding3
.into_iter()
.map(|element| element.into())
.collect(),
padding4: source
.padding4
.into_iter()
.map(|element| element.into())
.collect(),
padding5: source
.padding5
.into_iter()
.map(|element| element.into())
.collect(),
padding6: source
.padding6
.into_iter()
.map(|element| element.into())
.collect(),
padding7: source
.padding7
.into_iter()
.map(|element| element.into())
.collect(),
sub_account_id: source.sub_account_id.into(),
status: sqlx::types::Json(source.status),
padding8: source.padding8.to_vec(),
}
}
}
impl TryFrom<MinimalUserRow> for crate::accounts::minimal_user::MinimalUser {
type Error = carbon_core::error::Error;
fn try_from(source: MinimalUserRow) -> Result<Self, Self::Error> {
Ok(Self {
authority: *source.authority,
delegate: *source.delegate,
name: source.name.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 32 bytes"
.to_string(),
)
})?,
spot_positions: source
.spot_positions
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
padding1: source
.padding1
.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(),
)
})?,
padding2: source
.padding2
.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(),
)
})?,
padding3: source
.padding3
.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(),
)
})?,
padding4: source
.padding4
.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(),
)
})?,
padding5: source
.padding5
.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(),
)
})?,
padding6: source
.padding6
.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(),
)
})?,
padding7: source
.padding7
.into_iter()
.map(|element| {
element.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})
})
.collect::<Result<Vec<_>, carbon_core::error::Error>>()?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
sub_account_id: source.sub_account_id.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
status: source.status.0,
padding8: source.padding8.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 27 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::minimal_user::MinimalUser {
fn table() -> &'static str {
"minimal_user_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"authority",
"delegate",
"name",
"spot_positions",
"padding1",
"padding2",
"padding3",
"padding4",
"padding5",
"padding6",
"padding7",
"sub_account_id",
"status",
"padding8",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for MinimalUserRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO minimal_user_account (
"authority",
"delegate",
"name",
"spot_positions",
"padding1",
"padding2",
"padding3",
"padding4",
"padding5",
"padding6",
"padding7",
"sub_account_id",
"status",
"padding8",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16
)"#,
)
.bind(self.authority)
.bind(self.delegate)
.bind(&self.name)
.bind(&self.spot_positions)
.bind(&self.padding1)
.bind(&self.padding2)
.bind(&self.padding3)
.bind(&self.padding4)
.bind(&self.padding5)
.bind(&self.padding6)
.bind(&self.padding7)
.bind(self.sub_account_id)
.bind(&self.status)
.bind(&self.padding8)
.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 MinimalUserRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO minimal_user_account (
"authority",
"delegate",
"name",
"spot_positions",
"padding1",
"padding2",
"padding3",
"padding4",
"padding5",
"padding6",
"padding7",
"sub_account_id",
"status",
"padding8",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"authority" = EXCLUDED."authority",
"delegate" = EXCLUDED."delegate",
"name" = EXCLUDED."name",
"spot_positions" = EXCLUDED."spot_positions",
"padding1" = EXCLUDED."padding1",
"padding2" = EXCLUDED."padding2",
"padding3" = EXCLUDED."padding3",
"padding4" = EXCLUDED."padding4",
"padding5" = EXCLUDED."padding5",
"padding6" = EXCLUDED."padding6",
"padding7" = EXCLUDED."padding7",
"sub_account_id" = EXCLUDED."sub_account_id",
"status" = EXCLUDED."status",
"padding8" = EXCLUDED."padding8",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.authority)
.bind(self.delegate)
.bind(&self.name)
.bind(&self.spot_positions)
.bind(&self.padding1)
.bind(&self.padding2)
.bind(&self.padding3)
.bind(&self.padding4)
.bind(&self.padding5)
.bind(&self.padding6)
.bind(&self.padding7)
.bind(self.sub_account_id)
.bind(&self.status)
.bind(&self.padding8)
.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 MinimalUserRow {
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 minimal_user_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 MinimalUserRow {
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 minimal_user_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 MinimalUserMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for MinimalUserMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS minimal_user_account (
-- Account data
"authority" BYTEA NOT NULL,
"delegate" BYTEA NOT NULL,
"name" BYTEA NOT NULL,
"spot_positions" JSONB NOT NULL,
"padding1" NUMERIC(20)[] NOT NULL,
"padding2" NUMERIC(20)[] NOT NULL,
"padding3" NUMERIC(20)[] NOT NULL,
"padding4" NUMERIC(20)[] NOT NULL,
"padding5" NUMERIC(20)[] NOT NULL,
"padding6" NUMERIC(20)[] NOT NULL,
"padding7" INT4[] NOT NULL,
"sub_account_id" INT4 NOT NULL,
"status" JSONB NOT NULL,
"padding8" 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 minimal_user_account"#)
.execute(connection)
.await?;
Ok(())
}
}