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