Skip to content

Commit 164d7f3

Browse files
committed
👯 Improve uint128x2 and add tests
1 parent c73e32b commit 164d7f3

File tree

4 files changed

+48
-25
lines changed

4 files changed

+48
-25
lines changed

contracts/IProtocolFund.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
pragma solidity ^0.8.0;
44

5-
import {PROTOCOL_FUND} from "./Addresses.sol";
5+
import {PROTOCOL_FUND} from "./addresses.sol";
66
import {amountAddr} from "./amountAddr.sol";
77

88
interface IProtocolFund {

contracts/uint128x2.sol

+11-7
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ pragma solidity ^0.8.20;
44

55
type uint128x2 is uint256;
66

7-
function uint128x2From(uint256 hi, uint256 lo) pure returns (uint128x2) {
8-
return uint128x2.wrap(hi << 128 | lo);
7+
function uint128x2From(uint256 high, uint256 low) pure returns (uint128x2) {
8+
return uint128x2.wrap(high << 128 | low);
99
}
1010

11-
function hi128(uint128x2 n) pure returns (uint256) {
11+
function hi(uint128x2 n) pure returns (uint256) {
1212
return uint128x2.unwrap(n) >> 128;
1313
}
1414

15-
function lo128(uint128x2 n) pure returns (uint256) {
15+
function lo(uint128x2 n) pure returns (uint256) {
1616
return uint128(uint128x2.unwrap(n));
1717
}
1818

@@ -24,14 +24,18 @@ function decLo(uint128x2 self, uint256 delta) pure returns (uint128x2) {
2424

2525
function inc(uint128x2 self, uint256 delta) pure returns (uint128x2) {
2626
unchecked {
27-
return uint128x2.wrap(uint128x2.unwrap(self) + (delta | delta << 128));
27+
return uint128x2.wrap(uint128x2.unwrap(self) + (delta | (delta << 128)));
2828
}
2929
}
3030

3131
function dec(uint128x2 self, uint256 delta) pure returns (uint128x2) {
3232
unchecked {
33-
return uint128x2.wrap(uint128x2.unwrap(self) - (delta | delta << 128));
33+
return uint128x2.wrap(uint128x2.unwrap(self) - (delta | (delta << 128)));
3434
}
3535
}
3636

37-
using {hi128, lo128, decLo, inc, dec} for uint128x2 global;
37+
function equal(uint128x2 self, uint128x2 other) pure returns (bool) {
38+
return uint128x2.unwrap(self) == uint128x2.unwrap(other);
39+
}
40+
41+
using {hi, lo, decLo, inc, dec, equal as ==} for uint128x2 global;

test/addresses.t.sol

+13-13
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22

33
pragma solidity ^0.8.0;
44

5+
import {applyL1ToL2Alias, computeCreateAddress as computeZkSyncCreateAddress} from "contracts/IZkSync.sol";
56
import {
6-
PROTOCOL_FUND,
7-
PROTOCOL_FUND_DEPLOYER,
8-
PROTOCOL_FUND_ZKSYNC,
9-
PROTOCOL_FUND_ZKSYNC_DEPLOYER,
10-
KPASS_DEPLOYER,
11-
KPASS,
12-
KPASS_ZKSYNC,
13-
KPASS_ZKSYNC_DEPLOYER,
7+
KDAOL,
8+
KDAOL_DEPLOYER,
149
KDAO_MAINNET,
1510
KDAO_MAINNET_DEPLOYER,
1611
KDAO_ZKSYNC,
17-
KDAO_ZKSYNC_DEPLOYER,
1812
KDAO_ZKSYNC_ALIAS,
19-
KDAOL,
20-
KDAOL_DEPLOYER
21-
} from "contracts/Addresses.sol";
13+
KDAO_ZKSYNC_DEPLOYER,
14+
KPASS,
15+
KPASS_DEPLOYER,
16+
KPASS_ZKSYNC,
17+
KPASS_ZKSYNC_DEPLOYER,
18+
PROTOCOL_FUND,
19+
PROTOCOL_FUND_DEPLOYER,
20+
PROTOCOL_FUND_ZKSYNC,
21+
PROTOCOL_FUND_ZKSYNC_DEPLOYER
22+
} from "contracts/addresses.sol";
2223
import {Test} from "forge-std/Test.sol";
23-
import {computeCreateAddress as computeZkSyncCreateAddress, applyL1ToL2Alias} from "contracts/IZkSync.sol";
2424

2525
contract AddressesTest is Test {
2626
function testDeployerConsistency() public pure {

test/uint128x2.t.sol

+23-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,33 @@
22

33
pragma solidity ^0.8.0;
44

5-
import {Test} from "forge-std/Test.sol";
65
import {uint128x2, uint128x2From} from "contracts/uint128x2.sol";
6+
import {Test} from "forge-std/Test.sol";
77

8-
contract integersTest is Test {
8+
contract uint128x2Test is Test {
99
function testAccessors() public pure {
1010
uint128x2 pair = uint128x2From(1, 2);
1111

12-
assertEq(pair.lo128(), 2);
13-
assertEq(pair.hi128(), 1);
12+
assertEq(pair.lo(), 2);
13+
assertEq(pair.hi(), 1);
14+
}
15+
16+
function testIncDec() public pure {
17+
uint128x2 pair = uint128x2From(300, 500);
18+
pair = pair.inc(200);
19+
20+
assertEq(pair.hi(), 500);
21+
assertEq(pair.lo(), 700);
22+
23+
pair = pair.dec(200);
24+
25+
assertEq(pair.hi(), 300);
26+
assertEq(pair.lo(), 500);
27+
28+
uint128x2 pair2 = uint128x2From(type(uint128).max, 10);
29+
uint128x2 pair3 = pair2.dec(10);
30+
pair3 = pair3.inc(10);
31+
32+
assert(pair2 == pair3);
1433
}
1534
}

0 commit comments

Comments
 (0)