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

Market liquidity decimals #139

Open
wants to merge 84 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
76203a6
Make _tickToPrice() internal
mikeyrf Dec 10, 2021
8591b80
Fix comments in market and mothership
mikeyrf Dec 10, 2021
1735993
Remove commented out code from collateral manager
mikeyrf Dec 10, 2021
db811de
Remove open TODOs
mikeyrf Dec 10, 2021
259b577
Fix ethIs0 for marketFeed, check one market token = WETH
mikeyrf Dec 10, 2021
72d6bd5
Save call to marketFeed.token0()
mikeyrf Dec 10, 2021
79d039b
Revert ovl token, update ERC1155Supply to patch, add tests for token.…
mikeyrf Dec 11, 2021
44c6d6a
Fix inequality for isUnderwater
mikeyrf Dec 13, 2021
307146a
Fix underwater fee accounting
mikeyrf Dec 13, 2021
04d1a2d
Fix fee amount comment
mikeyrf Dec 13, 2021
e531e30
Add setter for margin burn rate
mikeyrf Dec 13, 2021
fbfba1c
Fix unwind tests for underwater fee accounting
mikeyrf Dec 13, 2021
d902965
Fix collateral active bool flip on enable/disableCollateral
mikeyrf Dec 13, 2021
2aa7584
Fix require msgs for mothership
mikeyrf Dec 13, 2021
403e173
Add mothership conftest
mikeyrf Dec 13, 2021
9f55e90
Add mothership config tests
mikeyrf Dec 13, 2021
7b27e5c
Set ovl in mothership constructor
mikeyrf Dec 13, 2021
2975956
Make ovl token immutable in mothership
mikeyrf Dec 13, 2021
06fe6e4
Add events to mothership
mikeyrf Dec 13, 2021
c68de85
Remove guardian role from mothership
mikeyrf Dec 13, 2021
834d485
Add mothership global param bounds
mikeyrf Dec 13, 2021
6f700ff
Fix mothership test dir w init, clean up mothership adjust params
mikeyrf Dec 13, 2021
0d26079
Add mothership success tests for market
mikeyrf Dec 14, 2021
611f1e6
Fix mothership success tests, init revert tests
mikeyrf Dec 14, 2021
e395ca8
Finish mothership market sucess tests
mikeyrf Dec 14, 2021
66fa1cf
Init mothership collateral tests
mikeyrf Dec 14, 2021
f65c7bc
Add mothership collateral tests
mikeyrf Dec 16, 2021
9d5cc51
Finish mothership collateral tests
mikeyrf Dec 16, 2021
d39c1c3
Fix mothership global param setters and getter
mikeyrf Dec 17, 2021
78d5330
Add mothership global param tests
mikeyrf Dec 17, 2021
491ce1f
Add setter for margin burn rate
mikeyrf Dec 13, 2021
e213ad2
Fix collateral active bool flip on enable/disableCollateral
mikeyrf Dec 13, 2021
9b86527
Fix require msgs for mothership
mikeyrf Dec 13, 2021
69ee4f5
Add mothership conftest
mikeyrf Dec 13, 2021
bd78105
Add mothership config tests
mikeyrf Dec 13, 2021
ff13d94
Rebase main - set ovl mothership
mikeyrf Dec 20, 2021
48ac18b
Make ovl token immutable in mothership
mikeyrf Dec 13, 2021
55da9bc
Rebase main - add events to mothership
mikeyrf Dec 20, 2021
dbff53f
Remove guardian role from mothership
mikeyrf Dec 13, 2021
216dcba
Add mothership global param bounds
mikeyrf Dec 13, 2021
9c7332a
Fix mothership test dir w init, clean up mothership adjust params
mikeyrf Dec 13, 2021
7e4d760
Rebase main - add mothership success tests
mikeyrf Dec 20, 2021
513a198
Fix mothership success tests, init revert tests
mikeyrf Dec 14, 2021
886b8f0
Rebase main conflicts - Finish mothership market sucess tests
mikeyrf Dec 20, 2021
178c7ce
Init mothership collateral tests
mikeyrf Dec 14, 2021
44c955d
Add mothership collateral tests
mikeyrf Dec 16, 2021
74e22d6
Finish mothership collateral tests
mikeyrf Dec 16, 2021
6d7bd76
Rebase main conflicts - Fix mothership global param setters and getter
mikeyrf Dec 20, 2021
eb826de
Add mothership global param tests
mikeyrf Dec 17, 2021
1dc9544
Merge branch 'c4-mothership' of github.com:overlay-market/overlay-v1-…
mikeyrf Dec 20, 2021
36e88c4
Fix conftest tests for mothership changes
mikeyrf Dec 20, 2021
c51f56c
Fix comments in conftest
mikeyrf Dec 20, 2021
52a2119
Fix inequality for isUnderwater
mikeyrf Dec 13, 2021
c3037b7
Rebase main conflicts - Fix underwater fee accounting
mikeyrf Dec 20, 2021
f7a0af5
Fix fee amount comment
mikeyrf Dec 13, 2021
b8a5759
Fix unwind tests for underwater fee accounting
mikeyrf Dec 13, 2021
7fde531
Merge branch 'c4-underwater' of github.com:overlay-market/overlay-v1-…
mikeyrf Dec 20, 2021
7f9d5c5
Rebase main conflicts - Revert ovl token, update ERC1155Supply to pat…
mikeyrf Dec 20, 2021
883f49c
Merge branch 'c4-ovl' of github.com:overlay-market/overlay-v1-core in…
mikeyrf Dec 20, 2021
b543094
Fix ethIs0 for marketFeed, check one market token = WETH
mikeyrf Dec 10, 2021
39a2362
Save call to marketFeed.token0()
mikeyrf Dec 10, 2021
973fa01
Merge branch 'c4-univ3' of github.com:overlay-market/overlay-v1-core …
mikeyrf Dec 20, 2021
fffe1bd
Make _tickToPrice() internal
mikeyrf Dec 10, 2021
0fd6b4c
Rebase main conflicts - Fix comments in market and mothership
mikeyrf Dec 20, 2021
1c45c4f
Rebase main conflicts - Remove commented out code from collateral man…
mikeyrf Dec 20, 2021
9211ef3
Rebase main conflicts - Remove open TODOs
mikeyrf Dec 20, 2021
8ebad78
Merge branch 'c4-style' of github.com:overlay-market/overlay-v1-core …
mikeyrf Dec 20, 2021
3f8ee88
Simplify comptroller.roll()
mikeyrf Dec 20, 2021
eb94fbf
Use _brrrrdExpected everywhere in oiCap()
mikeyrf Dec 20, 2021
faa732c
Add require statement for lev!=0
mikeyrf Dec 20, 2021
60704ba
Check for liquidation in value()
mikeyrf Dec 20, 2021
7db9440
Update OverlayV1UniswapV3Market.sol
Jan 4, 2022
24ff5cd
Merge pull request #1 from overlay-market/c4-collateral
mesozoic-technology Jan 9, 2022
38c7a16
Merge pull request #2 from overlay-market/c4-comptroller
mesozoic-technology Jan 9, 2022
e9afa2b
Merge pull request #3 from overlay-market/c4-mothership
mesozoic-technology Jan 9, 2022
37b300b
Merge branch 'main' into c4-ovl
mesozoic-technology Jan 9, 2022
36c7b24
Merge pull request #4 from realisation/c4-ovl
mesozoic-technology Jan 9, 2022
164458b
Merge branch 'main' into c4-style
mesozoic-technology Jan 9, 2022
2e721c2
Merge pull request #5 from realisation/c4-style
mesozoic-technology Jan 9, 2022
083878e
Merge branch 'main' into c4-underwater
mesozoic-technology Jan 9, 2022
98962be
Merge pull request #6 from realisation/c4-underwater
mesozoic-technology Jan 9, 2022
1a597c9
Merge branch 'main' into c4-univ3
mesozoic-technology Jan 9, 2022
9d28622
Merge pull request #7 from realisation/c4-univ3
mesozoic-technology Jan 9, 2022
7879029
added market liquidity decimals immutable and erc20 mock with varying…
Jan 10, 2022
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
4 changes: 2 additions & 2 deletions brownie-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ compiler:
solc:
version: 0.8.7
remappings:
- "@openzeppelin=OpenZeppelin/[email protected].2"
- "@openzeppelin=OpenZeppelin/[email protected].3"
optimizer:
enabled: true
runs: 400
Expand All @@ -21,4 +21,4 @@ compiler:

autofetch_sources: True
dependencies:
- OpenZeppelin/[email protected].2
- OpenZeppelin/[email protected].3
16 changes: 13 additions & 3 deletions contracts/OverlayToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,30 @@ contract OverlayToken is AccessControlEnumerable, ERC20("Overlay", "OVL") {
}

modifier onlyMinter() {
require(hasRole(MINTER_ROLE, msg.sender), "only minter");
require(hasRole(MINTER_ROLE, msg.sender), "ERC20: !minter");
_;
}

modifier onlyBurner() {
require(hasRole(BURNER_ROLE, msg.sender), "only burner");
require(hasRole(BURNER_ROLE, msg.sender), "ERC20: !burner");
_;
}

function mint(address _recipient, uint256 _amount) external onlyMinter {
_mint(_recipient, _amount);
}

function burn(address _account, uint256 _amount) external onlyBurner {
function burn(uint256 _amount) external onlyBurner {
_burn(msg.sender, _amount);
}

// See: OpenZeppelin Contracts v4.4.0 (token/ERC20/extensions/ERC20Burnable.sol)
function burnFrom(address _account, uint256 _amount) external onlyBurner {
uint256 _currentAllowance = allowance(_account, msg.sender);
require(_currentAllowance >= _amount, "ERC20: burn amount exceeds allowance");
unchecked {
_approve(_account, msg.sender, _currentAllowance - _amount);
}
_burn(_account, _amount);
}
}
43 changes: 26 additions & 17 deletions contracts/OverlayV1UniswapV3Market.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import "./interfaces/IUniswapV3Pool.sol";
import "./market/OverlayV1Market.sol";
import "./libraries/UniswapV3OracleLibrary/TickMath.sol";

interface IERC20Decimals { function decimals () view external returns (uint256); }

contract OverlayV1UniswapV3Market is OverlayV1Market {

using FixedPoint for uint256;
Expand All @@ -16,21 +18,22 @@ contract OverlayV1UniswapV3Market is OverlayV1Market {
uint256 public immutable macroWindow; // window size for main TWAP
uint256 public immutable microWindow; // window size for bid/ask TWAP

uint immutable marketLiquidityDecimals;
address public immutable marketFeed;
address public immutable ovlFeed;
address public immutable base;
address public immutable quote;
uint128 internal immutable baseAmount;

address internal immutable eth;
bool internal immutable ethIs0;
address internal immutable weth;
bool internal immutable wethIs0;

constructor(
address _mothership,
address _ovlFeed,
address _marketFeed,
address _quote,
address _eth,
address _weth,
uint128 _baseAmount,
uint256 _macroWindow,
uint256 _microWindow,
Expand All @@ -45,10 +48,12 @@ contract OverlayV1UniswapV3Market is OverlayV1Market {
_priceFrameCap
) {

require(_microWindow < _macroWindow, "OVLV1:micro>=macro");

// immutables
eth = _eth;
ethIs0 = IUniswapV3Pool(_ovlFeed).token0() == _eth;
weth = _weth;
ovlFeed = _ovlFeed;
marketLiquidityDecimals = IERC20Decimals(_weth).decimals();
marketFeed = _marketFeed;
baseAmount = _baseAmount;
macroWindow = _macroWindow;
Expand All @@ -57,6 +62,9 @@ contract OverlayV1UniswapV3Market is OverlayV1Market {
address _token0 = IUniswapV3Pool(_marketFeed).token0();
address _token1 = IUniswapV3Pool(_marketFeed).token1();

require(_token0 == _weth || _token1 == _weth, "OVLV1:token!=WETH");

wethIs0 = _token0 == _weth;
base = _token0 != _quote ? _token0 : _token1;
quote = _token0 == _quote ? _token0 : _token1;

Expand All @@ -67,8 +75,8 @@ contract OverlayV1UniswapV3Market is OverlayV1Market {
);

_pricePoints.push(PricePoint(
_tick,
_tick,
_tick,
_tick,
0
));

Expand Down Expand Up @@ -116,7 +124,7 @@ contract OverlayV1UniswapV3Market is OverlayV1Market {

uint _liquidity = (uint160(microWindow) << 128) / ( _liqs[0] - _liqs[1] );

_marketLiquidity = ethIs0
_marketLiquidity = wethIs0
? ( uint256(_liquidity) << 96 ) / _sqrtPrice
: FullMath.mulDiv(uint256(_liquidity), _sqrtPrice, X96);

Expand All @@ -135,25 +143,26 @@ contract OverlayV1UniswapV3Market is OverlayV1Market {
int24((_ticks[0] - _ticks[1]) / int56(int32(int(macroWindow)))),
1e18,
ovl,
eth
weth
);

}

price_ = PricePoint(
_microTick,
_macroTick,
_microTick,
_macroTick,
computeDepth(_marketLiquidity, _ovlPrice)
);

}


/// @notice Arithmetic to get depth
/// @dev Derived from cnstant product formula X*Y=K and tailored

/// @dev Derived from constant product formula X*Y=K and tailored
/// to Uniswap V3 selective liquidity provision.
/// @param _marketLiquidity Amount of liquidity in market in ETH terms.
/// @param _ovlPrice Price of OVL against ETH.
/// @param _marketLiquidity Amount of liquidity in market in weth terms.
/// @param _ovlPrice Price of OVL against weth.
/// @return depth_ Depth criteria for market in OVL terms.
function computeDepth (
uint _marketLiquidity,
Expand All @@ -162,15 +171,15 @@ contract OverlayV1UniswapV3Market is OverlayV1Market {
uint depth_
) {

depth_ = ((_marketLiquidity * 1e18) / _ovlPrice)
.mulUp(lmbda)
depth_ = ((_marketLiquidity * marketLiquidityDecimals) / _ovlPrice)
.mulUp(lmbda)
.divDown(2e18);

}

function _tickToPrice (
int24 _tick
) public override view returns (
) internal override view returns (
uint quote_
) {

Expand Down
71 changes: 34 additions & 37 deletions contracts/collateral/OverlayV1OVLCollateral.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Supply.sol";
import "../interfaces/IOverlayV1Market.sol";
import "../interfaces/IOverlayV1Mothership.sol";
import "../interfaces/IOverlayToken.sol";
import "../interfaces/IOverlayTokenNew.sol";

contract OverlayV1OVLCollateral is ERC1155Supply {

Expand All @@ -32,7 +31,7 @@ contract OverlayV1OVLCollateral is ERC1155Supply {
Position.Info[] public positions;

IOverlayV1Mothership public immutable mothership;
IOverlayTokenNew immutable public ovl;
IOverlayToken immutable public ovl;

uint256 public fees;
uint256 public liquidations;
Expand Down Expand Up @@ -156,13 +155,13 @@ contract OverlayV1OVLCollateral is ERC1155Supply {
/// @notice Disburses fees
function disburse () public {

( uint256 _marginBurnRate,
( address _feeTo,,
uint256 _feeBurnRate,
address _feeTo ) = mothership.getUpdateParams();
uint256 _marginBurnRate ) = mothership.getGlobalParams();

uint _feeForward = fees;
uint _feeBurn = _feeForward.mulUp(_feeBurnRate);
_feeForward = _feeForward - _feeBurn;
_feeForward -= _feeBurn;

uint _liqForward = liquidations;
uint _liqBurn = _liqForward.mulUp(_marginBurnRate);
Expand All @@ -178,7 +177,7 @@ contract OverlayV1OVLCollateral is ERC1155Supply {
_liqBurn
);

ovl.burn(address(this), _feeBurn + _liqBurn);
ovl.burn(_feeBurn + _liqBurn);
ovl.transfer(_feeTo, _feeForward + _liqForward);

}
Expand Down Expand Up @@ -236,7 +235,7 @@ contract OverlayV1OVLCollateral is ERC1155Supply {

/**
@notice Build a position on Overlay with OVL collateral
@dev This interacts with an Overlay Market to register oi and hold
@dev This interacts with an Overlay Market to register oi and hold
positions on behalf of users.
@dev Build event emitted
@param _market The address of the desired market to interact with
Expand All @@ -257,6 +256,7 @@ contract OverlayV1OVLCollateral is ERC1155Supply {

require(mothership.marketActive(_market), "OVLV1:!market");
require(_leverage <= marketInfo[_market].maxLeverage, "OVLV1:lev>max");
require(_leverage != 0, "OVLV1:lev==0");

( uint _oiAdjusted,
uint _collateralAdjusted,
Expand Down Expand Up @@ -289,9 +289,9 @@ contract OverlayV1OVLCollateral is ERC1155Supply {

emit Build(_market, _positionId, _oiAdjusted, _debtAdjusted);

ovl.transferFromBurn(msg.sender, address(this), _collateralAdjusted + _fee, _impact);
ovl.transferFrom(msg.sender, address(this), _collateralAdjusted + _impact + _fee);

// ovl.burn(msg.sender, _impact);
ovl.burn(_impact);

_mint(msg.sender, _positionId, _oiAdjusted, ""); // WARNING: last b/c erc1155 callback

Expand Down Expand Up @@ -326,7 +326,7 @@ contract OverlayV1OVLCollateral is ERC1155Supply {
pos.pricePoint
);

uint _totalPosShares = totalSupply(_positionId);
uint _totalPosShares = pos.oiShares;

uint _userOiShares = _shares;
uint _userNotional = _shares * pos.notional(_oi, _oiShares, _priceFrame) / _totalPosShares;
Expand All @@ -336,48 +336,45 @@ contract OverlayV1OVLCollateral is ERC1155Supply {

emit Unwind(pos.market, _positionId, _userOi, _userDebt);

// TODO: think through edge case of underwater position ... and fee adjustments ...
uint _feeAmount = _userNotional.mulUp(mothership.fee());

uint _userValueAdjusted = _userNotional - _feeAmount;
if (_userValueAdjusted > _userDebt) _userValueAdjusted -= _userDebt;
else _userValueAdjusted = 0;
if (_userValueAdjusted > _userDebt) {
_userValueAdjusted -= _userDebt;
} else {
// underwater position set to zero value with fees lowered appropriately
_userValueAdjusted = 0;
_feeAmount = _userNotional > _userDebt ? _userNotional - _userDebt : 0;
}

fees += _feeAmount; // adds to fee pot, which is transferred on update
fees += _feeAmount; // adds to fee pot, which is transferred on disburse

pos.debt -= _userDebt;
pos.cost -= _userCost;
pos.oiShares -= _userOiShares;

// ovl.transfer(msg.sender, _userCost);

IOverlayV1Market(pos.market).exitOI(
pos.isLong,
_userOi,
_userOiShares,
_userCost < _userValueAdjusted ? _userValueAdjusted - _userCost : 0,
_userCost < _userValueAdjusted ? 0 : _userCost - _userValueAdjusted
);

// mint/burn excess PnL = valueAdjusted - cost
if (_userCost < _userValueAdjusted) {

ovl.transferMint(
msg.sender,
_userCost,
_userValueAdjusted - _userCost
);

ovl.mint(address(this), _userValueAdjusted - _userCost);

} else {

ovl.transferBurn(
msg.sender,
_userValueAdjusted,
_userCost - _userValueAdjusted
);
ovl.burn(_userCost - _userValueAdjusted);

}


IOverlayV1Market(pos.market).exitOI(
pos.isLong,
_userOi,
_userOiShares,
_userCost < _userValueAdjusted ? _userValueAdjusted - _userCost : 0,
_userCost < _userValueAdjusted ? 0 : _userCost - _userValueAdjusted
);
ovl.transfer(msg.sender, _userValueAdjusted);

}

Expand Down Expand Up @@ -430,10 +427,8 @@ contract OverlayV1OVLCollateral is ERC1155Supply {
pos.cost - _value
);

// TODO: which is better on gas
pos.oiShares = 0;
pos.debt = 0;
// positions[positionId].oiShares = 0;

uint _toReward = _value.mulUp(_marketInfo.marginRewardRate);

Expand All @@ -446,8 +441,8 @@ contract OverlayV1OVLCollateral is ERC1155Supply {
_rewardsTo
);

// ovl.burn(address(this), pos.cost - _value);
ovl.transferBurn(_rewardsTo, _toReward, pos.cost - _value);
ovl.burn(pos.cost - _value);
ovl.transfer(_rewardsTo, _toReward);

}

Expand All @@ -468,6 +463,8 @@ contract OverlayV1OVLCollateral is ERC1155Supply {

Position.Info storage pos = positions[_positionId];

if (pos.oiShares == 0) return 0; // liquidated

IOverlayV1Market _market = IOverlayV1Market(pos.market);

( uint _oi,
Expand Down
6 changes: 4 additions & 2 deletions contracts/interfaces/IOverlayToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ pragma solidity ^0.8.7;

interface IOverlayToken {

function burn(address account, uint256 amount) external;
function burn(uint256 amount) external;

function mint(address account, uint256 amount) external;

function burnFrom(address account, uint256 amount) external;

function totalSupply() external view returns (uint256);

function balanceOf(address account) external view returns (uint256);
Expand All @@ -20,7 +22,7 @@ interface IOverlayToken {
function decimals() external view returns (uint);

function symbol () external view returns (string memory);

function name () external view returns (string memory);

function transferFrom(
Expand Down
Loading