Skip to content

Commit

Permalink
👷🏻 add abs() helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
JaredBorders authored Mar 11, 2024
2 parents dc0616c + 671a2d9 commit 3435a3e
Show file tree
Hide file tree
Showing 27 changed files with 1,439 additions and 571 deletions.
856 changes: 446 additions & 410 deletions .gas-snapshot

Large diffs are not rendered by default.

673 changes: 513 additions & 160 deletions lcov.info

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kwenta/quanto-dimensions",
"version": "1.0.8",
"version": "1.0.9",
"license": "GPL-3.0-or-later",
"homepage": "https://github.com/Kwenta/quanto-dimensions#readme",
"author": "Tom Harper, Florian, Jared Borders",
Expand Down
15 changes: 15 additions & 0 deletions src/Int128/BaseInt128/Interactions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import "./ValueType.sol";
import {BaseInt128} from "./ValueType.sol";
import {BaseInt256} from "../../Int256/BaseInt256/ValueType.sol";
import {BaseUint128} from "../../Uint128/BaseUint128/ValueType.sol";
import {BaseUint256} from "../../Uint256/BaseUint256/ValueType.sol";
import {InteractionsBaseInt256} from "../../Int256/BaseInt256/Interactions.sol";
import {USDPerBaseInt128} from "../USDPerBaseInt128/ValueType.sol";
import {USDInt128} from "../USDInt128/ValueType.sol";

Expand All @@ -17,6 +19,7 @@ library InteractionsBaseInt128 {
using DecimalMath for int128;
using SafeCastI128 for int128;
using SafeCastI256 for int256;
using InteractionsBaseInt256 for BaseInt256;

/// @notice Converts a BaseInt128 number into BaseInt256.
function to256(BaseInt128 x) internal pure returns (BaseInt256 result) {
Expand Down Expand Up @@ -45,4 +48,16 @@ library InteractionsBaseInt128 {
{
result = BaseInt256.wrap(x.unwrap().divDecimal(y));
}

/// @notice Returns the absolute value in BaseUint256.
function abs(BaseInt128 x) internal pure returns (BaseUint256) {
return x.unwrap() >= 0
? to256(x).toUint()
: (BaseInt256.wrap(0) - to256(x)).toUint();
}

/// @notice Returns the absolute value in BaseUint128.
function abs128(BaseInt128 x) internal pure returns (BaseUint128) {
return x.unwrap() >= 0 ? toUint(x) : toUint(BaseInt128.wrap(0) - x);
}
}
27 changes: 27 additions & 0 deletions src/Int128/BaseQuantoPerUSDInt128/Interactions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import "./ValueType.sol";
import {BaseQuantoPerUSDInt128} from "./ValueType.sol";
import {BaseQuantoPerUSDUint128} from
"../../Uint128/BaseQuantoPerUSDUint128/ValueType.sol";
import {BaseQuantoPerUSDUint256} from
"../../Uint256/BaseQuantoPerUSDUint256/ValueType.sol";
import {USDPerBaseInt128} from "../USDPerBaseInt128/ValueType.sol";
import {BaseQuantoPerUSDInt256} from
"../../Int256/BaseQuantoPerUSDInt256/ValueType.sol";
import {InteractionsBaseQuantoPerUSDInt256} from
"../../Int256/BaseQuantoPerUSDInt256/Interactions.sol";
import {USDPerQuantoInt128} from "../USDPerQuantoInt128/ValueType.sol";
import {BaseInt128} from "../BaseInt128/ValueType.sol";
import {QuantoInt128} from "../QuantoInt128/ValueType.sol";
Expand All @@ -21,6 +25,7 @@ library InteractionsBaseQuantoPerUSDInt128 {
using SafeCastI128 for int128;
using DecimalMath for int128;
using SafeCastI256 for int256;
using InteractionsBaseQuantoPerUSDInt256 for BaseQuantoPerUSDInt256;

/// @notice Converts a BaseQuantoPerUSDInt128 number into BaseQuantoPerUSDInt256.
function to256(BaseQuantoPerUSDInt128 x)
Expand Down Expand Up @@ -66,4 +71,26 @@ library InteractionsBaseQuantoPerUSDInt128 {
{
result = BaseQuantoPerUSDInt256.wrap(x.unwrap().divDecimal(y));
}

/// @notice Returns the absolute value in BaseQuantoPerUSDUint256.
function abs(BaseQuantoPerUSDInt128 x)
internal
pure
returns (BaseQuantoPerUSDUint256)
{
return x.unwrap() >= 0
? to256(x).toUint()
: (BaseQuantoPerUSDInt256.wrap(0) - to256(x)).toUint();
}

/// @notice Returns the absolute value in BaseQuantoPerUSDUint128.
function abs128(BaseQuantoPerUSDInt128 x)
internal
pure
returns (BaseQuantoPerUSDUint128)
{
return x.unwrap() >= 0
? toUint(x)
: toUint(BaseQuantoPerUSDInt128.wrap(0) - x);
}
}
16 changes: 16 additions & 0 deletions src/Int128/QuantoInt128/Interactions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ pragma solidity >=0.8.19;

import "./ValueType.sol";
import {QuantoUint128} from "../../Uint128/QuantoUint128/ValueType.sol";
import {QuantoUint256} from "../../Uint256/QuantoUint256/ValueType.sol";
import {USDPerQuantoInt128} from "../USDPerQuantoInt128/ValueType.sol";
import {QuantoInt256} from "../../Int256/QuantoInt256/ValueType.sol";
import {InteractionsQuantoInt256} from
"../../Int256/QuantoInt256/Interactions.sol";
import {USDInt128} from "../USDInt128/ValueType.sol";

import {DecimalMath} from "../../utils/DecimalMath.sol";
Expand All @@ -16,6 +19,7 @@ library InteractionsQuantoInt128 {
using SafeCastI128 for int128;
using DecimalMath for int128;
using SafeCastI256 for int256;
using InteractionsQuantoInt256 for QuantoInt256;

/// @notice Converts a QuantoInt128 number into QuantoInt256.
function to256(QuantoInt128 x)
Expand Down Expand Up @@ -52,4 +56,16 @@ library InteractionsQuantoInt128 {
{
result = QuantoInt256.wrap(x.unwrap().divDecimal(y));
}

/// @notice Returns the absolute value in QuantoUint256.
function abs(QuantoInt128 x) internal pure returns (QuantoUint256) {
return x.unwrap() >= 0
? to256(x).toUint()
: (QuantoInt256.wrap(0) - to256(x)).toUint();
}

/// @notice Returns the absolute value in QuantoUint128.
function abs128(QuantoInt128 x) internal pure returns (QuantoUint128) {
return x.unwrap() >= 0 ? toUint(x) : toUint(QuantoInt128.wrap(0) - x);
}
}
15 changes: 15 additions & 0 deletions src/Int128/USDInt128/Interactions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pragma solidity >=0.8.19;
import "./ValueType.sol";
import {USDUint128} from "../../Uint128/USDUint128/ValueType.sol";
import {USDInt256} from "../../Int256/USDInt256/ValueType.sol";
import {InteractionsUSDInt256} from "../../Int256/USDInt256/Interactions.sol";
import {USDUint256} from "../../Uint256/USDUint256/ValueType.sol";

import {DecimalMath} from "../../utils/DecimalMath.sol";
import {SafeCastI128} from "../../utils/SafeCast.sol";
Expand All @@ -12,6 +14,7 @@ import {SafeCastI128} from "../../utils/SafeCast.sol";
library InteractionsUSDInt128 {
using SafeCastI128 for int128;
using DecimalMath for int128;
using InteractionsUSDInt256 for USDInt256;

/// @notice Converts a USDInt128 number into USDInt256.
function to256(USDInt128 x) internal pure returns (USDInt256 result) {
Expand All @@ -31,4 +34,16 @@ library InteractionsUSDInt128 {
{
result = USDInt256.wrap(x.unwrap().divDecimal(y));
}

/// @notice Returns the absolute value in USDUint256.
function abs(USDInt128 x) internal pure returns (USDUint256) {
return x.unwrap() >= 0
? to256(x).toUint()
: (USDInt256.wrap(0) - to256(x)).toUint();
}

/// @notice Returns the absolute value in USDUint128.
function abs128(USDInt128 x) internal pure returns (USDUint128) {
return x.unwrap() >= 0 ? toUint(x) : toUint(USDInt128.wrap(0) - x);
}
}
25 changes: 25 additions & 0 deletions src/Int128/USDPerBaseInt128/Interactions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import "./ValueType.sol";
import {USDPerBaseUint128} from "../../Uint128/USDPerBaseUint128/ValueType.sol";
import {BaseQuantoPerUSDInt128} from "../BaseQuantoPerUSDInt128/ValueType.sol";
import {USDPerBaseInt256} from "../../Int256/USDPerBaseInt256/ValueType.sol";
import {InteractionsUSDPerBaseInt256} from
"../../Int256/USDPerBaseInt256/Interactions.sol";
import {USDPerBaseUint256} from "../../Uint256/USDPerBaseUint256/ValueType.sol";
import {BaseInt128} from "../BaseInt128/ValueType.sol";
import {QuantoInt128} from "../QuantoInt128/ValueType.sol";
import {USDInt128} from "../USDInt128/ValueType.sol";
Expand All @@ -18,6 +21,7 @@ library InteractionsUSDPerBaseInt128 {
using SafeCastI128 for int128;
using DecimalMath for int128;
using SafeCastI256 for int256;
using InteractionsUSDPerBaseInt256 for USDPerBaseInt256;

/// @notice Converts a USDPerBaseInt128 number into USDPerBaseInt256.
function to256(USDPerBaseInt128 x)
Expand Down Expand Up @@ -63,4 +67,25 @@ library InteractionsUSDPerBaseInt128 {
{
result = USDPerBaseInt256.wrap(x.unwrap().divDecimal(y));
}

/// @notice Returns the absolute value in USDPerBaseUint256.
function abs(USDPerBaseInt128 x)
internal
pure
returns (USDPerBaseUint256)
{
return x.unwrap() >= 0
? to256(x).toUint()
: (USDPerBaseInt256.wrap(0) - to256(x)).toUint();
}

/// @notice Returns the absolute value in USDPerBaseUint128.
function abs128(USDPerBaseInt128 x)
internal
pure
returns (USDPerBaseUint128)
{
return
x.unwrap() >= 0 ? toUint(x) : toUint(USDPerBaseInt128.wrap(0) - x);
}
}
26 changes: 26 additions & 0 deletions src/Int128/USDPerQuantoInt128/Interactions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {USDPerQuantoUint128} from
"../../Uint128/USDPerQuantoUint128/ValueType.sol";
import {BaseQuantoPerUSDInt128} from "../BaseQuantoPerUSDInt128/ValueType.sol";
import {USDPerQuantoInt256} from "../../Int256/USDPerQuantoInt256/ValueType.sol";
import {InteractionsUSDPerQuantoInt256} from
"../../Int256/USDPerQuantoInt256/Interactions.sol";
import {USDPerQuantoUint256} from
"../../Uint256/USDPerQuantoUint256/ValueType.sol";
import {BaseInt128} from "../BaseInt128/ValueType.sol";
import {QuantoInt128} from "../QuantoInt128/ValueType.sol";
import {USDInt128} from "../USDInt128/ValueType.sol";
Expand All @@ -19,6 +23,7 @@ library InteractionsUSDPerQuantoInt128 {
using SafeCastI128 for int128;
using DecimalMath for int128;
using SafeCastI256 for int256;
using InteractionsUSDPerQuantoInt256 for USDPerQuantoInt256;

/// @notice Converts a USDPerQuantoInt128 number into USDPerQuantoInt256.
function to256(USDPerQuantoInt128 x)
Expand Down Expand Up @@ -64,4 +69,25 @@ library InteractionsUSDPerQuantoInt128 {
{
result = USDPerQuantoInt256.wrap(x.unwrap().divDecimal(y));
}

/// @notice Returns the absolute value in USDPerQuantoUint256.
function abs(USDPerQuantoInt128 x)
internal
pure
returns (USDPerQuantoUint256)
{
return x.unwrap() >= 0
? to256(x).toUint()
: (USDPerQuantoInt256.wrap(0) - to256(x)).toUint();
}

/// @notice Returns the absolute value in USDPerQuantoUint128.
function abs128(USDPerQuantoInt128 x)
internal
pure
returns (USDPerQuantoUint128)
{
return
x.unwrap() >= 0 ? toUint(x) : toUint(USDPerQuantoInt128.wrap(0) - x);
}
}
5 changes: 5 additions & 0 deletions src/Int256/BaseInt256/Interactions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ library InteractionsBaseInt256 {
{
result = USDInt256.wrap(x.unwrap().mulDecimal(y.unwrap()));
}

/// @notice Returns the absolute value in BaseUint256
function abs(BaseInt256 x) internal pure returns (BaseUint256) {
return x.unwrap() >= 0 ? toUint(x) : toUint((BaseInt256.wrap(0) - x));
}
}
11 changes: 11 additions & 0 deletions src/Int256/BaseQuantoPerUSDInt256/Interactions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,15 @@ library InteractionsBaseQuantoPerUSDInt256 {
{
result = BaseInt256.wrap(x.unwrap().mulDecimal(y.unwrap()));
}

/// @notice Returns the absolute value in BaseQuantoPerUSDInt256
function abs(BaseQuantoPerUSDInt256 x)
internal
pure
returns (BaseQuantoPerUSDUint256)
{
return x.unwrap() >= 0
? toUint(x)
: toUint((BaseQuantoPerUSDInt256.wrap(0) - x));
}
}
5 changes: 5 additions & 0 deletions src/Int256/QuantoInt256/Interactions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ library InteractionsQuantoInt256 {
{
result = USDInt256.wrap(x.unwrap().mulDecimal(y.unwrap()));
}

/// @notice Returns the absolute value in BaseUint256
function abs(QuantoInt256 x) internal pure returns (QuantoUint256) {
return x.unwrap() >= 0 ? toUint(x) : toUint((QuantoInt256.wrap(0) - x));
}
}
5 changes: 5 additions & 0 deletions src/Int256/USDInt256/Interactions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ library InteractionsUSDInt256 {
function toUint(USDInt256 x) internal pure returns (USDUint256 result) {
result = USDUint256.wrap(unwrap(x).toUint());
}

/// @notice Returns the absolute value in BaseUint256
function abs(USDInt256 x) internal pure returns (USDUint256) {
return x.unwrap() >= 0 ? toUint(x) : toUint((USDInt256.wrap(0) - x));
}
}
10 changes: 10 additions & 0 deletions src/Int256/USDPerBaseInt256/Interactions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@ library InteractionsUSDPerBaseInt256 {
{
result = QuantoInt256.wrap(x.unwrap().mulDecimal(y.unwrap()));
}

/// @notice Returns the absolute value in BaseUint256
function abs(USDPerBaseInt256 x)
internal
pure
returns (USDPerBaseUint256)
{
return
x.unwrap() >= 0 ? toUint(x) : toUint((USDPerBaseInt256.wrap(0) - x));
}
}
11 changes: 11 additions & 0 deletions src/Int256/USDPerQuantoInt256/Interactions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,15 @@ library InteractionsUSDPerQuantoInt256 {
{
result = BaseInt256.wrap(x.unwrap().mulDecimal(y.unwrap()));
}

/// @notice Returns the absolute value in BaseUint256
function abs(USDPerQuantoInt256 x)
internal
pure
returns (USDPerQuantoUint256)
{
return x.unwrap() >= 0
? toUint(x)
: toUint((USDPerQuantoInt256.wrap(0) - x));
}
}
33 changes: 33 additions & 0 deletions test/Int128/BaseInt128.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
BaseInt128,
BaseUint128,
BaseInt256,
BaseUint256,
USDPerBaseInt128,
USDInt128,
InteractionsBaseInt128
Expand Down Expand Up @@ -446,4 +447,36 @@ contract BaseInt128Test is Test {
BaseInt256 result = BaseInt128.wrap(x).to256();
assertEq(result.unwrap(), int256(x));
}

function testBaseInt128Abs() public {
BaseInt128 x = BaseInt128.wrap(-100);
BaseUint256 result = x.abs();
assertEq(result.unwrap(), uint256(100));
result = BaseInt128.wrap(100).abs();
assertEq(result.unwrap(), uint256(100));
}

function testBaseInt128AbsFuzz(int128 x) public {
BaseUint256 result = BaseInt128.wrap(x).abs();
int256 _x = x;
assertEq(result.unwrap(), uint256(x < 0 ? -_x : _x));
}

function testBaseInt128Abs128() public {
BaseInt128 x = BaseInt128.wrap(-100);
BaseUint128 result = x.abs128();
assertEq(result.unwrap(), uint128(100));
result = BaseInt128.wrap(100).abs128();
assertEq(result.unwrap(), uint128(100));
}

function testBaseInt128Abs128Fuzz(int128 x) public {
if (x == type(int128).min) {
vm.expectRevert();
BaseInt128.wrap(x).abs128();
} else {
BaseUint128 result = BaseInt128.wrap(x).abs128();
assertEq(result.unwrap(), uint128(x < 0 ? -x : x));
}
}
}
Loading

0 comments on commit 3435a3e

Please sign in to comment.