use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U128, U64, U8},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct LockEscrowRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub pool: Pubkey,
pub owner: Pubkey,
pub escrow_vault: Pubkey,
pub bump: U8,
pub total_locked_amount: U64,
pub lp_per_token: U128,
pub unclaimed_fee_pending: U64,
pub a_fee: U64,
pub b_fee: U64,
}
impl LockEscrowRow {
pub fn from_parts(
source: crate::accounts::lock_escrow::LockEscrow,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
pool: source.pool.into(),
owner: source.owner.into(),
escrow_vault: source.escrow_vault.into(),
bump: source.bump.into(),
total_locked_amount: source.total_locked_amount.into(),
lp_per_token: source.lp_per_token.into(),
unclaimed_fee_pending: source.unclaimed_fee_pending.into(),
a_fee: source.a_fee.into(),
b_fee: source.b_fee.into(),
}
}
}
impl TryFrom<LockEscrowRow> for crate::accounts::lock_escrow::LockEscrow {
type Error = carbon_core::error::Error;
fn try_from(source: LockEscrowRow) -> Result<Self, Self::Error> {
Ok(Self {
pool: *source.pool,
owner: *source.owner,
escrow_vault: *source.escrow_vault,
bump: source.bump.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
total_locked_amount: *source.total_locked_amount,
lp_per_token: *source.lp_per_token,
unclaimed_fee_pending: *source.unclaimed_fee_pending,
a_fee: *source.a_fee,
b_fee: *source.b_fee,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::lock_escrow::LockEscrow {
fn table() -> &'static str {
"lock_escrow_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"pool",
"owner",
"escrow_vault",
"bump",
"total_locked_amount",
"lp_per_token",
"unclaimed_fee_pending",
"a_fee",
"b_fee",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for LockEscrowRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO lock_escrow_account (
"pool",
"owner",
"escrow_vault",
"bump",
"total_locked_amount",
"lp_per_token",
"unclaimed_fee_pending",
"a_fee",
"b_fee",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
)"#,
)
.bind(self.pool)
.bind(self.owner)
.bind(self.escrow_vault)
.bind(self.bump)
.bind(&self.total_locked_amount)
.bind(&self.lp_per_token)
.bind(&self.unclaimed_fee_pending)
.bind(&self.a_fee)
.bind(&self.b_fee)
.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 LockEscrowRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO lock_escrow_account (
"pool",
"owner",
"escrow_vault",
"bump",
"total_locked_amount",
"lp_per_token",
"unclaimed_fee_pending",
"a_fee",
"b_fee",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"pool" = EXCLUDED."pool",
"owner" = EXCLUDED."owner",
"escrow_vault" = EXCLUDED."escrow_vault",
"bump" = EXCLUDED."bump",
"total_locked_amount" = EXCLUDED."total_locked_amount",
"lp_per_token" = EXCLUDED."lp_per_token",
"unclaimed_fee_pending" = EXCLUDED."unclaimed_fee_pending",
"a_fee" = EXCLUDED."a_fee",
"b_fee" = EXCLUDED."b_fee",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.pool)
.bind(self.owner)
.bind(self.escrow_vault)
.bind(self.bump)
.bind(&self.total_locked_amount)
.bind(&self.lp_per_token)
.bind(&self.unclaimed_fee_pending)
.bind(&self.a_fee)
.bind(&self.b_fee)
.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 LockEscrowRow {
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 lock_escrow_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 LockEscrowRow {
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 lock_escrow_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 LockEscrowMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for LockEscrowMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS lock_escrow_account (
-- Account data
"pool" BYTEA NOT NULL,
"owner" BYTEA NOT NULL,
"escrow_vault" BYTEA NOT NULL,
"bump" INT2 NOT NULL,
"total_locked_amount" NUMERIC(20) NOT NULL,
"lp_per_token" NUMERIC(39) NOT NULL,
"unclaimed_fee_pending" NUMERIC(20) NOT NULL,
"a_fee" NUMERIC(20) NOT NULL,
"b_fee" 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 lock_escrow_account"#)
.execute(connection)
.await?;
Ok(())
}
}