Skip to content

Commit

Permalink
feat(market): split update function
Browse files Browse the repository at this point in the history
Now we have a public part that emits
`PublicUpdate` event and private part
that replaces all internal calls of
previously public `update` function.
  • Loading branch information
arthurka-o committed May 14, 2024
1 parent 94fa241 commit 2996e66
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
17 changes: 13 additions & 4 deletions contracts/OverlayV1Market.sol
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ contract OverlayV1Market is IOverlayV1Market, Pausable {
/// @param newDpUpperLimit new value for dpUpperLimit
event CacheRiskCalc(uint256 newDpUpperLimit);

/// @param oiLong oiLong after public update
/// @param oiShort oiShort after public update
event PublicUpdate(uint256 oiLong, uint256 oiShort);

constructor() {
(address _ov, address _feed, address _factory) =
IOverlayV1Deployer(msg.sender).parameters();
Expand Down Expand Up @@ -207,7 +211,7 @@ contract OverlayV1Market is IOverlayV1Market, Pausable {
// avoids stack too deep
{
// call to update before any effects
Oracle.Data memory data = update();
Oracle.Data memory data = _update();

// calculate notional, oi, and trading fees. fees charged on notional
// and added to collateral transferred in
Expand Down Expand Up @@ -313,7 +317,7 @@ contract OverlayV1Market is IOverlayV1Market, Pausable {
// avoids stack too deep
{
// call to update before any effects
Oracle.Data memory data = update();
Oracle.Data memory data = _update();

// check position exists
pos = positions.get(msg.sender, positionId);
Expand Down Expand Up @@ -431,7 +435,7 @@ contract OverlayV1Market is IOverlayV1Market, Pausable {
require(pos.exists(), "OVV1:!position");

// call to update before any effects
Oracle.Data memory data = update();
Oracle.Data memory data = _update();

// cache for gas savings
uint256 oiTotalOnSide = pos.isLong ? oiLong : oiShort;
Expand Down Expand Up @@ -511,7 +515,12 @@ contract OverlayV1Market is IOverlayV1Market, Pausable {

/// @dev updates market: pays funding and fetches freshest data from feed
/// @dev update is called every time market is interacted with
function update() public whenNotPaused returns (Oracle.Data memory) {
function update() external returns (Oracle.Data memory data) {
data = _update();
emit PublicUpdate(oiLong, oiShort);
}

function _update() private whenNotPaused returns (Oracle.Data memory) {
// pay funding for time elasped since last interaction w market
_payFunding();

Expand Down
30 changes: 30 additions & 0 deletions tests/OverlayV1Market.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,34 @@ contract MarketTest is Test {

assertEq(ov.balanceOf(address(market)), 0);
}

event PublicUpdate(uint256 oiLong, uint256 oiShort);

function testUpdate() public {
vm.startPrank(USER);

ov.approve(address(market), type(uint256).max);
market.build(1e18, 1e18, true, type(uint256).max);

uint256 oiLong = market.oiLong();
uint256 oiShort = market.oiShort();
uint256 timeElapsed = block.timestamp - market.timestampUpdateLast();
bool isLongOverweight = oiLong > oiShort;
uint256 oiOverweight = isLongOverweight ? oiLong : oiShort;
uint256 oiUnderweight = isLongOverweight ? oiShort : oiLong;

(oiOverweight, oiUnderweight) =
market.oiAfterFunding(oiOverweight, oiUnderweight, timeElapsed);
uint256 newoiLong = isLongOverweight ? oiOverweight : oiUnderweight;
uint256 newoiShort = isLongOverweight ? oiUnderweight : oiOverweight;

vm.expectEmit(false, false, false, true);
emit PublicUpdate(newoiLong, newoiShort);
market.update();

vm.stopPrank();

assertEq(market.oiLong(), newoiLong);
assertEq(market.oiShort(), newoiShort);
}
}

0 comments on commit 2996e66

Please sign in to comment.