Skip to content

Commit

Permalink
Prevent also new orders with pending one
Browse files Browse the repository at this point in the history
  • Loading branch information
leomassazza committed Jul 10, 2023
1 parent f9ebabc commit e7e1729
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
10 changes: 8 additions & 2 deletions markets/perps-market/contracts/modules/AsyncOrderModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {DecimalMath} from "@synthetixio/core-contracts/contracts/utils/DecimalMa
import {Account} from "@synthetixio/main/contracts/storage/Account.sol";
import {AccountRBAC} from "@synthetixio/main/contracts/storage/AccountRBAC.sol";
import {IPythVerifier} from "../interfaces/external/IPythVerifier.sol";
import {IAccountModule} from "../interfaces/IAccountModule.sol";
import {IAsyncOrderModule} from "../interfaces/IAsyncOrderModule.sol";
import {PerpsAccount, SNX_USD_MARKET_ID} from "../storage/PerpsAccount.sol";
import {MathUtil} from "../utils/MathUtil.sol";
Expand Down Expand Up @@ -75,7 +76,12 @@ contract AsyncOrderModule is IAsyncOrderModule {
PerpsPrice.getCurrentPrice(commitment.marketId)
);

PerpsAccount.load(commitment.accountId).addOrderMarket(commitment.marketId);
// Check if there is a pending orders
if (PerpsAccount.load(commitment.accountId).hasPendingOrders) {
revert IAccountModule.PendingOrdersExist();
}

PerpsAccount.load(commitment.accountId).addPendingOrder();

// TODO include fees in event
emit OrderCommitted(
Expand Down Expand Up @@ -115,7 +121,7 @@ contract AsyncOrderModule is IAsyncOrderModule {
order.checkCancellationEligibility(settlementStrategy);
order.reset();

PerpsAccount.load(accountId).removeOrderMarket(marketId);
PerpsAccount.load(accountId).removePendingOrder();

emit OrderCanceled(marketId, accountId, order.settlementTime, order.acceptablePrice);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ contract PerpsAccountModule is IAccountModule {
}

// Check if there are pending orders
if (account.hasPendingOrders()) {
if (account.hasPendingOrders) {
revert PendingOrdersExist();
}

Expand Down
16 changes: 5 additions & 11 deletions markets/perps-market/contracts/storage/PerpsAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ library PerpsAccount {
// synth marketId => amount
mapping(uint128 => uint256) collateralAmounts;
uint128 id;
bool hasPendingOrders;
SetUtil.UintSet activeCollateralTypes;
SetUtil.UintSet openPositionMarketIds;
SetUtil.UintSet openOrderMarketIds;
}

error InsufficientCollateralAvailableForWithdraw(uint available, uint required);
Expand Down Expand Up @@ -90,18 +90,12 @@ library PerpsAccount {
}
}

function addOrderMarket(Data storage self, uint orderMarketId) internal {
if (!self.openOrderMarketIds.contains(orderMarketId)) {
self.openOrderMarketIds.add(orderMarketId);
}
}

function removeOrderMarket(Data storage self, uint orderMarketId) internal {
self.openOrderMarketIds.remove(orderMarketId);
function addPendingOrder(Data storage self) internal {
self.hasPendingOrders = true;
}

function hasPendingOrders(Data storage self) internal view returns (bool) {
return self.openOrderMarketIds.length() > 0;
function removePendingOrder(Data storage self) internal {
self.hasPendingOrders = false;
}

function addCollateralAmount(
Expand Down

0 comments on commit e7e1729

Please sign in to comment.