Skip to content

Commit

Permalink
fix: use lib for encoding / decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
GalloDaSballo committed Oct 14, 2024
1 parent e84c00d commit 12f94ed
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
9 changes: 8 additions & 1 deletion src/BribeInitiative.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import {IBribeInitiative} from "./interfaces/IBribeInitiative.sol";

import {DoubleLinkedList} from "./utils/DoubleLinkedList.sol";


import {EncodingDecodingLib} from "src/utils/EncodingDecodingLib.sol";


contract BribeInitiative is IInitiative, IBribeInitiative {
using SafeERC20 for IERC20;
using DoubleLinkedList for DoubleLinkedList.List;
Expand Down Expand Up @@ -162,8 +166,11 @@ contract BribeInitiative is IInitiative, IBribeInitiative {
emit ModifyLQTYAllocation(_user, _epoch, _lqty, _averageTimestamp);
}

function _encodeLQTYAllocation(uint88 _lqty, uint32 _averageTimestamp) private pure returns (uint224) {
return EncodingDecodingLib.encodeLQTYAllocation(_lqty, _averageTimestamp);
}
function _decodeLQTYAllocation(uint224 _value) private pure returns (uint88, uint32) {
return (uint88(_value >> 32), uint32(_value));
return EncodingDecodingLib.decodeLQTYAllocation(_value);
}

function _loadTotalLQTYAllocation(uint16 _epoch) private view returns (uint88, uint32) {
Expand Down
13 changes: 13 additions & 0 deletions src/utils/EncodingDecodingLib.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

library EncodingDecodingLib {
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));
}
}
25 changes: 8 additions & 17 deletions test/EncodingDecoding.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,20 @@ 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));
}
}
import {EncodingDecodingLib} from "src/utils/EncodingDecodingLib.sol";

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);
uint224 encodedValue = EncodingDecodingLib.encodeLQTYAllocation(lqty, averageTimestamp);
(uint88 decodedLqty, uint32 decodedAverageTimestamp) = EncodingDecodingLib.decodeLQTYAllocation(encodedValue);

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

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

assertEq(reEncoded, encodedValue);
assertEq(reDecodedLqty, decodedLqty);
Expand All @@ -46,10 +37,10 @@ contract EncodingDecodingTest is Test {
// 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);
(uint88 decodedLqty, uint32 decodedAverageTimestamp) = EncodingDecodingLib.decodeLQTYAllocation(encodedValue);

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

assertEq(encodedValue, encodedValue2, "encoded values not equal");
assertEq(decodedLqty, decodedLqty2, "decoded lqty not equal");
Expand Down

0 comments on commit 12f94ed

Please sign in to comment.