2023-07-11 00:03:05 +03:00
|
|
|
use crate::errors::{Web3ProxyErrorContext, Web3ProxyResult};
|
|
|
|
use entities::{
|
|
|
|
admin_increase_balance_receipt, increase_on_chain_balance_receipt, referee, referrer,
|
|
|
|
rpc_accounting_v2, rpc_key, stripe_increase_balance_receipt,
|
|
|
|
};
|
2023-07-10 06:13:03 +03:00
|
|
|
use migration::sea_orm::prelude::Decimal;
|
2023-07-11 00:03:05 +03:00
|
|
|
use migration::sea_orm::DbConn;
|
|
|
|
use migration::sea_orm::{ColumnTrait, EntityTrait, QueryFilter, QuerySelect};
|
|
|
|
use migration::{Func, SimpleExpr};
|
|
|
|
use serde::ser::SerializeStruct;
|
2023-07-12 10:35:07 +03:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_json::json;
|
|
|
|
use tracing::info;
|
2023-07-10 05:23:32 +03:00
|
|
|
|
2023-07-11 00:03:05 +03:00
|
|
|
/// Implements the balance getter which combines data from several tables
|
2023-07-12 10:35:07 +03:00
|
|
|
#[derive(Clone, Debug, Default, Deserialize)]
|
2023-07-10 05:23:32 +03:00
|
|
|
pub struct Balance {
|
2023-07-11 00:03:05 +03:00
|
|
|
pub admin_deposits: Decimal,
|
|
|
|
pub chain_deposits: Decimal,
|
|
|
|
pub one_time_referee_bonus: Decimal,
|
2023-07-12 10:35:07 +03:00
|
|
|
pub referal_bonus: Decimal,
|
2023-07-11 00:03:05 +03:00
|
|
|
pub stripe_deposits: Decimal,
|
2023-07-12 10:35:07 +03:00
|
|
|
pub total_cache_misses: u64,
|
|
|
|
pub total_frontend_requests: u64,
|
|
|
|
/// this includes credits spent inside a "free" or "downgraded" tier
|
|
|
|
/// this always increments and so will always be >= total_spent_paid_credits
|
2023-07-10 05:23:32 +03:00
|
|
|
pub total_spent: Decimal,
|
2023-07-11 00:03:05 +03:00
|
|
|
pub total_spent_paid_credits: Decimal,
|
|
|
|
pub user_id: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for Balance {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
2023-07-12 10:35:07 +03:00
|
|
|
let mut state = serializer.serialize_struct("balance", 14)?;
|
2023-07-11 00:03:05 +03:00
|
|
|
|
|
|
|
state.serialize_field("admin_deposits", &self.admin_deposits)?;
|
|
|
|
state.serialize_field("chain_deposits", &self.chain_deposits)?;
|
|
|
|
state.serialize_field("one_time_referee_bonus", &self.one_time_referee_bonus)?;
|
2023-07-12 10:35:07 +03:00
|
|
|
state.serialize_field("referal_bonus", &self.referal_bonus)?;
|
2023-07-11 00:03:05 +03:00
|
|
|
state.serialize_field("stripe_deposits", &self.stripe_deposits)?;
|
2023-07-12 10:35:07 +03:00
|
|
|
state.serialize_field("total_cache_misses", &self.total_cache_misses)?;
|
|
|
|
state.serialize_field("total_frontend_requests", &self.total_frontend_requests)?;
|
2023-07-11 00:03:05 +03:00
|
|
|
state.serialize_field("total_spent", &self.total_spent)?;
|
|
|
|
state.serialize_field("total_spent_paid_credits", &self.total_spent_paid_credits)?;
|
|
|
|
state.serialize_field("user_id", &self.user_id)?;
|
|
|
|
|
|
|
|
state.serialize_field("active_premium", &self.active_premium())?;
|
|
|
|
state.serialize_field("was_ever_premium", &self.was_ever_premium())?;
|
|
|
|
state.serialize_field("balance", &self.remaining())?;
|
|
|
|
state.serialize_field("total_deposits", &self.total_deposits())?;
|
|
|
|
|
|
|
|
state.end()
|
|
|
|
}
|
2023-07-10 05:23:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Balance {
|
2023-07-10 07:37:50 +03:00
|
|
|
pub fn active_premium(&self) -> bool {
|
2023-07-11 00:03:05 +03:00
|
|
|
self.was_ever_premium() && self.total_deposits() > self.total_spent_paid_credits
|
2023-07-10 07:37:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn was_ever_premium(&self) -> bool {
|
2023-07-12 20:24:16 +03:00
|
|
|
// TODO: technically we should also check that user_tier.downgrade_tier_id.is_some()
|
|
|
|
// but now we set premium automatically on deposit, so its fine for now
|
2023-07-11 00:03:05 +03:00
|
|
|
self.user_id != 0 && self.total_deposits() >= Decimal::from(10)
|
2023-07-10 07:37:50 +03:00
|
|
|
}
|
|
|
|
|
2023-07-10 05:23:32 +03:00
|
|
|
pub fn remaining(&self) -> Decimal {
|
2023-07-11 00:03:05 +03:00
|
|
|
self.total_deposits() - self.total_spent_paid_credits
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn total_deposits(&self) -> Decimal {
|
|
|
|
self.admin_deposits
|
|
|
|
+ self.chain_deposits
|
|
|
|
+ self.referal_bonus
|
|
|
|
+ self.one_time_referee_bonus
|
|
|
|
+ self.stripe_deposits
|
2023-07-10 05:23:32 +03:00
|
|
|
}
|
|
|
|
|
2023-07-11 00:03:05 +03:00
|
|
|
/// TODO: do this with a single db query
|
2023-07-10 06:13:03 +03:00
|
|
|
pub async fn try_from_db(db_conn: &DbConn, user_id: u64) -> Web3ProxyResult<Option<Self>> {
|
|
|
|
// Return early if user_id == 0
|
|
|
|
if user_id == 0 {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
2023-07-10 05:23:32 +03:00
|
|
|
|
2023-07-11 00:03:05 +03:00
|
|
|
let (admin_deposits,) = admin_increase_balance_receipt::Entity::find()
|
|
|
|
.select_only()
|
|
|
|
.column_as(
|
|
|
|
SimpleExpr::from(Func::coalesce([
|
|
|
|
admin_increase_balance_receipt::Column::Amount.sum(),
|
|
|
|
0.into(),
|
|
|
|
])),
|
|
|
|
"admin_deposits",
|
|
|
|
)
|
|
|
|
.filter(admin_increase_balance_receipt::Column::DepositToUserId.eq(user_id))
|
|
|
|
.into_tuple()
|
|
|
|
.one(db_conn)
|
|
|
|
.await
|
|
|
|
.web3_context("fetching admin deposits")?
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
let (chain_deposits,) = increase_on_chain_balance_receipt::Entity::find()
|
|
|
|
.select_only()
|
|
|
|
.column_as(
|
|
|
|
SimpleExpr::from(Func::coalesce([
|
|
|
|
increase_on_chain_balance_receipt::Column::Amount.sum(),
|
|
|
|
0.into(),
|
|
|
|
])),
|
|
|
|
"chain_deposits",
|
|
|
|
)
|
|
|
|
.filter(increase_on_chain_balance_receipt::Column::DepositToUserId.eq(user_id))
|
|
|
|
.into_tuple()
|
|
|
|
.one(db_conn)
|
|
|
|
.await
|
|
|
|
.web3_context("fetching chain deposits")?
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
let (stripe_deposits,) = stripe_increase_balance_receipt::Entity::find()
|
|
|
|
.select_only()
|
|
|
|
.column_as(
|
|
|
|
SimpleExpr::from(Func::coalesce([
|
|
|
|
stripe_increase_balance_receipt::Column::Amount.sum(),
|
|
|
|
0.into(),
|
|
|
|
])),
|
|
|
|
"stripe_deposits",
|
|
|
|
)
|
|
|
|
.filter(stripe_increase_balance_receipt::Column::DepositToUserId.eq(user_id))
|
|
|
|
.into_tuple()
|
|
|
|
.one(db_conn)
|
|
|
|
.await
|
|
|
|
.web3_context("fetching stripe deposits")?
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
2023-07-12 10:35:07 +03:00
|
|
|
let (total_cache_misses, total_frontend_requests, total_spent_paid_credits, total_spent) =
|
|
|
|
rpc_accounting_v2::Entity::find()
|
|
|
|
.select_only()
|
|
|
|
.column_as(
|
|
|
|
SimpleExpr::from(Func::coalesce([
|
|
|
|
rpc_accounting_v2::Column::CacheMisses.sum(),
|
|
|
|
0.into(),
|
|
|
|
])),
|
|
|
|
"total_cache_misses",
|
|
|
|
)
|
|
|
|
.column_as(
|
|
|
|
SimpleExpr::from(Func::coalesce([
|
|
|
|
rpc_accounting_v2::Column::FrontendRequests.sum(),
|
|
|
|
0.into(),
|
|
|
|
])),
|
|
|
|
"total_frontend_requests",
|
|
|
|
)
|
|
|
|
.column_as(
|
|
|
|
SimpleExpr::from(Func::coalesce([
|
|
|
|
rpc_accounting_v2::Column::SumCreditsUsed.sum(),
|
|
|
|
0.into(),
|
|
|
|
])),
|
|
|
|
"total_spent_paid_credits",
|
|
|
|
)
|
|
|
|
.column_as(
|
|
|
|
SimpleExpr::from(Func::coalesce([
|
|
|
|
rpc_accounting_v2::Column::SumInclFreeCreditsUsed.sum(),
|
|
|
|
0.into(),
|
|
|
|
])),
|
|
|
|
"total_spent",
|
|
|
|
)
|
|
|
|
.inner_join(rpc_key::Entity)
|
|
|
|
// .filter(rpc_key::Column::Id.eq(rpc_accounting_v2::Column::RpcKeyId)) // TODO: i think the inner_join function handles this
|
|
|
|
.filter(rpc_key::Column::UserId.eq(user_id))
|
|
|
|
.into_tuple::<(Decimal, Decimal, Decimal, Decimal)>()
|
|
|
|
.one(db_conn)
|
|
|
|
.await
|
|
|
|
.web3_context("fetching total_spent_paid_credits and total_spent")?
|
|
|
|
.unwrap_or_default();
|
2023-07-11 00:03:05 +03:00
|
|
|
|
|
|
|
let one_time_referee_bonus = referee::Entity::find()
|
|
|
|
.select_only()
|
|
|
|
.column_as(
|
|
|
|
referee::Column::OneTimeBonusAppliedForReferee,
|
|
|
|
"one_time_bonus_applied_for_referee",
|
|
|
|
)
|
|
|
|
.filter(referee::Column::UserId.eq(user_id))
|
|
|
|
.into_tuple()
|
|
|
|
.one(db_conn)
|
|
|
|
.await
|
2023-07-11 04:09:58 +03:00
|
|
|
.web3_context("fetching one time referee bonus")?
|
2023-07-11 00:03:05 +03:00
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
let referal_bonus = referee::Entity::find()
|
|
|
|
.select_only()
|
|
|
|
.column_as(
|
|
|
|
SimpleExpr::from(Func::coalesce([
|
|
|
|
referee::Column::CreditsAppliedForReferrer.sum(),
|
|
|
|
0.into(),
|
|
|
|
])),
|
|
|
|
"credits_applied_for_referrer",
|
|
|
|
)
|
2023-07-11 02:25:01 +03:00
|
|
|
.inner_join(referrer::Entity)
|
2023-07-11 00:03:05 +03:00
|
|
|
.filter(referrer::Column::UserId.eq(user_id))
|
|
|
|
.into_tuple()
|
|
|
|
.one(db_conn)
|
|
|
|
.await
|
2023-07-11 04:09:58 +03:00
|
|
|
.web3_context("fetching referal bonus")?
|
2023-07-11 00:03:05 +03:00
|
|
|
.unwrap_or_default();
|
|
|
|
|
2023-07-12 10:35:07 +03:00
|
|
|
let total_cache_misses: u64 = total_cache_misses.try_into()?;
|
|
|
|
let total_frontend_requests: u64 = total_frontend_requests.try_into()?;
|
|
|
|
|
2023-07-11 00:03:05 +03:00
|
|
|
let balance = Self {
|
|
|
|
admin_deposits,
|
|
|
|
chain_deposits,
|
|
|
|
referal_bonus,
|
|
|
|
one_time_referee_bonus,
|
|
|
|
stripe_deposits,
|
2023-07-12 10:35:07 +03:00
|
|
|
total_cache_misses,
|
|
|
|
total_frontend_requests,
|
2023-07-11 00:03:05 +03:00
|
|
|
total_spent,
|
|
|
|
total_spent_paid_credits,
|
|
|
|
user_id,
|
2023-07-10 06:13:03 +03:00
|
|
|
};
|
2023-07-10 05:23:32 +03:00
|
|
|
|
2023-07-12 10:35:07 +03:00
|
|
|
// TODO: lower log level
|
|
|
|
info!("balance: {:#}", json!(&balance));
|
|
|
|
|
2023-07-10 06:13:03 +03:00
|
|
|
// Return None if there is no entry
|
|
|
|
Ok(Some(balance))
|
|
|
|
}
|
2023-07-10 05:23:32 +03:00
|
|
|
}
|