Skip to content

Commit

Permalink
Added casting in support of PrizeDistribution struct (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
asselstine authored Nov 27, 2021
1 parent f111f9a commit b63fb05
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
16 changes: 16 additions & 0 deletions contracts/libraries/ExtendedSafeCastLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ pragma solidity 0.8.6;
* all math on `uint256` and `int256` and then downcasting.
*/
library ExtendedSafeCastLib {

/**
* @dev Returns the downcasted uint104 from uint256, reverting on
* overflow (when the input is greater than largest uint104).
*
* Counterpart to Solidity's `uint104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*/
function toUint104(uint256 _value) internal pure returns (uint104) {
require(_value <= type(uint104).max, "SafeCast: value doesn't fit in 104 bits");
return uint104(_value);
}

/**
* @dev Returns the downcasted uint208 from uint256, reverting on
* overflow (when the input is greater than largest uint208).
Expand Down
8 changes: 8 additions & 0 deletions contracts/test/libraries/ExtendedSafeCastLibHarness.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import "../../libraries/ExtendedSafeCastLib.sol";
contract ExtendedSafeCastLibHarness {
using ExtendedSafeCastLib for uint256;

function toUint104(uint256 value) external pure returns (uint104) {
return value.toUint104();
}

function toUint208(uint256 value) external pure returns (uint208) {
return value.toUint208();
}

function toUint224(uint256 value) external pure returns (uint224) {
return value.toUint224();
}
}
31 changes: 31 additions & 0 deletions test/libraries/ExtendedSafeCast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ describe('ExtendedSafeCastLib', () => {
ExtendedSafeCastLib = await ExtendedSafeCastLibFactory.deploy();
});

describe('toUint104()', () => {
it('should return uint256 downcasted to uint104', async () => {
const value = toWei('1');

expect(await ExtendedSafeCastLib.toUint104(value)).to.equal(value);
});
it('should fail to return value if value passed does not fit in 104 bits', async () => {
const value = BigNumber.from(2).pow(104);

await expect(ExtendedSafeCastLib.toUint104(value)).to.be.revertedWith(
"SafeCast: value doesn't fit in 104 bits",
);
});
});

describe('toUint208()', () => {
it('should return uint256 downcasted to uint208', async () => {
const value = toWei('1');
Expand All @@ -28,4 +43,20 @@ describe('ExtendedSafeCastLib', () => {
);
});
});

describe('toUint224()', () => {
it('should return uint256 downcasted to uint224', async () => {
const value = toWei('1');

expect(await ExtendedSafeCastLib.toUint224(value)).to.equal(value);
});

it('should fail to return value if value passed does not fit in 208 bits', async () => {
const value = BigNumber.from(2).pow(224);

await expect(ExtendedSafeCastLib.toUint224(value)).to.be.revertedWith(
"SafeCast: value doesn't fit in 224 bits",
);
});
});
});

0 comments on commit b63fb05

Please sign in to comment.