Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
proletesseract committed Nov 23, 2023
1 parent 2b34664 commit 96b94b6
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 58 deletions.
47 changes: 23 additions & 24 deletions test/unit/root/flowrate/FlowRateDetection.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import "forge-std/Test.sol";

import {FlowRateDetection} from "../../../../src/root/flowrate/FlowRateDetection.sol";

contract FlowRateDetectionT is FlowRateDetection {
contract FlowRateDetectionT is FlowRateDetection {
function activateWithdrawalQueue() external {
_activateWithdrawalQueue();
}

function deactivateWithdrawalQueue() external {
_deactivateWithdrawalQueue();
}

function setFlowRateThreshold(address token, uint256 capacity, uint256 refillRate) external {
_setFlowRateThreshold(token, capacity, refillRate);
}

function updateFlowRateBucket(address token, uint256 amount) external returns (bool delayWithdrawal) {
return _updateFlowRateBucket(token, amount);

}
}

Expand All @@ -28,15 +30,15 @@ abstract contract FlowRateDetectionTests is Test {
uint256 public CAPACITY = 10000;
uint256 public REFILL_RATE = 50;

function setUp() public virtual {
function setUp() public virtual {
flowRateDetection = new FlowRateDetectionT();
}
}

contract UninitializedFlowRateDetectionTest is FlowRateDetectionTests {
function testUninitFlowRateBuckets() public {
( uint256 capacity, uint256 depth, uint256 refillTime, uint256 refillRate)
= flowRateDetection.flowRateBuckets(TOKEN);
(uint256 capacity, uint256 depth, uint256 refillTime, uint256 refillRate) =
flowRateDetection.flowRateBuckets(TOKEN);
assertEq(capacity, 0, "Capacity");
assertEq(depth, 0, "Depth");
assertEq(refillTime, 0, "Refill time");
Expand All @@ -49,7 +51,6 @@ contract UninitializedFlowRateDetectionTest is FlowRateDetectionTests {
}
}


contract ControlFlowRateDetectionTest is FlowRateDetectionTests {
function testActivateWithdrawalQueue() public {
flowRateDetection.activateWithdrawalQueue();
Expand All @@ -68,8 +69,8 @@ contract ControlFlowRateDetectionTest is FlowRateDetectionTests {

function testSetFlowRateThreshold() public {
flowRateDetection.setFlowRateThreshold(TOKEN, CAPACITY, REFILL_RATE);
( uint256 capacity, uint256 depth, uint256 refillTime, uint256 refillRate)
= flowRateDetection.flowRateBuckets(TOKEN);
(uint256 capacity, uint256 depth, uint256 refillTime, uint256 refillRate) =
flowRateDetection.flowRateBuckets(TOKEN);
assertEq(capacity, CAPACITY, "Capacity");
assertEq(depth, CAPACITY, "Depth");
assertEq(refillTime, 0, "Refill time");
Expand All @@ -85,6 +86,7 @@ contract ControlFlowRateDetectionTest is FlowRateDetectionTests {
vm.expectRevert(abi.encodeWithSelector(FlowRateDetection.InvalidCapacity.selector));
flowRateDetection.setFlowRateThreshold(TOKEN, 0, REFILL_RATE);
}

function testSetFlowRateThresholdBadFillRate() public {
vm.expectRevert(abi.encodeWithSelector(FlowRateDetection.InvalidRefillRate.selector));
flowRateDetection.setFlowRateThreshold(TOKEN, CAPACITY, 0);
Expand All @@ -94,7 +96,7 @@ contract ControlFlowRateDetectionTest is FlowRateDetectionTests {
contract OperationalFlowRateDetectionTest is FlowRateDetectionTests {
event WithdrawalForNonFlowRatedToken(address indexed token, uint256 amount);

function setUp() public override {
function setUp() public override {
super.setUp();
flowRateDetection.setFlowRateThreshold(TOKEN, CAPACITY, REFILL_RATE);
}
Expand All @@ -104,8 +106,8 @@ contract OperationalFlowRateDetectionTest is FlowRateDetectionTests {
uint256 now1 = 150000;
vm.warp(now1);
bool notConfigured = flowRateDetection.updateFlowRateBucket(TOKEN, numTokens);
( uint256 capacity, uint256 depth, uint256 refillTime, uint256 refillRate)
= flowRateDetection.flowRateBuckets(TOKEN);
(uint256 capacity, uint256 depth, uint256 refillTime, uint256 refillRate) =
flowRateDetection.flowRateBuckets(TOKEN);
assertEq(capacity, CAPACITY, "Capacity");
assertEq(depth, CAPACITY - numTokens, "Depth");
assertEq(refillTime, now1, "Refill time");
Expand All @@ -128,11 +130,11 @@ contract OperationalFlowRateDetectionTest is FlowRateDetectionTests {
vm.warp(now2);
notConfigured = flowRateDetection.updateFlowRateBucket(TOKEN, numTokens2);
assertEq(notConfigured, false, "Not configured");
( uint256 capacity, uint256 depth, uint256 refillTime, uint256 refillRate)
= flowRateDetection.flowRateBuckets(TOKEN);
(uint256 capacity, uint256 depth, uint256 refillTime, uint256 refillRate) =
flowRateDetection.flowRateBuckets(TOKEN);

uint256 calcDepth = CAPACITY - numTokens1 + REFILL_RATE * (now2 - now1);
if (calcDepth > CAPACITY) { calcDepth = CAPACITY; }
if (calcDepth > CAPACITY) calcDepth = CAPACITY;
calcDepth -= numTokens2;
assertEq(capacity, CAPACITY, "Capacity");
assertEq(depth, calcDepth, "Depth");
Expand All @@ -146,7 +148,7 @@ contract OperationalFlowRateDetectionTest is FlowRateDetectionTests {
assertEq(notConfigured, false, "Not configured");
(capacity, depth, refillTime, refillRate) = flowRateDetection.flowRateBuckets(TOKEN);
calcDepth = calcDepth + REFILL_RATE * (now3 - now2);
if (calcDepth > CAPACITY) { calcDepth = CAPACITY; }
if (calcDepth > CAPACITY) calcDepth = CAPACITY;
calcDepth -= numTokens3;
assertEq(capacity, CAPACITY, "Capacity");
assertEq(depth, calcDepth, "Depth");
Expand All @@ -167,8 +169,8 @@ contract OperationalFlowRateDetectionTest is FlowRateDetectionTests {
vm.warp(now2);
notConfigured = flowRateDetection.updateFlowRateBucket(TOKEN, numTokens2);
assertEq(notConfigured, false, "Not configured");
( uint256 capacity, uint256 depth, uint256 refillTime, uint256 refillRate)
= flowRateDetection.flowRateBuckets(TOKEN);
(uint256 capacity, uint256 depth, uint256 refillTime, uint256 refillRate) =
flowRateDetection.flowRateBuckets(TOKEN);

uint256 calcDepth = CAPACITY - numTokens2;
assertEq(capacity, CAPACITY, "Capacity");
Expand All @@ -191,8 +193,8 @@ contract OperationalFlowRateDetectionTest is FlowRateDetectionTests {
vm.warp(now1);
bool notConfigured = flowRateDetection.updateFlowRateBucket(TOKEN, numTokens1);
assertEq(notConfigured, false, "Not configured");
( uint256 capacity, uint256 depth, uint256 refillTime, uint256 refillRate)
= flowRateDetection.flowRateBuckets(TOKEN);
(uint256 capacity, uint256 depth, uint256 refillTime, uint256 refillRate) =
flowRateDetection.flowRateBuckets(TOKEN);
assertEq(capacity, CAPACITY, "Capacity");
assertEq(depth, 0, "Depth");
assertEq(refillTime, now1, "Refill time");
Expand All @@ -215,8 +217,8 @@ contract OperationalFlowRateDetectionTest is FlowRateDetectionTests {
vm.warp(now2);
notConfigured = flowRateDetection.updateFlowRateBucket(TOKEN, numTokens2);
assertEq(notConfigured, false, "Not configured");
( uint256 capacity, uint256 depth, uint256 refillTime, uint256 refillRate)
= flowRateDetection.flowRateBuckets(TOKEN);
(uint256 capacity, uint256 depth, uint256 refillTime, uint256 refillRate) =
flowRateDetection.flowRateBuckets(TOKEN);

uint256 calcDepth = REFILL_RATE * (now2 - now1) - numTokens2;
assertEq(capacity, CAPACITY, "Capacity");
Expand All @@ -239,6 +241,3 @@ contract OperationalFlowRateDetectionTest is FlowRateDetectionTests {
assertEq(notConfigured, true, "Not configured");
}
}



93 changes: 59 additions & 34 deletions test/unit/root/flowrate/FlowRateWithdrawalQueue.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,84 @@ import {
IFlowRateWithdrawalQueueErrors
} from "../../../../src/root/flowrate/FlowRateWithdrawalQueue.sol";

contract FlowRateWithdrawalQueueT is FlowRateWithdrawalQueue {
contract FlowRateWithdrawalQueueT is FlowRateWithdrawalQueue {
uint256 public constant DEFAULT_WITHDRAW_DELAY = 60 * 60 * 24;



function init() external {
__FlowRateWithdrawalQueue_init();
}

function setWithdrawalDelay(uint256 delay) external {
_setWithdrawalDelay(delay);
}

function enqueueWithdrawal(address receiver, address withdrawer, address token, uint256 amount) external {
_enqueueWithdrawal(receiver, withdrawer, token, amount);
}
function processWithdrawal(
address receiver, uint256 index
) external returns (address withdrawer, address token, uint256 amount) {

function processWithdrawal(address receiver, uint256 index)
external
returns (address withdrawer, address token, uint256 amount)
{
return _processWithdrawal(receiver, index);
}

}


abstract contract FlowRateWithdrawalQueueTests is Test, IFlowRateWithdrawalQueueErrors {
// Indicates a withdrawal has been queued.
event EnQueuedWithdrawal(address indexed token, address indexed withdrawer, address indexed receiver, uint256 amount, uint256 timestamp, uint256 index);
event EnQueuedWithdrawal(
address indexed token,
address indexed withdrawer,
address indexed receiver,
uint256 amount,
uint256 timestamp,
uint256 index
);
// Indicates a withdrawal has been processed.
event ProcessedWithdrawal(address indexed token, address indexed withdrawer, address indexed receiver, uint256 amount, uint256 timestamp, uint256 index);
event ProcessedWithdrawal(
address indexed token,
address indexed withdrawer,
address indexed receiver,
uint256 amount,
uint256 timestamp,
uint256 index
);

FlowRateWithdrawalQueueT flowRateWithdrawalQueue;

function setUp() public virtual {
function setUp() public virtual {
flowRateWithdrawalQueue = new FlowRateWithdrawalQueueT();
}

function checkValuesZero(FlowRateWithdrawalQueue.PendingWithdrawal memory pending) internal {
function checkValuesZero(FlowRateWithdrawalQueue.PendingWithdrawal memory pending) internal {
checkValues(pending, address(0), address(0), 0, 0);
}

function checkValues(FlowRateWithdrawalQueue.PendingWithdrawal memory pending,
address withdrawer, address token, uint256 amount, uint256 time) internal {
function checkValues(
FlowRateWithdrawalQueue.PendingWithdrawal memory pending,
address withdrawer,
address token,
uint256 amount,
uint256 time
) internal {
assertEq(pending.withdrawer, withdrawer, "Withdrawer");
assertEq(pending.token, token, "Token");
assertEq(pending.amount, amount, "Amount");
assertEq(pending.timestamp, time, "Time stamp");
}

function checkFindValues(FlowRateWithdrawalQueue.FindPendingWithdrawal memory pending,
uint256 index, uint256 amount, uint256 time) internal {
function checkFindValues(
FlowRateWithdrawalQueue.FindPendingWithdrawal memory pending,
uint256 index,
uint256 amount,
uint256 time
) internal {
assertEq(pending.index, index, "Index");
assertEq(pending.amount, amount, "Amount");
assertEq(pending.timestamp, time, "Time stamp");
}

}


contract UninitializedFlowRateWithdrawalQueueTests is FlowRateWithdrawalQueueTests {
address constant USER = address(125);
address constant TOKEN = address(126);
Expand All @@ -76,7 +97,9 @@ contract UninitializedFlowRateWithdrawalQueueTests is FlowRateWithdrawalQueueTes
}

function testEmptyProcessWithdrawal() public {
vm.expectRevert(abi.encodeWithSelector(IFlowRateWithdrawalQueueErrors.IndexOutsideWithdrawalQueue.selector, 0, 0));
vm.expectRevert(
abi.encodeWithSelector(IFlowRateWithdrawalQueueErrors.IndexOutsideWithdrawalQueue.selector, 0, 0)
);
flowRateWithdrawalQueue.processWithdrawal(USER, 0);
}

Expand Down Expand Up @@ -178,7 +201,8 @@ contract OperationalFlowRateWithdrawalQueueTests is FlowRateWithdrawalQueueTests
uint256[] memory indices = new uint256[](2);
indices[0] = 0;
indices[1] = 1;
FlowRateWithdrawalQueue.PendingWithdrawal[] memory pending = flowRateWithdrawalQueue.getPendingWithdrawals(RUSER1, indices);
FlowRateWithdrawalQueue.PendingWithdrawal[] memory pending =
flowRateWithdrawalQueue.getPendingWithdrawals(RUSER1, indices);
assertEq(pending.length, 2, "Pending withdrawal length");
assertEq(pending[0].withdrawer, WUSER1, "Withdrawer");
assertEq(pending[0].token, TOKEN1, "Token");
Expand Down Expand Up @@ -235,15 +259,15 @@ contract OperationalFlowRateWithdrawalQueueTests is FlowRateWithdrawalQueueTests
assertEq(withdrawer, WUSER1, "Withdrawer");
assertEq(token, TOKEN1, "Token");
assertEq(amount, amount1, "Amount");

vm.expectEmit(true, true, true, true);
emit ProcessedWithdrawal(TOKEN2, WUSER2, RUSER1, amount2, now3, 1);
(withdrawer, token, amount) = flowRateWithdrawalQueue.processWithdrawal(RUSER1, 1);
assertEq(withdrawer, WUSER2, "Withdrawer");
assertEq(token, TOKEN2, "Token");
assertEq(amount, amount2, "Amount");
}

function testProcessOutOfOrder() public {
uint256 now1 = 100;
vm.warp(now1);
Expand Down Expand Up @@ -271,7 +295,7 @@ contract OperationalFlowRateWithdrawalQueueTests is FlowRateWithdrawalQueueTests

vm.expectEmit(true, true, true, true);
emit ProcessedWithdrawal(TOKEN3, WUSER1, RUSER1, amount3, now4, 2);
(withdrawer, token, amount) = flowRateWithdrawalQueue.processWithdrawal(RUSER1, 2);
(withdrawer, token, amount) = flowRateWithdrawalQueue.processWithdrawal(RUSER1, 2);
assertEq(withdrawer, WUSER1, "Withdrawer");
assertEq(token, TOKEN3, "Token");
assertEq(amount, amount3, "Amount");
Expand All @@ -283,7 +307,6 @@ contract OperationalFlowRateWithdrawalQueueTests is FlowRateWithdrawalQueueTests
assertEq(token, TOKEN1, "Token");
assertEq(amount, amount1, "Amount");
}


function testProcessOutside() public {
uint256 now1 = 100;
Expand All @@ -296,7 +319,9 @@ contract OperationalFlowRateWithdrawalQueueTests is FlowRateWithdrawalQueueTests

uint256 outOfBoundsIndex = 1;

vm.expectRevert(abi.encodeWithSelector(IFlowRateWithdrawalQueueErrors.IndexOutsideWithdrawalQueue.selector, 1, 1));
vm.expectRevert(
abi.encodeWithSelector(IFlowRateWithdrawalQueueErrors.IndexOutsideWithdrawalQueue.selector, 1, 1)
);
flowRateWithdrawalQueue.processWithdrawal(RUSER1, outOfBoundsIndex);
}

Expand All @@ -310,7 +335,9 @@ contract OperationalFlowRateWithdrawalQueueTests is FlowRateWithdrawalQueueTests
vm.warp(tooEarly);
uint256 okTime = now1 + withdrawalDelay;

vm.expectRevert(abi.encodeWithSelector(IFlowRateWithdrawalQueueErrors.WithdrawalRequestTooEarly.selector, tooEarly, okTime));
vm.expectRevert(
abi.encodeWithSelector(IFlowRateWithdrawalQueueErrors.WithdrawalRequestTooEarly.selector, tooEarly, okTime)
);
flowRateWithdrawalQueue.processWithdrawal(RUSER1, 0);
}

Expand All @@ -324,7 +351,9 @@ contract OperationalFlowRateWithdrawalQueueTests is FlowRateWithdrawalQueueTests
vm.warp(okTime);
flowRateWithdrawalQueue.processWithdrawal(RUSER1, 0);

vm.expectRevert(abi.encodeWithSelector(IFlowRateWithdrawalQueueErrors.WithdrawalAlreadyProcessed.selector, RUSER1, 0));
vm.expectRevert(
abi.encodeWithSelector(IFlowRateWithdrawalQueueErrors.WithdrawalAlreadyProcessed.selector, RUSER1, 0)
);
flowRateWithdrawalQueue.processWithdrawal(RUSER1, 0);
}

Expand Down Expand Up @@ -363,7 +392,6 @@ contract OperationalFlowRateWithdrawalQueueTests is FlowRateWithdrawalQueueTests
checkValues(pending[3], WUSER2, TOKEN2, amount2, now2);
}


function testFindPendingWithdrawals() public {
uint256 amount1 = 123;
uint256 amount2 = 456;
Expand Down Expand Up @@ -407,7 +435,7 @@ contract OperationalFlowRateWithdrawalQueueTests is FlowRateWithdrawalQueueTests

function testEnqueueProcessMultiple() public {
uint256 timeNow = 100;
// Loop around some times enqueuing and then dequeuing.
// Loop around some times enqueuing and then dequeuing.
for (uint256 i = 0; i < 5; i++) {
timeNow += 100;
vm.warp(timeNow);
Expand All @@ -426,14 +454,11 @@ contract OperationalFlowRateWithdrawalQueueTests is FlowRateWithdrawalQueueTests

vm.expectEmit(true, true, true, true);
emit ProcessedWithdrawal(TOKEN1, WUSER1, RUSER1, amount, timeNow, i);
(address withdrawer, address token, uint256 amountOut) = flowRateWithdrawalQueue.processWithdrawal(RUSER1, i);
(address withdrawer, address token, uint256 amountOut) =
flowRateWithdrawalQueue.processWithdrawal(RUSER1, i);
assertEq(withdrawer, WUSER1, "Withdrawer");
assertEq(token, TOKEN1, "Token");
assertEq(amountOut, amount, "Amount");
}
}
}




0 comments on commit 96b94b6

Please sign in to comment.