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

collateral manager struct packing #128

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
237 changes: 171 additions & 66 deletions contracts/collateral/OverlayV1OVLCollateral.sol

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions contracts/interfaces/IOverlayV1Market.sol
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ interface IOverlayV1Market is IERC1155 {
function enterOI (
bool _isLong,
uint _collateral,
uint _leverage
uint _leverage,
uint _fee
) external returns (
uint oiAdjusted_,
uint collateralAdjusted_,
uint debtAdjusted_,
uint fee_,
uint exactedFee_,
uint impact_,
uint pricePointNext_
);
Expand Down
6 changes: 6 additions & 0 deletions contracts/interfaces/IOverlayV1OVLCollateral.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ interface IOverlayV1OVLCollateral is IERC1155 {
uint _maxLeverage
) external;

function marketIndexes (
address _market
) external view returns (
uint marketIx_
);

function marginMaintenance(
address _market
) external view returns (
Expand Down
27 changes: 14 additions & 13 deletions contracts/libraries/Position.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ library Position {
using FixedPoint for uint256;

struct Info {
address market; // the market for the position
uint8 market; // the market for the position
bool isLong; // whether long or short
uint leverage; // discrete initial leverage amount
uint pricePoint; // pricePointIndex
uint256 oiShares; // shares of total open interest on long/short side, depending on isLong value
uint256 debt; // total debt associated with this position
uint256 cost; // total amount of collateral initially locked; effectively, cost to enter position
uint16 leverage; // discrete initial leverage amount
uint32 pricePoint; // pricePointIndex
uint112 oiShares; // shares of total open interest on long/short side, depending on isLong value
uint112 debt; // total debt associated with this position
uint112 cost; // total amount of collateral initially locked; effectively, cost to enter position
}


uint256 constant TWO = 2e18;

function _initialOi (
Expand All @@ -36,7 +37,7 @@ library Position {
uint256 totalOiShares
) private pure returns (uint256 oi_) {

oi_ = _self.oiShares
oi_ = uint(_self.oiShares)
.mulDown(totalOi)
.divUp(totalOiShares);

Expand All @@ -55,12 +56,12 @@ library Position {
if (_self.isLong) { // oi * priceFrame - debt

val_ = __oi.mulDown(priceFrame);
val_ -= Math.min(val_, _self.debt); // floor to 0
val_ -= Math.min(val_, uint(_self.debt)); // floor to 0

} else { // oi * (2 - priceFrame) - debt

val_ = __oi.mulDown(2e18);
val_ -= Math.min(val_, _self.debt + __oi.mulDown(priceFrame)); // floor to 0
val_ -= Math.min(val_, uint(_self.debt) + __oi.mulDown(priceFrame)); // floor to 0

}

Expand All @@ -78,8 +79,8 @@ library Position {

bool _long = _self.isLong;

if (_long) isUnder = __oi.mulDown(priceFrame) < _self.debt;
else isUnder = __oi.mulDown(priceFrame) + _self.debt < ( __oi * 2 );
if (_long) isUnder = __oi.mulDown(priceFrame) < uint(_self.debt);
else isUnder = __oi.mulDown(priceFrame) + uint(_self.debt) < ( __oi * 2 );

}

Expand All @@ -98,7 +99,7 @@ library Position {
priceFrame
);

notion = val + _self.debt;
notion = val + uint(_self.debt);

}

Expand Down Expand Up @@ -200,7 +201,7 @@ library Position {
uint256 _posInitialOi = _initialOi(_self);

uint256 _oiFrame = _posInitialOi.mulUp(_marginMaintenance)
.add(_self.debt)
.add(uint(_self.debt))
.divDown(_posOi);

if (_self.isLong) liqPrice = _priceEntry.mulUp(_oiFrame);
Expand Down
13 changes: 7 additions & 6 deletions contracts/market/OverlayV1Market.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@ abstract contract OverlayV1Market is OverlayV1Governance {
@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 fee_ The protocol fee to be taken
@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,
uint _collateral,
uint _leverage
uint _leverage,
uint _fee
) external onlyCollateral returns (
uint oiAdjusted_,
uint collateralAdjusted_,
uint debtAdjusted_,
uint fee_,
uint exactedFee_,
uint impact_,
uint pricePointNext_
) {
Expand All @@ -67,13 +68,13 @@ abstract contract OverlayV1Market is OverlayV1Governance {
uint _impact = intake(_isLong, _oi, _cap);

// Call to `FixedPoint` contract
fee_ = _oi.mulDown(mothership.fee());
exactedFee_ = _oi.mulDown(_fee);

impact_ = _impact;

require(_collateral >= MIN_COLLAT + impact_ + fee_ , "OVLV1:collat<min");
require(_collateral >= MIN_COLLAT + impact_ + exactedFee_ , "OVLV1:collat<min");

collateralAdjusted_ = _collateral - _impact - fee_;
collateralAdjusted_ = _collateral - _impact - exactedFee_;

oiAdjusted_ = collateralAdjusted_ * _leverage;

Expand Down
16 changes: 12 additions & 4 deletions tests/markets/collateral/ovl/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ def test_build_success_zero_impact(ovl_collateral, token, mothership, market,
pos_debt,
pos_cost) = ovl_collateral.positions(pid)

assert pos_market == market
market_ix = ovl_collateral.marketIndexes(market)

assert pos_market == market_ix
assert pos_islong == is_long
assert pos_lev == leverage
assert pos_price_idx == market.pricePointNextIndex() - 1
Expand Down Expand Up @@ -765,7 +767,9 @@ def test_build_w_impact(
pos_debt,
pos_cost) = ovl_collateral.positions(pid)

assert pos_market == market
market_ix = ovl_collateral.marketIndexes(market)

assert pos_market == market_ix
assert pos_islong == is_long
assert pos_lev == leverage
assert pos_price_idx == market.pricePointNextIndex() - 1
Expand Down Expand Up @@ -981,7 +985,9 @@ def test_build_multiple_in_one_impact_window(
pos_debt,
pos_cost) = ovl_collateral.positions(pid)

assert pos_market == market
market_ix = ovl_collateral.marketIndexes(market)

assert pos_market == market_ix
assert pos_islong == is_long
assert pos_lev == leverage
assert pos_price_idx == market.pricePointNextIndex() - 1
Expand Down Expand Up @@ -1146,7 +1152,9 @@ def test_build_multiple_in_multiple_impact_windows(
pos_debt,
pos_cost) = ovl_collateral.positions(pid)

assert pos_market == market
market_ix = ovl_collateral.marketIndexes(market)

assert pos_market == market_ix
assert pos_islong == is_long
assert pos_lev == leverage
assert pos_price_idx == market.pricePointNextIndex() - 1
Expand Down
4 changes: 4 additions & 0 deletions tests/markets/collateral/ovl/test_liquidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ def test_liquidate_with_funding(
_ = ovl_collateral.liquidate(pos_id, alice, {'from': alice})
exit_bid, exit_ask, _ = market.pricePoints(
market.pricePointNextIndex() - 1)

print("margin maintenance", margin_maintenance)
print("pos oi shares", pos_oi_shares)

assert pos_val < margin_maintenance * pos_oi_shares

# check alice oi still there
Expand Down
16 changes: 10 additions & 6 deletions tests/markets/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,10 @@ def comptroller(gov, feed_infos, token, feed_owner):
BRRRR_WINDOW_MICRO # brrrr window micro - accumulator window
],
"OverlayV1OVLCollateral", [
.06e18, # margin maintenance
.5e18, # margin reward rate
.0015e18, # fee
100, # max leverage
.5e18, # margin reward rate
.06e18, # margin maintenance
],
get_uni_feeds,
),
Expand Down Expand Up @@ -314,9 +315,10 @@ def create_mothership(token, feed_infos, fees, alice, bob, gov, feed_owner,
[int]: Accumulator window - brrrr window micro
ovlc_name [str]: OverlayV1OVLCollateral contract name
ovlc_args:
[int]: maintenance margin [uint]
[int]: fee [uint]
[int]: max leverage [uint]
[int]: margin reward rate [uint]
[int]: max leverage
[int]: maintenance margin [uint]
get_uni_feeds []: TODO

Output:
Expand Down Expand Up @@ -385,8 +387,10 @@ def create_mothership(
ovl_collateral = gov.deploy(ovlc_type, "our_uri", mothership)

# Governor sets the market information which includes the maintenance
# margin, margin reward rate, and max leverage
ovl_collateral.setMarketInfo(market, *ovlc_args, {"from": gov})
# fee, max leverage, margin reward rate, margin
ovl_collateral.addMarket(market, *ovlc_args, {"from": gov})

print("OVLC ARGS", ovlc_args)

# Governor makes call to mothership contract, making it aware of the
# new collateral contract
Expand Down
16 changes: 5 additions & 11 deletions tests/markets/test_conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_create_token(token, gov, alice, bob):
print(token)


def test_create_mothership(mothership, gov):
def test_create_mothership(mothership, fees):
'''
TODO: ovl(), marketExists(), allMarkets(), totalMarkets()
- check mothership events fired
Expand All @@ -74,16 +74,10 @@ def test_create_mothership(mothership, gov):
# print(mothership.getGlobalParams())

# Test mothership `getGlobalParams` external view function
margin_burn_rate = 0.5e18
fee_burn_rate = 0.5e18
fee_to = '0x46C0a5326E643E4f71D3149d50B48216e174Ae84'
expect = (margin_burn_rate, fee_burn_rate, fee_to)
margin_burn_rate = int(.5e18)
fee_burn_rate = int(.5e18)
expect = (margin_burn_rate, fee_burn_rate, fees)

actual = mothership.getUpdateParams()
assert actual == expect

# eth = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
assert 523 == 523
# assert 3 == 3
# assert 5 == 5
# assert 5 == 5
assert actual == expect