Skip to content

Commit

Permalink
small test on fee calculations (#91)
Browse files Browse the repository at this point in the history
* small test on fee calculations

* cargo fmt and small fix for missing .env file
  • Loading branch information
arkanoider committed Jun 19, 2023
1 parent d561179 commit 52a4f86
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ async fn main() -> Result<()> {

#[cfg(test)]
mod tests {
use dotenvy::dotenv;
use mostro_core::order::NewOrder;
use mostro_core::Message;
use std::env::var;
use std::time::{SystemTime, UNIX_EPOCH};

#[test]
fn test_order_deserialize_serialize() {
Expand All @@ -78,4 +81,41 @@ mod tests {
let message = Message::from_json(sample_message).unwrap();
assert!(!message.verify());
}

#[test]
fn test_fee_rounding() {
dotenv().ok();
let fee = var("FEE")
.unwrap_or(0.003.to_string())
.parse::<f64>()
.unwrap_or(0.003)
/ 2.0;

let mut amt = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.subsec_micros() as i64;

// Test 1000 "random" amounts
for _i in 1..=1000 {
let fee_calculated = fee * amt as f64;
let rounded_fee = fee_calculated.round();
// Seller side
let seller_total_amt = rounded_fee as i64 + amt;
assert_eq!(amt, seller_total_amt - rounded_fee as i64);
// Buyer side

let buyer_total_amt = amt - rounded_fee as i64;
assert_eq!(amt, buyer_total_amt + rounded_fee as i64);

let nonce = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.subsec_millis() as i64;

amt %= 100_000_i64;
amt *= (rounded_fee as i64) % 100_i64;
amt += nonce;
}
}
}

0 comments on commit 52a4f86

Please sign in to comment.