Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

Commit

Permalink
Comments on Market _update function and children
Browse files Browse the repository at this point in the history
  • Loading branch information
sicknick99 committed Dec 11, 2021
1 parent 8452ddc commit a3f58b8
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 79 deletions.
6 changes: 6 additions & 0 deletions contracts/market/OverlayV1Choreographer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ abstract contract OverlayV1Choreographer is
_;
}

// TODO
struct Tempo {
// TODO
uint32 updated;
// TODO
uint32 compounded;
// TODO
uint8 impactCycloid;
// TODO
uint8 brrrrdCycloid;
// TODO
uint32 brrrrdFiling;
}

Expand Down
51 changes: 42 additions & 9 deletions contracts/market/OverlayV1Comptroller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,21 @@ abstract contract OverlayV1Comptroller {

}

/**
@dev Called by internal contract function: _oiCap
@dev Calls internal contract function: scry
@param _brrrrdCycloid TODO
@returns brrrrd_ TODO
@returns antiBrrrrd_ TODO
*/
function getBrrrrd (
uint8 _brrrrdCycloid
) public view returns (
uint brrrrd_,
uint antiBrrrrd_
) {

// Call to internal contract function
( ,Roller memory _rollerNow,
Roller memory _rollerThen ) = scry(
getBrrrrdRoller,
Expand All @@ -176,7 +184,7 @@ abstract contract OverlayV1Comptroller {
@dev The impact is a measure of the demand placed on the market over a
@dev rolling window. It determines the amount of collateral to be burnt.
@dev This is akin to slippage in an order book model.
@dev Called by `OverlayV1Market` contract function: `enterOI`
@dev Called by OverlayV1Market contract function: enterOI
@param _isLong Whether it is taking out open interest on the long or short side
@param _oi The amount of open interest attempting to be taken out
@param _cap The current open interest cap
Expand Down Expand Up @@ -299,7 +307,7 @@ abstract contract OverlayV1Comptroller {
has occurred.
@param _brrrrdExpected How much the market expects to print before
engaging the dynamic cap. Only passed if printing has occurred.
@dev Called by `OverlayV1Market` contract function: `update`
@dev Called by OverlayV1Market contract function: update
*/
function _computeOiCap (
bool _dynamic,
Expand All @@ -320,21 +328,24 @@ abstract contract OverlayV1Comptroller {

}

/**
@notice The open interest cap for the market
@dev Returns the open interest cap for the market.
@dev Called by `OverlayV1Market` contract function: `update`
@return cap_ The open interest cap.
*/
function oiCap () public virtual view returns (uint cap_);

/**
@notice The open interest cap for the market.
@dev Called by OverlayV1Market contract function: update
@dev Calls internal contract function: getBrrrrd
@param _depth TODO
@param _brrrrdCycloid TODO
@return cap_ The open interest cap for the market
*/
function _oiCap (
uint _depth,
uint8 _brrrrdCycloid
) internal virtual view returns (
uint cap_
) {

// Calls internal contract function
( uint _brrrrd,
uint _antiBrrrrd ) = getBrrrrd(_brrrrdCycloid);

Expand Down Expand Up @@ -465,7 +476,9 @@ abstract contract OverlayV1Comptroller {
/**
@notice First part of retrieving historic roller values
@dev Checks to see if the current roller is satisfactory and if not
searches deeper into the roller array.
@dev searches deeper into the roller array.
@dev Called by internal contract function: getBrrrrd
@dev Calls internal contract function: scryRollers
@param _getter The getter for either impact or brrrrd rollers
@param _chord The length of roller array in question
@param _cycloid The current impact or brrrrd cycloid
Expand Down Expand Up @@ -503,6 +516,7 @@ abstract contract OverlayV1Comptroller {

} else if (_time != rollerNow_.time) rollerNow_.time = _time;

// Calls internal contract function
rollerThen_ = scryRollers(
_getter,
_chord,
Expand All @@ -513,6 +527,15 @@ abstract contract OverlayV1Comptroller {
}


/**
@dev Called by internal contract function: scry
@dev Calls internal contract function: binarySearch
@param _getter TODO
@param _chord TODO
@param _cycloid TODO
@param _target TODO
@return beforeOrAt_ TODO
*/
function scryRollers (
function (uint) internal view returns(Roller memory) _getter,
uint _chord,
Expand Down Expand Up @@ -542,6 +565,7 @@ abstract contract OverlayV1Comptroller {
if ( beforeOrAt_.time <= 1 ) beforeOrAt_ = _getter(0);

if (_target <= beforeOrAt_.time) return beforeOrAt_;
// Calls internal contract function
else return binarySearch(
_getter,
uint16(_chord),
Expand All @@ -551,6 +575,15 @@ abstract contract OverlayV1Comptroller {

}

/**
@notice TODO
@dev Called by internal contract function: scryRollers
@param _getter TODO
@param _cycloid TODO
@param _chord TODO
@param _target TODO
@return beforeOrAt_ TODO
*/
function binarySearch(
function (uint) internal view returns(Roller memory) _getter,
uint16 _cycloid,
Expand Down
86 changes: 49 additions & 37 deletions contracts/market/OverlayV1Market.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,19 @@ abstract contract OverlayV1Market is OverlayV1Choreographer {
@dev This is invoked by Overlay collateral manager contracts, which
@dev can be for OVL, ERC20's, Overlay positions, NFTs, or what have you.
@dev The calculations for impact and fees are performed here.
@dev Calls `OverlayV1Comptroller` contract function: `intake`
@dev Calls `Position` contract function: `mulDown`
@dev Calls `OverlayV1OI` contract function: `addOi`
@param _isLong The side of the market to enter open interest on.
@param _collateral The amount of collateral in OVL terms to take the position out with.
@param _leverage The leverage with which to take out the position.
@return oiAdjusted_ Amount of open interest after impact and fees.
@return collateralAdjusted_ Amount of collateral after impact and fees.
@return debtAdjusted_ Amount of debt after impact and fees.
@return exactedFee_ The protocol fee to be taken.
@return impact_ The market impact for the build.
@return pricePointNext_ The index of the price point for the position.
@dev Calls OverlayV1Choreographer contract struct: Tempo
@dev Calls OverlayV1Comptroller contract function: intake
@dev Calls Position contract function: mulDown
@dev Calls OverlayV1OI contract function: addOi
@param _isLong The side of the market to enter open interest on
@param _collateral The amount of collateral in OVL terms to take the position out with
@param _leverage The leverage with which to take out the position
@return oiAdjusted_ Amount of open interest after impact and fees
@return collateralAdjusted_ Amount of collateral after impact and fees
@return debtAdjusted_ Amount of debt after impact and fees
@return exactedFee_ The protocol fee to be taken
@return impact_ The market impact for the build
@return pricePointNext_ The index of the price point for the position
*/
function enterOI (
bool _isLong,
Expand All @@ -59,13 +60,14 @@ abstract contract OverlayV1Market is OverlayV1Choreographer {
) {

uint _cap;
// Call to `Position` contract
// Call to Position contract
// Calculate open interest
uint _oi = _collateral.mulDown(_leverage);

// TODO
OverlayV1Choreographer.Tempo memory _tempo = tempo;

// Call to internal function.
// Call to internal function
// Updates the market with the latest price, cap, and pay funding
( _cap,
_tempo.updated,
Expand All @@ -75,7 +77,7 @@ abstract contract OverlayV1Market is OverlayV1Choreographer {
_tempo.brrrrdCycloid
);

// Call to `OverlayV1Comptroller` contract
// Call to OverlayV1Comptroller contract
// Takes in the OI and applies Overlay's monetary policy
( impact_,
_tempo.impactCycloid,
Expand All @@ -93,7 +95,7 @@ abstract contract OverlayV1Market is OverlayV1Choreographer {

pricePointNext_ = _pricePoints.length - 1;

// Call to `Position` contract
// Call to Position contract
exactedFee_ = _oi.mulDown(_fee);

require(_collateral >= MIN_COLLAT + impact_ + exactedFee_ , "OVLV1:collat<min");
Expand All @@ -104,7 +106,7 @@ abstract contract OverlayV1Market is OverlayV1Choreographer {

debtAdjusted_ = oiAdjusted_ - collateralAdjusted_;

// Call to `OverlayV1OI` contract
// Call to OverlayV1OI contract
addOi(_isLong, oiAdjusted_, _cap);

}
Expand All @@ -115,13 +117,13 @@ abstract contract OverlayV1Market is OverlayV1Choreographer {
@dev This is called by the collateral managers to retrieve the necessary
@dev information to calculate the specifics of each position, for
@dev instance the PnL or if it is liquidatable.
@param _isLong Whether the data is being retrieved for a long or short.
@param _isLong Whether the data is being retrieved for a long or short
@param _pricePoint Index of the initial price point
@param oi_ Total outstanding open interest on that side of the market.
@param oiShares_ Total outstanding open interest shares on that side.
@param oi_ Total outstanding open interest on that side of the market
@param oiShares_ Total outstanding open interest shares on that side
@param priceFrame_ The price multiple comprised of the entry and exit
prices for the position, with the exit price being the current one. Longs
receive the bid on exit and the ask on entry shorts the opposite.
receive the bid on exit and the ask on entry shorts the opposite
*/
function exitData (
bool _isLong,
Expand Down Expand Up @@ -159,11 +161,11 @@ abstract contract OverlayV1Market is OverlayV1Choreographer {
@dev open interest in OVL terms to remove as well as open interest shares
@dev to remove. It also registers printing or burning of OVL in the
@dev process.
@param _isLong The side from which to remove open interest.
@param _oi The open interest to remove in OVL terms.
@param _oiShares The open interest shares to remove.
@param _brrrr How much was printed on closing the position.
@param _antiBrrrr How much was burnt on closing the position.
@param _isLong The side from which to remove open interest
@param _oi The open interest to remove in OVL terms
@param _oiShares The open interest shares to remove
@param _brrrr How much was printed on closing the position
@param _antiBrrrr How much was burnt on closing the position
*/
function exitOI (
bool _isLong,
Expand Down Expand Up @@ -191,15 +193,13 @@ abstract contract OverlayV1Market is OverlayV1Choreographer {
}

/**
@notice Internal update function to price, cap, and pay funding.
@dev This function updates the market with the latest price and
@dev conditionally reads the depth of the market feed. The market needs
@dev an update on the first call of any block.
@dev Calls `OverlayV1PricePoint` contract function: `fetchPricePoint`
@dev Calls `OverlayV1PricePoint` contract function: `setPricePointNext`
@dev Calls `OverlayV1OI` contract function: `epochs`
@dev Calls `OverlayV1OI` contract function: `payFunding`
@dev Calls `OverlayV1Comptroller` contract function: `oiCap`
@notice Public function to update price, cap, and pay funding.
@dev This function calls the internal _update function which updates the
@dev market with the latest price and conditionally reads the depth of
@dev the market feed. The market needs an update on the first call of any
@dev block.
@dev Calls OverlayV1Choreographer contract struct: Tempo.
@dev Calls the internal contract function: _update.
*/
function update () public {

Expand All @@ -217,11 +217,16 @@ abstract contract OverlayV1Market is OverlayV1Choreographer {
}

/**
@notice Internal update function to price, cap, and pay funding.
@notice Internal function to update price, cap, and pay funding.
@dev This function updates the market with the latest price and
@dev conditionally reads the depth of the market feed. The market needs
@dev an update on the first call of any block.
@return cap_ The open interest cap for the market.
@dev Calls OverlayV1PricePoint contract function: fetchPricePoint
@dev Calls OverlayV1PricePoint contract function: setPricePointNext
@dev Calls OverlayV1PricePoint contract function: pricePointCurrent
@dev Calls OverlayV1OI contract function: epochs
@dev Calls OverlayV1OI contract function: payFunding
@dev Calls OverlayV1Comptroller contract function: oiCap
*/
function _update (
uint32 _updated,
Expand All @@ -238,26 +243,33 @@ abstract contract OverlayV1Market is OverlayV1Choreographer {

if (_now != _updated) {


// Call to OverlayV1PricePoint contract
PricePoint memory _pricePoint = fetchPricePoint();

// Call to OverlayV1PricePoint contract
setPricePointNext(_pricePoint);

_depth = _pricePoint.depth;

updated = _now;

// Call to OverlayV1PricePoint contract
} else (,,_depth) = pricePointCurrent();

// Call to OverlayV1OI contract function
( uint32 _compoundings,
uint32 _tCompounding ) = epochs(_now, _compounded);

if (0 < _compoundings) {

// Call to OverlayV1OI contract
payFunding(k, _compoundings);
_compounded = _tCompounding;

}

// Call to OverlayV1Comptroller contract
cap_ = _oiCap(_depth, _brrrrdCycloid);
updated_ = _updated;
compounded_ = _compounded;
Expand Down
Loading

0 comments on commit a3f58b8

Please sign in to comment.