Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enchance frontrun test #206

Open
wants to merge 3 commits into
base: arthurka/frontrun-test
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/frontrun.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"blockTime": 2,
"frontrunCollateral": 50000000000000000000,
"frontrunLeverage": 2000000000000000000,
"frontrunCollateral": 100000000000000000000,
"frontrunLeverage": 5000000000000000000,
"initialBalance": 1000000000000000000000,
"initialCollateral": 10000000000000000000,
"initialLeverage": 5000000000000000000,
Expand Down
117 changes: 98 additions & 19 deletions tests/frontrun/Frontrun.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ contract FrontrunTest is Test {
function setUp() public {
setUpConfig();

vm.writeFile("tests/frontrun/frontrun_results.csv", "waitBlocks,profit\n");

vm.writeFile("tests/frontrun/frontrun_results.csv", "waitTimeToUnwind,timeToCorrectPrice,profit,priceOverMicroWindow,priceOverMacroWindow\n");
vm.writeFile("tests/frontrun/frontrun_results_fuzz_spike.csv", "waitTimeToUnwind,spike,profit,priceOverMicroWindow,priceOverMacroWindow\n");
ov = new OverlayV1Token();

ov.grantRole(ADMIN_ROLE, GOVERNOR);
Expand All @@ -71,9 +71,11 @@ contract FrontrunTest is Test {
setUpMarket();
}

function testFuzz_FrontrunAttempt(uint256 waitBlocks) public {
// Limit waitBlock values from 1 to 500
waitBlocks = bound(waitBlocks, 1, 500);
function testFuzz_FrontrunAttempt(uint256 waitTimeToUnwind, uint256 timeToCorrectPriceInMinutes) public {
// Limit waitTimeToUnwind values from 1 to 500
waitTimeToUnwind = bound(waitTimeToUnwind, 1, 1000);
timeToCorrectPriceInMinutes = bound(timeToCorrectPriceInMinutes, 1, 10); // up to 10 minutes
uint256 timeToCorrectPrice = timeToCorrectPriceInMinutes * 60;

// Initialize market with two positions
vm.startPrank(USER);
Expand All @@ -99,21 +101,95 @@ contract FrontrunTest is Test {
uint256 positionId = market.build(
config.frontrunCollateral, config.frontrunLeverage, true, type(uint256).max
);
uint256 buildTimestamp = block.timestamp;
uint256 buildBlock = block.number;
vm.stopPrank();

if (waitTimeToUnwind > timeToCorrectPrice) {
// Wait for specified time
vm.warp(block.timestamp + timeToCorrectPrice);
vm.roll(block.number + timeToCorrectPrice * BLOCK_TIME_MULTIPLIER / config.blockTime);

// Price returns back to normal
updateAggregatorPrice(aggregator.latestRoundId() + 1, config.initialPrice);
}

// Wait `waitTimeToUnwind`
vm.warp(buildTimestamp + waitTimeToUnwind);
vm.roll(buildBlock + waitTimeToUnwind * BLOCK_TIME_MULTIPLIER / config.blockTime);

// Update the price again to not get `stale price`
updateAggregatorPrice(aggregator.latestRoundId() + 1, config.initialPrice);

// Unwind
vm.prank(USER1);
market.unwind(positionId, 1e18, 0);

Oracle.Data memory data2 = feed.latest();
console2.log("priceOverMicroWindow", int256(data2.priceOverMicroWindow));
console2.log("priceOverMacroWindow", int256(data2.priceOverMacroWindow));

// Profit
uint256 finalBalance = ov.balanceOf(USER1);
int256 profit = int256(finalBalance) - int256(initialBalance);

console2.log("Unwound at block time:", block.timestamp);
console2.log("Profit:", profit);

// Write data into `frontrun_results.csv`
writeDataToCSV("tests/frontrun/frontrun_results", waitTimeToUnwind, profit, timeToCorrectPrice);
}

function testFuzz_FrontrunAttempt_fuzzSpike(uint256 waitTimeToUnwind, uint256 rawSpike) public {
// Limit waitBlock values from 1 to 500
waitTimeToUnwind = bound(waitTimeToUnwind, 1, 500);
// Limit spike values from 10 to 50%
rawSpike = bound(rawSpike, 1, 5);
uint256 spike = config.initialPrice * (10 + rawSpike) / 10;

// Initialize market with two positions
vm.startPrank(USER);
ov.approve(address(market), type(uint256).max);
// One is long
market.build(config.initialCollateral, config.initialLeverage, true, type(uint256).max);
// Another is short
market.build(config.initialCollateral, config.initialLeverage, false, 0);
vm.stopPrank();

// Set up USER1 for frontrunning
vm.prank(GOVERNOR);
ov.mint(USER1, config.initialBalance);
uint256 initialBalance = ov.balanceOf(USER1);

// Price spike
uint80 currentRoundId = aggregator.latestRoundId();
updateAggregatorPrice(currentRoundId + 1, spike);

// Frontrunner builds a long position
vm.startPrank(USER1);
ov.approve(address(market), type(uint256).max);
uint256 positionId = market.build(
config.frontrunCollateral, config.frontrunLeverage, true, type(uint256).max
);
uint256 buildTimestamp = block.timestamp;
uint256 buildBlock = block.number;
vm.stopPrank();

// Wait for specified time
vm.warp(block.timestamp + config.waitTime);
vm.roll(block.number + config.waitTime * BLOCK_TIME_MULTIPLIER / config.blockTime);
if (waitTimeToUnwind > config.waitTime) {
// Wait for specified time
vm.warp(block.timestamp + config.waitTime);
vm.roll(block.number + config.waitTime * BLOCK_TIME_MULTIPLIER / config.blockTime);

// Price returns back to normal
updateAggregatorPrice(currentRoundId + 2, config.initialPrice);
// Price returns back to normal
updateAggregatorPrice(aggregator.latestRoundId() + 1, config.initialPrice);
}

// Wait `waitBlocks` amount
vm.roll(block.number + waitBlocks);
vm.warp(block.timestamp + config.blockTime * waitBlocks / BLOCK_TIME_MULTIPLIER);
// Wait `waitTimeToUnwind`
vm.warp(buildTimestamp + waitTimeToUnwind);
vm.roll(buildBlock + waitTimeToUnwind * BLOCK_TIME_MULTIPLIER / config.blockTime);

// Update the price again to not get `stale price`
updateAggregatorPrice(currentRoundId + 3, config.initialPrice);
updateAggregatorPrice(aggregator.latestRoundId() + 1, config.initialPrice);

// Unwind
vm.prank(USER1);
Expand All @@ -131,13 +207,16 @@ contract FrontrunTest is Test {
console2.log("Profit:", profit);

// Write data into `frontrun_results.csv`
writeDataToCSV(waitBlocks, profit);
writeDataToCSV("tests/frontrun/frontrun_results_fuzz_spike", waitTimeToUnwind, profit, rawSpike * 10);
}

function writeDataToCSV(uint256 waitBlocks, int256 profit) internal {
function writeDataToCSV(string memory fileName, uint256 waitTimeToUnwind, int256 profit, uint256 fuzzValue) internal {
Oracle.Data memory data = feed.latest();
string memory csvLine =
string(abi.encodePacked(vm.toString(waitBlocks), ",", vm.toString(profit)));
vm.writeLine("tests/frontrun/frontrun_results.csv", csvLine);
string(abi.encodePacked(vm.toString(waitTimeToUnwind), ",", vm.toString(fuzzValue), ",", vm.toString(profit), ",", vm.toString(data.priceOverMicroWindow), ",", vm.toString(data.priceOverMacroWindow)));
string memory filenamePath = string(abi.encodePacked(fileName, ".csv"));
console2.log("filenamePath", filenamePath);
vm.writeLine(filenamePath, csvLine);
}

function setUpMarket() private {
Expand All @@ -150,7 +229,7 @@ contract FrontrunTest is Test {
params[0] = 122000000000; // k
params[1] = 500000000000000000; // lmbda
params[2] = 2500000000000000; // delta
params[3] = 5000000000000000000; // capPayoff
params[3] = 50000000000000000000; // capPayoff
params[4] = 8e23; // capNotional
params[5] = 5000000000000000000; // capLeverage
params[6] = 2592000; // circuitBreakerWindow
Expand Down
Loading