Skip to content

Commit

Permalink
Rename to xPerAccountCaps and not passing self to validators
Browse files Browse the repository at this point in the history
  • Loading branch information
leomassazza committed Aug 16, 2023
1 parent 214798a commit 79e4476
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ interface IGlobalPerpsMarketModule {
* @param maxPositionsPerAccount The max number of concurrent Positions per Account
* @param maxCollateralsPerAccount The max number of concurrent Collaterals per Account
*/
event MaxPerAccountSet(uint128 maxPositionsPerAccount, uint128 maxCollateralsPerAccount);
event PerAccountCapsSet(uint128 maxPositionsPerAccount, uint128 maxCollateralsPerAccount);

/**
* @notice Thrown when the fee collector does not implement the IFeeCollector interface
Expand Down Expand Up @@ -130,7 +130,7 @@ interface IGlobalPerpsMarketModule {
* @param maxPositionsPerAccount The max number of concurrent Positions per Account
* @param maxCollateralsPerAccount The max number of concurrent Collaterals per Account
*/
function setMaxPerAccount(
function setPerAccountCaps(
uint128 maxPositionsPerAccount,
uint128 maxCollateralsPerAccount
) external;
Expand All @@ -140,7 +140,7 @@ interface IGlobalPerpsMarketModule {
* @param maxPositionsPerAccount The max number of concurrent Positions per Account
* @param maxCollateralsPerAccount The max number of concurrent Collaterals per Account
*/
function getMaxPerAccount()
function getPerAccountCaps()
external
returns (uint128 maxPositionsPerAccount, uint128 maxCollateralsPerAccount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ contract GlobalPerpsMarketModule is IGlobalPerpsMarketModule {
/**
* @inheritdoc IGlobalPerpsMarketModule
*/
function setMaxPerAccount(
function setPerAccountCaps(
uint128 maxPositionsPerAccount,
uint128 maxCollateralsPerAccount
) external override {
Expand All @@ -169,13 +169,13 @@ contract GlobalPerpsMarketModule is IGlobalPerpsMarketModule {
store.maxPositionsPerAccount = maxPositionsPerAccount;
store.maxCollateralsPerAccount = maxCollateralsPerAccount;

emit MaxPerAccountSet(maxPositionsPerAccount, maxCollateralsPerAccount);
emit PerAccountCapsSet(maxPositionsPerAccount, maxCollateralsPerAccount);
}

/**
* @inheritdoc IGlobalPerpsMarketModule
*/
function getMaxPerAccount()
function getPerAccountCaps()
external
view
override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ contract PerpsAccountModule is IPerpsAccountModule {
PerpsAccount.Data storage account = PerpsAccount.create(accountId);
uint128 perpsMarketId = perpsMarketFactory.perpsMarketId;

account.validateMaxCollaterals(synthMarketId);
PerpsAccount.validateMaxCollaterals(accountId, synthMarketId);

AsyncOrder.checkPendingOrder(account.id);

Expand Down
2 changes: 1 addition & 1 deletion markets/perps-market/contracts/storage/AsyncOrder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ library AsyncOrder {
) internal {
checkPendingOrder(newRequest.accountId);

PerpsAccount.load(newRequest.accountId).validateMaxPositions(newRequest.marketId);
PerpsAccount.validateMaxPositions(newRequest.accountId, newRequest.marketId);

// Replace previous (or empty) order with the commitment request
self.settlementTime = block.timestamp + strategy.settlementDelay;
Expand Down
14 changes: 8 additions & 6 deletions markets/perps-market/contracts/storage/PerpsAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,25 @@ library PerpsAccount {
}
}

function validateMaxPositions(Data storage self, uint128 marketId) internal view {
if (PerpsMarket.accountPosition(marketId, self.id).size == 0) {
function validateMaxPositions(uint128 accountId, uint128 marketId) internal view {
if (PerpsMarket.accountPosition(marketId, accountId).size == 0) {
uint128 maxPositionsPerAccount = GlobalPerpsMarketConfiguration
.load()
.maxPositionsPerAccount;
if (maxPositionsPerAccount <= self.openPositionMarketIds.length()) {
if (maxPositionsPerAccount <= load(accountId).openPositionMarketIds.length()) {
revert MaxPositionsPerAccountReached(maxPositionsPerAccount);
}
}
}

function validateMaxCollaterals(Data storage self, uint128 synthMarketId) internal view {
if (self.collateralAmounts[synthMarketId] == 0) {
function validateMaxCollaterals(uint128 accountId, uint128 synthMarketId) internal view {
Data storage account = load(accountId);

if (account.collateralAmounts[synthMarketId] == 0) {
uint128 maxCollateralsPerAccount = GlobalPerpsMarketConfiguration
.load()
.maxCollateralsPerAccount;
if (maxCollateralsPerAccount <= self.activeCollateralTypes.length()) {
if (maxCollateralsPerAccount <= account.activeCollateralTypes.length()) {
revert MaxCollateralsPerAccountReached(maxCollateralsPerAccount);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('Create Market test', () => {
});

before('ensure per account max is set to zero', async () => {
await systems().PerpsMarket.connect(owner()).setMaxPerAccount(0, 0);
await systems().PerpsMarket.connect(owner()).setPerAccountCaps(0, 0);
});

it('reverts when trying add collateral if max collaterals per account is zero', async () => {
Expand All @@ -152,7 +152,7 @@ describe('Create Market test', () => {

describe('when max collaterals per account is set to non-zero', () => {
before('set max collaterals per account', async () => {
await systems().PerpsMarket.connect(owner()).setMaxPerAccount(0, 1000);
await systems().PerpsMarket.connect(owner()).setPerAccountCaps(0, 1000);
});

before('add collateral', async () => {
Expand All @@ -179,7 +179,7 @@ describe('Create Market test', () => {

describe('when max positions per account is set to non-zero', () => {
before('set max positions per account', async () => {
await systems().PerpsMarket.connect(owner()).setMaxPerAccount(1000, 1000);
await systems().PerpsMarket.connect(owner()).setPerAccountCaps(1000, 1000);
});
it('should be able to use the market', async () => {
await systems()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('Markets - Max Collaterals per account', () => {
});

before('ensure max collaterals is set to 0', async () => {
await systems().PerpsMarket.connect(owner()).setMaxPerAccount(_UNLIMMITED, 0);
await systems().PerpsMarket.connect(owner()).setPerAccountCaps(_UNLIMMITED, 0);
});

const restore = snapshotCheckpoint(provider);
Expand All @@ -78,7 +78,7 @@ describe('Markets - Max Collaterals per account', () => {
before(restore);

before('set max collaterals per account', async () => {
await systems().PerpsMarket.connect(owner()).setMaxPerAccount(_UNLIMMITED, 1);
await systems().PerpsMarket.connect(owner()).setPerAccountCaps(_UNLIMMITED, 1);
});

it('should be able to add collateral', async () => {
Expand All @@ -102,7 +102,7 @@ describe('Markets - Max Collaterals per account', () => {
before(restore);

before('set max collaterals per account', async () => {
await systems().PerpsMarket.connect(owner()).setMaxPerAccount(_UNLIMMITED, _UNLIMMITED);
await systems().PerpsMarket.connect(owner()).setPerAccountCaps(_UNLIMMITED, _UNLIMMITED);
});

it('should be able to add more than one collateral type (two)', async () => {
Expand All @@ -113,7 +113,7 @@ describe('Markets - Max Collaterals per account', () => {

describe('when reducing the max collaterals per account', () => {
before('reduce max collaterals per account', async () => {
await systems().PerpsMarket.connect(owner()).setMaxPerAccount(_UNLIMMITED, 2);
await systems().PerpsMarket.connect(owner()).setPerAccountCaps(_UNLIMMITED, 2);
});

it('should revert when attempting to add a 3rd collateral', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('Markets - Max positions per account', () => {
});

before('ensure max positions is set to 0', async () => {
await systems().PerpsMarket.connect(owner()).setMaxPerAccount(0, _UNLIMMITED);
await systems().PerpsMarket.connect(owner()).setPerAccountCaps(0, _UNLIMMITED);
});

const restore = snapshotCheckpoint(provider);
Expand All @@ -94,7 +94,7 @@ describe('Markets - Max positions per account', () => {
before(restore);

before('set max positions per account', async () => {
await systems().PerpsMarket.connect(owner()).setMaxPerAccount(1, _UNLIMMITED);
await systems().PerpsMarket.connect(owner()).setPerAccountCaps(1, _UNLIMMITED);
});

it('should be able to open a position', async () => {
Expand Down Expand Up @@ -134,7 +134,7 @@ describe('Markets - Max positions per account', () => {
before(restore);

before('set max positions per account', async () => {
await systems().PerpsMarket.connect(owner()).setMaxPerAccount(_UNLIMMITED, _UNLIMMITED);
await systems().PerpsMarket.connect(owner()).setPerAccountCaps(_UNLIMMITED, _UNLIMMITED);
});

it('should be able to open more than one position position', async () => {
Expand All @@ -153,7 +153,7 @@ describe('Markets - Max positions per account', () => {

describe('when reducing the max positions per account', () => {
before('reduce max positions per account', async () => {
await systems().PerpsMarket.connect(owner()).setMaxPerAccount(2, _UNLIMMITED);
await systems().PerpsMarket.connect(owner()).setPerAccountCaps(2, _UNLIMMITED);
});
it('should revert when attempting to open a 3rd position', async () => {
await assertRevert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export function bootstrapMarkets(data: BootstrapArgs) {
const { maxPositionsPerAccount, maxCollateralsPerAccount } = data;
await systems()
.PerpsMarket.connect(owner())
.setMaxPerAccount(
.setPerAccountCaps(
maxPositionsPerAccount ? maxPositionsPerAccount : 100000,
maxCollateralsPerAccount ? maxCollateralsPerAccount : 100000
);
Expand Down

0 comments on commit 79e4476

Please sign in to comment.