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

Gas opt ovl collateral #104

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
72b9319
removed ERC1155Supply and referred to pos.oiShares instead of total s…
Nov 25, 2021
49bedb9
added total supply function that reads from position.oiShares
Nov 25, 2021
5c8cb3b
overrode balanceOf function to return 0 if total supply of a position…
Nov 25, 2021
5b2135f
corrected failing test
Nov 25, 2021
295d5a5
deprecated unwind liquidated revert
Nov 25, 2021
5165a27
corrected test with new revert string
Nov 25, 2021
8de401f
accounting for positions array length in balanceOf wrapper and totalS…
Nov 25, 2021
de0d55f
local deploy script
Nov 25, 2021
e0aa88d
shortened brrrrd rollers via getter/setter functions
Dec 3, 2021
e5e778f
changed BAL# to OVL# in errors
Dec 5, 2021
0080bb5
removed old CHORD and fixed comparison to _chord param
Dec 5, 2021
b1f6dfc
removed atOrAfter_ from scry rollers
Dec 5, 2021
657caea
packed position struct more tightly
Dec 5, 2021
8e134c5
altered build flow on saving and finding position - 27k gas savings
Dec 5, 2021
480de55
shifting position to memory in unwind saves 9k gas
Dec 5, 2021
a69f6cd
super tiny savings assigning fro memory to storage on build
Dec 5, 2021
9f82ae8
rearranged end of build function to place mint visually last
Dec 5, 2021
8e4d7cb
remove redundant position id from stack
Dec 5, 2021
cc51883
eliminated burden of retrieving fee from market contract by passing i…
Dec 5, 2021
a43cb1a
fixed compiler problems
Dec 5, 2021
d3350a5
corrected order of debt and collateral arguments
Dec 5, 2021
a4d6cee
progress
Dec 5, 2021
bfe782f
merged 1155-supply
Dec 8, 2021
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
7 changes: 4 additions & 3 deletions contracts/OverlayV1UniswapV3Market.sol
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,13 @@ contract OverlayV1UniswapV3Market is OverlayV1Market {
uint _marketLiquidity,
uint _ovlPrice
) public override view returns (
uint depth_
uint112 depth_
) {

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

}

Expand Down
24 changes: 15 additions & 9 deletions contracts/Scratchpad.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@ pragma solidity ^0.8.7;

contract Scratchpad {

int56[][] public observations;
constructor () { }

constructor( int56[][] memory _observations ) {
function one () internal pure returns (uint) {
return 1;
}

uint len = _observations.length;
for (uint i = 0; i < len; i++) observations.push(_observations[i]);
function two () internal pure returns (uint) {
return 2;
}

// immutables
function curry (function() internal pure returns(uint) func) internal pure returns (uint) {
return func();
}

function include_observations ( int56[][] calldata _observations ) external {
function one_and_two () public pure returns (uint, uint) {

uint len = _observations.length;
for (uint i = 0; i < len; i++) observations.push(_observations[i]);
return (
curry(one),
curry(two)
);

}

}
}
157 changes: 109 additions & 48 deletions contracts/collateral/OverlayV1OVLCollateral.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ pragma solidity ^0.8.7;

import "../libraries/Position.sol";
import "../libraries/FixedPoint.sol";
import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Supply.sol";
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "../interfaces/IOverlayV1Market.sol";
import "../interfaces/IOverlayV1Mothership.sol";
import "../interfaces/IOverlayToken.sol";
import "../interfaces/IOverlayTokenNew.sol";

contract OverlayV1OVLCollateral is ERC1155Supply {
contract OverlayV1OVLCollateral is ERC1155 {

event log(string k, uint v);
event log_addr(string k, address v);
Expand All @@ -20,8 +20,14 @@ contract OverlayV1OVLCollateral is ERC1155Supply {

bytes32 constant private GOVERNOR = keccak256("GOVERNOR");

mapping (address => mapping(uint => uint)) internal currentBlockPositionsLong;
mapping (address => mapping(uint => uint)) internal currentBlockPositionsShort;
struct LastPosition {
uint32 pricePoint;
uint32 positionId;
}

mapping (address => mapping(uint => LastPosition)) internal lastPositionsLong;
mapping (address => mapping(uint => LastPosition)) internal lastPositionsShort;

mapping (address => MarketInfo) public marketInfo;
struct MarketInfo {
uint marginMaintenance;
Expand Down Expand Up @@ -83,14 +89,48 @@ contract OverlayV1OVLCollateral is ERC1155Supply {
market: address(0),
isLong: false,
leverage: 0,
pricePoint: 0,
oiShares: 0,
pricePoint: 0,
debt: 0,
cost: 0
}));

}

function totalSupply (
uint _positionId
) public view returns (
uint256 totalSupply_
) {

if (_positionId >= positions.length) {

totalSupply_ = 0;

} else totalSupply_ = positions[_positionId].oiShares;

}

function balanceOf (
address _account,
uint256 _positionId
) public view override returns (
uint256 balance_
) {

if ( positions.length <= _positionId ) {

balance_ = 0;

} else if ( positions[_positionId].oiShares == 0 ) {

balance_ = 0;

} else balance_ = super.balanceOf(_account, _positionId);


}

function setMarketInfo (
address _market,
uint _marginMaintenance,
Expand Down Expand Up @@ -165,38 +205,56 @@ contract OverlayV1OVLCollateral is ERC1155Supply {

}

function getCurrentBlockPositionId (
function storePosition (
address _market,
bool _isLong,
uint _leverage,
uint _oi,
uint _debt,
uint _cost,
uint _pricePointNext
) internal returns (
uint positionId_
) {

mapping(uint=>uint) storage _currentBlockPositions = _isLong
? currentBlockPositionsLong[_market]
: currentBlockPositionsShort[_market];

positionId_ = _currentBlockPositions[_leverage];
mapping(uint=>LastPosition) storage lastPositions = _isLong
? lastPositionsLong[_market]
: lastPositionsShort[_market];

Position.Info storage position = positions[positionId_];
LastPosition memory _lastPosition = _isLong
? lastPositions[_leverage]
: lastPositions[_leverage];

if (position.pricePoint < _pricePointNext) {
if (_lastPosition.pricePoint < _pricePointNext) {

positions.push(Position.Info({
market: _market,
isLong: _isLong,
leverage: _leverage,
pricePoint: _pricePointNext,
oiShares: 0,
debt: 0,
cost: 0
leverage: uint8(_leverage),
pricePoint: uint32(_pricePointNext),
oiShares: uint112(_oi),
debt: uint112(_debt),
cost: uint112(_cost)
}));

positionId_ = positions.length - 1;

_currentBlockPositions[_leverage] = positionId_;
lastPositions[_leverage] = LastPosition(
uint32(_pricePointNext),
uint32(positionId_)
);

} else {

positionId_ = _lastPosition.positionId;

Position.Info memory pos = positions[positionId_];

pos.oiShares += uint112(_oi);
pos.debt += uint112(_debt);
pos.cost += uint112(_cost);

positions[positionId_] = pos;

}

Expand Down Expand Up @@ -228,41 +286,41 @@ contract OverlayV1OVLCollateral is ERC1155Supply {
( uint _oiAdjusted,
uint _collateralAdjusted,
uint _debtAdjusted,
uint _fee,
uint _exactedFee,
uint _impact,
uint _pricePointNext ) = IOverlayV1Market(_market)
.enterOI(
_isLong,
_collateral,
_leverage
_leverage,
mothership.fee()
);

require(_oiAdjusted >= _oiMinimum, "OVLV1:oi<min");

uint _positionId = getCurrentBlockPositionId(
fees += _exactedFee;

positionId_ = storePosition(
_market,
_isLong,
_leverage,
_oiAdjusted,
_debtAdjusted,
_collateralAdjusted,
_pricePointNext
);

Position.Info storage pos = positions[_positionId];

pos.oiShares += _oiAdjusted;
pos.cost += _collateralAdjusted;
pos.debt += _debtAdjusted;

fees += _fee;

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

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

// ovl.burn(msg.sender, _impact);
ovl.transferFromBurn(
msg.sender,
address(this),
_collateralAdjusted + _exactedFee,
_impact
);

_mint(msg.sender, _positionId, _oiAdjusted, ""); // WARNING: last b/c erc1155 callback
emit Build(_market, positionId_, _oiAdjusted, _debtAdjusted);

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

}

Expand All @@ -277,9 +335,7 @@ contract OverlayV1OVLCollateral is ERC1155Supply {

require( 0 < _shares && _shares <= balanceOf(msg.sender, _positionId), "OVLV1:!shares");

Position.Info storage pos = positions[_positionId];

require(0 < pos.oiShares, "OVLV1:liquidated");
Position.Info memory pos = positions[_positionId];

{

Expand All @@ -291,13 +347,16 @@ contract OverlayV1OVLCollateral is ERC1155Supply {
pos.pricePoint
);

uint _totalPosShares = totalSupply(_positionId);

uint _userOiShares = _shares;
uint _userNotional = _shares * pos.notional(_oi, _oiShares, _priceFrame) / _totalPosShares;
uint _userDebt = _shares * pos.debt / _totalPosShares;
uint _userCost = _shares * pos.cost / _totalPosShares;
uint _userOi = _shares * pos.oi(_oi, _oiShares) / _totalPosShares;
uint _totalPosShares = pos.oiShares;
uint _userDebt = _userOiShares * pos.debt / _totalPosShares;
uint _userCost = _userOiShares * pos.cost / _totalPosShares;
uint _userOi = _userOiShares * pos._oi(_oi, _oiShares) / _totalPosShares;
uint _userNotional = _userOiShares * pos._notional(
_oi,
_oiShares,
_priceFrame
) / _totalPosShares;

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

Expand All @@ -310,9 +369,11 @@ contract OverlayV1OVLCollateral is ERC1155Supply {

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

pos.debt -= _userDebt;
pos.cost -= _userCost;
pos.oiShares -= _userOiShares;
pos.debt -= uint112(_userDebt);
pos.cost -= uint112(_userCost);
pos.oiShares -= uint112(_userOiShares);

positions[_positionId] = pos;

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

Expand Down
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
Loading