Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
akshatmittal committed Jun 21, 2023
1 parent 0a598fd commit f14ea24
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 75 deletions.
17 changes: 12 additions & 5 deletions contracts/libraries/Fixed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -559,15 +559,22 @@ library FixLib {
uint192 c,
RoundingMode rounding
) internal pure returns (uint192) {
// return safeDiv(safeMul(a, b, CEIL), c, rounding);
// see safeMul and safeDiv for explanations
if (a == 0 || b == 0) return 0;
if (a == FIX_MAX || b == FIX_MAX || c == 0) return FIX_MAX;

uint256 rawDelta = uint256(b) * a; // {D36} = {D18} * {D18}
uint256 raw = _divrnd(rawDelta, uint256(c), rounding);
if (raw >= FIX_MAX) return FIX_MAX;

return uint192(raw);
unchecked {
uint256 rawDelta = uint256(b) * a; // {D36} = {D18} * {D18}
// overflow
if (rawDelta / b != a) {
rawDelta = type(uint256).max;
}
uint256 raw = _divrnd(rawDelta, uint256(c), rounding);
if (raw >= FIX_MAX) return FIX_MAX;

return uint192(raw);
}
}
}

Expand Down
222 changes: 174 additions & 48 deletions contracts/libraries/test/FixedCallerMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,153 +4,279 @@ pragma solidity ^0.8.19;
import "../Fixed.sol";

// Simple mock for Fixed library.
// prettier-ignore
contract FixedCallerMock {
function toFix_(uint256 x) public pure returns (uint192 ) {
function toFix_(uint256 x) public pure returns (uint192) {
return toFix(x);
}
function shiftl_toFix_(uint256 x, int8 d) public pure returns (uint192 ) {

function shiftl_toFix_(uint256 x, int8 d) public pure returns (uint192) {
return shiftl_toFix(x, d);
}
function shiftl_toFix_Rnd(uint256 x, int8 d, RoundingMode rnd) public pure returns (uint192 ) {

function shiftl_toFix_Rnd(
uint256 x,
int8 d,
RoundingMode rnd
) public pure returns (uint192) {
return shiftl_toFix(x, d, rnd);
}
function divFix_(uint256 x, uint192 y) public pure returns (uint192 ) {

function divFix_(uint256 x, uint192 y) public pure returns (uint192) {
return divFix(x, y);
}

function divuu_(uint256 x, uint256 y) public pure returns (uint256) {
return divuu(x, y);
}
function fixMin_(uint192 x, uint192 y) public pure returns (uint192 ) {

function fixMin_(uint192 x, uint192 y) public pure returns (uint192) {
return fixMin(x, y);
}
function fixMax_(uint192 x, uint192 y) public pure returns (uint192 ) {

function fixMax_(uint192 x, uint192 y) public pure returns (uint192) {
return fixMax(x, y);
}

function abs_(int256 x) public pure returns (uint256) {
return abs(x);
}
function divrnd_(uint256 n, uint256 d, RoundingMode rnd) public pure returns (uint256) {

function divrnd_(
uint256 n,
uint256 d,
RoundingMode rnd
) public pure returns (uint256) {
return _divrnd(n, d, rnd);
}
function toUint(uint192 x) public pure returns (uint256 ) {

function toUint(uint192 x) public pure returns (uint256) {
return FixLib.toUint(x);
}
function toUintRnd(uint192 x, RoundingMode rnd) public pure returns (uint256 ) {

function toUintRnd(uint192 x, RoundingMode rnd) public pure returns (uint256) {
return FixLib.toUint(x, rnd);
}
function shiftl(uint192 x, int8 decimals) public pure returns (uint192 ) {

function shiftl(uint192 x, int8 decimals) public pure returns (uint192) {
return FixLib.shiftl(x, decimals);
}
function shiftlRnd(uint192 x, int8 decimals, RoundingMode rnd) public pure returns (uint192 ) {

function shiftlRnd(
uint192 x,
int8 decimals,
RoundingMode rnd
) public pure returns (uint192) {
return FixLib.shiftl(x, decimals, rnd);
}
function plus(uint192 x, uint192 y) public pure returns (uint192 ) {

function plus(uint192 x, uint192 y) public pure returns (uint192) {
return FixLib.plus(x, y);
}
function plusu(uint192 x, uint256 y) public pure returns (uint192 ) {

function plusu(uint192 x, uint256 y) public pure returns (uint192) {
return FixLib.plusu(x, y);
}
function minus(uint192 x, uint192 y) public pure returns (uint192 ) {

function minus(uint192 x, uint192 y) public pure returns (uint192) {
return FixLib.minus(x, y);
}
function minusu(uint192 x, uint256 y) public pure returns (uint192 ) {

function minusu(uint192 x, uint256 y) public pure returns (uint192) {
return FixLib.minusu(x, y);
}
function mul(uint192 x, uint192 y) public pure returns (uint192 ) {

function mul(uint192 x, uint192 y) public pure returns (uint192) {
return FixLib.mul(x, y);
}
function mulRnd(uint192 x, uint192 y, RoundingMode rnd) public pure returns (uint192 ) {

function mulRnd(
uint192 x,
uint192 y,
RoundingMode rnd
) public pure returns (uint192) {
return FixLib.mul(x, y, rnd);
}
function mulu(uint192 x, uint256 y) public pure returns (uint192 ) {

function mulu(uint192 x, uint256 y) public pure returns (uint192) {
return FixLib.mulu(x, y);
}
function div(uint192 x, uint192 y) public pure returns (uint192 ) {

function div(uint192 x, uint192 y) public pure returns (uint192) {
return FixLib.div(x, y);
}
function divRnd(uint192 x, uint192 y, RoundingMode rnd) public pure returns (uint192 ) {

function divRnd(
uint192 x,
uint192 y,
RoundingMode rnd
) public pure returns (uint192) {
return FixLib.div(x, y, rnd);
}
function divu(uint192 x, uint256 y) public pure returns (uint192 ) {

function divu(uint192 x, uint256 y) public pure returns (uint192) {
return FixLib.divu(x, y);
}
function divuRnd(uint192 x, uint256 y, RoundingMode rnd) public pure returns (uint192 ) {

function divuRnd(
uint192 x,
uint256 y,
RoundingMode rnd
) public pure returns (uint192) {
return FixLib.divu(x, y, rnd);
}
function powu(uint192 x, uint48 y) public pure returns (uint192 ) {

function powu(uint192 x, uint48 y) public pure returns (uint192) {
return FixLib.powu(x, y);
}
function lt(uint192 x, uint192 y) public pure returns (bool) {

function lt(uint192 x, uint192 y) public pure returns (bool) {
return FixLib.lt(x, y);
}
function lte(uint192 x, uint192 y) public pure returns (bool) {

function lte(uint192 x, uint192 y) public pure returns (bool) {
return FixLib.lte(x, y);
}
function gt(uint192 x, uint192 y) public pure returns (bool) {

function gt(uint192 x, uint192 y) public pure returns (bool) {
return FixLib.gt(x, y);
}
function gte(uint192 x, uint192 y) public pure returns (bool) {

function gte(uint192 x, uint192 y) public pure returns (bool) {
return FixLib.gte(x, y);
}
function eq(uint192 x, uint192 y) public pure returns (bool) {

function eq(uint192 x, uint192 y) public pure returns (bool) {
return FixLib.eq(x, y);
}
function neq(uint192 x, uint192 y) public pure returns (bool) {

function neq(uint192 x, uint192 y) public pure returns (bool) {
return FixLib.neq(x, y);
}
function near(uint192 x, uint192 y, uint192 epsilon) public pure returns (bool) {

function near(
uint192 x,
uint192 y,
uint192 epsilon
) public pure returns (bool) {
return FixLib.near(x, y, epsilon);
}

// ================ chained operations
function shiftl_toUint(uint192 x, int8 d) public pure returns (uint256) {
function shiftl_toUint(uint192 x, int8 d) public pure returns (uint256) {
return FixLib.shiftl_toUint(x, d);
}
function shiftl_toUintRnd(uint192 x, int8 d, RoundingMode rnd) public pure returns (uint256) {

function shiftl_toUintRnd(
uint192 x,
int8 d,
RoundingMode rnd
) public pure returns (uint256) {
return FixLib.shiftl_toUint(x, d, rnd);
}
function mulu_toUint(uint192 x, uint256 y) public pure returns (uint256) {

function mulu_toUint(uint192 x, uint256 y) public pure returns (uint256) {
return FixLib.mulu_toUint(x, y);
}
function mulu_toUintRnd(uint192 x, uint256 y, RoundingMode rnd) public pure returns (uint256) {

function mulu_toUintRnd(
uint192 x,
uint256 y,
RoundingMode rnd
) public pure returns (uint256) {
return FixLib.mulu_toUint(x, y, rnd);
}
function mul_toUint(uint192 x, uint192 y) public pure returns (uint256) {

function mul_toUint(uint192 x, uint192 y) public pure returns (uint256) {
return FixLib.mul_toUint(x, y);
}
function mul_toUintRnd(uint192 x, uint192 y, RoundingMode rnd) public pure returns (uint256) {

function mul_toUintRnd(
uint192 x,
uint192 y,
RoundingMode rnd
) public pure returns (uint256) {
return FixLib.mul_toUint(x, y, rnd);
}
function muluDivu(uint192 x, uint256 y, uint256 z) public pure returns (uint192 ) {

function muluDivu(
uint192 x,
uint256 y,
uint256 z
) public pure returns (uint192) {
return FixLib.muluDivu(x, y, z);
}
function muluDivuRnd(uint192 x, uint256 y, uint256 z, RoundingMode rnd) public pure returns (uint192 ) {

function muluDivuRnd(
uint192 x,
uint256 y,
uint256 z,
RoundingMode rnd
) public pure returns (uint192) {
return FixLib.muluDivu(x, y, z, rnd);
}
function mulDiv(uint192 x, uint192 y, uint192 z) public pure returns (uint192 ) {

function mulDiv(
uint192 x,
uint192 y,
uint192 z
) public pure returns (uint192) {
return FixLib.mulDiv(x, y, z);
}
function mulDivRnd(uint192 x, uint192 y, uint192 z, RoundingMode rnd) public pure returns (uint192 ) {

function mulDivRnd(
uint192 x,
uint192 y,
uint192 z,
RoundingMode rnd
) public pure returns (uint192) {
return FixLib.mulDiv(x, y, z, rnd);
}

// ============== safe* operations
function safeMul_(uint192 a, uint192 b, RoundingMode rnd) public pure returns (uint192) {
function safeMul(
uint192 a,
uint192 b,
RoundingMode rnd
) public pure returns (uint192) {
return FixLib.safeMul(a, b, rnd);
}

function safeDiv(
uint192 x,
uint192 y,
RoundingMode rnd
) public pure returns (uint192) {
return FixLib.safeDiv(x, y, rnd);
}

function safeMulDiv(
uint192 x,
uint192 y,
uint192 z,
RoundingMode rnd
) public pure returns (uint192) {
return FixLib.safeMulDiv(x, y, z, rnd);
}

// ================ wide muldiv operations
function mulDiv256_(uint256 x, uint256 y, uint256 z) public pure returns (uint256) {
function mulDiv256_(
uint256 x,
uint256 y,
uint256 z
) public pure returns (uint256) {
return mulDiv256(x, y, z);
}
function mulDiv256Rnd_(uint256 x, uint256 y, uint256 z, RoundingMode rnd)
public pure returns (uint256) {

function mulDiv256Rnd_(
uint256 x,
uint256 y,
uint256 z,
RoundingMode rnd
) public pure returns (uint256) {
return mulDiv256(x, y, z, rnd);
}

function fullMul_(uint256 x, uint256 y) public pure returns (uint256 h, uint256 l) {
return fullMul(x, y);
}



}
8 changes: 1 addition & 7 deletions contracts/p0/mixins/TradingLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,7 @@ library TradingLibP0 {
// surplus: add-in optimistic estimate of baskets purchaseable

// needs overflow protection: using high price of asset which can be FIX_MAX
if (high == FIX_MAX) {
// {BU} = {UoA/tok} * {tok} / {UoA/BU}
deltaTop += int256(uint256(FIX_MAX));
} else {
// {BU} = {UoA/tok} * {tok} / {UoA/BU}
deltaTop += int256(uint256(high.mulDiv(bal - anchor, buPriceLow, CEIL)));
}
deltaTop += int256(uint256(high.safeMulDiv(bal - anchor, buPriceLow, CEIL)));
}
}

Expand Down
Loading

0 comments on commit f14ea24

Please sign in to comment.