Skip to content

Commit e84c00d

Browse files
Merge pull request #24 from liquity/allocation-encoding-tests
Allocation encoding tests
2 parents 03c5315 + 4bb1a58 commit e84c00d

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,3 @@
77
[submodule "lib/v4-core"]
88
path = lib/v4-core
99
url = https://github.com/Uniswap/v4-core
10-
[submodule "lib/solmate"]
11-
path = lib/solmate
12-
url = https://github.com/Transmissions11/solmate

lib/solmate

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Governance.sol

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import {Multicall} from "./utils/Multicall.sol";
1717
import {WAD, PermitParams} from "./utils/Types.sol";
1818
import {safeCallWithMinGas} from "./utils/SafeCallMinGas.sol";
1919

20-
import {SafeCastLib} from "lib/solmate/src/utils/SafeCastLib.sol";
21-
2220
/// @title Governance: Modular Initiative based Governance
2321
contract Governance is Multicall, UserProxyFactory, ReentrancyGuard, IGovernance {
2422
using SafeERC20 for IERC20;

test/EncodingDecoding.t.sol

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.24;
3+
4+
import {Test, console2} from "forge-std/Test.sol";
5+
6+
library EncodingDecoding {
7+
function encodeLQTYAllocation(uint88 _lqty, uint32 _averageTimestamp) public pure returns (uint224) {
8+
uint224 _value = (uint224(_lqty) << 32) | _averageTimestamp;
9+
return _value;
10+
}
11+
12+
function decodeLQTYAllocation(uint224 _value) public pure returns (uint88, uint32) {
13+
return (uint88(_value >> 32), uint32(_value));
14+
}
15+
}
16+
17+
contract EncodingDecodingTest is Test {
18+
// value -> encoding -> decoding -> value
19+
function test_encoding_and_decoding_symmetrical(uint88 lqty, uint32 averageTimestamp) public {
20+
uint224 encodedValue = EncodingDecoding.encodeLQTYAllocation(lqty, averageTimestamp);
21+
(uint88 decodedLqty, uint32 decodedAverageTimestamp) = EncodingDecoding.decodeLQTYAllocation(encodedValue);
22+
23+
assertEq(lqty, decodedLqty);
24+
assertEq(averageTimestamp, decodedAverageTimestamp);
25+
26+
// Redo
27+
uint224 reEncoded = EncodingDecoding.encodeLQTYAllocation(decodedLqty, decodedAverageTimestamp);
28+
(uint88 reDecodedLqty, uint32 reDecodedAverageTimestamp) = EncodingDecoding.decodeLQTYAllocation(encodedValue);
29+
30+
assertEq(reEncoded, encodedValue);
31+
assertEq(reDecodedLqty, decodedLqty);
32+
assertEq(reDecodedAverageTimestamp, decodedAverageTimestamp);
33+
}
34+
35+
36+
/// We expect this test to fail as the encoding is ambigous past u120
37+
function testFail_encoding_not_equal_reproducer() public {
38+
_receive_undo_compare(18371677541005923091065047412368542483005086202);
39+
}
40+
41+
// receive -> undo -> check -> redo -> compare
42+
function test_receive_undo_compare(uint120 encodedValue) public {
43+
_receive_undo_compare(encodedValue);
44+
}
45+
46+
// receive -> undo -> check -> redo -> compare
47+
function _receive_undo_compare(uint224 encodedValue) public {
48+
/// These values fail because we could pass a value that is bigger than intended
49+
(uint88 decodedLqty, uint32 decodedAverageTimestamp) = EncodingDecoding.decodeLQTYAllocation(encodedValue);
50+
51+
uint224 encodedValue2 = EncodingDecoding.encodeLQTYAllocation(decodedLqty, decodedAverageTimestamp);
52+
(uint88 decodedLqty2, uint32 decodedAverageTimestamp2) = EncodingDecoding.decodeLQTYAllocation(encodedValue2);
53+
54+
assertEq(encodedValue, encodedValue2, "encoded values not equal");
55+
assertEq(decodedLqty, decodedLqty2, "decoded lqty not equal");
56+
assertEq(decodedAverageTimestamp, decodedAverageTimestamp2, "decoded timestamps not equal");
57+
}
58+
59+
60+
}

0 commit comments

Comments
 (0)