@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:
Lawrence Forman
2019-07-31 15:56:36 -04:00
parent c54d69e5ae
commit c30d59d5d3
11 changed files with 249 additions and 47 deletions

View 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);
}