Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocation encoding tests #24

Merged
merged 9 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@
[submodule "lib/v4-core"]
path = lib/v4-core
url = https://github.com/Uniswap/v4-core
[submodule "lib/solmate"]
path = lib/solmate
url = https://github.com/Transmissions11/solmate
1 change: 0 additions & 1 deletion lib/solmate
Submodule solmate deleted from 97bdb2
2 changes: 0 additions & 2 deletions src/Governance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import {Multicall} from "./utils/Multicall.sol";
import {WAD, PermitParams} from "./utils/Types.sol";
import {safeCallWithMinGas} from "./utils/SafeCallMinGas.sol";

import {SafeCastLib} from "lib/solmate/src/utils/SafeCastLib.sol";

/// @title Governance: Modular Initiative based Governance
contract Governance is Multicall, UserProxyFactory, ReentrancyGuard, IGovernance {
using SafeERC20 for IERC20;
Expand Down
60 changes: 60 additions & 0 deletions test/EncodingDecoding.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import {Test, console2} from "forge-std/Test.sol";

library EncodingDecoding {
function encodeLQTYAllocation(uint88 _lqty, uint32 _averageTimestamp) public pure returns (uint224) {
uint224 _value = (uint224(_lqty) << 32) | _averageTimestamp;
return _value;
}

function decodeLQTYAllocation(uint224 _value) public pure returns (uint88, uint32) {
return (uint88(_value >> 32), uint32(_value));
}
}

contract EncodingDecodingTest is Test {
// value -> encoding -> decoding -> value
function test_encoding_and_decoding_symmetrical(uint88 lqty, uint32 averageTimestamp) public {
uint224 encodedValue = EncodingDecoding.encodeLQTYAllocation(lqty, averageTimestamp);
(uint88 decodedLqty, uint32 decodedAverageTimestamp) = EncodingDecoding.decodeLQTYAllocation(encodedValue);

assertEq(lqty, decodedLqty);
assertEq(averageTimestamp, decodedAverageTimestamp);

// Redo
uint224 reEncoded = EncodingDecoding.encodeLQTYAllocation(decodedLqty, decodedAverageTimestamp);
(uint88 reDecodedLqty, uint32 reDecodedAverageTimestamp) = EncodingDecoding.decodeLQTYAllocation(encodedValue);

assertEq(reEncoded, encodedValue);
assertEq(reDecodedLqty, decodedLqty);
assertEq(reDecodedAverageTimestamp, decodedAverageTimestamp);
}


/// We expect this test to fail as the encoding is ambigous past u120
function testFail_encoding_not_equal_reproducer() public {
_receive_undo_compare(18371677541005923091065047412368542483005086202);
}

// receive -> undo -> check -> redo -> compare
function test_receive_undo_compare(uint120 encodedValue) public {
_receive_undo_compare(encodedValue);
}

// receive -> undo -> check -> redo -> compare
function _receive_undo_compare(uint224 encodedValue) public {
/// These values fail because we could pass a value that is bigger than intended
(uint88 decodedLqty, uint32 decodedAverageTimestamp) = EncodingDecoding.decodeLQTYAllocation(encodedValue);

uint224 encodedValue2 = EncodingDecoding.encodeLQTYAllocation(decodedLqty, decodedAverageTimestamp);
(uint88 decodedLqty2, uint32 decodedAverageTimestamp2) = EncodingDecoding.decodeLQTYAllocation(encodedValue2);

assertEq(encodedValue, encodedValue2, "encoded values not equal");
assertEq(decodedLqty, decodedLqty2, "decoded lqty not equal");
assertEq(decodedAverageTimestamp, decodedAverageTimestamp2, "decoded timestamps not equal");
}


}
Loading