``` def simulate_swap(Rx, Ry, target_tokens, fee_rate=0.5): """ Simulate buying target_tokens from a constant product AMM (Uniswap v2 style) with a given fee rate (e.g. 0.5 = 50% fee). Args: Rx (float): initial reserve in quote currency ($) Ry (float): initial reserve in base currency (tokens) target_tokens (float): amount of tokens to acquire fee_rate (float): fee as fraction (0.5 = 50%) Returns: dict: results with dollars spent, avg price, slippage, final spot price """ # Initial price P0 = Rx / Ry # Cost before fee (Uniswap formula) cost_no_fee = (Rx * target_tokens) / (Ry - target_tokens) # Add fee cost_with_fee = cost_no_fee * (1 + fee_rate) # Average execution price avg_price_no_fee = cost_no_fee / target_tokens avg_price_with_fee = cost_with_fee / target_tokens # Final spot price after trade P_final = (Rx + cost_no_fee) / (Ry - target_tokens) # Slippage (vs initial spot price) slippage_pct = (avg_price_with_fee / P0 - 1) * 100 return { "initial_price": P0, "dollars_needed_no_fee": cost_no_fee, "dollars_needed_with_fee": cost_with_fee, "avg_price_no_fee": avg_price_no_fee, "avg_price_with_fee": avg_price_with_fee, "final_spot_price": P_final, "slippage_pct": slippage_pct } # Example parameters (your case) Rx = 90_000 # $ reserve Ry = 20_000_000 # token reserve target_tokens = 6_000_000 fee_rate = 0.5 results = simulate_swap(Rx, Ry, target_tokens, fee_rate) print(results) ```