-
Notifications
You must be signed in to change notification settings - Fork 58
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
Max oi cap #1669
Conversation
Codecov Report
@@ 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
📣 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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
There was a problem hiding this 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 👍🏽
PerpsMarket.validatePositionSize( | ||
perpsMarketData, | ||
marketConfig.maxMarketValue, | ||
oldPosition.size, | ||
order.sizeDelta | ||
) |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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 👍🏽
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()})` | ||
); | ||
}); |
There was a problem hiding this comment.
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
on it. Found a TODO comment from you in the Size.test.ts file where you wrote |
There was a problem hiding this 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)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you mean for readability ?
There was a problem hiding this comment.
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({ |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🖖🏽
There was a problem hiding this 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!
this check needs to be implemented for every order type. Limit and market