Skip to content

Latest commit

 

History

History
461 lines (389 loc) · 13.3 KB

SafeMath96.md

File metadata and controls

461 lines (389 loc) · 13.3 KB

SafeMath96 contract. (SafeMath96.sol)

View Source: contracts/governance/Staking/SafeMath96.sol

↘ Derived Contracts: CheckpointsShared, FeeSharingCollector, GovernorAlpha, ILoanToken, ILoanTokenWRBTC, StakingInterface, StakingShared, TimelockInterface

SafeMath96 contract

Improved Solidity's arithmetic operations with added overflow checks.

Functions


safe32

function safe32(uint256 n, string errorMessage) internal pure
returns(uint32)

Arguments

Name Type Description
n uint256
errorMessage string
Source Code
function safe32(uint256 n, string memory errorMessage) internal pure returns (uint32) {
        require(n < 2**32, errorMessage);
        return uint32(n);
    }

safe64

function safe64(uint256 n, string errorMessage) internal pure
returns(uint64)

Arguments

Name Type Description
n uint256
errorMessage string
Source Code
function safe64(uint256 n, string memory errorMessage) internal pure returns (uint64) {
        require(n < 2**64, errorMessage);
        return uint64(n);
    }

safe96

function safe96(uint256 n, string errorMessage) internal pure
returns(uint96)

Arguments

Name Type Description
n uint256
errorMessage string
Source Code
function safe96(uint256 n, string memory errorMessage) internal pure returns (uint96) {
        require(n < 2**96, errorMessage);
        return uint96(n);
    }

add96

Adds two unsigned integers, reverting on overflow.

function add96(uint96 a, uint96 b, string errorMessage) internal pure
returns(uint96)

Arguments

Name Type Description
a uint96 First integer.
b uint96 Second integer.
errorMessage string The revert message on overflow.

Returns

The safe addition a+b.

Source Code
function add96(
        uint96 a,
        uint96 b,
        string memory errorMessage
    ) internal pure returns (uint96) {
        uint96 c = a + b;
        require(c >= a, errorMessage);
        return c;
    }

sub96

Substracts two unsigned integers, reverting on underflow.

function sub96(uint96 a, uint96 b, string errorMessage) internal pure
returns(uint96)

Arguments

Name Type Description
a uint96 First integer.
b uint96 Second integer.
errorMessage string The revert message on underflow.

Returns

The safe substraction a-b.

Source Code
function sub96(
        uint96 a,
        uint96 b,
        string memory errorMessage
    ) internal pure returns (uint96) {
        require(b <= a, errorMessage);
        return a - b;
    }

mul96

Multiplies two unsigned integers, reverting on overflow.

function mul96(uint96 a, uint96 b, string errorMessage) internal pure
returns(uint96)

Arguments

Name Type Description
a uint96 First integer.
b uint96 Second integer.
errorMessage string The revert message on overflow.

Returns

The safe product a*b.

Source Code
function mul96(
        uint96 a,
        uint96 b,
        string memory errorMessage
    ) internal pure returns (uint96) {
        if (a == 0) {
            return 0;
        }

        uint96 c = a * b;
        require(c / a == b, errorMessage);

        return c;
    }

div96

Divides two unsigned integers, reverting on overflow.

function div96(uint96 a, uint96 b, string errorMessage) internal pure
returns(uint96)

Arguments

Name Type Description
a uint96 First integer.
b uint96 Second integer.
errorMessage string The revert message on overflow.

Returns

The safe division a/b.

Source Code
function div96(
        uint96 a,
        uint96 b,
        string memory errorMessage
    ) internal pure returns (uint96) {
        // Solidity only automatically asserts when dividing by 0
        require(b > 0, errorMessage);
        uint96 c = a / b;
        // assert(a == b * c + a % b); // There is no case in which this doesn't hold

        return c;
    }

Contracts