fix: Backtesting #6: Baseline strategies (HODL, full-range, fixed-width) + reporting (#320)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
openhands 2026-02-27 13:08:53 +00:00
parent 33c5244e53
commit 77f0fd82fd
6 changed files with 1095 additions and 6 deletions

View file

@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.19;
import { BaselineStrategies } from "./BaselineStrategies.sol";
import { MockToken } from "./MockToken.sol";
import { StrategyExecutor } from "./StrategyExecutor.sol";
import { TickMath } from "@aperture/uni-v3-lib/TickMath.sol";
@ -94,7 +95,7 @@ contract EventReplayer is IUniswapV3MintCallback, IUniswapV3SwapCallback {
* Pass 0 to omit the denominator from progress logs.
*/
function replay(string memory eventsFile, uint256 totalEvents) external {
_replayWithStrategy(eventsFile, totalEvents, StrategyExecutor(address(0)));
_replayWithStrategy(eventsFile, totalEvents, StrategyExecutor(address(0)), BaselineStrategies(address(0)));
}
/**
@ -105,10 +106,35 @@ contract EventReplayer is IUniswapV3MintCallback, IUniswapV3SwapCallback {
* Pass address(0) to disable strategy integration.
*/
function replay(string memory eventsFile, uint256 totalEvents, StrategyExecutor strategyExecutor) external {
_replayWithStrategy(eventsFile, totalEvents, strategyExecutor);
_replayWithStrategy(eventsFile, totalEvents, strategyExecutor, BaselineStrategies(address(0)));
}
function _replayWithStrategy(string memory eventsFile, uint256 totalEvents, StrategyExecutor strategyExecutor) internal {
/**
* @notice Replay events, trigger KrAIken recenter, and update baseline strategies all in one pass.
* @param eventsFile Path to the .jsonl events cache.
* @param totalEvents Total event count (for progress logs).
* @param strategyExecutor KrAIken StrategyExecutor (pass address(0) to disable).
* @param baselines Baseline strategies to update each block (pass address(0) to disable).
*/
function replay(
string memory eventsFile,
uint256 totalEvents,
StrategyExecutor strategyExecutor,
BaselineStrategies baselines
)
external
{
_replayWithStrategy(eventsFile, totalEvents, strategyExecutor, baselines);
}
function _replayWithStrategy(
string memory eventsFile,
uint256 totalEvents,
StrategyExecutor strategyExecutor,
BaselineStrategies baselines
)
internal
{
uint256 idx = 0;
// Track the last Swap event's expected state for drift measurement.
@ -136,6 +162,11 @@ contract EventReplayer is IUniswapV3MintCallback, IUniswapV3SwapCallback {
strategyExecutor.maybeRecenter(blockNum);
}
// Update baseline strategies for the same block.
if (address(baselines) != address(0)) {
baselines.maybeUpdate(blockNum);
}
if (_streq(eventName, "Swap")) {
(int24 expTick, uint160 expSqrtPrice) = _replaySwap(line);
// Update reference state only when the swap was not skipped.