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 13, 2024
1 parent 94fa241 commit 6999221
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"editor.defaultFormatter": "rmcmk.forge-format-vsc",
"editor.formatOnPaste": false,
"editor.formatOnSave": true
}
Empty file added configs/truflationConfig.json
Empty file.
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() public returns (Oracle.Data memory) {
_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
20 changes: 20 additions & 0 deletions tests/OverlayV1Market.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,24 @@ 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 expectedOiLong = 122132849603;

vm.expectEmit(false, false, false, true);
emit PublicUpdate(expectedOiLong, 0);
market.update();

vm.stopPrank();

assertEq(market.oiLong(), expectedOiLong);
assertEq(market.oiShort(), 0);
}
}
11 changes: 11 additions & 0 deletions tests/feeds/chainlink/TruflationFeed.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pragma solidity 0.8.10;

import {Test, console2} from "forge-std/Test.sol";

contract TruflationFeedTest is Test {
function setUp() public {}

function test() public {
console2.log("Hello, World!");
}
}

0 comments on commit 6999221

Please sign in to comment.