// SPDX-License-Identifier: Apache-2.0 pragma solidity ^0.8.13; import "@equilibria/root/number/types/Fixed18.sol"; import "./PackedAccumulator.sol"; /// @dev Accumulator type struct Accumulator { /// @dev maker accumulator per share Fixed18 maker; /// @dev taker accumulator per share Fixed18 taker; } using AccumulatorLib for Accumulator global; /** * @title AccountAccumulatorLib * @notice Library that surfaces math operations for the Accumulator type. * @dev Accumulators track the cumulative change in position value over time for the maker and taker positions * respectively. Account-level accumulators can then use two of these values `a` and `a'` to compute the * change in position value since last sync. This change in value is then used to compute P&L and fees. */ library AccumulatorLib { /** * @notice Creates a packed accumulator from an accumulator * @param self an accumulator * @return New packed accumulator */ function pack(Accumulator memory self) internal pure returns (PackedAccumulator memory) { return PackedAccumulator({maker: self.maker.pack(), taker: self.taker.pack()}); } /** * @notice Adds two accumulators together * @param a The first accumulator to sum * @param b The second accumulator to sum * @return The resulting summed accumulator */ function add(Accumulator memory a, Accumulator memory b) internal pure returns (Accumulator memory) { return Accumulator({maker: a.maker.add(b.maker), taker: a.taker.add(b.taker)}); } /** * @notice Subtracts accumulator `b` from `a` * @param a The accumulator to subtract from * @param b The accumulator to subtract * @return The resulting subtracted accumulator */ function sub(Accumulator memory a, Accumulator memory b) internal pure returns (Accumulator memory) { return Accumulator({maker: a.maker.sub(b.maker), taker: a.taker.sub(b.taker)}); } /** * @notice Multiplies two accumulators together * @param a The first accumulator to multiply * @param b The second accumulator to multiply * @return The resulting multiplied accumulator */ function mul(Accumulator memory a, Accumulator memory b) internal pure returns (Accumulator memory) { return Accumulator({maker: a.maker.mul(b.maker), taker: a.taker.mul(b.taker)}); } /** * @notice Sums the maker and taker together from a single accumulator * @param self The struct to operate on * @return The sum of its maker and taker */ function sum(Accumulator memory self) internal pure returns (Fixed18) { return self.maker.add(self.taker); } }