When the market is disabled, increasing the size of existing positions is prohibited.But due to the implementation issue of the isIncreasing
function, if the position direction changes, it will be considered a position reduction. Therefore, traders can further increase their positions when the market is disabled by changing the position direction twice.
When the market is disabled, increasing the size of existing positions is prohibited.
Therefore, traders can further increase their positions when the market is disabled by changing the position direction twice.
function test_IncressPosition() external givenInitializeContract {
uint256 initialMarginRate = 8217;
uint256 marginValueUsd =
13_253_914_045_971_528_446_902_025_648_670_080_298_521_971_387_101_561_427_594_888_947_150_272_593_920;
bool isLong = false;
uint256 marketId = 3825;
changePrank({ msgSender: users.naruto.account });
MarketConfig memory fuzzMarketConfig = getFuzzMarketConfig(marketId);
initialMarginRate = bound({ x: initialMarginRate, min: fuzzMarketConfig.imr, max: MAX_MARGIN_REQUIREMENTS });
marginValueUsd = bound({
x: marginValueUsd,
min: USDC_MIN_DEPOSIT_MARGIN,
max: convertUd60x18ToTokenAmount(address(usdc), USDC_DEPOSIT_CAP_X18)
});
deal({ token: address(usdc), to: users.naruto.account, give: marginValueUsd });
uint128 tradingAccountId = createAccountAndDeposit(marginValueUsd, address(usdc));
int128 sizeDelta = fuzzOrderSizeDelta(
FuzzOrderSizeDeltaParams({
tradingAccountId: tradingAccountId,
marketId: fuzzMarketConfig.marketId,
settlementConfigurationId: SettlementConfiguration.MARKET_ORDER_CONFIGURATION_ID,
initialMarginRate: ud60x18(initialMarginRate),
marginValueUsd: ud60x18(marginValueUsd),
maxSkew: ud60x18(fuzzMarketConfig.maxSkew),
minTradeSize: ud60x18(fuzzMarketConfig.minTradeSize),
price: ud60x18(fuzzMarketConfig.mockUsdPrice),
isLong: isLong,
shouldDiscountFees: true
})
);
perpsEngine.createMarketOrder(
OrderBranch.CreateMarketOrderParams({
tradingAccountId: tradingAccountId,
marketId: fuzzMarketConfig.marketId,
sizeDelta: sizeDelta
})
);
bytes memory mockSignedReport =
getMockedSignedReport(fuzzMarketConfig.streamId, fuzzMarketConfig.mockUsdPrice);
address marketOrderKeeper = marketOrderKeepers[fuzzMarketConfig.marketId];
changePrank({ msgSender: users.owner.account });
MarketOrderKeeper(marketOrderKeeper).setForwarder(users.keepersForwarder.account);
bytes memory performData = abi.encode(mockSignedReport, abi.encode(tradingAccountId));
UD60x18 firstFillPriceX18 =
perpsEngine.getMarkPrice(fuzzMarketConfig.marketId, fuzzMarketConfig.mockUsdPrice, sizeDelta);
(,,, UD60x18 firstOrderFeeUsdX18,,) = perpsEngine.simulateTrade(
tradingAccountId,
fuzzMarketConfig.marketId,
SettlementConfiguration.MARKET_ORDER_CONFIGURATION_ID,
sizeDelta
);
vm.expectEmit({ emitter: address(perpsEngine) });
emit SettlementBranch.LogFillOrder(
marketOrderKeeper,
tradingAccountId,
fuzzMarketConfig.marketId,
sizeDelta,
firstFillPriceX18.intoUint256(),
firstOrderFeeUsdX18.intoUint256(),
DEFAULT_SETTLEMENT_FEE,
0,
0
);
changePrank({ msgSender: users.keepersForwarder.account });
MarketOrderKeeper(marketOrderKeeper).performUpkeep(performData);
changePrank({ msgSender: users.owner.account });
perpsEngine.updatePerpMarketStatus(uint128(5), false);
changePrank({ msgSender: users.naruto.account });
perpsEngine.createMarketOrder(
OrderBranch.CreateMarketOrderParams({
tradingAccountId: tradingAccountId,
marketId: 5,
sizeDelta: 200_000_000_000_000_000
})
);
sizeDelta = 200_000_000_000_000_000;
fuzzMarketConfig = getFuzzMarketConfig(uint128(5));
performData = abi.encode(mockSignedReport, abi.encode(tradingAccountId));
firstFillPriceX18 = perpsEngine.getMarkPrice(uint128(5), fuzzMarketConfig.mockUsdPrice, sizeDelta);
(,,, firstOrderFeeUsdX18,,) = perpsEngine.simulateTrade(
tradingAccountId,
fuzzMarketConfig.marketId,
SettlementConfiguration.MARKET_ORDER_CONFIGURATION_ID,
sizeDelta
);
changePrank({ msgSender: users.keepersForwarder.account });
MarketOrderKeeper(marketOrderKeeper).performUpkeep(performData);
changePrank({ msgSender: users.naruto.account });
perpsEngine.createMarketOrder(
OrderBranch.CreateMarketOrderParams({
tradingAccountId: tradingAccountId,
marketId: 5,
sizeDelta: -400_000_000_000_000_000
})
);
sizeDelta = -400_000_000_000_000_000;
fuzzMarketConfig = getFuzzMarketConfig(uint128(5));
performData = abi.encode(mockSignedReport, abi.encode(tradingAccountId));
firstFillPriceX18 = perpsEngine.getMarkPrice(uint128(5), fuzzMarketConfig.mockUsdPrice, sizeDelta);
(,,, firstOrderFeeUsdX18,,) = perpsEngine.simulateTrade(
tradingAccountId,
fuzzMarketConfig.marketId,
SettlementConfiguration.MARKET_ORDER_CONFIGURATION_ID,
sizeDelta
);
changePrank({ msgSender: users.keepersForwarder.account });
MarketOrderKeeper(marketOrderKeeper).performUpkeep(performData);
}
In the case where the market is disabled, users can still change the direction of their positions and even increase their positions.