@0x/types
: Add FillResults
, MatchedFillResults
, and
`BatchMatchedFillResults` types. `@0x/contracts-utils`: Add reference functions for `SafeMath`. `@0x/contracts-exchange-libs`: Add reference functions for `LibMath` and `LibFillResults`. `@0x/contracts-test-utils`: Move `*FillResults` types to `@0x/types`. `@0x/contracts-test-utils`: Add `log_utils.ts`. `@0x/contracts-test-utils`: Add `hexRandom()` to `hex_utils.ts`. `@0x/contracts-test-utils`: Add the contstants: `MAX_UINT256`, `ADDRESS_LENGTH`.
This commit is contained in:
44
contracts/utils/src/reference_functions.ts
Normal file
44
contracts/utils/src/reference_functions.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { AnyRevertError, BigNumber, SafeMathRevertErrors } from '@0x/utils';
|
||||
|
||||
const MAX_UINT256 = new BigNumber(2).pow(256).minus(1);
|
||||
|
||||
export function safeAdd(a: BigNumber, b: BigNumber): BigNumber {
|
||||
const r = a.plus(b);
|
||||
if (r.isGreaterThan(MAX_UINT256)) {
|
||||
throw new SafeMathRevertErrors.SafeMathError(
|
||||
SafeMathRevertErrors.SafeMathErrorCodes.Uint256AdditionOverflow,
|
||||
a,
|
||||
b,
|
||||
);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
export function safeSub(a: BigNumber, b: BigNumber): BigNumber {
|
||||
const r = a.minus(b);
|
||||
if (r.isLessThan(0)) {
|
||||
throw new SafeMathRevertErrors.SafeMathError(
|
||||
SafeMathRevertErrors.SafeMathErrorCodes.Uint256SubtractionUnderflow,
|
||||
a,
|
||||
b,
|
||||
);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
export function safeMul(a: BigNumber, b: BigNumber): BigNumber {
|
||||
const r = a.times(b);
|
||||
if (r.isGreaterThan(MAX_UINT256)) {
|
||||
// Solidity implementation does not throw a reason.
|
||||
throw new AnyRevertError();
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
export function safeDiv(a: BigNumber, b: BigNumber): BigNumber {
|
||||
if (b.isEqualTo(0)) {
|
||||
// Solidity implementation does not throw a reason.
|
||||
throw new AnyRevertError();
|
||||
}
|
||||
return a.dividedToIntegerBy(b);
|
||||
}
|
Reference in New Issue
Block a user