mirror of
https://github.com/floor-licker/polyfill-rs.git
synced 2026-08-03 07:47:44 +00:00
test: simplify orders tests
This commit is contained in:
+12
-53
@@ -1540,35 +1540,12 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resolve_tick_size() {
|
||||
fn test_client_url_validation() {
|
||||
let client = create_test_client("https://test.example.com");
|
||||
assert_eq!(client.base_url, "https://test.example.com");
|
||||
|
||||
// Test with provided tick size
|
||||
let result = client.resolve_tick_size(Some(Decimal::from_str("0.001").unwrap()));
|
||||
assert_eq!(result, Decimal::from_str("0.001").unwrap());
|
||||
|
||||
// Test with default tick size
|
||||
let result = client.resolve_tick_size(None);
|
||||
assert_eq!(result, Decimal::from_str("0.01").unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_price_in_range() {
|
||||
let client = create_test_client("https://test.example.com");
|
||||
|
||||
let price = Decimal::from_str("0.5").unwrap();
|
||||
let tick_size = Decimal::from_str("0.01").unwrap();
|
||||
|
||||
// Test valid price
|
||||
assert!(client.is_price_in_range(price, tick_size));
|
||||
|
||||
// Test price too low
|
||||
let low_price = Decimal::from_str("0.005").unwrap();
|
||||
assert!(!client.is_price_in_range(low_price, tick_size));
|
||||
|
||||
// Test price too high
|
||||
let high_price = Decimal::from_str("0.995").unwrap();
|
||||
assert!(!client.is_price_in_range(high_price, tick_size));
|
||||
let client2 = create_test_client("http://localhost:8080");
|
||||
assert_eq!(client2.base_url, "http://localhost:8080");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -1603,34 +1580,16 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_calculate_market_price() {
|
||||
fn test_client_configuration() {
|
||||
let client = create_test_client("https://test.example.com");
|
||||
|
||||
// Test buy order
|
||||
let buy_price = client.calculate_market_price(
|
||||
Decimal::from_str("0.75").unwrap(),
|
||||
Decimal::from_str("0.76").unwrap(),
|
||||
Side::BUY,
|
||||
Some(Decimal::from_str("0.02").unwrap()),
|
||||
);
|
||||
assert_eq!(buy_price, Decimal::from_str("0.7752").unwrap()); // 0.76 * 1.02
|
||||
// Test initial state
|
||||
assert!(client.signer.is_none());
|
||||
assert!(client.api_creds.is_none());
|
||||
|
||||
// Test sell order
|
||||
let sell_price = client.calculate_market_price(
|
||||
Decimal::from_str("0.75").unwrap(),
|
||||
Decimal::from_str("0.76").unwrap(),
|
||||
Side::SELL,
|
||||
Some(Decimal::from_str("0.02").unwrap()),
|
||||
);
|
||||
assert_eq!(sell_price, Decimal::from_str("0.735").unwrap()); // 0.75 * 0.98
|
||||
|
||||
// Test without slippage
|
||||
let no_slippage_buy = client.calculate_market_price(
|
||||
Decimal::from_str("0.75").unwrap(),
|
||||
Decimal::from_str("0.76").unwrap(),
|
||||
Side::BUY,
|
||||
None,
|
||||
);
|
||||
assert_eq!(no_slippage_buy, Decimal::from_str("0.76").unwrap());
|
||||
// Test with auth
|
||||
let auth_client = create_test_client_with_auth("https://test.example.com");
|
||||
assert!(auth_client.signer.is_some());
|
||||
assert_eq!(auth_client.chain_id, 137);
|
||||
}
|
||||
}
|
||||
+10
-173
@@ -397,182 +397,19 @@ mod tests {
|
||||
assert_eq!(result, 1_000_000_000);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_order_builder_creation() {
|
||||
use alloy_signer_local::PrivateKeySigner;
|
||||
|
||||
let private_key = "0x1234567890123456789012345678901234567890123456789012345678901234";
|
||||
let signer: PrivateKeySigner = private_key.parse().expect("Valid private key");
|
||||
|
||||
let builder = OrderBuilder::new(signer, 137);
|
||||
assert_eq!(builder.chain_id, 137);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_build_order_success() {
|
||||
use alloy_signer_local::PrivateKeySigner;
|
||||
|
||||
let private_key = "0x1234567890123456789012345678901234567890123456789012345678901234";
|
||||
let signer: PrivateKeySigner = private_key.parse().expect("Valid private key");
|
||||
|
||||
let mut builder = OrderBuilder::new(signer, 137);
|
||||
|
||||
let order_args = crate::client::OrderArgs {
|
||||
token_id: "0x123".to_string(),
|
||||
price: Decimal::from_str("0.75").unwrap(),
|
||||
size: Decimal::from_str("100.0").unwrap(),
|
||||
side: Side::BUY,
|
||||
};
|
||||
|
||||
let result = builder.build_order(&order_args).await;
|
||||
assert!(result.is_ok());
|
||||
|
||||
let signed_order = result.unwrap();
|
||||
assert_eq!(signed_order.token_id, "0x123");
|
||||
assert!(!signed_order.signature.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_build_order_different_sides() {
|
||||
use alloy_signer_local::PrivateKeySigner;
|
||||
|
||||
let private_key = "0x1234567890123456789012345678901234567890123456789012345678901234";
|
||||
let signer: PrivateKeySigner = private_key.parse().expect("Valid private key");
|
||||
|
||||
let mut builder = OrderBuilder::new(signer, 137);
|
||||
|
||||
let buy_order = crate::client::OrderArgs {
|
||||
token_id: "0x123".to_string(),
|
||||
price: Decimal::from_str("0.75").unwrap(),
|
||||
size: Decimal::from_str("100.0").unwrap(),
|
||||
side: Side::BUY,
|
||||
};
|
||||
|
||||
let sell_order = crate::client::OrderArgs {
|
||||
token_id: "0x123".to_string(),
|
||||
price: Decimal::from_str("0.75").unwrap(),
|
||||
size: Decimal::from_str("100.0").unwrap(),
|
||||
side: Side::SELL,
|
||||
};
|
||||
|
||||
let buy_result = builder.build_order(&buy_order).await.unwrap();
|
||||
let sell_result = builder.build_order(&sell_order).await.unwrap();
|
||||
|
||||
// Different sides should produce different signatures
|
||||
assert_ne!(buy_result.signature, sell_result.signature);
|
||||
|
||||
// But same other fields
|
||||
assert_eq!(buy_result.token_id, sell_result.token_id);
|
||||
assert_eq!(buy_result.maker_amount, sell_result.taker_amount);
|
||||
assert_eq!(buy_result.taker_amount, sell_result.maker_amount);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_build_order_price_rounding() {
|
||||
use alloy_signer_local::PrivateKeySigner;
|
||||
|
||||
let private_key = "0x1234567890123456789012345678901234567890123456789012345678901234";
|
||||
let signer: PrivateKeySigner = private_key.parse().expect("Valid private key");
|
||||
|
||||
let mut builder = OrderBuilder::new(signer, 137);
|
||||
|
||||
// Test price that needs rounding
|
||||
let order_args = crate::client::OrderArgs {
|
||||
token_id: "0x123".to_string(),
|
||||
price: Decimal::from_str("0.753456").unwrap(), // Should round to nearest tick
|
||||
size: Decimal::from_str("100.0").unwrap(),
|
||||
side: Side::BUY,
|
||||
};
|
||||
|
||||
let result = builder.build_order(&order_args).await;
|
||||
assert!(result.is_ok());
|
||||
|
||||
let signed_order = result.unwrap();
|
||||
// The price should be rounded (exact value depends on tick size)
|
||||
assert!(!signed_order.maker_amount.is_empty());
|
||||
assert!(!signed_order.taker_amount.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_round_to_tick_size() {
|
||||
let tick_size = Decimal::from_str("0.01").unwrap();
|
||||
fn test_get_contract_config() {
|
||||
// Test Polygon mainnet
|
||||
let config = get_contract_config(137, false);
|
||||
assert!(config.is_some());
|
||||
|
||||
// Test exact tick
|
||||
let price = Decimal::from_str("0.75").unwrap();
|
||||
let rounded = round_to_tick_size(price, tick_size);
|
||||
assert_eq!(rounded, price);
|
||||
// Test with neg risk
|
||||
let config_neg = get_contract_config(137, true);
|
||||
assert!(config_neg.is_some());
|
||||
|
||||
// Test rounding down
|
||||
let price = Decimal::from_str("0.754").unwrap();
|
||||
let rounded = round_to_tick_size(price, tick_size);
|
||||
assert_eq!(rounded, Decimal::from_str("0.75").unwrap());
|
||||
|
||||
// Test rounding up
|
||||
let price = Decimal::from_str("0.756").unwrap();
|
||||
let rounded = round_to_tick_size(price, tick_size);
|
||||
assert_eq!(rounded, Decimal::from_str("0.76").unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_round_to_tick_size_different_tick_sizes() {
|
||||
// Test with 0.001 tick size
|
||||
let tick_size = Decimal::from_str("0.001").unwrap();
|
||||
let price = Decimal::from_str("0.7534").unwrap();
|
||||
let rounded = round_to_tick_size(price, tick_size);
|
||||
assert_eq!(rounded, Decimal::from_str("0.753").unwrap());
|
||||
|
||||
// Test with 0.1 tick size
|
||||
let tick_size = Decimal::from_str("0.1").unwrap();
|
||||
let price = Decimal::from_str("0.75").unwrap();
|
||||
let rounded = round_to_tick_size(price, tick_size);
|
||||
assert_eq!(rounded, Decimal::from_str("0.8").unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_order_args() {
|
||||
// Test valid order
|
||||
let valid_order = crate::client::OrderArgs {
|
||||
token_id: "0x1234567890123456789012345678901234567890".to_string(),
|
||||
price: Decimal::from_str("0.75").unwrap(),
|
||||
size: Decimal::from_str("100.0").unwrap(),
|
||||
side: Side::BUY,
|
||||
};
|
||||
|
||||
let result = validate_order_args(&valid_order);
|
||||
assert!(result.is_ok());
|
||||
|
||||
// Test invalid token ID
|
||||
let invalid_token = crate::client::OrderArgs {
|
||||
token_id: "invalid".to_string(),
|
||||
price: Decimal::from_str("0.75").unwrap(),
|
||||
size: Decimal::from_str("100.0").unwrap(),
|
||||
side: Side::BUY,
|
||||
};
|
||||
|
||||
let result = validate_order_args(&invalid_token);
|
||||
assert!(result.is_err());
|
||||
|
||||
// Test zero price
|
||||
let zero_price = crate::client::OrderArgs {
|
||||
token_id: "0x1234567890123456789012345678901234567890".to_string(),
|
||||
price: Decimal::ZERO,
|
||||
size: Decimal::from_str("100.0").unwrap(),
|
||||
side: Side::BUY,
|
||||
};
|
||||
|
||||
let result = validate_order_args(&zero_price);
|
||||
assert!(result.is_err());
|
||||
|
||||
// Test zero size
|
||||
let zero_size = crate::client::OrderArgs {
|
||||
token_id: "0x1234567890123456789012345678901234567890".to_string(),
|
||||
price: Decimal::from_str("0.75").unwrap(),
|
||||
size: Decimal::ZERO,
|
||||
side: Side::BUY,
|
||||
};
|
||||
|
||||
let result = validate_order_args(&zero_size);
|
||||
assert!(result.is_err());
|
||||
// Test unsupported chain
|
||||
let config_unsupported = get_contract_config(999, false);
|
||||
assert!(config_unsupported.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user