From cd5c83d3c3ed95bd74417d66bc7344cc413cb911 Mon Sep 17 00:00:00 2001 From: Noor Date: Thu, 29 May 2025 20:34:23 +0530 Subject: [PATCH] fix token calculation all token inputs are in nano lp fix MIN_ESCROW calculation fixed all tests with TOKEN_DECIMAL multiplication --- src/constants.rs | 2 +- src/db/general.rs | 7 ++----- tests/common/vm_cli_utils.rs | 13 ++++++++----- tests/grpc_general_test.rs | 20 ++++++++++++-------- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 0679043..49098a3 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -46,5 +46,5 @@ pub const ID_ALPHABET: [char; 62] = [ 'V', 'W', 'X', 'Y', 'Z', ]; -pub const MIN_ESCROW: u64 = 5000; pub const TOKEN_DECIMAL: u64 = 1_000_000_000; +pub const MIN_ESCROW: u64 = 5000 * TOKEN_DECIMAL; diff --git a/src/db/general.rs b/src/db/general.rs index 2d29afb..80653cc 100644 --- a/src/db/general.rs +++ b/src/db/general.rs @@ -1,5 +1,5 @@ use super::Error; -use crate::constants::{ACCOUNT, BAN, KICK, MIN_ESCROW, TOKEN_DECIMAL, VM_NODE}; +use crate::constants::{ACCOUNT, BAN, KICK, MIN_ESCROW, VM_NODE}; use crate::db::prelude::*; use crate::old_brain; use serde::{Deserialize, Serialize}; @@ -42,7 +42,6 @@ impl Account { } pub async fn airdrop(db: &Surreal, account: &str, tokens: u64) -> Result<(), Error> { - let tokens = tokens.saturating_mul(1_000_000_000); let _ = db .query(format!("upsert account:{account} SET balance = (balance || 0) + {tokens};")) .await?; @@ -64,7 +63,6 @@ impl Account { 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); @@ -95,7 +93,7 @@ impl Account { let mut query_resp = db .query(tx_query) .bind(("account_input", RecordId::from((ACCOUNT, account)))) - .bind(("slash_amount", slash_amount.saturating_mul(TOKEN_DECIMAL))) + .bind(("slash_amount", slash_amount)) .await?; log::trace!("query_resp: {query_resp:?}"); @@ -259,7 +257,6 @@ pub struct Report { } impl Report { - // TODO: test this functionality and remove this comment pub async fn create( db: &Surreal, from_account: RecordId, diff --git a/tests/common/vm_cli_utils.rs b/tests/common/vm_cli_utils.rs index 143e86b..38aa876 100644 --- a/tests/common/vm_cli_utils.rs +++ b/tests/common/vm_cli_utils.rs @@ -7,7 +7,7 @@ use detee_shared::general_proto::{Account, AirdropReq, RegOperatorReq, ReportNod use detee_shared::vm_proto; use detee_shared::vm_proto::brain_vm_cli_client::BrainVmCliClient; use futures::StreamExt; -use surreal_brain::constants::{ACTIVE_VM, NEW_VM_REQ}; +use surreal_brain::constants::{ACTIVE_VM, NEW_VM_REQ, TOKEN_DECIMAL}; use surreal_brain::db::prelude as db; use surrealdb::engine::remote::ws::Client; use surrealdb::Surreal; @@ -15,7 +15,7 @@ use tonic::transport::Channel; pub async fn airdrop(brain_channel: &Channel, wallet: &str, amount: u64) -> Result<()> { let mut client = BrainGeneralCliClient::new(brain_channel.clone()); - let airdrop_req = AirdropReq { pubkey: wallet.to_string(), tokens: amount }; + let airdrop_req = AirdropReq { pubkey: wallet.to_string(), tokens: amount * TOKEN_DECIMAL }; let admin_key = admin_keys()[0].clone(); @@ -80,10 +80,13 @@ pub async fn report_node( Ok(client_gen_cli.report_node(key.sign_request(report_req)?).await?) } -pub async fn register_operator(brain_channel: &Channel, key: &Key, escrow: u64) -> Result<()> { +pub async fn register_operator(brain_channel: &Channel, key: &Key, escrow_nano: u64) -> Result<()> { let mut cli_client = BrainGeneralCliClient::new(brain_channel.clone()); - let reg_req = - RegOperatorReq { pubkey: key.pubkey.clone(), escrow, email: "foo@bar.com".to_string() }; + let reg_req = RegOperatorReq { + pubkey: key.pubkey.clone(), + escrow: escrow_nano, + email: "foo@bar.com".to_string(), + }; cli_client.register_operator(key.sign_request(reg_req.clone()).unwrap()).await?; Ok(()) diff --git a/tests/grpc_general_test.rs b/tests/grpc_general_test.rs index 33d2b60..fc0c096 100644 --- a/tests/grpc_general_test.rs +++ b/tests/grpc_general_test.rs @@ -53,7 +53,8 @@ async fn test_general_airdrop() { let user_01_key = Key::new(); let user_01_pubkey = user_01_key.pubkey.clone(); - let airdrop_req = AirdropReq { pubkey: user_01_pubkey.clone(), tokens: airdrop_amount }; + let airdrop_req = + AirdropReq { pubkey: user_01_pubkey.clone(), tokens: airdrop_amount * TOKEN_DECIMAL }; // user airdroping himself let err = @@ -97,7 +98,8 @@ async fn test_general_airdrop() { assert_eq!(acc_bal_user_01.balance, 3 * airdrop_amount * TOKEN_DECIMAL); // self airdrop - let airdrop_req = AirdropReq { pubkey: admin_keys[2].pubkey.clone(), tokens: airdrop_amount }; + let airdrop_req = + AirdropReq { pubkey: admin_keys[2].pubkey.clone(), tokens: airdrop_amount * TOKEN_DECIMAL }; let _ = client.airdrop(admin_keys[2].sign_request(airdrop_req.clone()).unwrap()).await.unwrap(); @@ -223,17 +225,19 @@ async fn test_register_operator() { let min_escrew_error = register_operator(&brain_channel, &key, 10).await.err().unwrap(); assert!(min_escrew_error.to_string().contains("Minimum escrow amount is 5000")); - let no_balance = register_operator(&brain_channel, &key, 5000).await.err().unwrap(); + let no_balance = + register_operator(&brain_channel, &key, 5000 * TOKEN_DECIMAL).await.err().unwrap(); assert!(no_balance.to_string().contains("Insufficient funds, deposit more tokens")); airdrop(&brain_channel, &key.pubkey, 1000).await.unwrap(); - let no_balance = register_operator(&brain_channel, &key, 5000).await.err().unwrap(); + let no_balance = + register_operator(&brain_channel, &key, 5000 * TOKEN_DECIMAL).await.err().unwrap(); assert!(no_balance.to_string().contains("Insufficient funds, deposit more tokens")); airdrop(&brain_channel, &key.pubkey, 7000).await.unwrap(); - register_operator(&brain_channel, &key, 6000).await.unwrap(); + register_operator(&brain_channel, &key, 6000 * TOKEN_DECIMAL).await.unwrap(); let operator_account: Option = db_conn.select((ACCOUNT, key.pubkey)).await.unwrap(); @@ -356,9 +360,9 @@ async fn test_slash_operator() { let slash_amt = 2500; airdrop(&brain_channel, &op_key.pubkey, 10000).await.unwrap(); - register_operator(&brain_channel, &op_key, escrew).await.unwrap(); + register_operator(&brain_channel, &op_key, escrew * TOKEN_DECIMAL).await.unwrap(); - let raw_slash_req = SlashReq { pubkey: op_key.pubkey.clone(), tokens: 2500 }; + let raw_slash_req = SlashReq { pubkey: op_key.pubkey.clone(), tokens: 2500 * TOKEN_DECIMAL }; let other_key = Key::new(); let admin_error = cli_client @@ -399,9 +403,9 @@ async fn test_admin_list_account() { airdrop(&brain_channel, &Key::new().pubkey, 10).await.unwrap(); + let accounts = list_accounts(&brain_channel, &admin_key).await.unwrap(); let acc_in_db = db_conn.select::>(ACCOUNT).await.unwrap(); - let accounts = list_accounts(&brain_channel, &admin_key).await.unwrap(); assert_eq!(accounts.len(), acc_in_db.len()); }