mirror of
https://github.com/floor-licker/polyfill-rs.git
synced 2026-08-24 18:08:10 +00:00
Merge pull request #48 from floor-licker/fix/exact-order-tick-size-parsing
fix: require exact order tick sizes
This commit is contained in:
+34
-3
@@ -6,8 +6,8 @@
|
|||||||
use crate::auth::{sign_order_message, SignedOrderMessage};
|
use crate::auth::{sign_order_message, SignedOrderMessage};
|
||||||
use crate::errors::{PolyfillError, Result};
|
use crate::errors::{PolyfillError, Result};
|
||||||
use crate::types::{
|
use crate::types::{
|
||||||
decimal_to_price, CreateOrderOptions, MarketOrderArgs, OrderArgs, OrderType, Side,
|
CreateOrderOptions, MarketOrderArgs, OrderArgs, OrderType, Side, SignedOrderRequest,
|
||||||
SignedOrderRequest,
|
SCALE_FACTOR,
|
||||||
};
|
};
|
||||||
use alloy_primitives::{keccak256, Address, B256, U256};
|
use alloy_primitives::{keccak256, Address, B256, U256};
|
||||||
use alloy_signer_local::PrivateKeySigner;
|
use alloy_signer_local::PrivateKeySigner;
|
||||||
@@ -184,7 +184,15 @@ fn decimal_to_token_u32(amt: Decimal) -> u32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_round_config(tick_size: Decimal) -> Result<&'static RoundConfig> {
|
fn parse_round_config(tick_size: Decimal) -> Result<&'static RoundConfig> {
|
||||||
let tick_size_ticks = decimal_to_price(tick_size)
|
let scaled = tick_size * Decimal::from(SCALE_FACTOR);
|
||||||
|
if !scaled.is_integer() {
|
||||||
|
return Err(PolyfillError::validation(format!(
|
||||||
|
"Unsupported tick size {tick_size}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
let tick_size_ticks: u32 = scaled
|
||||||
|
.try_into()
|
||||||
.map_err(|_| PolyfillError::validation(format!("Unsupported tick size {tick_size}")))?;
|
.map_err(|_| PolyfillError::validation(format!("Unsupported tick size {tick_size}")))?;
|
||||||
|
|
||||||
match tick_size_ticks {
|
match tick_size_ticks {
|
||||||
@@ -612,6 +620,29 @@ mod tests {
|
|||||||
assert_eq!(result, 1_000_000_000);
|
assert_eq!(result, 1_000_000_000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_round_config_accepts_exact_supported_tick_sizes() {
|
||||||
|
let config = parse_round_config(Decimal::from_str("0.1").unwrap()).unwrap();
|
||||||
|
assert_eq!(config.price, 1);
|
||||||
|
|
||||||
|
let config = parse_round_config(Decimal::from_str("0.0100").unwrap()).unwrap();
|
||||||
|
assert_eq!(config.price, 2);
|
||||||
|
|
||||||
|
let config = parse_round_config(Decimal::from_str("0.001").unwrap()).unwrap();
|
||||||
|
assert_eq!(config.price, 3);
|
||||||
|
|
||||||
|
let config = parse_round_config(Decimal::from_str("0.00010").unwrap()).unwrap();
|
||||||
|
assert_eq!(config.price, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_round_config_rejects_fractional_or_unsupported_tick_sizes() {
|
||||||
|
for tick_size in ["0.00005", "0.00009", "0.00011", "0.0002", "0", "-0.01"] {
|
||||||
|
let result = parse_round_config(Decimal::from_str(tick_size).unwrap());
|
||||||
|
assert!(matches!(result, Err(PolyfillError::Validation { .. })));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_contract_config() {
|
fn test_get_contract_config() {
|
||||||
// Test Polygon mainnet
|
// Test Polygon mainnet
|
||||||
|
|||||||
Reference in New Issue
Block a user