register operator
This commit is contained in:
parent
198f43f472
commit
d1e5244a18
@ -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;
|
||||
|
@ -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<Client>) -> Result<Option<Self>, Error> {
|
||||
let account: Option<Self> = db.upsert(self.id.clone()).content(self).await?;
|
||||
Ok(account)
|
||||
}
|
||||
|
||||
pub async fn operator_reg(
|
||||
db: &Surreal<Client>,
|
||||
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 {
|
||||
|
@ -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<AppDaemonMsg>),
|
||||
#[error("AppDaemon Error {0}")]
|
||||
NewAppDaemonResp(String),
|
||||
#[error("Minimum escrow amount is {MIN_ESCROW}")]
|
||||
MinimalEscrow,
|
||||
#[error("Insufficient funds, deposit more tokens")]
|
||||
InsufficientFunds,
|
||||
}
|
||||
|
@ -107,15 +107,22 @@ impl BrainGeneralCli for GeneralCliServer {
|
||||
|
||||
async fn register_operator(
|
||||
&self,
|
||||
_req: Request<RegOperatorReq>,
|
||||
req: Request<RegOperatorReq>,
|
||||
) -> Result<Response<Empty>, 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<KickReq>) -> Result<Response<KickResp>, Status> {
|
||||
|
Loading…
Reference in New Issue
Block a user