Skip to content

Commit 039bb9d

Browse files
odyslamclaude
andcommitted
feat: auto-detect backtesting script path
Add automatic path detection for transaction_fetcher.sh using find command as fallback when standard paths fail. This removes the need to manually set CREDIBLE_STD_PATH in most cases. - Add more standard search paths (Soldeer, git submodules, monorepo) - Add _autoDetectScriptPath() that uses find to locate the script - Cache results to avoid repeated filesystem lookups Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 822b2d9 commit 039bb9d

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/CredibleTestWithBacktesting.sol

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ abstract contract CredibleTestWithBacktesting is CredibleTest, Test {
151151
}
152152

153153
/// @notice Find the transaction_fetcher.sh script path
154-
/// @dev Checks environment variable first, then searches common locations
154+
/// @dev Checks environment variable first, then searches common locations,
155+
/// and finally uses `find` to auto-detect the script location.
155156
/// Override _getScriptSearchPaths() to customize search locations
156157
/// @return The path to transaction_fetcher.sh
157158
function _findScriptPath() internal virtual returns (string memory) {
@@ -188,14 +189,65 @@ abstract contract CredibleTestWithBacktesting is CredibleTest, Test {
188189
}
189190
}
190191

192+
// Auto-detect using find command as fallback
193+
string memory autoDetectedPath = _autoDetectScriptPath();
194+
if (bytes(autoDetectedPath).length > 0) {
195+
_cachedScriptPath = autoDetectedPath;
196+
return _cachedScriptPath;
197+
}
198+
191199
// No valid path found - provide helpful error message
192200
revert(
193201
"transaction_fetcher.sh not found. "
194202
"Set CREDIBLE_STD_PATH environment variable or override _getScriptSearchPaths(). "
195-
"Standard locations checked: lib/credible-std/..., dependencies/credible-std/..., ../credible-std/..."
203+
"Auto-detection also failed. Ensure credible-std is installed in your project."
196204
);
197205
}
198206

207+
/// @notice Auto-detect the script path using find command
208+
/// @dev Searches the project directory for transaction_fetcher.sh
209+
/// @return The detected path, or empty string if not found
210+
function _autoDetectScriptPath() internal virtual returns (string memory) {
211+
// Use find to locate the script, searching common dependency directories
212+
// Limit depth to avoid searching too deep and improve performance
213+
string[] memory findCmd = new string[](3);
214+
findCmd[0] = "bash";
215+
findCmd[1] = "-c";
216+
findCmd[2] = "find . -maxdepth 6 -type f -name 'transaction_fetcher.sh' -path '*/credible-std/*' 2>/dev/null | head -1";
217+
218+
try vm.ffi(findCmd) returns (bytes memory result) {
219+
string memory foundPath = string(result);
220+
// Trim whitespace/newlines
221+
bytes memory pathBytes = bytes(foundPath);
222+
uint256 len = pathBytes.length;
223+
while (len > 0 && (pathBytes[len - 1] == 0x0a || pathBytes[len - 1] == 0x0d || pathBytes[len - 1] == 0x20)) {
224+
len--;
225+
}
226+
if (len == 0) {
227+
return "";
228+
}
229+
bytes memory trimmed = new bytes(len);
230+
for (uint256 i = 0; i < len; i++) {
231+
trimmed[i] = pathBytes[i];
232+
}
233+
foundPath = string(trimmed);
234+
235+
// Verify the found path exists
236+
string[] memory testCmd = new string[](3);
237+
testCmd[0] = "test";
238+
testCmd[1] = "-f";
239+
testCmd[2] = foundPath;
240+
241+
try vm.ffi(testCmd) {
242+
return foundPath;
243+
} catch {
244+
return "";
245+
}
246+
} catch {
247+
return "";
248+
}
249+
}
250+
199251
/// @notice Fetch transactions using FFI
200252
function _fetchTransactions(
201253
address targetContract,

src/utils/BacktestingUtils.sol

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,16 @@ library BacktestingUtils {
313313
/// @notice Get the standard search paths for transaction_fetcher.sh
314314
/// @return Array of paths to check, in order of preference
315315
function getDefaultScriptSearchPaths() internal pure returns (string[] memory) {
316-
string[] memory paths = new string[](3);
316+
string[] memory paths = new string[](6);
317317
paths[0] = "lib/credible-std/scripts/backtesting/transaction_fetcher.sh";
318318
paths[1] = "dependencies/credible-std/scripts/backtesting/transaction_fetcher.sh";
319319
paths[2] = "../credible-std/scripts/backtesting/transaction_fetcher.sh";
320+
// Soldeer paths
321+
paths[3] = "dependencies/@phylax-systems/credible-std/scripts/backtesting/transaction_fetcher.sh";
322+
// Git submodule paths
323+
paths[4] = "modules/credible-std/scripts/backtesting/transaction_fetcher.sh";
324+
// Monorepo paths
325+
paths[5] = "packages/credible-std/scripts/backtesting/transaction_fetcher.sh";
320326
return paths;
321327
}
322328
}

0 commit comments

Comments
 (0)