7.1 LMSR Implementation Details
Implementing LMSR on-chain requires careful optimization due to compute constraints and the mathematical complexity of exponential and logarithmic functions.
Fixed-Point Arithmetic
Solana programs cannot use floating-point operations. Path Protocol implements LMSR using fixed-point arithmetic:
// Fixed-point representation: multiply by 10^6 for precision
const FIXED_POINT_SCALE: u128 = 1_000_000;
// Example: 1.5 represented as 1_500_000
// Example: 0.001 represented as 1_000Exponential Approximation
The LMSR cost function requires computing e^(q_i/b). Path uses a Taylor series approximation:
/// Approximates e^x using Taylor series: e^x ≈ 1 + x + x²/2! + x³/3! + ...
/// Limited to 10 terms for compute efficiency
pub fn fixed_point_exp(x: u64, b: u64) -> Result<u128> {
// Calculate x/b ratio in fixed point
let ratio = (x as u128 * FIXED_POINT_SCALE) / b as u128;
// Taylor series terms
let mut result = FIXED_POINT_SCALE; // 1.0
let mut term = FIXED_POINT_SCALE;
for n in 1..=10 {
term = (term ratio) / (FIXED_POINT_SCALE n as u128);
result += term;
if term < 100 { break; } // Early termination for small terms
}
Ok(result)
}Accuracy:
For x/b ratios in range [-5, 5], Taylor series with 10 terms achieves <0.01% error.
Logarithm Approximation
For ln(x), Path uses a piecewise linear approximation with lookup tables:
Alternative: For higher accuracy requirements, Path may use Solana's log2 syscall combined with change-of-base formula:
Cost Function Implementation
Compute Units:
Binary market (N=2): ~30,000 CU
5-outcome market: ~60,000 CU
10-outcome market: ~100,000 CU
Well within Solana's 200,000 CU instruction limit.
Price Calculation
Output Format:
Prices returned as basis points (10000 = 100%):
5000 = 50%
7500 = 75%
2500 = 25%
Buy Shares Calculation
Finding the number of shares for a given cost requires solving:
This is solved iteratively using binary search:
Convergence:
Typically converges in 10-15 iterations with <0.1% precision.
Last updated

