diff --git a/src/policies/CDAuctioneer.sol b/src/policies/CDAuctioneer.sol index c8ad1036..e622f815 100644 --- a/src/policies/CDAuctioneer.sol +++ b/src/policies/CDAuctioneer.sol @@ -225,8 +225,6 @@ contract CDAuctioneer is IConvertibleDepositAuctioneer, Policy, RolesConsumer, R // Cycle through the ticks until the deposit is fully converted while (remainingDeposit > 0) { - // TODO what if the target is reached? - uint256 depositAmount = remainingDeposit; uint256 convertibleAmount = _getConvertedDeposit(remainingDeposit, updatedTickPrice); @@ -333,6 +331,7 @@ contract CDAuctioneer is IConvertibleDepositAuctioneer, Policy, RolesConsumer, R /// /// It uses the following approach: /// - Calculate the added capacity based on the time passed since the last bid, and add it to the current capacity to get the new capacity + /// - If the calculation is occurring on a new day, the tick size will reset to the standard /// - Until the new capacity is <= to the tick size, reduce the capacity by the tick size and reduce the price by the tick step /// - If the calculated price is ever lower than the minimum price, the new price is set to the minimum price and the capacity is set to the tick size function getCurrentTick() public view onlyActive returns (Tick memory tick) { @@ -346,11 +345,15 @@ contract CDAuctioneer is IConvertibleDepositAuctioneer, Policy, RolesConsumer, R tick = _previousTick; uint256 newCapacity = tick.capacity + capacityToAdd; + // If the current date is on a different day to the last bid, the tick size will reset to the standard + if (block.timestamp / 86400 > _previousTick.lastUpdate / 86400) + tick.tickSize = _auctionParameters.tickSize; + // Iterate over the ticks until the capacity is within the tick size // This is the opposite of what happens in the bid function - while (newCapacity > _auctionParameters.tickSize) { + while (newCapacity > tick.tickSize) { // Reduce the capacity by the tick size - newCapacity -= _auctionParameters.tickSize; + newCapacity -= tick.tickSize; // Adjust the tick price by the tick step, in the opposite direction to the bid function tick.price = tick.price.mulDivUp(ONE_HUNDRED_PERCENT, _tickStep); @@ -359,7 +362,7 @@ contract CDAuctioneer is IConvertibleDepositAuctioneer, Policy, RolesConsumer, R // Tick capacity is full if the min price is exceeded if (tick.price < _auctionParameters.minPrice) { tick.price = _auctionParameters.minPrice; - newCapacity = _auctionParameters.tickSize; + newCapacity = tick.tickSize; break; } } diff --git a/src/test/policies/ConvertibleDepositAuctioneer/bid.t.sol b/src/test/policies/ConvertibleDepositAuctioneer/bid.t.sol index caa6e11e..64eede91 100644 --- a/src/test/policies/ConvertibleDepositAuctioneer/bid.t.sol +++ b/src/test/policies/ConvertibleDepositAuctioneer/bid.t.sol @@ -8,8 +8,6 @@ import {FullMath} from "src/libraries/FullMath.sol"; import {IConvertibleDepositAuctioneer} from "src/policies/interfaces/IConvertibleDepositAuctioneer.sol"; import {CDPOSv1} from "src/modules/CDPOS/CDPOS.v1.sol"; -import {console2} from "forge-std/console2.sol"; - contract ConvertibleDepositAuctioneerBidTest is ConvertibleDepositAuctioneerTest { function _assertConvertibleDepositPosition( uint256 bidAmount_, @@ -703,25 +701,25 @@ contract ConvertibleDepositAuctioneerBidTest is ConvertibleDepositAuctioneerTest // Tick two: 10e9, price is 165e17, max bid amount is 165e18 // Tick three: 5e9, price is 1815e16, max bid amount is 9075e16 // Total bid amount = 150e18 + 165e18 + 9075e16 = 40575e16 - uint256 reserveTokenBalance = 150e18 + 165e18 + 9075e16; - uint256 bidAmount = bound(bidAmount_, 315e18, reserveTokenBalance - 1); + uint256 bidOneAmount = 150e18; + uint256 bidTwoAmount = 165e18; + uint256 bidThreeMaxAmount = 9075e16; + uint256 reserveTokenBalance = bidOneAmount + bidTwoAmount + bidThreeMaxAmount; + uint256 bidAmount = bound(bidAmount_, bidOneAmount + bidTwoAmount, reserveTokenBalance - 1); uint256 tickThreePrice = 1815e16; + uint256 tickThreeBidAmount = bidAmount - bidOneAmount - bidTwoAmount; - uint256 tickOneConvertedAmount = (150e18 * 1e9) / 15e18; - uint256 tickTwoConvertedAmount = (165e18 * 1e9) / 165e17; - uint256 tickThreeConvertedAmount = ((bidAmount - 150e18 - 165e18) * 1e9) / tickThreePrice; + uint256 tickOneConvertedAmount = (bidOneAmount * 1e9) / 15e18; + uint256 tickTwoConvertedAmount = (bidTwoAmount * 1e9) / 165e17; + uint256 tickThreeConvertedAmount = (tickThreeBidAmount * 1e9) / tickThreePrice; uint256 expectedConvertedAmount = tickOneConvertedAmount + tickTwoConvertedAmount + tickThreeConvertedAmount; - console2.log("tickOneConvertedAmount", tickOneConvertedAmount); - console2.log("tickTwoConvertedAmount", tickTwoConvertedAmount); - console2.log("tickThreeConvertedAmount", tickThreeConvertedAmount); - // Recalculate the bid amount, in case tickThreeConvertedAmount is 0 - uint256 expectedDepositIn = 150e18 + 165e18 + tickThreeConvertedAmount * tickThreePrice / 1e9; - console2.log("expectedDepositIn", expectedDepositIn); - console2.log("tick three deposit", tickThreeConvertedAmount * tickThreePrice / 1e9); + uint256 expectedDepositIn = bidOneAmount + + bidTwoAmount + + (tickThreeConvertedAmount == 0 ? 0 : tickThreeBidAmount); // Check preview (uint256 previewOhmOut, ) = auctioneer.previewBid(bidAmount); @@ -765,8 +763,8 @@ contract ConvertibleDepositAuctioneerBidTest is ConvertibleDepositAuctioneerTest ) public givenInitialized - givenAddressHasReserveToken(recipient, 795064875e12) - givenReserveTokenSpendingIsApproved(recipient, address(convertibleDepository), 795064875e12) + givenAddressHasReserveToken(recipient, 796064875e12) + givenReserveTokenSpendingIsApproved(recipient, address(convertibleDepository), 796064875e12) { // We want the converted amount to be >= 2 * day target, 40e9 // Tick one: 10e9, price is 15e18, max bid amount is 150e18 @@ -774,35 +772,30 @@ contract ConvertibleDepositAuctioneerBidTest is ConvertibleDepositAuctioneerTest // Tick three: 5e9, price is 1815e16, max bid amount is 9075e16 // Tick four: 5e9, price is 19965e15, max bid amount is 99825e15 // Tick five: 5e9, price is 219615e14, max bid amount is 1098075e14 - // Tick six: 5e9, price is 2395765e13, max bid amount is 11978825e13 - // Tick seven: 2.5e9, price is 25954315e12, max bid amount is 59894125e12 - // Total bid amount = 150e18 + 165e18 + 9075e16 + 99825e15 + 1098075e14 + 11978825e13 + 59894125e12 = 795064875e12 - uint256 reserveTokenBalance = 795064875e12; - uint256 bidAmount = bound(bidAmount_, 73517075e13, reserveTokenBalance - 1); + // Tick six: 5e9, price is 2415765e13, max bid amount is 12078825e13 + // Tick seven: 2.5e9, price is 26573415e12, max bid amount is 59894125e12 + // Max bid amount = 150e18 + 165e18 + 9075e16 + 99825e15 + 1098075e14 + 12078825e13 + 59894125e12 = 796064875e12 + // Ticks one to six bid amount = 150e18 + 165e18 + 9075e16 + 99825e15 + 1098075e14 + 12078825e13 = 73617075e13 + uint256 reserveTokenBalance = 796064875e12; + uint256 bidAmount = bound(bidAmount_, 73617075e13, reserveTokenBalance - 1); uint256 expectedConvertedAmount; + uint256 expectedDepositIn; { - uint256 tickOneConvertedAmount = (150e18 * 1e9) / 15e18; - uint256 tickTwoConvertedAmount = (165e18 * 1e9) / 165e17; - uint256 tickThreeConvertedAmount = (9075e16 * 1e9) / 1815e16; - uint256 tickFourConvertedAmount = (99825e15 * 1e9) / 19965e15; - uint256 tickFiveConvertedAmount = (1098075e14 * 1e9) / 219615e14; - uint256 tickSixConvertedAmount = (11978825e13 * 1e9) / 2395765e13; - uint256 tickSevenConvertedAmount = ((bidAmount - - 150e18 - - 165e18 - - 9075e16 - - 99825e15 - - 1098075e14 - - 11978825e13) * 1e9) / 25954315e12; - expectedConvertedAmount = - tickOneConvertedAmount + - tickTwoConvertedAmount + - tickThreeConvertedAmount + - tickFourConvertedAmount + - tickFiveConvertedAmount + - tickSixConvertedAmount + - tickSevenConvertedAmount; + // Tick one: 150e18 * 1e9 / 15e18 = 10e9 + // Tick two: 165e18 * 1e9 / 165e17 = 10e9 + // Tick three: 9075e16 * 1e9 / 1815e16 = 5e9 + // Tick four: 99825e15 * 1e9 / 19965e15 = 5e9 + // Tick five: 1098075e14 * 1e9 / 219615e14 = 5e9 + // Tick six: 12078825e23 * 1e9 / 2395765e13 = 5e9 + uint256 ticksOneToSixConvertedAmount = 40e9; + uint256 tickSevenConvertedAmount = ((bidAmount - 73617075e13) * 1e9) / 26573415e12; + expectedConvertedAmount = ticksOneToSixConvertedAmount + tickSevenConvertedAmount; + + // Recalculate the bid amount, in case tickSevenConvertedAmount is 0 + expectedDepositIn = + 73617075e13 + + (tickSevenConvertedAmount == 0 ? 0 : (bidAmount - 73617075e13)); } // Check preview @@ -817,9 +810,9 @@ contract ConvertibleDepositAuctioneerBidTest is ConvertibleDepositAuctioneerTest // Assert returned values _assertConvertibleDepositPosition( - bidAmount, + expectedDepositIn, expectedConvertedAmount, - reserveTokenBalance - bidAmount, + reserveTokenBalance - expectedDepositIn, 0, 0, ohmOut, @@ -827,7 +820,7 @@ contract ConvertibleDepositAuctioneerBidTest is ConvertibleDepositAuctioneerTest ); // Assert the day state - assertEq(auctioneer.getDayState().deposits, bidAmount, "day deposits"); + assertEq(auctioneer.getDayState().deposits, expectedDepositIn, "day deposits"); assertEq(auctioneer.getDayState().convertible, expectedConvertedAmount, "day convertible"); // Assert the state @@ -836,7 +829,7 @@ contract ConvertibleDepositAuctioneerBidTest is ConvertibleDepositAuctioneerTest // Assert the tick _assertPreviousTick( 10e9 + 10e9 + 5e9 + 5e9 + 5e9 + 5e9 + 25e8 - expectedConvertedAmount, - 25954315e12, + 26573415e12, 25e8, // The tick size is halved twice as the target is met or exceeded twice uint48(block.timestamp) ); diff --git a/src/test/policies/ConvertibleDepositAuctioneer/getCurrentTick.t.sol b/src/test/policies/ConvertibleDepositAuctioneer/getCurrentTick.t.sol index 0c6bf32a..f200470e 100644 --- a/src/test/policies/ConvertibleDepositAuctioneer/getCurrentTick.t.sol +++ b/src/test/policies/ConvertibleDepositAuctioneer/getCurrentTick.t.sol @@ -16,9 +16,13 @@ contract ConvertibleDepositAuctioneerCurrentTickTest is ConvertibleDepositAuctio // [X] the tick price remains at the min price // [X] the tick capacity remains at the standard tick size // when the new capacity (current tick capacity + added capacity) is equal to the current tick size - // given the current tick size is less than the standard tick size - // [ ] the tick price is unchanged - // [ ] the tick capacity is set to the current tick size + // given the current tick size is 5e9 + // given the current timestamp is on a different day to the last bid + // [X] the tick capacity is set to the standard tick size + // [X] the tick size is set to the standard tick size + // [X] the tick price is unchanged + // [X] the tick capacity is set to the current tick size + // [X] the tick size does not change // [X] the tick price is unchanged // [X] the tick capacity is set to the standard tick size // when the new capacity is less than the current tick size @@ -30,19 +34,25 @@ contract ConvertibleDepositAuctioneerCurrentTickTest is ConvertibleDepositAuctio // [X] the tick capacity is set to the new capacity // given the tick step is > 100e2 // when the new price is lower than the minimum price - // given the current tick size is less than the standard tick size + // given the current tick size is 5e9 + // given the current timestamp is on a different day to the last bid + // [X] the tick capacity is set to the standard tick size + // [X] the tick size is set to the standard tick size // [ ] the tick price is set to the minimum price // [ ] the tick capacity is set to the current tick size + // [ ] the tick size does not change // [X] the tick price is set to the minimum price // [X] the capacity is set to the standard tick size - // given the current tick size is less than the standard tick size - // [ ] it reduces the price by the tick step until the total capacity is less than the current tick size - // [ ] the tick capacity is set to the remainder + // given the current tick size is 5e9 + // given the current timestamp is on a different day to the last bid + // [X] the tick capacity is set to the standard tick size + // [X] the tick size is set to the standard tick size + // [X] it reduces the price by the tick step until the total capacity is less than the current tick size + // [X] the tick capacity is set to the remainder + // [X] the tick size does not change // [X] it reduces the price by the tick step until the total capacity is less than the standard tick size // [X] the tick capacity is set to the remainder - // TODO test that the tick size is reset to the standard tick size at the next day - function test_contractNotInitialized_reverts() public { // Expect revert vm.expectRevert(IConvertibleDepositAuctioneer.CDAuctioneer_NotActive.selector); @@ -74,6 +84,7 @@ contract ConvertibleDepositAuctioneerCurrentTickTest is ConvertibleDepositAuctio // Assert current tick assertEq(tick.capacity, expectedTickCapacity, "capacity"); assertEq(tick.price, expectedTickPrice, "price"); + assertEq(tick.tickSize, 10e9, "new tick size"); } function test_fullCapacity(uint48 secondsPassed_) public givenInitialized { @@ -103,6 +114,7 @@ contract ConvertibleDepositAuctioneerCurrentTickTest is ConvertibleDepositAuctio // Assert current tick assertEq(tick.capacity, expectedTickCapacity, "capacity"); assertEq(tick.price, expectedTickPrice, "price"); + assertEq(tick.tickSize, 10e9, "new tick size"); } function test_newCapacityEqualToTickSize() public givenInitialized givenRecipientHasBid(75e18) { @@ -144,6 +156,69 @@ contract ConvertibleDepositAuctioneerCurrentTickTest is ConvertibleDepositAuctio // Assert tick capacity assertEq(tick.capacity, 10e9, "new tick capacity"); assertEq(tick.price, 15e18, "new tick price"); + assertEq(tick.tickSize, 10e9, "new tick size"); + } + + function test_newCapacityEqualToTickSize_dayTargetMet() + public + givenInitialized + givenRecipientHasBid(360375e15) + { + // Bid size of 360375e15 results in: + // 1. 360375e15 * 1e9 / 15e18 = 24,025,000,000. Greater than tick size of 10e9. Bid amount becomes 150e18. New price is 15e18 * 110e2 / 100e2 = 165e17 + // 2. (360375e15 - 150e18) * 1e9 / 165e17 = 12,750,000,000. Greater than the tick size of 10e9. Bid amount becomes 165e18. New price is 165e17 * 110e2 / 100e2 = 1815e16. Day target met, so tick size becomes 5e9. + // 3. (360375e15 - 150e18 - 165e18) * 1e9 / 1815e16 = 25e8. Less than the tick size of 5e9. + + // Remaining capacity is 25e8 + // Added capacity will be 25e8 + // New capacity will be 25e8 + 25e8 = 5e9 + + // Calculate the expected tick price + // Tick price remains at 1815e16 + + // Warp forward + uint48 timePassed = 10800; + vm.warp(block.timestamp + timePassed); + + // Call function + IConvertibleDepositAuctioneer.Tick memory tick = auctioneer.getCurrentTick(); + + // Assert tick capacity + assertEq(tick.capacity, 5e9, "new tick capacity"); + assertEq(tick.price, 1815e16, "new tick price"); + assertEq(tick.tickSize, 5e9, "new tick size"); + } + + function test_newCapacityEqualToTickSize_dayTargetMet_nextDay() + public + givenInitialized + givenRecipientHasBid(378861111101700000000) + { + // 150e18 + 165e18 + 63861111101700000000 = 378861111101700000000 + // Bid size of 378861111101700000000 results in: + // 1. 378861111101700000000 * 1e9 / 15e18 = 24,025,000,000. Greater than tick size of 10e9. Bid amount becomes 150e18. New price is 15e18 * 110e2 / 100e2 = 165e17 + // 2. (378861111101700000000 - 150e18) * 1e9 / 165e17 = 12,750,000,000. Greater than the tick size of 10e9. Bid amount becomes 165e18. New price is 165e17 * 110e2 / 100e2 = 1815e16. Day target met, so tick size becomes 5e9. + // 3. (378861111101700000000 - 150e18 - 165e18) * 1e9 / 1815e16 = 3518518518. Less than the tick size of 5e9. + // Remaining capacity is 5e9 - 3518518518 = 1481481482 + + // 20e9*36800/(24*60*60) = 8518518518 added capacity + // New capacity will be 1481481482 + 8518518518 = 10e9 + + // Calculate the expected tick price + // As it is the next day, the tick size will reset to 10e9 + + // Warp forward + // This will result in the next day being reached + uint48 timePassed = 36800; + vm.warp(block.timestamp + timePassed); + + // Call function + IConvertibleDepositAuctioneer.Tick memory tick = auctioneer.getCurrentTick(); + + // Assert tick capacity + assertEq(tick.capacity, 10e9, "new tick capacity"); + assertEq(tick.price, 1815e16, "new tick price"); + assertEq(tick.tickSize, 10e9, "new tick size"); } function test_newCapacityLessThanTickSize() @@ -167,6 +242,7 @@ contract ConvertibleDepositAuctioneerCurrentTickTest is ConvertibleDepositAuctio // Assert tick capacity assertEq(tick.capacity, 9e9, "new tick capacity"); assertEq(tick.price, 15e18, "new tick price"); + assertEq(tick.tickSize, 10e9, "new tick size"); } function test_newCapacityGreaterThanTickSize() @@ -193,6 +269,7 @@ contract ConvertibleDepositAuctioneerCurrentTickTest is ConvertibleDepositAuctio // Assert tick capacity assertEq(tick.capacity, 10e9, "new tick capacity"); assertEq(tick.price, 15e18, "new tick price"); + assertEq(tick.tickSize, 10e9, "new tick size"); } function test_tickStepSame_newCapacityGreaterThanTickSize() @@ -219,6 +296,7 @@ contract ConvertibleDepositAuctioneerCurrentTickTest is ConvertibleDepositAuctio // Assert tick capacity assertEq(tick.capacity, 2e9, "new tick capacity"); assertEq(tick.price, 15e18, "new tick price"); + assertEq(tick.tickSize, 10e9, "new tick size"); } function test_tickStepSame_newCapacityLessThanTickSize() @@ -244,6 +322,7 @@ contract ConvertibleDepositAuctioneerCurrentTickTest is ConvertibleDepositAuctio // Assert tick capacity assertEq(tick.capacity, 75e8, "new tick capacity"); assertEq(tick.price, 15e18, "new tick price"); + assertEq(tick.tickSize, 10e9, "new tick size"); } function test_tickStepSame_newCapacityEqualToTickSize() @@ -269,9 +348,41 @@ contract ConvertibleDepositAuctioneerCurrentTickTest is ConvertibleDepositAuctio // Assert tick capacity assertEq(tick.capacity, 10e9, "new tick capacity"); assertEq(tick.price, 15e18, "new tick price"); + assertEq(tick.tickSize, 10e9, "new tick size"); } function test_tickPriceAboveMinimum_newCapacityGreaterThanTickSize() + public + givenInitialized + givenRecipientHasBid(270e18) + { + // Bid size of 270e18 results in: + // 1. 270e18 * 1e9 / 15e18 = 18e9. Greater than tick size of 10e9. Bid amount becomes 150e18. New price is 15e18 * 110e2 / 100e2 = 165e17 + // 2. (270e18 - 150e18) * 1e9 / 165e17 = 7272727272. Less than the tick size of 10e9, so the tick price remains unchanged. + // Remaining capacity is 10e9 - 7272727272 = 2727272728 + + // 20e9*32400/86400 = 7,500,000,000 + // Added capacity will be 7,500,000,000 + // New capacity will be 2727272728 + 7,500,000,000 = 10227272728 + + // Calculate the expected tick price + // Excess capacity = 10227272728 - 10e9 = 227272728 + // Tick price = 165e17 * 100e2 / 110e2 = 15e18 + + // Warp forward + uint48 timePassed = 32400; + vm.warp(block.timestamp + timePassed); + + // Call function + IConvertibleDepositAuctioneer.Tick memory tick = auctioneer.getCurrentTick(); + + // Assert tick capacity + assertEq(tick.capacity, 227272728, "new tick capacity"); + assertEq(tick.price, 15e18, "new tick price"); + assertEq(tick.tickSize, 10e9, "new tick size"); + } + + function test_tickPriceAboveMinimum_newCapacityGreaterThanTickSize_dayTargetMet() public givenInitialized givenRecipientHasBid(330e18) @@ -280,13 +391,13 @@ contract ConvertibleDepositAuctioneerCurrentTickTest is ConvertibleDepositAuctio // 1. 330e18 * 1e9 / 15e18 = 22e9. Greater than tick size of 10e9. Bid amount becomes 150e18. New price is 15e18 * 110e2 / 100e2 = 165e17 // 2. (330e18 - 150e18) * 1e9 / 165e17 = 10,909,090,909. Greater than tick size of 10e9. Bid amount becomes 165e18. New price is 165e17 * 110e2 / 100e2 = 1815e16 // 3. (330e18 - 150e18 - 165e18) * 1e9 / 1815e16 = 826,446,280 - // Remaining capacity is 10e9 - 826,446,280 = 9,173,553,720 + // Remaining capacity is 5e9 - 826,446,280 = 4,173,553,720 // Added capacity will be 5e9 - // New capacity will be 9,173,553,720 + 5e9 = 14,173,553,720 + // New capacity will be 4,173,553,720 + 5e9 = 9,173,553,720 // Calculate the expected tick price - // Excess capacity = 14,173,553,720 - 10e9 = 4,173,553,720 + // Excess capacity = 9,173,553,720 - 5e9 = 4,173,553,720 // Tick price = 165e17 // Warp forward @@ -299,9 +410,78 @@ contract ConvertibleDepositAuctioneerCurrentTickTest is ConvertibleDepositAuctio // Assert tick capacity assertEq(tick.capacity, 4173553720, "new tick capacity"); assertEq(tick.price, 165e17, "new tick price"); + assertEq(tick.tickSize, 5e9, "new tick size"); + } + + function test_tickPriceAboveMinimum_newCapacityGreaterThanTickSize_dayTargetMet_nextDay() + public + givenInitialized + givenRecipientHasBid(330e18) + { + // Bid size of 330e18 results in: + // 1. 330e18 * 1e9 / 15e18 = 22e9. Greater than tick size of 10e9. Bid amount becomes 150e18. New price is 15e18 * 110e2 / 100e2 = 165e17 + // 2. (330e18 - 150e18) * 1e9 / 165e17 = 10,909,090,909. Greater than tick size of 10e9. Bid amount becomes 165e18. New price is 165e17 * 110e2 / 100e2 = 1815e16 + // 3. (330e18 - 150e18 - 165e18) * 1e9 / 1815e16 = 826,446,280 + // Remaining capacity is 5e9 - 826,446,280 = 4173553720 + + // 20e9*36800/(24*60*60) = 8518518518 added capacity + // New capacity will be 8518518518 + 4173553720 = 12692072238 + + // Calculate the expected tick price + // As it is the next day, the tick size will reset to 10e9 + // Excess capacity = 12692072238 - 10e9 = 2692072238 + // Tick price = 165e17 + + // Warp forward + uint48 timePassed = 36800; + vm.warp(block.timestamp + timePassed); + + // Call function + IConvertibleDepositAuctioneer.Tick memory tick = auctioneer.getCurrentTick(); + + // Assert tick capacity + assertEq(tick.capacity, 2692072238, "new tick capacity"); + assertEq(tick.price, 165e17, "new tick price"); + assertEq(tick.tickSize, 10e9, "new tick size"); } function test_tickPriceAboveMinimum_newPriceBelowMinimum() + public + givenInitialized + givenRecipientHasBid(270e18) + { + // Bid size of 270e18 results in: + // 1. 270e18 * 1e9 / 15e18 = 18e9. Greater than tick size of 10e9. Bid amount becomes 150e18. New price is 15e18 * 110e2 / 100e2 = 165e17 + // 2. (270e18 - 150e18) * 1e9 / 165e17 = 7,272,727,273. Less than tick size of 10e9. + // Remaining capacity is 10e9 - 7,272,727,273 = 2,727,272,727 + + // Added capacity will be 30e9 + // New capacity will be 2,727,272,727 + 30e9 = 32,727,272,727 + + // Calculate the expected tick price + // 1. Excess capacity = 32,727,272,727 - 10e9 = 22,727,272,727 + // Tick price = 165e17 + // 2. Excess capacity = 22,727,272,727 - 10e9 = 12,727,272,727 + // Tick price = 165e17 * 100e2 / 110e2 = 15e18 + // 3. Excess capacity = 12,727,272,727 - 10e9 = 2,727,272,727 + // Tick price = 15e18 * 100e2 / 110e2 = 13636363636363636364 + // Tick price is below the minimum price, so it is set to the minimum price + // New capacity = tick size = 10e9 + + // Warp forward + uint48 timePassed = 6 * 21600; + vm.warp(block.timestamp + timePassed); + + // Call function + IConvertibleDepositAuctioneer.Tick memory tick = auctioneer.getCurrentTick(); + + // Assert tick capacity + assertEq(tick.capacity, 10e9, "new tick capacity"); + assertEq(tick.price, 15e18, "new tick price"); + assertEq(tick.tickSize, 10e9, "new tick size"); + } + + function test_tickPriceAboveMinimum_newPriceBelowMinimum_dayTargetMet_nextDay() public givenInitialized givenRecipientHasBid(330e18) @@ -310,12 +490,13 @@ contract ConvertibleDepositAuctioneerCurrentTickTest is ConvertibleDepositAuctio // 1. 330e18 * 1e9 / 15e18 = 22e9. Greater than tick size of 10e9. Bid amount becomes 150e18. New price is 15e18 * 110e2 / 100e2 = 165e17 // 2. (330e18 - 150e18) * 1e9 / 165e17 = 10,909,090,909. Greater than tick size of 10e9. Bid amount becomes 165e18. New price is 165e17 * 110e2 / 100e2 = 1815e16 // 3. (330e18 - 150e18 - 165e18) * 1e9 / 1815e16 = 826,446,280 - // Remaining capacity is 10e9 - 826,446,280 = 9,173,553,720 + // Remaining capacity is 5e9 - 826,446,280 = 4,173,553,720 - // Added capacity will be 25e9 - // New capacity will be 9,173,553,720 + 25e9 = 34,173,553,720 + // Added capacity will be 30e9 + // New capacity will be 4,173,553,720 + 30e9 = 34,173,553,720 // Calculate the expected tick price + // As it is the next day, the tick size will reset to 10e9 // 1. Excess capacity = 34,173,553,720 - 10e9 = 24,173,553,720 // Tick price = 165e17 // 2. Excess capacity = 24,173,553,720 - 10e9 = 14,173,553,720 @@ -323,10 +504,10 @@ contract ConvertibleDepositAuctioneerCurrentTickTest is ConvertibleDepositAuctio // 3. Excess capacity = 14,173,553,720 - 10e9 = 4,173,553,720 // Tick price = 15e18 * 100e2 / 110e2 = 13636363636363636364 // Tick price is below the minimum price, so it is set to the minimum price - // New capacity = tick size = 10e9 + // New capacity = current tick size = 10e9 // Warp forward - uint48 timePassed = 5 * 21600; + uint48 timePassed = 6 * 21600; vm.warp(block.timestamp + timePassed); // Call function @@ -335,5 +516,6 @@ contract ConvertibleDepositAuctioneerCurrentTickTest is ConvertibleDepositAuctio // Assert tick capacity assertEq(tick.capacity, 10e9, "new tick capacity"); assertEq(tick.price, 15e18, "new tick price"); + assertEq(tick.tickSize, 10e9, "new tick size"); } }