Skip to content

Commit

Permalink
fix invariants
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrizzle committed Jul 18, 2023
1 parent 917a392 commit 8cfc715
Show file tree
Hide file tree
Showing 6 changed files with 9,566 additions and 9,475 deletions.
3 changes: 1 addition & 2 deletions packages/perennial-vault/contracts/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,7 @@ contract Vault is IVault, Instance {
latestOracleVersion.price.abs() :
global.latestPrice.abs();
context.markets[marketId].currentPosition = currentPosition.maker;
context.markets[marketId].currentNet =
Fixed6Lib.from(currentPosition.long).sub(Fixed6Lib.from(currentPosition.short)).abs();
context.markets[marketId].currentNet = currentPosition.net();
context.totalWeight += registration.weight;

// local
Expand Down
8 changes: 2 additions & 6 deletions packages/perennial-vault/contracts/lib/StrategyLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,8 @@ library StrategyLib {
// minimum position size before crossing the net position
context.currentAccountPosition.maker.sub(
context.currentPosition.maker
.sub(
Fixed6Lib.from(context.currentPosition.long) // TODO: cleanup
.sub(Fixed6Lib.from(context.currentPosition.short))
.abs()
.min(context.currentPosition.maker)
).min(context.currentAccountPosition.maker)
.sub(context.currentPosition.net().min(context.currentPosition.maker))
.min(context.currentAccountPosition.maker)
),
// maximum position size before crossing the maker limit
context.currentAccountPosition.maker.add(
Expand Down
16 changes: 8 additions & 8 deletions packages/perennial/contracts/Market.sol
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,8 @@ contract Market is IMarket, Instance {
_invariant(context, account, newOrder, collateral, protected);

// store
if (context.global.currentId > context.latestPosition.global.id) // don't re-store if already settled
_pendingPosition[context.global.currentId].store(context.currentPosition.global);
if (context.local.currentId > context.latestPosition.local.id) // don't re-store if already settled
_pendingPositions[account][context.local.currentId].store(context.currentPosition.local);
_pendingPosition[context.global.currentId].store(context.currentPosition.global);
_pendingPositions[account][context.local.currentId].store(context.currentPosition.local);

_endGas(context);

Expand Down Expand Up @@ -548,8 +546,10 @@ contract Market is IMarket, Instance {
if (!_collateralized(context, context.currentPosition.local))
{ if (LOG_REVERTS) console.log("MarketInsufficientCollateralizationError2"); revert MarketInsufficientCollateralizationError(); }

if (!protected && context.global.currentId > context.latestPosition.global.id + context.protocolParameter.maxPendingIds)
{ if (LOG_REVERTS) console.log("MarketExceedsPendingIdLimitError"); revert MarketExceedsPendingIdLimitError(); }
if (
!protected &&
context.global.currentId > context.latestPosition.global.id + context.protocolParameter.maxPendingIds
) { if (LOG_REVERTS) console.log("MarketExceedsPendingIdLimitError"); revert MarketExceedsPendingIdLimitError(); }

if (!protected && !_collateralized(context, context.latestPosition.local))
{ if (LOG_REVERTS) console.log("MarketInsufficientCollateralizationError1"); revert MarketInsufficientCollateralizationError(); }
Expand All @@ -567,7 +567,7 @@ contract Market is IMarket, Instance {
if (
!protected &&
!context.marketParameter.closed &&
(!context.marketParameter.makerCloseAlways || newOrder.increasesMaker()) &&
(!context.marketParameter.makerCloseAlways) &&
(!context.marketParameter.takerCloseAlways || newOrder.increasesTaker()) &&
newOrder.efficiency.lt(Fixed6Lib.ZERO) &&
context.currentPosition.global.efficiency().lt(context.riskParameter.efficiencyLimit)
Expand All @@ -576,7 +576,7 @@ contract Market is IMarket, Instance {
if (
!protected &&
!context.marketParameter.closed &&
(!context.marketParameter.makerCloseAlways || newOrder.increasesMaker()) &&
(!context.marketParameter.makerCloseAlways) &&
(!context.marketParameter.takerCloseAlways || newOrder.increasesTaker()) &&
context.currentPosition.global.socialized() &&
newOrder.decreasesLiquidity()
Expand Down
9 changes: 3 additions & 6 deletions packages/perennial/contracts/types/Order.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct Order {
Fixed6 efficiency;
UFixed6 fee;
UFixed6 keeper;
Fixed6 net;
}
using OrderLib for Order global;

Expand Down Expand Up @@ -44,19 +45,15 @@ library OrderLib {
}

function increasesPosition(Order memory self) internal pure returns (bool) {
return increasesMaker(self) || increasesTaker(self);
}

function increasesMaker(Order memory self) internal pure returns (bool) {
return self.maker.gt(Fixed6Lib.ZERO);
return self.maker.gt(Fixed6Lib.ZERO) || increasesTaker(self);
}

function increasesTaker(Order memory self) internal pure returns (bool) {
return self.long.gt(Fixed6Lib.ZERO) || self.short.gt(Fixed6Lib.ZERO);
}

function decreasesLiquidity(Order memory self) internal pure returns (bool) {
return self.maker.lt(Fixed6Lib.ZERO) || self.impact.gt(Fixed6Lib.ZERO);
return self.maker.lt(self.net);
}

function isEmpty(Order memory self) internal pure returns (bool) {
Expand Down
9 changes: 7 additions & 2 deletions packages/perennial/contracts/types/Position.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ library PositionLib {

/// @dev update the current global position
function update(Position memory self, uint256 currentId, uint256 currentTimestamp, Order memory order) internal pure {
(Fixed6 latestSkew, UFixed6 latestEfficiency) = (skew(self), efficiency(self));
(UFixed6 latestNet, Fixed6 latestSkew, UFixed6 latestEfficiency) = (net(self), skew(self), efficiency(self));

if (self.id != currentId) _prepare(self);
(self.id, self.timestamp, self.maker, self.long, self.short) = (
Expand All @@ -99,7 +99,8 @@ library PositionLib {
UFixed6Lib.from(Fixed6Lib.from(self.short).add(order.short))
);

(order.skew, order.impact, order.efficiency) = (
(order.net, order.skew, order.impact, order.efficiency) = (
Fixed6Lib.from(net(self)).sub(Fixed6Lib.from(latestNet)),
skew(self).sub(latestSkew).abs(),
Fixed6Lib.from(skew(self).abs()).sub(Fixed6Lib.from(latestSkew.abs())),
Fixed6Lib.from(efficiency(self)).sub(Fixed6Lib.from(latestEfficiency))
Expand Down Expand Up @@ -145,6 +146,10 @@ library PositionLib {
return self.long.min(self.short);
}

function net(Position memory self) internal pure returns (UFixed6) {
return Fixed6Lib.from(self.long).sub(Fixed6Lib.from(self.short)).abs();
}

function skew(Position memory self) internal pure returns (Fixed6) {
return major(self).isZero() ?
Fixed6Lib.ZERO :
Expand Down
Loading

0 comments on commit 8cfc715

Please sign in to comment.