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

Max oi cap #1669

merged 24 commits into from
Jun 28, 2023

Conversation

fritzschoff
Copy link
Contributor

@fritzschoff fritzschoff commented Jun 19, 2023

this check needs to be implemented for every order type. Limit and market

@codecov
Copy link

codecov bot commented Jun 19, 2023

Codecov Report

Merging #1669 (cc4a682) into main (b481ab2) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##             main    #1669   +/-   ##
=======================================
  Coverage   72.77%   72.77%           
=======================================
  Files          57       57           
  Lines         720      720           
  Branches      236      236           
=======================================
  Hits          524      524           
  Misses        167      167           
  Partials       29       29           
Flag Coverage Δ
core-contracts 93.26% <ø> (ø)
core-modules 90.47% <ø> (ø)
core-utils 68.57% <ø> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

uint256 sizeDeltaInUint256 = order.sizeDelta < 0
? order.sizeDelta.mulDecimal(-1).toUint()
: order.sizeDelta.toUint();
if (marketConfig.maxMarketValue < perpsMarketData.size + sizeDeltaInUint256) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need some additional logic here..
The perpsMarketData.size is currently storing the absolute value of all shorts and long. I think maxMarketValue is suppose to be a cap on either side, at least that's how it works on perps v2.

So I think we need to track shortSize and longSize in PerpsMarket.sol. (I would also think it's clearer if we name them shortOI and longOI).
Maybe @sunnyvempati can shine in if this is suppose to work differently compared to preps v2.

Copy link
Contributor

Choose a reason for hiding this comment

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

After talking to sunny, We can figure it out by looking at size + skew. see https://github.com/Synthetixio/synthetix-v3/blob/main/markets/perps-market/contracts/storage/PerpsMarket.sol#L226

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cool thanks! Will have a look!

Copy link
Contributor

@sunnyvempati sunnyvempati left a comment

Choose a reason for hiding this comment

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

I'd recommend creating a new test file for testing max market size. Some scenarios we can test:

  • max market size is 100, current size is 50, someone creates new position for -100
  • max market size is 100, current size is 100, someone creates new position for -101 (fails)
  • one trader going from long to short, test both positive/negative cases
    any others?

let's be as thorough as possible. pretty much every part of validatePositionSize should be tested with different order scenarios. pretty critical to get this right 👍🏽

Comment on lines 204 to 209
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.

@@ -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 👍🏽

Comment on lines 91 to 111
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

@fritzschoff
Copy link
Contributor Author

I'd recommend creating a new test file for testing max market size. Some scenarios we can test:

  • max market size is 100, current size is 50, someone creates new position for -100
  • max market size is 100, current size is 100, someone creates new position for -101 (fails)
  • one trader going from long to short, test both positive/negative cases
    any others?

let's be as thorough as possible. pretty much every part of validatePositionSize should be tested with different order scenarios. pretty critical to get this right 👍🏽

on it. Found a TODO comment from you in the Size.test.ts file where you wrote // TODO: test maxMarketSize here as well

Copy link
Contributor

@sunnyvempati sunnyvempati left a comment

Choose a reason for hiding this comment

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

most of the comments are nonblocking, but lets remove the hardcoded value in bootstrapPerpsMarket

if (MathUtil.sameSide(oldSize, newSize) && MathUtil.abs(newSize) <= MathUtil.abs(oldSize)) {
return false;
if (
!(MathUtil.sameSide(oldSize, newSize) && MathUtil.abs(newSize) <= MathUtil.abs(oldSize))
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe create a new variable for this condition?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you mean for readability ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there is a comment above the if statement

describe('success', () => {
beforeEach(restore);
beforeEach('add collateral to margin', async () => {
await depositCollateral({
Copy link
Contributor

Choose a reason for hiding this comment

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

can call modifyCollateral directly if only depositing snxUSD, just fyi 👍🏽

before('set max market value', async () => {
await contracts.PerpsMarket.connect(marketOwner).setMaxMarketValue(
marketId,
bn(10_000_000)
Copy link
Contributor

Choose a reason for hiding this comment

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

dont hardcode this for every market. create an optional parameter that gets passed in here. if it doesn't exist then default to this value

before('set max market value', async () => {
await contracts.PerpsMarket.connect(marketOwner).setMaxMarketValue(
marketId,
maxMarketValue ? maxMarketValue : bn(10_000_000)
Copy link
Contributor

Choose a reason for hiding this comment

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

🖖🏽

Copy link
Contributor

@sunnyvempati sunnyvempati left a comment

Choose a reason for hiding this comment

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

nice work on this!

@fritzschoff fritzschoff merged commit 106ac44 into main Jun 28, 2023
19 checks passed
@fritzschoff fritzschoff deleted the max-oi-cap branch June 28, 2023 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants