protocol/contracts/utils/src/reference_functions.ts
Elena c4b1f043c6
Remove obsoleted code from 0x/contracts-utils (#643)
* Remove TestAuthorizable

* Remove contracts/utils Authorizable and IAuthorizable

* Remove contracts/utils D18 contract

* Remove contracts/utils LibAddress contracts

* Remove contracts/utils LogDecoding contracts

* Remove contracts/utils Refundable contracts

* Remove contracts/utils LibAuthorizableRichErrors contracts

* Remove contracts/utils EIP712 contracts

* Remove contracts/utils TestRefundableReceiver contracts

* Remove contracts/utils EIP1271 contracts

* Remove contracts/utils ReentrancyGuardV06 contracts

* Remove contracts/utils LibMath contracts

* Remove contracts/utils LibFractions contracts

* Remove contracts/utils LibMathRichErrors contracts

* Remove DeploymentConstants as per PR review
https://github.com/0xProject/protocol/pull/643#pullrequestreview-1264963784
2023-02-06 19:35:39 +02:00

55 lines
1.5 KiB
TypeScript

import { BigNumber, LibMathRevertErrors, SafeMathRevertErrors } from '@0x/utils';
const MAX_UINT256 = new BigNumber(2).pow(256).minus(1);
/**
* Add two `uint256` values. Reverts on overflow.
*/
export function safeAdd(a: BigNumber, b: BigNumber): BigNumber {
const r = a.plus(b);
if (r.isGreaterThan(MAX_UINT256)) {
throw new SafeMathRevertErrors.Uint256BinOpError(SafeMathRevertErrors.BinOpErrorCodes.AdditionOverflow, a, b);
}
return r;
}
/**
* Subract two `uint256` values. Reverts on overflow.
*/
export function safeSub(a: BigNumber, b: BigNumber): BigNumber {
const r = a.minus(b);
if (r.isLessThan(0)) {
throw new SafeMathRevertErrors.Uint256BinOpError(
SafeMathRevertErrors.BinOpErrorCodes.SubtractionUnderflow,
a,
b,
);
}
return r;
}
/**
* Multiplies two `uint256` values. Reverts on overflow.
*/
export function safeMul(a: BigNumber, b: BigNumber): BigNumber {
const r = a.times(b);
if (r.isGreaterThan(MAX_UINT256)) {
throw new SafeMathRevertErrors.Uint256BinOpError(
SafeMathRevertErrors.BinOpErrorCodes.MultiplicationOverflow,
a,
b,
);
}
return r;
}
/**
* Divides two `uint256` values. Reverts on division by zero.
*/
export function safeDiv(a: BigNumber, b: BigNumber): BigNumber {
if (b.isEqualTo(0)) {
throw new SafeMathRevertErrors.Uint256BinOpError(SafeMathRevertErrors.BinOpErrorCodes.DivisionByZero, a, b);
}
return a.dividedToIntegerBy(b);
}