fix: register vm node creates operator account in db
This commit is contained in:
		
							parent
							
								
									e025c26930
								
							
						
					
					
						commit
						faf262b9df
					
				| @ -30,6 +30,18 @@ impl Account { | |||||||
|         Ok(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> { |     pub async fn airdrop(db: &Surreal<Client>, account: &str, tokens: u64) -> Result<(), Error> { | ||||||
|         let tokens = tokens.saturating_mul(1_000_000_000); |         let tokens = tokens.saturating_mul(1_000_000_000); | ||||||
|         let _ = db |         let _ = db | ||||||
|  | |||||||
| @ -22,6 +22,8 @@ pub enum Error { | |||||||
|     StdIo(#[from] std::io::Error), |     StdIo(#[from] std::io::Error), | ||||||
|     #[error(transparent)] |     #[error(transparent)] | ||||||
|     TimeOut(#[from] tokio::time::error::Elapsed), |     TimeOut(#[from] tokio::time::error::Elapsed), | ||||||
|  |     #[error("Failed to create account")] | ||||||
|  |     FailedToCreateDBEntry, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub mod prelude { | pub mod prelude { | ||||||
|  | |||||||
| @ -3,6 +3,7 @@ use std::time::Duration; | |||||||
| 
 | 
 | ||||||
| use super::Error; | use super::Error; | ||||||
| use crate::constants::{ACCOUNT, ACTIVE_VM, DELETED_VM, NEW_VM_REQ, VM_NODE}; | use crate::constants::{ACCOUNT, ACTIVE_VM, DELETED_VM, NEW_VM_REQ, VM_NODE}; | ||||||
|  | use crate::db::general; | ||||||
| use crate::db::general::Report; | use crate::db::general::Report; | ||||||
| use crate::old_brain; | use crate::old_brain; | ||||||
| use serde::{Deserialize, Serialize}; | use serde::{Deserialize, Serialize}; | ||||||
| @ -50,6 +51,7 @@ impl VmNodeResources { | |||||||
| 
 | 
 | ||||||
| impl VmNode { | impl VmNode { | ||||||
|     pub async fn register(self, db: &Surreal<Client>) -> Result<(), Error> { |     pub async fn register(self, db: &Surreal<Client>) -> Result<(), Error> { | ||||||
|  |         general::Account::get_or_create(db, &self.operator.key().to_string()).await?; | ||||||
|         let _: Option<VmNode> = db.upsert(self.id.clone()).content(self).await?; |         let _: Option<VmNode> = db.upsert(self.id.clone()).content(self).await?; | ||||||
|         Ok(()) |         Ok(()) | ||||||
|     } |     } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user