Skip to content

Commit

Permalink
CDAuctioneer: WIP getUpdatedTick() tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJem committed Jan 2, 2025
1 parent 7826525 commit 2e375b5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/policies/CDAuctioneer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,10 @@ contract CDAuctioneer is IConvertibleDepositAuctioneer, Policy, RolesConsumer, R
/// TODO document approach
///
/// @return tick The updated tick
function getUpdatedTick() public view returns (Tick memory tick) {
function getUpdatedTick() public view onlyActive returns (Tick memory tick) {
// If the price is 0, the auction parameters have not been set and we cannot determine the new tick
if (currentTick.price == 0) revert CDAuctioneer_InvalidState();

// find amount of time passed and new capacity to add
uint256 timePassed = block.timestamp - state.lastUpdate;
uint256 newCapacity = (state.target * timePassed) / 1 days;
Expand Down
3 changes: 3 additions & 0 deletions src/policies/interfaces/IConvertibleDepositAuctioneer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ interface IConvertibleDepositAuctioneer {
/// @notice Emitted when the contract is not active
error CDAuctioneer_NotActive();

/// @notice Emitted when the state is invalid
error CDAuctioneer_InvalidState();

// ========== DATA STRUCTURES ========== //

/// @notice State of the auction
Expand Down
30 changes: 17 additions & 13 deletions src/test/policies/ConvertibleDepositAuctioneer/getUpdatedTick.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ contract ConvertibleDepositAuctioneerUpdatedTickTest is ConvertibleDepositAuctio
// given the contract is inactive
// [X] it reverts
// given a bid has never been received
// [ ] it calculates the new capacity based on the time since contact activation
// [X] it calculates the new capacity based on the time since contract activation
// given less than 1 day has passed
// [ ] the tick capacity is unchanged
// [ ] the tick price is unchanged
Expand All @@ -35,10 +35,7 @@ contract ConvertibleDepositAuctioneerUpdatedTickTest is ConvertibleDepositAuctio
function test_invalidAuctionParameters_reverts() public givenContractActive {
// Expect revert
vm.expectRevert(
abi.encodeWithSelector(
IConvertibleDepositAuctioneer.CDAuctioneer_InvalidParams.selector,
"auction parameters"
)
abi.encodeWithSelector(IConvertibleDepositAuctioneer.CDAuctioneer_InvalidState.selector)
);

// Call function
Expand Down Expand Up @@ -67,26 +64,33 @@ contract ConvertibleDepositAuctioneerUpdatedTickTest is ConvertibleDepositAuctio

// Expected values
// Tick size = 10e9
// Tick step = 9e17
// Current tick capacity = tick size = 10e9
// Current tick price =
// Current tick price = min price = 15e18
// New capacity added = target * days passed = 20e9 * 2 = 40e9
// New capacity = 10e9 + 40e9 = 50e9
// Iteration 1:
// New capacity = 50e9 - 10e9 = 40e9
// Tick price = 10e9 * 1e18 / 9e17 = 10e9
// Tick price = 15e18 * 1e18 / 9e17 = 16666666666666666667 (rounded up)
// Iteration 2:
// New capacity = 40e9 - 10e9 = 30e9
// Tick price = 10e9 * 9e17 / 9e17 = 10e9
// Tick price = 16666666666666666667 * 1e18 / 9e17 = 18518518518518518519 (rounded up)
// Iteration 3:
// New capacity = 30e9 - 10e9 = 20e9
// Tick price = 10e9 * 9e17 / 9e17 = 10e9
// uint256 expectedCapacity = TARGET + TARGET * 2;
// uint256 expectedPrice = expectedCapacity - TICK_SIZE
// Tick price = 18518518518518518519 * 1e18 / 9e17 = 20576131687242798355 (rounded up)
// Iteration 4:
// New capacity = 20e9 - 10e9 = 10e9
// Tick price = 20576131687242798355 * 1e18 / 9e17 = 22862368541380887062
//
// New capacity is not > tick size, so we stop
uint256 expectedTickPrice = 22862368541380887062;
uint256 expectedTickCapacity = 10e9;

// Call function
IConvertibleDepositAuctioneer.Tick memory tick = auctioneer.getUpdatedTick();

// Assertions
assertEq(tick.capacity, TICK_SIZE);
// Assert current tick
assertEq(tick.capacity, expectedTickCapacity, "capacity");
assertEq(tick.price, expectedTickPrice, "price");
}
}

0 comments on commit 2e375b5

Please sign in to comment.