fix app price calculation
fix overflow when calculating account escrow calculates app price per minute based on resource usage
This commit is contained in:
parent
2fe8d38156
commit
871e26edc2
@ -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<Client>) -> Result<Vec<Self>, Error> {
|
pub async fn list_all(db: &Surreal<Client>) -> Result<Vec<Self>, Error> {
|
||||||
let mut query_response = db.query(format!("SELECT * FROM {ACTIVE_APP} FETCH out;")).await?;
|
let mut query_response = db.query(format!("SELECT * FROM {ACTIVE_APP} FETCH out;")).await?;
|
||||||
let active_apps: Vec<Self> = query_response.take(0)?;
|
let active_apps: Vec<Self> = query_response.take(0)?;
|
||||||
|
@ -61,7 +61,7 @@ impl Account {
|
|||||||
email: &str,
|
email: &str,
|
||||||
escrow: u64,
|
escrow: u64,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let escrow = escrow * TOKEN_DECIMAL;
|
let escrow = escrow.saturating_mul(TOKEN_DECIMAL);
|
||||||
let mut op_account = Self::get(db, wallet).await?;
|
let mut op_account = Self::get(db, wallet).await?;
|
||||||
let op_total_balance = op_account.balance.saturating_add(op_account.escrow);
|
let op_total_balance = op_account.balance.saturating_add(op_account.escrow);
|
||||||
if op_total_balance < escrow {
|
if op_total_balance < escrow {
|
||||||
|
@ -274,6 +274,7 @@ impl From<VmNodeResources> for db::VmNodeResources {
|
|||||||
|
|
||||||
impl From<db::ActiveAppWithNode> for AppContract {
|
impl From<db::ActiveAppWithNode> for AppContract {
|
||||||
fn from(value: db::ActiveAppWithNode) -> Self {
|
fn from(value: db::ActiveAppWithNode) -> Self {
|
||||||
|
let nano_per_minute = value.price_per_minute();
|
||||||
let public_package_mr_enclave =
|
let public_package_mr_enclave =
|
||||||
Some(hex::decode(value.mr_enclave.clone()).unwrap_or_default());
|
Some(hex::decode(value.mr_enclave.clone()).unwrap_or_default());
|
||||||
|
|
||||||
@ -297,7 +298,7 @@ impl From<db::ActiveAppWithNode> for AppContract {
|
|||||||
|
|
||||||
created_at: value.created_at.to_rfc3339(),
|
created_at: value.created_at.to_rfc3339(),
|
||||||
updated_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,
|
locked_nano: value.locked_nano,
|
||||||
collected_at: value.collected_at.to_rfc3339(),
|
collected_at: value.collected_at.to_rfc3339(),
|
||||||
hratls_pubkey: value.hratls_pubkey,
|
hratls_pubkey: value.hratls_pubkey,
|
||||||
|
Loading…
Reference in New Issue
Block a user