From ef699fa6a224de863ffe48347a5ab95d3d8ba2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ernesto=20Garc=C3=ADa?= Date: Fri, 1 Dec 2023 07:46:46 -0600 Subject: [PATCH] Update Math `try*` operations return reference (#4775) --- contracts/utils/math/Math.sol | 20 ++++++++++---------- docs/modules/ROOT/pages/utilities.adoc | 8 ++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/contracts/utils/math/Math.sol b/contracts/utils/math/Math.sol index 86316cb29..3a1d5a4b2 100644 --- a/contracts/utils/math/Math.sol +++ b/contracts/utils/math/Math.sol @@ -20,9 +20,9 @@ library Math { } /** - * @dev Returns the addition of two unsigned integers, with an overflow flag. + * @dev Returns the addition of two unsigned integers, with an success flag (no overflow). */ - function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) { + function tryAdd(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) { unchecked { uint256 c = a + b; if (c < a) return (false, 0); @@ -31,9 +31,9 @@ library Math { } /** - * @dev Returns the subtraction of two unsigned integers, with an overflow flag. + * @dev Returns the subtraction of two unsigned integers, with an success flag (no overflow). */ - function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) { + function trySub(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) { unchecked { if (b > a) return (false, 0); return (true, a - b); @@ -41,9 +41,9 @@ library Math { } /** - * @dev Returns the multiplication of two unsigned integers, with an overflow flag. + * @dev Returns the multiplication of two unsigned integers, with an success flag (no overflow). */ - function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) { + function tryMul(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) { unchecked { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. @@ -56,9 +56,9 @@ library Math { } /** - * @dev Returns the division of two unsigned integers, with a division by zero flag. + * @dev Returns the division of two unsigned integers, with a success flag (no division by zero). */ - function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) { + function tryDiv(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) { unchecked { if (b == 0) return (false, 0); return (true, a / b); @@ -66,9 +66,9 @@ library Math { } /** - * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag. + * @dev Returns the remainder of dividing two unsigned integers, with a success flag (no division by zero). */ - function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) { + function tryMod(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) { unchecked { if (b == 0) return (false, 0); return (true, a % b); diff --git a/docs/modules/ROOT/pages/utilities.adoc b/docs/modules/ROOT/pages/utilities.adoc index f940d0d22..02ae4efff 100644 --- a/docs/modules/ROOT/pages/utilities.adoc +++ b/docs/modules/ROOT/pages/utilities.adoc @@ -82,10 +82,10 @@ contract MyContract { using SignedMath for int256; function tryOperations(uint256 a, uint256 b) internal pure { - (bool overflowsAdd, uint256 resultAdd) = x.tryAdd(y); - (bool overflowsSub, uint256 resultSub) = x.trySub(y); - (bool overflowsMul, uint256 resultMul) = x.tryMul(y); - (bool overflowsDiv, uint256 resultDiv) = x.tryDiv(y); + (bool succededAdd, uint256 resultAdd) = x.tryAdd(y); + (bool succededSub, uint256 resultSub) = x.trySub(y); + (bool succededMul, uint256 resultMul) = x.tryMul(y); + (bool succededDiv, uint256 resultDiv) = x.tryDiv(y); // ... }