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

Max oi cap #1669

Merged
merged 24 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
16 changes: 16 additions & 0 deletions markets/perps-market/contracts/storage/AsyncOrder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ library AsyncOrder {
if (order.sizeDelta == 0) {
revert ZeroSizeOrder();
}

SimulateDataRuntime memory runtime;

PerpsAccount.Data storage account = PerpsAccount.load(order.accountId);
Expand Down Expand Up @@ -199,7 +200,22 @@ library AsyncOrder {
// TODO: validate position size
oldPosition = PerpsMarket.load(order.marketId).positions[order.accountId];

if (
PerpsMarket.validatePositionSize(
perpsMarketData,
marketConfig.maxMarketValue,
oldPosition.size,
order.sizeDelta
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let the validatePositionSize just revert instead of it returning a bool.

that function needs to be refactored; but first would write the tests and make sure its working properly.

) {
revert PerpsMarketConfiguration.MaxOpenInterestReached(
order.marketId,
marketConfig.maxMarketValue
);
}

runtime.newPositionSize = oldPosition.size + order.sizeDelta.to128();

(, , runtime.initialRequiredMargin, , ) = marketConfig.calculateRequiredMargins(
runtime.newPositionSize,
runtime.fillPrice
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ library PerpsMarketConfiguration {
using DecimalMath for uint256;
using SafeCastI128 for int128;

error MaxOpenInterestReached(uint128 marketId, uint256 maxMarketValue);

struct Data {
OrderFee.Data orderFees;
SettlementStrategy.Data[] settlementStrategies;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ describe('ModifyCollateral Withdraw', () => {
maxLiquidationLimitAccumulationMultiplier: bn(1),
liquidationRewardRatio: bn(0.05),
},
maxMarketValue: bn(9999999999),
settlementStrategy: {
settlementReward: bn(0),
},
Expand All @@ -147,6 +148,7 @@ describe('ModifyCollateral Withdraw', () => {
maxLiquidationLimitAccumulationMultiplier: bn(1),
liquidationRewardRatio: bn(0.05),
},
maxMarketValue: bn(9999999999),
settlementStrategy: {
settlementReward: bn(0),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('Liquidation - margin', async () => {
maxLiquidationLimitAccumulationMultiplier: bn(1),
liquidationRewardRatio: bn(0.05),
},
maxMarketValue: bn(9999999),
settlementStrategy: {
settlementReward: bn(0),
},
Expand All @@ -31,6 +32,7 @@ describe('Liquidation - margin', async () => {
maxLiquidationLimitAccumulationMultiplier: bn(1),
liquidationRewardRatio: bn(0.05),
},
maxMarketValue: bn(9999999),
settlementStrategy: {
settlementReward: bn(0),
},
Expand All @@ -46,6 +48,7 @@ describe('Liquidation - margin', async () => {
maxLiquidationLimitAccumulationMultiplier: bn(1),
liquidationRewardRatio: bn(0.05),
},
maxMarketValue: bn(9999999),
settlementStrategy: {
settlementReward: bn(0),
},
Expand All @@ -61,6 +64,7 @@ describe('Liquidation - margin', async () => {
maxLiquidationLimitAccumulationMultiplier: bn(1),
liquidationRewardRatio: bn(0.05),
},
maxMarketValue: bn(9999999),
settlementStrategy: {
settlementReward: bn(0),
},
Expand All @@ -76,6 +80,7 @@ describe('Liquidation - margin', async () => {
maxLiquidationLimitAccumulationMultiplier: bn(1),
liquidationRewardRatio: bn(0.05),
},
maxMarketValue: bn(9999999),
settlementStrategy: {
settlementReward: bn(0),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ describe('Create Market test', () => {
);
});
});

describe('after market is created', () => {
before('set max market value', async () => {
tx = await systems()
.PerpsMarket.connect(marketOwner)
.setMaxMarketValue(marketId, bn(99999999));
});

it('should emit MaxMarketValueSet event', async () => {
await assertEvent(
tx,
`MaxMarketValueSet(${marketId}, ${bn(99999999).toString()})`,
systems().PerpsMarket
);
});
});
});

describe('change ownership', async () => {
Expand Down Expand Up @@ -178,6 +194,10 @@ describe('Create Market test', () => {
await systems().PerpsMarket.createMarket(name, token, marketOwner.getAddress());
});

before('set max market value', async () => {
await systems().PerpsMarket.connect(marketOwner).setMaxMarketValue(marketId, bn(99999999));
});

before('create price nodes', async () => {
const results = await createOracleNode(owner(), price, systems().OracleManager);
oracleNodeId = results.oracleNodeId;
Expand Down Expand Up @@ -255,7 +275,7 @@ describe('Create Market test', () => {
await systems().PerpsMarket.connect(trader1()).modifyCollateral(2, 0, bn(10_000));
});

it('sohuld be able to use the market', async () => {
it('should be able to use the market', async () => {
await systems()
.PerpsMarket.connect(marketOwner)
.commitOrder({
Expand Down
1 change: 1 addition & 0 deletions markets/perps-market/test/integration/Market/Size.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('Market - size test', () => {
name: 'Ether',
token: 'snxETH',
price: bn(2000),
maxMarketValue: bn(9999999),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of needing to pass it in everywhere, in the bootstrapPerpsMarket, have a default value it can set it to when its not present 👍🏽

},
],
traderAccountIds: [2, 3],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('Cancel Offchain Async Order test', () => {
token: 'snxETH',
price: bn(1000),
fundingParams: { skewScale: bn(100_000), maxFundingVelocity: bn(0) },
maxMarketValue: bn(9999999),
},
],
traderAccountIds: [2, 3],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('Commit Offchain Async Order test', () => {
token: 'snxETH',
price: bn(1000),
fundingParams: { skewScale: bn(100_000), maxFundingVelocity: bn(0) },
maxMarketValue: bn(5000),
},
],
traderAccountIds: [2, 3],
Expand Down Expand Up @@ -86,6 +87,28 @@ describe('Commit Offchain Async Order test', () => {
'InsufficientMargin'
);
});

it('reverts if max market value is reached', async () => {
await depositCollateral({
accountId: () => 2,
collaterals: [{ snxUSDAmount: () => bn(100_000) }],
systems,
trader: trader1,
});
await assertRevert(
systems()
.PerpsMarket.connect(trader1())
.commitOrder({
marketId: ethMarketId,
accountId: 2,
sizeDelta: bn(90_000),
settlementStrategyId: 0,
acceptablePrice: bn(1450), // 5% slippage
trackingCode: ethers.constants.HashZero,
}),
`MaxOpenInterestReached(${ethMarketId}, ${bn(5000).toString()})`
);
});
Copy link
Contributor

@sunnyvempati sunnyvempati Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately, there are many other cases to be tested here. see review comment for other scenarios

});

const restoreToCommit = snapshotCheckpoint(provider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('Offchain Async Order - Price tests', () => {
token: 'snxETH',
price: bn(1000),
fundingParams: { skewScale: bn(100_000), maxFundingVelocity: bn(0) },
maxMarketValue: bn(999999),
},
],
traderAccountIds: [2, 3],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('Settle Offchain Async Order test', () => {
token: 'snxETH',
price: bn(1000),
fundingParams: { skewScale: bn(100_000), maxFundingVelocity: bn(0) },
maxMarketValue: bn(9999999),
},
],
traderAccountIds: [2, 3],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('Position - funding', () => {
token: 'snxETH',
price: bn(2000),
fundingParams: { skewScale: bn(10_000), maxFundingVelocity: bn(3) },
maxMarketValue: bn(999999999999999),
},
],
traderAccountIds: [2, 3],
Expand Down