From d1e5244a185dfc378cf8a8d54b07f639357c1793 Mon Sep 17 00:00:00 2001 From: Noor Date: Thu, 15 May 2025 12:52:23 +0530 Subject: [PATCH] register operator --- src/constants.rs | 3 +++ src/db/general.rs | 29 ++++++++++++++++++++++++++++- src/db/mod.rs | 6 +++++- src/grpc/general.rs | 23 +++++++++++++++-------- 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 751f441..94df60b 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -42,3 +42,6 @@ pub const ID_ALPHABET: [char; 62] = [ 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ]; + +pub const MIN_ESCROW: u64 = 5000; +pub const TOKEN_DECIMAL: u64 = 1_000_000_000; diff --git a/src/db/general.rs b/src/db/general.rs index 62774ae..86c49df 100644 --- a/src/db/general.rs +++ b/src/db/general.rs @@ -1,4 +1,4 @@ -use crate::constants::ACCOUNT; +use crate::constants::{ACCOUNT, MIN_ESCROW, TOKEN_DECIMAL}; use crate::db::prelude::*; use super::Error; @@ -49,6 +49,33 @@ impl Account { .await?; Ok(()) } + + pub async fn save(self, db: &Surreal) -> Result, Error> { + let account: Option = db.upsert(self.id.clone()).content(self).await?; + Ok(account) + } + + pub async fn operator_reg( + db: &Surreal, + wallet: &str, + email: &str, + escrow: u64, + ) -> Result<(), Error> { + if escrow < MIN_ESCROW { + return Err(Error::MinimalEscrow); + } + let mut op_account = Self::get(db, wallet).await?; + let escrow = escrow.saturating_mul(TOKEN_DECIMAL); + let op_total_balance = op_account.balance.saturating_add(op_account.escrow); + if op_total_balance < escrow { + return Err(Error::InsufficientFunds); + } + op_account.email = email.to_string(); + op_account.balance = op_total_balance.saturating_sub(escrow); + op_account.escrow = escrow; + op_account.save(db).await?; + Ok(()) + } } impl Account { diff --git a/src/db/mod.rs b/src/db/mod.rs index 500f7b3..eabb36c 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -2,7 +2,9 @@ pub mod app; pub mod general; pub mod vm; -use crate::constants::{APP_NODE, DELETED_APP, DELETED_VM, NEW_APP_REQ, NEW_VM_REQ, UPDATE_VM_REQ}; +use crate::constants::{ + APP_NODE, DELETED_APP, DELETED_VM, MIN_ESCROW, NEW_APP_REQ, NEW_VM_REQ, UPDATE_VM_REQ, +}; use crate::old_brain; use prelude::*; use serde::{Deserialize, Serialize}; @@ -30,6 +32,8 @@ pub enum Error { AppDaemonConnection(#[from] tokio::sync::mpsc::error::SendError), #[error("AppDaemon Error {0}")] NewAppDaemonResp(String), + #[error("Minimum escrow amount is {MIN_ESCROW}")] + MinimalEscrow, #[error("Insufficient funds, deposit more tokens")] InsufficientFunds, } diff --git a/src/grpc/general.rs b/src/grpc/general.rs index 925e6e0..2976314 100644 --- a/src/grpc/general.rs +++ b/src/grpc/general.rs @@ -107,15 +107,22 @@ impl BrainGeneralCli for GeneralCliServer { async fn register_operator( &self, - _req: Request, + req: Request, ) -> Result, Status> { - todo!(); - // let req = check_sig_from_req(req)?; - // info!("Regitering new operator: {req:?}"); - // match self.data.register_operator(req) { - // Ok(()) => Ok(Response::new(Empty {})), - // Err(e) => Err(Status::failed_precondition(e.to_string())), - // } + let req = check_sig_from_req(req)?; + log::info!("Regitering new operator: {req:?}"); + match db::Account::operator_reg(&self.db, &req.pubkey, &req.email, req.escrow).await { + Ok(()) => Ok(Response::new(Empty {})), + Err(e) if matches!(e, db::Error::InsufficientFunds | db::Error::MinimalEscrow) => { + Err(Status::failed_precondition(e.to_string())) + } + Err(e) => { + log::info!("Failed to register operator: {e:?}"); + Err(Status::unknown( + "Unknown error. Please try again or contact the DeTEE devs team.", + )) + } + } } async fn kick_contract(&self, _req: Request) -> Result, Status> {