predict-sdk 0.1.0

Rust SDK for Predict.fun prediction market - order building, EIP-712 signing, and real-time WebSocket data
Documentation
use rust_decimal::Decimal;

/// Round a decimal to N significant digits
///
/// This matches the TypeScript SDK's `toSignificantDigits` function
pub fn round_to_significant_digits(value: Decimal, sig_figs: u32) -> Decimal {
    if value.is_zero() {
        return Decimal::ZERO;
    }

    let abs_value = value.abs();

    // Convert to string to count significant digits
    let str_value = abs_value.to_string();

    // Count all digits
    let all_digits: String = str_value.chars().filter(|c| c.is_ascii_digit()).collect();
    let total_digits = all_digits.len();

    // If we already have fewer digits than sig_figs, return as-is
    if total_digits <= sig_figs as usize {
        return value;
    }

    // Calculate how many digits to keep
    let excess = total_digits - sig_figs as usize;
    let divisor = Decimal::from(10_i64.pow(excess as u32));

    // Divide, round, then multiply back
    let truncated = (abs_value / divisor).floor() * divisor;

    if value.is_sign_negative() {
        -truncated
    } else {
        truncated
    }
}

// Tests are in integration tests