protocol/contracts/staking/test/unit_tests/lib_safe_downcast_test.ts
F. Eugene Aumson f11d8a5bd8
@0x/order-utils refactors for v3: orderParsingUtils, signatureUtils, orderHashUtils, RevertErrors, transactionHashUtils (#2321)
* 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
2019-11-14 17:14:24 -05:00

86 lines
3.4 KiB
TypeScript

import { blockchainTests, expect, Numberish } from '@0x/contracts-test-utils';
import { SafeMathRevertErrors } from '@0x/contracts-utils';
import { BigNumber } from '@0x/utils';
import { artifacts } from '../artifacts';
import { TestLibSafeDowncastContract } from '../wrappers';
blockchainTests('LibSafeDowncast unit tests', env => {
let testContract: TestLibSafeDowncastContract;
before(async () => {
testContract = await TestLibSafeDowncastContract.deployFrom0xArtifactAsync(
artifacts.TestLibSafeDowncast,
env.provider,
env.txDefaults,
artifacts,
);
});
const MAX_UINT_64 = new BigNumber(2).pow(64).minus(1);
const MAX_UINT_96 = new BigNumber(2).pow(96).minus(1);
const MAX_UINT_256 = new BigNumber(2).pow(256).minus(1);
describe('downcastToUint96', () => {
async function verifyCorrectDowncastAsync(n: Numberish): Promise<void> {
const actual = await testContract.downcastToUint96(new BigNumber(n)).callAsync();
expect(actual).to.bignumber.eq(n);
}
function toDowncastError(n: Numberish): SafeMathRevertErrors.Uint256DowncastError {
return new SafeMathRevertErrors.Uint256DowncastError(
SafeMathRevertErrors.DowncastErrorCodes.ValueTooLargeToDowncastToUint96,
new BigNumber(n),
);
}
it('correctly downcasts 0', async () => {
return verifyCorrectDowncastAsync(0);
});
it('correctly downcasts 1337', async () => {
return verifyCorrectDowncastAsync(1337);
});
it('correctly downcasts MAX_UINT_96', async () => {
return verifyCorrectDowncastAsync(MAX_UINT_96);
});
it('reverts on MAX_UINT_96 + 1', async () => {
const n = MAX_UINT_96.plus(1);
return expect(verifyCorrectDowncastAsync(n)).to.revertWith(toDowncastError(n));
});
it('reverts on MAX_UINT_256', async () => {
const n = MAX_UINT_256;
return expect(verifyCorrectDowncastAsync(n)).to.revertWith(toDowncastError(n));
});
});
describe('downcastToUint64', () => {
async function verifyCorrectDowncastAsync(n: Numberish): Promise<void> {
const actual = await testContract.downcastToUint64(new BigNumber(n)).callAsync();
expect(actual).to.bignumber.eq(n);
}
function toDowncastError(n: Numberish): SafeMathRevertErrors.Uint256DowncastError {
return new SafeMathRevertErrors.Uint256DowncastError(
SafeMathRevertErrors.DowncastErrorCodes.ValueTooLargeToDowncastToUint64,
new BigNumber(n),
);
}
it('correctly downcasts 0', async () => {
return verifyCorrectDowncastAsync(0);
});
it('correctly downcasts 1337', async () => {
return verifyCorrectDowncastAsync(1337);
});
it('correctly downcasts MAX_UINT_64', async () => {
return verifyCorrectDowncastAsync(MAX_UINT_64);
});
it('reverts on MAX_UINT_64 + 1', async () => {
const n = MAX_UINT_64.plus(1);
return expect(verifyCorrectDowncastAsync(n)).to.revertWith(toDowncastError(n));
});
it('reverts on MAX_UINT_256', async () => {
const n = MAX_UINT_256;
return expect(verifyCorrectDowncastAsync(n)).to.revertWith(toDowncastError(n));
});
});
});