Skip to content

Commit

Permalink
Let call site emit MarketUpdated event to avoid emitting events from …
Browse files Browse the repository at this point in the history
…library
  • Loading branch information
0xjocke committed Jul 5, 2023
1 parent c77e2f6 commit 0f26123
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ interface IAsyncOrderModule {
uint256 acceptablePrice
);

event MarketUpdated(
uint128 marketId,
int256 skew,
uint256 size,
int256 sizeDelta,
int256 currentFundingRate,
int256 currentFundingVelocity
);

error OrderAlreadyCommitted(uint128 marketId, uint128 accountId);

function commitOrder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface IAsyncOrderSettlementModule {
bytes32 trackingCode;
}

function settle(uint128 marketId, uint128 accountId) external;
function settle(uint128 marketId, uint128 accountId) external view;

function settlePythOrder(bytes calldata result, bytes calldata extraData) external payable;
}
13 changes: 13 additions & 0 deletions markets/perps-market/contracts/interfaces/IMarketEvents.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;

interface IMarketEvents {
event MarketUpdated(
uint128 marketId,
int256 skew,
uint256 size,
int256 sizeDelta,
int256 currentFundingRate,
int256 currentFundingVelocity
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import {GlobalPerpsMarket} from "../storage/GlobalPerpsMarket.sol";
import {PerpsMarketConfiguration} from "../storage/PerpsMarketConfiguration.sol";
import {SettlementStrategy} from "../storage/SettlementStrategy.sol";
import {PerpsMarketFactory} from "../storage/PerpsMarketFactory.sol";
import {IMarketEvents} from "../interfaces/IMarketEvents.sol";

contract AsyncOrderSettlementModule is IAsyncOrderSettlementModule {
contract AsyncOrderSettlementModule is IAsyncOrderSettlementModule, IMarketEvents {
using DecimalMath for int256;
using DecimalMath for uint256;
using DecimalMath for int64;
Expand All @@ -37,7 +38,7 @@ contract AsyncOrderSettlementModule is IAsyncOrderSettlementModule {

int256 public constant PRECISION = 18;

function settle(uint128 marketId, uint128 accountId) external {
function settle(uint128 marketId, uint128 accountId) external view {
GlobalPerpsMarket.load().checkLiquidation(accountId);
(
AsyncOrder.Data storage order,
Expand Down Expand Up @@ -137,9 +138,18 @@ contract AsyncOrderSettlementModule is IAsyncOrderSettlementModule {
// all gets deposited below with fees
}

// after pnl is realized, update position
PerpsMarket.Data storage market = PerpsMarket.loadValid(runtime.marketId);
market.updatePositionData(runtime.accountId, newPosition);
// after pnl is realized, update position on the perps market, this will also update the position.
PerpsMarket.MarketUpdateData memory updateData = PerpsMarket
.loadValid(runtime.marketId)
.updatePositionData(runtime.accountId, newPosition);
emit MarketUpdated(
updateData.marketId,
updateData.skew,
updateData.size,
updateData.sizeDelta,
updateData.currentFundingRate,
updateData.currentFundingVelocity
);

perpsAccount.updatePositionMarkets(runtime.marketId, runtime.newPositionSize);
perpsAccount.deductFromAccount(totalFees);
Expand Down
40 changes: 22 additions & 18 deletions markets/perps-market/contracts/storage/PerpsMarket.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ library PerpsMarket {

error PriceFeedNotSet(uint128 marketId);

event MarketUpdated(
uint128 marketId,
int256 skew,
uint256 size,
int256 sizeDelta,
int256 currentFundingRate,
int256 currentFundingVelocity
);

struct Data {
address owner;
address nominatedOwner;
Expand Down Expand Up @@ -142,11 +133,23 @@ library PerpsMarket {
}
}

struct MarketUpdateData {
uint128 marketId;
int256 skew;
uint256 size;
int256 sizeDelta;
int256 currentFundingRate;
int256 currentFundingVelocity;
}

/**
* @dev If you call this method, please ensure you emit an event so offchain solution can index market state history properly
*/
function updatePositionData(
Data storage self,
uint128 accountId,
Position.Data memory newPosition
) internal {
) internal returns (MarketUpdateData memory) {
Position.Data storage oldPosition = self.positions[accountId];
int128 oldPositionSize = oldPosition.size;

Expand All @@ -155,14 +158,15 @@ library PerpsMarket {
oldPosition.updatePosition(newPosition);
int128 sizeDelta = newPosition.size - oldPositionSize;
// TODO add current market debt
emit MarketUpdated(
self.id,
self.skew,
self.size,
sizeDelta,
currentFundingRate(self),
currentFundingVelocity(self)
);
return
MarketUpdateData(
self.id,
self.skew,
self.size,
sizeDelta,
self.lastFundingRate,
currentFundingVelocity(self)
);
}

function loadWithVerifiedOwner(
Expand Down

0 comments on commit 0f26123

Please sign in to comment.