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

Inverse multiplex and quanto markets #131

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7902992
conftest alterations to test uni mock alone
Nov 17, 2021
ed89c83
removed print statements from reflect feed
Nov 17, 2021
c4fa332
script to deploy uni mock to ropsten with all mock data
Nov 17, 2021
408a6c9
reversed feed before loading observations
Nov 17, 2021
dabb1ac
mock feed for axs plus minor improvements
Nov 17, 2021
f0cf6aa
rennamed weth_axs feed deploy
Nov 17, 2021
533c875
rennamed weth dai feed deploy
Nov 17, 2021
0973fd4
deploy ovl token
Nov 17, 2021
e3f1a58
deploy mothership
Nov 17, 2021
9499c2f
deploy eth dai market
Nov 17, 2021
4af8f5a
deploy ovl collateral
Nov 17, 2021
79eaf01
rennamed scripts to conform to python imports
Nov 17, 2021
49e3ddb
combined all deploy scripts into rough draft deployment script
Nov 17, 2021
31c9d54
deleted deployment scripts in separate files
Nov 17, 2021
c4e32ea
updated local deploy script
Nov 19, 2021
a3d643f
corrected path for feeds in local deploy
Nov 19, 2021
ee3fcc8
remove print_logs
bigboydiamonds Nov 19, 2021
30de795
removed prints
Nov 19, 2021
f1c8afd
adjusted deploy script to have real mocked erc20 in feed
Dec 1, 2021
cf7d126
fixed deployment bug setting the market feed to the depth feed
Dec 1, 2021
b2098ed
added wbtc_weth feed
Dec 2, 2021
6bdcada
script to test log multiplexing
Dec 2, 2021
98280e6
research.
Dec 3, 2021
ee7bf61
progress.
Dec 3, 2021
d1e3fd1
removed test test
Dec 3, 2021
1008a70
flake8 linted scripts dir
Dec 3, 2021
3766a16
finished linting
Dec 3, 2021
4e42923
renamed OverlayV1Market to OverlayV1QuantoMarket
Jan 7, 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
185 changes: 185 additions & 0 deletions contracts/OverlayV1UniswapV3InverseMarket.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

import "./libraries/FixedPoint.sol";
import "./libraries/UniswapV3OracleLibrary/UniswapV3OracleLibraryV2.sol";
import "./interfaces/IUniswapV3Pool.sol";
import "./market/OverlayV1Market.sol";
import "./libraries/UniswapV3OracleLibrary/TickMath.sol";

contract OverlayV1UniswapV3InverseMarket is OverlayV1Market {

using FixedPoint for uint256;

uint256 internal X96 = 0x1000000000000000000000000;

uint256 public immutable macroWindow; // window size for main TWAP
uint256 public immutable microWindow; // window size for bid/ask TWAP

address public immutable ovlFeed;
address public immutable base;
address public immutable quote;
uint128 internal immutable baseAmount;

bool internal immutable baseIs0;

constructor(
address _mothership,
address _ovlFeed,
address _quote,
address _base,
uint128 _baseAmount,
uint256 _macroWindow,
uint256 _microWindow,
uint256 _priceFrameCap
) OverlayV1Market (
_mothership
) OverlayV1Comptroller (
_microWindow
) OverlayV1OI (
_microWindow
) OverlayV1PricePoint (
_priceFrameCap
) {

// immutables
baseIs0 = IUniswapV3Pool(_ovlFeed).token0() == _base;
ovlFeed = _ovlFeed;
baseAmount = _baseAmount;
macroWindow = _macroWindow;
microWindow = _microWindow;

address _token0 = IUniswapV3Pool(_ovlFeed).token0();
address _token1 = IUniswapV3Pool(_ovlFeed).token1();

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

int24 _tick = OracleLibraryV2.consult(
_ovlFeed,
uint32(_macroWindow),
uint32(0)
);

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

uint _price = OracleLibraryV2.getQuoteAtTick(
_tick,
uint128(_baseAmount),
_token0 != _quote ? _token0 : _token1,
_token0 == _quote ? _token0 : _token1
);

emit NewPricePoint(_price, _price, 0);

}


/// @notice Reads the current price and depth information
/// @dev Reads price and depth of market feed
/// @return price_ Price point
function fetchPricePoint () public view override returns (
PricePoint memory price_
) {

int56[] memory _ticks;
uint160[] memory _liqs;

uint _ovlPrice;
uint _baseLiquidity;

int24 _microTick;
int24 _macroTick;

{

uint32[] memory _secondsAgo = new uint32[](3);
_secondsAgo[1] = uint32(microWindow);
_secondsAgo[2] = uint32(macroWindow);

( _ticks, _liqs ) = IUniswapV3Pool(ovlFeed).observe(_secondsAgo);

_macroTick = int24(( _ticks[0] - _ticks[2]) / int56(int32(int(macroWindow))));

_microTick = int24((_ticks[0] - _ticks[1]) / int56(int32(int(microWindow))));

uint _sqrtPrice = TickMath.getSqrtRatioAtTick(_microTick);

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

_baseLiquidity = baseIs0
? ( uint256(_liquidity) << 96 ) / _sqrtPrice
: FullMath.mulDiv(uint256(_liquidity), _sqrtPrice, X96);

_ovlPrice = OracleLibraryV2.getQuoteAtTick(
int24((_ticks[0] - _ticks[1]) / int56(int32(int(macroWindow)))),
baseAmount,
quote,
base
);

}


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

}


/// @notice Arithmetic to get depth
/// @dev Derived from cnstant 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.
/// @return depth_ Depth criteria for market in OVL terms.
function computeDepth (
uint _marketLiquidity,
uint _ovlPrice
) public override view returns (
uint depth_
) {

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

}

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

uint160 sqrtRatioX96 = TickMath.getSqrtRatioAtTick(_tick);

// better precision if no overflow when squared
if (sqrtRatioX96 <= type(uint128).max) {

uint256 ratioX192 = uint256(sqrtRatioX96) * sqrtRatioX96;

quote_ = base < quote
? FullMath.mulDiv(ratioX192, baseAmount, 1 << 192)
: FullMath.mulDiv(1 << 192, baseAmount, ratioX192);

} else {

uint256 ratioX128 = FullMath.mulDiv(sqrtRatioX96, sqrtRatioX96, 1 << 64);

quote_ = base < quote
? FullMath.mulDiv(ratioX128, baseAmount, 1 << 128)
: FullMath.mulDiv(1 << 128, baseAmount, ratioX128);

}

}

}
Loading