protocol/contracts/staking/test/unit_tests/lib_safe_downcast_test.ts
Xianny f0d7d10fe7
update abi-gen with new method interfaces (#2325)
* update abi-gen with new method interfaces

* wip: get all packages to build

* wip: get all packages to build

* Fix two contract wrapper calls

* Export necessary types part of the contract wrapper public interfaces

* Revive and fix wrapper_unit_tests

* Remove duplicate type

* Fix lib_exchange_rich_error_decoder tests

* Fix remaining test failures in contracts-* packages

* Prettier fixes

* remove transactionHelper

* lint and update changelogs

* Fix prettier

* Revert changes to reference docs

* Add back changelog already published and add revert changelog entry

* Add missing CHANGELOG entries

* Add missing comma

* Update mesh-rpc-client dep

* Update Mesh RPC logic in @0x/orderbook to v6.0.1-beta

* Align package versions
2019-11-14 11:22:29 -05:00

85 lines
3.4 KiB
TypeScript

import { blockchainTests, expect, Numberish } from '@0x/contracts-test-utils';
import { BigNumber, SafeMathRevertErrors } 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));
});
});
});