Skip to content

Commit

Permalink
Merge pull request #172 from overlay-market/s/refactor-position
Browse files Browse the repository at this point in the history
Refactor Position.sol
  • Loading branch information
magnetto90 authored Jan 25, 2024
2 parents 7cf85eb + 8172b12 commit e842ac0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 43 deletions.
14 changes: 5 additions & 9 deletions contracts/libraries/Position.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ library Position {
/// @notice Whether the position exists
/// @dev Is false if position has been liquidated or fraction remaining == 0
function exists(Info memory self) internal pure returns (bool exists_) {
return (!self.liquidated && self.fractionRemaining > 0);
return (self.fractionRemaining > 0);
}

/*///////////////////////////////////////////////////////////////
Expand All @@ -102,7 +102,6 @@ library Position {
pure
returns (uint16)
{
require(fractionRemoved <= ONE, "OVV1:fraction>max");
uint256 fractionRemaining = _fractionRemaining(self).mulDown(ONE - fractionRemoved);
return fractionRemaining.toUint16Fixed();
}
Expand Down Expand Up @@ -163,17 +162,15 @@ library Position {
/// @notice accounting for amount of position remaining
/// @dev use mulUp to avoid rounding leftovers on unwind
function notionalInitial(Info memory self, uint256 fraction) internal pure returns (uint256) {
uint256 fractionRemaining = _fractionRemaining(self);
uint256 notionalForRemaining = _notionalInitial(self).mulUp(fractionRemaining);
uint256 notionalForRemaining = _notionalInitial(self).mulUp(_fractionRemaining(self));
return notionalForRemaining.mulUp(fraction);
}

/// @notice Computes the initial open interest of position when built
/// @notice accounting for amount of position remaining
/// @dev use mulUp to avoid rounding leftovers on unwind
function oiInitial(Info memory self, uint256 fraction) internal pure returns (uint256) {
uint256 fractionRemaining = _fractionRemaining(self);
uint256 oiInitialForRemaining = _oiInitial(self).mulUp(fractionRemaining);
uint256 oiInitialForRemaining = _oiInitial(self).mulUp(_fractionRemaining(self));
return oiInitialForRemaining.mulUp(fraction);
}

Expand All @@ -190,8 +187,7 @@ library Position {
/// @notice for amount of position remaining
/// @dev use mulUp to avoid rounding leftovers on unwind
function debtInitial(Info memory self, uint256 fraction) internal pure returns (uint256) {
uint256 fractionRemaining = _fractionRemaining(self);
uint256 debtForRemaining = _debtInitial(self).mulUp(fractionRemaining);
uint256 debtForRemaining = _debtInitial(self).mulUp(_fractionRemaining(self));
return debtForRemaining.mulUp(fraction);
}

Expand Down Expand Up @@ -317,7 +313,7 @@ library Position {
uint256 fraction = ONE;
uint256 posNotionalInitial = notionalInitial(self, fraction);

if (self.liquidated || self.fractionRemaining == 0) {
if (self.fractionRemaining == 0) {
// already been liquidated or doesn't exist
// latter covers edge case of val == 0 and MM + liq fee == 0
return false;
Expand Down
34 changes: 0 additions & 34 deletions tests/libraries/position/test_fraction.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from brownie import reverts
from brownie.test import given, strategy
from decimal import Decimal

Expand Down Expand Up @@ -69,36 +68,3 @@ def test_updated_fraction_remaining(position, fraction_remaining,
expect = updated_fraction_remaining
actual = position.updatedFractionRemaining(pos, fraction_removed_256)
assert expect == actual


def test_updated_fraction_remaining_reverts_when_gt_max(position):
notional = 10000000000000000000 # 10
debt = 2000000000000000000 # 2
is_long = True
liquidated = False
fraction_remaining = 9000 # 0.9

mid_price = 100000000000000000000 # 100
entry_price = 101000000000000000000 # 101

mid_tick = price_to_tick(mid_price)
entry_tick = price_to_tick(entry_price)

oi = int((notional / mid_price) * 1000000000000000000) # 0.1
shares_to_oi_ratio = 800000000000000000 # 0.8
oi_shares = int(Decimal(oi) * Decimal(shares_to_oi_ratio)
/ Decimal(1e18)) # 0.08

pos = (notional, debt, mid_tick, entry_tick, is_long,
liquidated, oi_shares, fraction_remaining)

# check fails when greater than ONE
fraction_removed = 1000000000000000001 # 1 greater than ONE (18 decimals)
with reverts("OVV1:fraction>max"):
position.updatedFractionRemaining(pos, fraction_removed)

# check succeeds when equal to ONE
fraction_removed = 1000000000000000000
expect = 0
actual = position.updatedFractionRemaining(pos, fraction_removed)
assert expect == actual

0 comments on commit e842ac0

Please sign in to comment.