* move orderParsingUtils from order-utils to connect * Remove many functions from signatureUtils Removed from the exported object, that is. All of them are used in other existing code, so they were all moved to be as local to their usage as possible. * remove orderHashUtils.isValidOrderHash() * Move all *RevertErrors from order-utils... ...into their respective @0x/contracts- packages. * Refactor @0x/order-utils' orderHashUtils away - Move existing routines into @0x/contracts-test-utils - Migrate non-contract-test callers to a newly-exposed getOrderHash() method in DevUtils. * Move all *RevertErrors from @0x/utils... ...into their respective @0x/contracts- packages. * rm transactionHashUtils.isValidTransactionHash() * DevUtils.sol: Fail yarn test if too big to deploy * Refactor @0x/order-utils transactionHashUtils away - Move existing routines into @0x/contracts-test-utils - Migrate non-contract-test callers to a newly-exposed getTransactionHash() method in DevUtils. * Consolidate `Removed export...` CHANGELOG entries * Rm EthBalanceChecker from devutils wrapper exports * Stop importing from '.' or '.../src' * fix builds * fix prettier; dangling promise * increase max bundle size
94 lines
3.6 KiB
TypeScript
94 lines
3.6 KiB
TypeScript
import { constants, describe, expect } from '@0x/contracts-test-utils';
|
|
|
|
import { safeAdd, safeDiv, safeMul, safeSub } from '../src/reference_functions';
|
|
import SafeMathRevertErrors = require('../src/safe_math_revert_errors');
|
|
|
|
describe('Reference Functions', () => {
|
|
const { ONE_ETHER, MAX_UINT256, ZERO_AMOUNT } = constants;
|
|
const DEFAULT_VALUES = {
|
|
a: ONE_ETHER.times(2),
|
|
b: ONE_ETHER,
|
|
};
|
|
describe('SafeMath', () => {
|
|
describe('safeAdd', () => {
|
|
it('adds two numbers', () => {
|
|
const { a, b } = DEFAULT_VALUES;
|
|
const expected = a.plus(b);
|
|
const actual = safeAdd(a, b);
|
|
expect(actual).to.bignumber.eq(expected);
|
|
});
|
|
|
|
it('reverts on overflow', () => {
|
|
const a = MAX_UINT256.dividedToIntegerBy(2);
|
|
const b = MAX_UINT256.dividedToIntegerBy(2).plus(2);
|
|
const expectedError = new SafeMathRevertErrors.Uint256BinOpError(
|
|
SafeMathRevertErrors.BinOpErrorCodes.AdditionOverflow,
|
|
a,
|
|
b,
|
|
);
|
|
expect(() => safeAdd(a, b)).to.throw(expectedError.message);
|
|
});
|
|
});
|
|
|
|
describe('safeSub', () => {
|
|
it('subracts two numbers', () => {
|
|
const { a, b } = DEFAULT_VALUES;
|
|
const expected = a.minus(b);
|
|
const actual = safeSub(a, b);
|
|
expect(actual).to.bignumber.eq(expected);
|
|
});
|
|
|
|
it('reverts on underflow', () => {
|
|
const a = MAX_UINT256.dividedToIntegerBy(2);
|
|
const b = MAX_UINT256.dividedToIntegerBy(2).plus(2);
|
|
const expectedError = new SafeMathRevertErrors.Uint256BinOpError(
|
|
SafeMathRevertErrors.BinOpErrorCodes.SubtractionUnderflow,
|
|
a,
|
|
b,
|
|
);
|
|
expect(() => safeSub(a, b)).to.throw(expectedError.message);
|
|
});
|
|
});
|
|
|
|
describe('safeMul', () => {
|
|
it('multiplies two numbers', () => {
|
|
const { a, b } = DEFAULT_VALUES;
|
|
const expected = a.times(b);
|
|
const actual = safeMul(a, b);
|
|
expect(actual).to.bignumber.eq(expected);
|
|
});
|
|
|
|
it('reverts on overflow', () => {
|
|
const a = MAX_UINT256.dividedToIntegerBy(2);
|
|
const b = MAX_UINT256.dividedToIntegerBy(2).plus(2);
|
|
const expectedError = new SafeMathRevertErrors.Uint256BinOpError(
|
|
SafeMathRevertErrors.BinOpErrorCodes.MultiplicationOverflow,
|
|
a,
|
|
b,
|
|
);
|
|
expect(() => safeMul(a, b)).to.throw(expectedError.message);
|
|
});
|
|
});
|
|
|
|
describe('safeDiv', () => {
|
|
it('multiplies two numbers', () => {
|
|
const { a, b } = DEFAULT_VALUES;
|
|
const expected = a.times(b);
|
|
const actual = safeMul(a, b);
|
|
expect(actual).to.bignumber.eq(expected);
|
|
});
|
|
|
|
it('reverts if denominator is zero', () => {
|
|
const a = MAX_UINT256.dividedToIntegerBy(2);
|
|
const b = ZERO_AMOUNT;
|
|
const expectedError = new SafeMathRevertErrors.Uint256BinOpError(
|
|
SafeMathRevertErrors.BinOpErrorCodes.DivisionByZero,
|
|
a,
|
|
b,
|
|
);
|
|
expect(() => safeDiv(a, b)).to.throw(expectedError.message);
|
|
});
|
|
});
|
|
});
|
|
});
|