Skip to content

Commit 430817f

Browse files
committed
add arithmetic functions for uint256-int256 operations
1 parent 1b8a952 commit 430817f

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

contracts/utils/UintUtils.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ library UintUtils {
1111

1212
bytes16 private constant HEX_SYMBOLS = '0123456789abcdef';
1313

14+
function add(uint256 a, int256 b) internal pure returns (uint256) {
15+
return b < 0 ? sub(a, -b) : a + uint256(b);
16+
}
17+
18+
function sub(uint256 a, int256 b) internal pure returns (uint256) {
19+
return b < 0 ? add(a, -b) : a - uint256(b);
20+
}
21+
1422
function toString(uint256 value) internal pure returns (string memory) {
1523
if (value == 0) {
1624
return '0';

contracts/utils/UintUtilsMock.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ import { UintUtils } from './UintUtils.sol';
77
contract UintUtilsMock {
88
using UintUtils for uint256;
99

10+
function add(uint256 a, int256 b) external pure returns (uint256) {
11+
return a.add(b);
12+
}
13+
14+
function sub(uint256 a, int256 b) external pure returns (uint256) {
15+
return a.sub(b);
16+
}
17+
1018
function toString(uint256 number) external pure returns (string memory) {
1119
return number.toString();
1220
}

test/utils/UintUtils.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PANIC_CODES } from '@nomicfoundation/hardhat-chai-matchers/panic';
12
import {
23
UintUtilsMock,
34
UintUtilsMock__factory,
@@ -14,6 +15,36 @@ describe('UintUtils', function () {
1415
});
1516

1617
describe('__internal', function () {
18+
describe('#add(uint256,int256)', function () {
19+
it('adds unsigned and signed integers', async () => {
20+
expect(await instance.callStatic.add(1, 1)).to.equal(2);
21+
expect(await instance.callStatic.add(1, -1)).to.equal(0);
22+
});
23+
24+
describe('reverts if', () => {
25+
it('signed integer is negative and has absolute value greater than unsigned integer', async () => {
26+
await expect(instance.callStatic.add(0, -1)).to.be.revertedWithPanic(
27+
PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW,
28+
);
29+
});
30+
});
31+
});
32+
33+
describe('#sub(uint256,int256)', function () {
34+
it('subtracts unsigned and signed integers', async () => {
35+
expect(await instance.callStatic.sub(1, 1)).to.equal(0);
36+
expect(await instance.callStatic.sub(1, -1)).to.equal(2);
37+
});
38+
39+
describe('reverts if', () => {
40+
it('signed integer is negative and has absolute value greater than unsigned integer', async () => {
41+
await expect(instance.callStatic.sub(0, 1)).to.be.revertedWithPanic(
42+
PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW,
43+
);
44+
});
45+
});
46+
});
47+
1748
describe('#toString(uint256)', function () {
1849
it('returns base-10 string representation of number', async function () {
1950
for (let i = 0; i < 12; i++) {

0 commit comments

Comments
 (0)