Skip to content

Commit

Permalink
Add oi and oiShares to main functions
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetto90 committed Mar 19, 2024
1 parent bb703a3 commit 0090939
Showing 1 changed file with 53 additions and 12 deletions.
65 changes: 53 additions & 12 deletions contracts/OverlayV1Market.sol
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ contract OverlayV1Market is IOverlayV1Market, Pausable {
require(collateral >= params.get(Risk.Parameters.MinCollateral), "OVV1:collateral<min");

uint256 oi;
uint256 oiShares;
uint256 debt;
uint256 price;
uint256 tradingFee;
Expand Down Expand Up @@ -237,7 +238,7 @@ contract OverlayV1Market is IOverlayV1Market, Pausable {

// add new position's open interest to the side's aggregate oi value
// and increase number of oi shares issued
uint256 oiShares = _addToOiAggregates(oi, capOi, isLong);
oiShares = _addToOiAggregates(oi, capOi, isLong);

// assemble position info data
// check position is not immediately liquidatable prior to storing
Expand Down Expand Up @@ -271,7 +272,11 @@ contract OverlayV1Market is IOverlayV1Market, Pausable {
}

// emit build event
emit Build(msg.sender, positionId_, oi, debt, isLong, price);
if (isLong) {
emit Build(msg.sender, positionId_, oi, debt, isLong, price, oiLong, oiLongShares);
} else {
emit Build(msg.sender, positionId_, oi, debt, isLong, price, oiShort, oiShortShares);
}

// transfer in the OV collateral needed to back the position + fees
// trading fees charged as a percentage on notional size of position
Expand All @@ -296,13 +301,14 @@ contract OverlayV1Market is IOverlayV1Market, Pausable {
uint256 cost;
uint256 price;
uint256 tradingFee;
Position.Info memory pos;
// avoids stack too deep
{
// call to update before any effects
Oracle.Data memory data = update();

// check position exists
Position.Info memory pos = positions.get(msg.sender, positionId);
pos = positions.get(msg.sender, positionId);
require(pos.exists(), "OVV1:!position");

// cache for gas savings
Expand Down Expand Up @@ -377,7 +383,27 @@ contract OverlayV1Market is IOverlayV1Market, Pausable {
}

// emit unwind event
emit Unwind(msg.sender, positionId, fraction, int256(value) - int256(cost), price);
if (pos.isLong) {
emit Unwind(
msg.sender,
positionId,
fraction,
int256(value) - int256(cost),
price,
oiLong,
oiLongShares
);
} else {
emit Unwind(
msg.sender,
positionId,
fraction,
int256(value) - int256(cost),
price,
oiShort,
oiShortShares
);
}

// mint or burn the pnl for the position
if (value >= cost) {
Expand All @@ -401,10 +427,11 @@ contract OverlayV1Market is IOverlayV1Market, Pausable {
uint256 liquidationFee;
uint256 marginToBurn;
uint256 marginRemaining;
Position.Info memory pos;
// avoids stack too deep
{
// check position exists
Position.Info memory pos = positions.get(owner, positionId);
pos = positions.get(owner, positionId);
require(pos.exists(), "OVV1:!position");

// call to update before any effects
Expand Down Expand Up @@ -466,13 +493,27 @@ contract OverlayV1Market is IOverlayV1Market, Pausable {
}

// emit liquidate event
emit Liquidate(
msg.sender,
owner,
positionId,
int256(value) - int256(cost) - int256(marginToBurn),
price
);
if (pos.isLong) {
emit Liquidate(
msg.sender,
owner,
positionId,
int256(value) - int256(cost) - int256(marginToBurn),
price,
oiLong,
oiLongShares
);
} else {
emit Liquidate(
msg.sender,
owner,
positionId,
int256(value) - int256(cost) - int256(marginToBurn),
price,
oiShort,
oiShortShares
);
}

// burn the pnl for the position + insurance margin
ov.burn(cost - value + marginToBurn);
Expand Down

0 comments on commit 0090939

Please sign in to comment.