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

Fix validation issue caused by truncation of risk params #465

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions packages/perennial/contracts/types/RiskParameter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,13 @@ library RiskParameterStorageLib {

if (self.minMargin.lt(self.minMaintenance)) revert RiskParameterStorageInvalidError();

UFixed6 scaleLimit = self.makerLimit.div(self.efficiencyLimit).mul(protocolParameter.minScale);
if (self.takerFee.scale.lt(scaleLimit) || self.makerFee.scale.lt(scaleLimit))
(UFixed6 makerLimitTruncated, UFixed6 takerFeeScaleTruncated, UFixed6 makerFeeScaleTruncated) = (
UFixed6Lib.from(self.makerLimit.truncate()),
UFixed6Lib.from(self.takerFee.scale.truncate()),
UFixed6Lib.from(self.makerFee.scale.truncate())
);
UFixed6 scaleLimit = makerLimitTruncated.div(self.efficiencyLimit).mul(protocolParameter.minScale);
if (takerFeeScaleTruncated.lt(scaleLimit) || makerFeeScaleTruncated.lt(scaleLimit))
revert RiskParameterStorageInvalidError();
}

Expand Down
25 changes: 25 additions & 0 deletions packages/perennial/test/unit/types/RiskParameter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,31 @@ describe('RiskParameter', () => {
),
).to.be.revertedWithCustomError(riskParameterStorage, 'RiskParameterStorageInvalidError')
})

it('reverts if truncation would violate protocol limits', async () => {
await expect(
riskParameter.validateAndStore(
{
...VALID_RISK_PARAMETER,
makerLimit: parse6decimal('3.9'),
efficiencyLimit: parse6decimal('1'),
takerFee: {
linearFee: 2,
proportionalFee: 3,
adiabaticFee: 18,
scale: parse6decimal('1.95'),
},
},
{
...PROTOCOL_PARAMETER,
minScale: parse6decimal('0.50'),
minEfficiency: parse6decimal('1'),
},
),
// truncation of makerLimit and takerFee.scale leave makerLimit=3, takerFee.scale=1,
// exceeding protocol efficiency limit
).to.be.revertedWithCustomError(riskParameterStorage, 'RiskParameterStorageInvalidError')
})
})

describe('.pController_k', () => {
Expand Down
Loading