diff --git a/src/db/app.rs b/src/db/app.rs index 6b2766a..8e0ed25 100644 --- a/src/db/app.rs +++ b/src/db/app.rs @@ -592,6 +592,17 @@ impl ActiveAppWithNode { } } + fn total_units(&self) -> f64 { + // TODO: Optimize this based on price of hardware. + (self.vcpus as f64 * 5f64) + + (self.memory_mib as f64 / 200f64) + + (self.disk_size_mib as f64 / 1024f64 / 10f64) + } + + pub fn price_per_minute(&self) -> u64 { + (self.total_units() * self.price_per_unit as f64) as u64 + } + pub async fn list_all(db: &Surreal) -> Result, Error> { let mut query_response = db.query(format!("SELECT * FROM {ACTIVE_APP} FETCH out;")).await?; let active_apps: Vec = query_response.take(0)?; diff --git a/src/db/general.rs b/src/db/general.rs index afe8b3d..7def9ed 100644 --- a/src/db/general.rs +++ b/src/db/general.rs @@ -61,7 +61,7 @@ impl Account { email: &str, escrow: u64, ) -> Result<(), Error> { - let escrow = escrow * TOKEN_DECIMAL; + let escrow = escrow.saturating_mul(TOKEN_DECIMAL); let mut op_account = Self::get(db, wallet).await?; let op_total_balance = op_account.balance.saturating_add(op_account.escrow); if op_total_balance < escrow { diff --git a/src/grpc/types.rs b/src/grpc/types.rs index 03e116d..af6968b 100644 --- a/src/grpc/types.rs +++ b/src/grpc/types.rs @@ -274,6 +274,7 @@ impl From for db::VmNodeResources { impl From for AppContract { fn from(value: db::ActiveAppWithNode) -> Self { + let nano_per_minute = value.price_per_minute(); let public_package_mr_enclave = Some(hex::decode(value.mr_enclave.clone()).unwrap_or_default()); @@ -297,7 +298,7 @@ impl From for AppContract { created_at: value.created_at.to_rfc3339(), updated_at: value.created_at.to_rfc3339(), - nano_per_minute: value.price_per_unit, + nano_per_minute, locked_nano: value.locked_nano, collected_at: value.collected_at.to_rfc3339(), hratls_pubkey: value.hratls_pubkey,