fix: register vm node creates operator account in db

This commit is contained in:
Noor 2025-05-07 20:33:51 +05:30 committed by ghe0
parent 519bd1ed7b
commit 18c647ff3e
Signed by: ghe0
GPG Key ID: 451028EE56A0FBB4
3 changed files with 16 additions and 1 deletions

@ -30,6 +30,18 @@ impl Account {
Ok(account)
}
pub async fn get_or_create(db: &Surreal<Client>, address: &str) -> Result<Self, Error> {
let id = (ACCOUNT, address);
match db.select(id).await? {
Some(account) => Ok(account),
None => {
let account: Option<Self> = db.create(id).await?;
account.ok_or(Error::FailedToCreateDBEntry)
}
}
}
pub async fn airdrop(db: &Surreal<Client>, account: &str, tokens: u64) -> Result<(), Error> {
let tokens = tokens.saturating_mul(1_000_000_000);
let _ = db

@ -22,6 +22,8 @@ pub enum Error {
StdIo(#[from] std::io::Error),
#[error(transparent)]
TimeOut(#[from] tokio::time::error::Elapsed),
#[error("Failed to create account")]
FailedToCreateDBEntry,
}
pub mod prelude {

@ -5,7 +5,7 @@ use super::Error;
use crate::constants::{
ACCOUNT, ACTIVE_VM, DELETED_VM, NEW_VM_REQ, UPDATE_VM_REQ, VM_NODE, VM_UPDATE_EVENT,
};
use crate::db::{MeasurementArgs, Report, VmNodeFilters};
use crate::db::{Account, MeasurementArgs, Report, VmNodeFilters};
use crate::old_brain;
use serde::{Deserialize, Serialize};
use surrealdb::engine::remote::ws::Client;
@ -52,6 +52,7 @@ impl VmNodeResources {
impl VmNode {
pub async fn register(self, db: &Surreal<Client>) -> Result<(), Error> {
Account::get_or_create(db, &self.operator.key().to_string()).await?;
let _: Option<VmNode> = db.upsert(self.id.clone()).content(self).await?;
Ok(())
}