Skip to content

Commit

Permalink
chore: forge fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
bingen committed May 28, 2024
1 parent a1c19e8 commit 452ee45
Show file tree
Hide file tree
Showing 17 changed files with 306 additions and 207 deletions.
6 changes: 3 additions & 3 deletions contracts/src/ActivePool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,18 @@ contract ActivePool is Ownable, CheckContract, IActivePool {

function mintAggInterest() external override {
_requireCallerIsSP();
aggRecordedDebt += _mintAggInterest();
aggRecordedDebt += _mintAggInterest();
}

function _mintAggInterest() internal returns (uint256) {
uint256 aggInterest = calcPendingAggInterest();
// Mint part of the BOLD interest to the SP.
// TODO: implement interest minting to LPs

if (aggInterest > 0) {
uint256 spYield = SP_YIELD_SPLIT * aggInterest / 1e18;
uint256 remainderToLPs = aggInterest - spYield;

boldToken.mint(address(interestRouter), remainderToLPs);
boldToken.mint(address(stabilityPool), spYield);

Expand Down
5 changes: 4 additions & 1 deletion contracts/src/BoldToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,10 @@ contract BoldToken is Ownable, IBoldToken {
}

function _requireCallerIsCRorSP() internal view {
require(msg.sender == collateralRegistryAddress || stabilityPoolAddresses[msg.sender], "Bold: Caller is neither the Registry nor the StabilityPool");
require(
msg.sender == collateralRegistryAddress || stabilityPoolAddresses[msg.sender],
"Bold: Caller is neither the Registry nor the StabilityPool"
);
}

function _requireCallerIsTroveMorSP() internal view {
Expand Down
48 changes: 34 additions & 14 deletions contracts/src/CollateralRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,19 @@ contract CollateralRegistry is LiquityBase, ICollateralRegistry {
}

// Account => index => commitment
mapping (address => mapping (uint256 => RedemptionCommitment)) redemptionCommitments;
mapping(address => mapping(uint256 => RedemptionCommitment)) redemptionCommitments;

event BaseRateUpdated(uint256 _baseRate);
event LastFeeOpTimeUpdated(uint256 _lastFeeOpTime);
event RedemptionCommited(uint256 _redemptionId, uint256 _boldAmount, uint256 _maxFeePercentage);
event RedemptionWithdrawn(uint256 _redemptionId, uint256 _redemptionRefund, uint256 _penalty);

constructor(IBoldToken _boldToken, address _interestRouterAddress, IERC20[] memory _tokens, ITroveManager[] memory _troveManagers) {
event RedemptionWithdrawn(uint256 _redemptionId, uint256 _redemptionRefund, uint256 _penalty);

constructor(
IBoldToken _boldToken,
address _interestRouterAddress,
IERC20[] memory _tokens,
ITroveManager[] memory _troveManagers
) {
uint256 numTokens = _tokens.length;
require(numTokens > 0, "Collateral list cannot be empty");
require(numTokens < 10, "Collateral list too long");
Expand Down Expand Up @@ -135,13 +140,19 @@ contract CollateralRegistry is LiquityBase, ICollateralRegistry {
}

// _redemptionId is per user
function commitRedemption(uint256 _redemptionId, uint256 _boldAmount, uint64 _maxIterationsPerCollateral, uint64 _maxFeePercentage) external override {
function commitRedemption(
uint256 _redemptionId,
uint256 _boldAmount,
uint64 _maxIterationsPerCollateral,
uint64 _maxFeePercentage
) external override {
_requireValidRedemptionId(msg.sender, _redemptionId);
_requireValidMaxFeePercentage(_maxFeePercentage);
_requireAmountGreaterThanZero(_boldAmount);
_requireBoldBalanceCoversRedemption(boldToken, msg.sender, _boldAmount);

redemptionCommitments[msg.sender][_redemptionId] = RedemptionCommitment(_boldAmount, uint64(block.timestamp), _maxIterationsPerCollateral, _maxFeePercentage);
redemptionCommitments[msg.sender][_redemptionId] =
RedemptionCommitment(_boldAmount, uint64(block.timestamp), _maxIterationsPerCollateral, _maxFeePercentage);

// Account for the committed amount
boldRedemptionCommitments += _boldAmount;
Expand Down Expand Up @@ -177,8 +188,9 @@ contract CollateralRegistry is LiquityBase, ICollateralRegistry {
// We only compute it here, and update it at the end,
// because the final redeemed amount may be less than the requested amount
// Redeemers should take this into account in order to request the optimal amount to not overpay
uint256 redemptionRate =
_calcRedemptionRate(_getUpdatedBaseRateFromRedemption(redemptionCommitment.boldAmount, totals.boldSupplyAtStart));
uint256 redemptionRate = _calcRedemptionRate(
_getUpdatedBaseRateFromRedemption(redemptionCommitment.boldAmount, totals.boldSupplyAtStart)
);
require(redemptionRate <= redemptionCommitment.maxFeePercentage, "CR: Fee exceeded provided maximum");
// Implicit by the above and the _requireValidMaxFeePercentage checks
//require(newBaseRate < DECIMAL_PRECISION, "CR: Fee would eat up all collateral");
Expand Down Expand Up @@ -206,7 +218,11 @@ contract CollateralRegistry is LiquityBase, ICollateralRegistry {
if (redeemAmount > 0) {
ITroveManager troveManager = getTroveManager(index);
uint256 redeemedAmount = troveManager.redeemCollateral(
msg.sender, redeemAmount, prices[index], redemptionRate, redemptionCommitment.maxIterationsPerCollateral
msg.sender,
redeemAmount,
prices[index],
redemptionRate,
redemptionCommitment.maxIterationsPerCollateral
);
totals.redeemedAmount += redeemedAmount;
}
Expand Down Expand Up @@ -355,7 +371,12 @@ contract CollateralRegistry is LiquityBase, ICollateralRegistry {
return _calcRedemptionFee(getRedemptionRateWithDecay(), _ETHDrawn);
}

function getEffectiveRedemptionFeeInBold(uint256 _redeemAmount, uint256 _extraSeconds) public view override returns (uint256) {
function getEffectiveRedemptionFeeInBold(uint256 _redeemAmount, uint256 _extraSeconds)
public
view
override
returns (uint256)
{
uint256 totalBoldSupply = boldToken.totalSupply();
uint256 newBaseRate = _getFutureBaseRateFromRedemption(_redeemAmount, totalBoldSupply, _extraSeconds);
return _calcRedemptionFee(_calcRedemptionRate(newBaseRate), _redeemAmount);
Expand Down Expand Up @@ -411,9 +432,7 @@ contract CollateralRegistry is LiquityBase, ICollateralRegistry {
uint256 boldBalance = _boldToken.balanceOf(_redeemer);
// Confirm redeemer's balance is less than total Bold supply
assert(boldBalance <= _boldToken.totalSupply());
require(
boldBalance >= _amount, "CR: Requested redemption amount must be <= user's Bold token balance"
);
require(boldBalance >= _amount, "CR: Requested redemption amount must be <= user's Bold token balance");
}

function _requireValidRedemptionId(address _account, uint256 _redemptionId) internal view {
Expand All @@ -426,7 +445,8 @@ contract CollateralRegistry is LiquityBase, ICollateralRegistry {

function _requireValidTime(RedemptionCommitment memory _redemptionCommitment) internal view {
require(
_redemptionCommitment.timestamp + REDEMPTION_INTERVAL_MIN <= block.timestamp && block.timestamp <= _redemptionCommitment.timestamp + REDEMPTION_INTERVAL_MAX,
_redemptionCommitment.timestamp + REDEMPTION_INTERVAL_MIN <= block.timestamp
&& block.timestamp <= _redemptionCommitment.timestamp + REDEMPTION_INTERVAL_MAX,
"CR: commitment out of redemption window"
);
}
Expand Down
12 changes: 10 additions & 2 deletions contracts/src/Interfaces/ICollateralRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ interface ICollateralRegistry {
function baseRate() external view returns (uint256);
function boldRedemptionCommitments() external view returns (uint256);

function commitRedemption(uint256 _redemptionId, uint256 _boldAmount, uint64 _maxIterationsPerCollateral, uint64 _maxFeePercentage) external;
function commitRedemption(
uint256 _redemptionId,
uint256 _boldAmount,
uint64 _maxIterationsPerCollateral,
uint64 _maxFeePercentage
) external;
function executeRedemption(uint256 _redemptionId) external;
function withdrawRedemption(uint256 _redemptionId) external;
// getters
Expand All @@ -20,5 +25,8 @@ interface ICollateralRegistry {
function getRedemptionRateWithDecay() external view returns (uint256);

function getRedemptionFeeWithDecay(uint256 _ETHDrawn) external view returns (uint256);
function getEffectiveRedemptionFeeInBold(uint256 _redeemAmount, uint256 _extraSeconds) external view returns (uint256);
function getEffectiveRedemptionFeeInBold(uint256 _redeemAmount, uint256 _extraSeconds)
external
view
returns (uint256);
}
25 changes: 13 additions & 12 deletions contracts/src/StabilityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ contract StabilityPool is LiquityBase, Ownable, CheckContract, IStabilityPool {
uint256 currentETHGain = getDepositorETHGain(msg.sender);
uint256 currentYieldGain = getDepositorYieldGain(msg.sender);
uint256 compoundedBoldDeposit = getCompoundedBoldDeposit(msg.sender);

uint256 boldLoss = initialDeposit - compoundedBoldDeposit; // Needed only for event log
(uint256 keptYieldGain, uint256 yieldGainToSend) = _getYieldToKeepOrSend(currentYieldGain, _doClaim);

Expand All @@ -316,7 +316,7 @@ contract StabilityPool is LiquityBase, Ownable, CheckContract, IStabilityPool {

_depositBoldtoSP(msg.sender, _topUp);
_decreaseYieldGainsOwed(currentYieldGain);

_sendBoldtoDepositor(msg.sender, yieldGainToSend);
_stashOrSendETHGains(msg.sender, currentETHGain, boldLoss, _doClaim);

Expand All @@ -327,11 +327,11 @@ contract StabilityPool is LiquityBase, Ownable, CheckContract, IStabilityPool {
function _getYieldToKeepOrSend(uint256 _currentYieldGain, bool _doClaim) internal pure returns (uint256, uint256) {
uint256 yieldToKeep;
uint256 yieldToSend;

if (_doClaim) {
yieldToKeep = 0;
yieldToSend = _currentYieldGain;
} else {
} else {
yieldToKeep = _currentYieldGain;
yieldToSend = 0;
}
Expand All @@ -352,7 +352,7 @@ contract StabilityPool is LiquityBase, Ownable, CheckContract, IStabilityPool {
_requireUserHasDeposit(initialDeposit);

activePool.mintAggInterest();

uint256 currentETHGain = getDepositorETHGain(msg.sender);
uint256 currentYieldGain = getDepositorYieldGain(msg.sender);

Expand Down Expand Up @@ -407,11 +407,11 @@ contract StabilityPool is LiquityBase, Ownable, CheckContract, IStabilityPool {
_requireUserHasNoDeposit(msg.sender);
assert(getDepositorETHGain(msg.sender) == 0);
assert(getDepositorYieldGain(msg.sender) == 0);

activePool.mintAggInterest();

uint256 ETHToSend = _getTotalETHGainAndZeroStash(msg.sender, 0);

_sendETHGainToDepositor(ETHToSend);

assert(stashedETH[msg.sender] == 0);
Expand All @@ -423,27 +423,28 @@ contract StabilityPool is LiquityBase, Ownable, CheckContract, IStabilityPool {

function triggerBoldRewards(uint256 _boldYield) external {
_requireCallerIsActivePool();

uint256 totalBoldDepositsCached = totalBoldDeposits; // cached to save an SLOAD
/*
* When total deposits is 0, B is not updated. In this case, the BOLD issued can not be obtained by later
* depositors - it is missed out on, and remains in the balance of the SP.
*
*/
if (totalBoldDepositsCached == 0 || _boldYield == 0) {
return;}
return;
}

yieldGainsOwed += _boldYield;

uint256 yieldPerUnitStaked =_computeYieldPerUnitStaked(_boldYield, totalBoldDepositsCached);
uint256 yieldPerUnitStaked = _computeYieldPerUnitStaked(_boldYield, totalBoldDepositsCached);

uint256 marginalYieldGain = yieldPerUnitStaked * P;
epochToScaleToB[currentEpoch][currentScale] = epochToScaleToB[currentEpoch][currentScale] + marginalYieldGain;

emit B_Updated(epochToScaleToB[currentEpoch][currentScale], currentEpoch, currentScale);
}

function _computeYieldPerUnitStaked(uint256 _yield, uint256 _totalBoldDeposits) internal returns (uint) {
function _computeYieldPerUnitStaked(uint256 _yield, uint256 _totalBoldDeposits) internal returns (uint256) {
/*
* Calculate the BOLD-per-unit staked. Division uses a "feedback" error correction, to keep the
* cumulative error low in the running total B:
Expand Down Expand Up @@ -599,7 +600,7 @@ contract StabilityPool is LiquityBase, Ownable, CheckContract, IStabilityPool {
if (_amount == 0) return;
uint256 newYieldGainsOwed = yieldGainsOwed - _amount;
yieldGainsOwed = newYieldGainsOwed;
emit YieldGainsOwedUpdated(newYieldGainsOwed);
emit YieldGainsOwedUpdated(newYieldGainsOwed);
}

// --- Reward calculator functions for depositor ---
Expand Down
8 changes: 5 additions & 3 deletions contracts/src/TroveManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ contract TroveManager is ERC721, LiquityBase, Ownable, ITroveManager {
event TroveSnapshotsUpdated(uint256 _L_ETH, uint256 _L_boldDebt);
event TroveIndexUpdated(uint256 _troveId, uint256 _newIndex);
event RedemptionFeePaidToTrove(uint256 indexed _troveId, uint256 _ETHFee);

enum TroveManagerOperation {
getAndApplyRedistributionGains,
liquidate,
Expand Down Expand Up @@ -628,7 +629,7 @@ contract TroveManager is ERC721, LiquityBase, Ownable, ITroveManager {
// TODO: should we leave gas compensation (and corresponding debt) untouched for zombie Troves? Currently it's not touched.
singleRedemption.BoldLot = LiquityMath._min(_maxBoldamount, entireTroveDebt - BOLD_GAS_COMPENSATION);

// Get the amount of ETH equal in USD value to the BoldLot redeemed
// Get the amount of ETH equal in USD value to the BoldLot redeemed
uint256 correspondingETH = singleRedemption.BoldLot * DECIMAL_PRECISION / _price;
// Calculate the ETHFee separately (for events)
singleRedemption.ETHFee = correspondingETH * _redemptionRate / DECIMAL_PRECISION;
Expand Down Expand Up @@ -710,8 +711,9 @@ contract TroveManager is ERC721, LiquityBase, Ownable, ITroveManager {
continue;
}

SingleRedemptionValues memory singleRedemption =
_redeemCollateralFromTrove(contractsCache, currentTroveId, totals.remainingBold, _price, _redemptionRate);
SingleRedemptionValues memory singleRedemption = _redeemCollateralFromTrove(
contractsCache, currentTroveId, totals.remainingBold, _price, _redemptionRate
);

totals.totalBoldToRedeem = totals.totalBoldToRedeem + singleRedemption.BoldLot;
totals.totalRedistDebtGains = totals.totalRedistDebtGains + singleRedemption.redistDebtGain;
Expand Down
3 changes: 2 additions & 1 deletion contracts/src/deployment.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ function _deployAndConnectContracts(TroveManagerParams[] memory troveManagerPara
}

// TODO: interest router should be common (not per collateral branch)
collateralRegistry = new CollateralRegistry(boldToken, address(contractsArray[0].interestRouter), collaterals, troveManagers);
collateralRegistry =
new CollateralRegistry(boldToken, address(contractsArray[0].interestRouter), collaterals, troveManagers);
boldToken.setCollateralRegistry(address(collateralRegistry));
// Set registry in TroveManagers
for (uint256 i = 0; i < numCollaterals; i++) {
Expand Down
1 change: 1 addition & 0 deletions contracts/src/test/TestContracts/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ contract BaseTest is Test {
function getShareofSPReward(address _depositor, uint256 _reward) public returns (uint256) {
return _reward * stabilityPool.getCompoundedBoldDeposit(_depositor) / stabilityPool.getTotalBoldDeposits();
}

function logContractAddresses() public view {
console.log("ActivePool addr: ", address(activePool));
console.log("BorrowerOps addr: ", address(borrowerOperations));
Expand Down
1 change: 0 additions & 1 deletion contracts/src/test/TestContracts/BoldTokenTester.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity 0.8.18;

import "../../BoldToken.sol";


contract BoldTokenTester is BoldToken {
function unprotectedMint(address _account, uint256 _amount) external {
_mint(_account, _amount);
Expand Down
9 changes: 6 additions & 3 deletions contracts/src/test/TestContracts/CollateralRegistryTester.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import "../../CollateralRegistry.sol";
for testing the parent's internal functions. */

contract CollateralRegistryTester is CollateralRegistry {
constructor(IBoldToken _boldToken, address _interestRouterAddress, IERC20[] memory _tokens, ITroveManager[] memory _troveManagers)
CollateralRegistry(_boldToken, _interestRouterAddress, _tokens, _troveManagers)
{}
constructor(
IBoldToken _boldToken,
address _interestRouterAddress,
IERC20[] memory _tokens,
ITroveManager[] memory _troveManagers
) CollateralRegistry(_boldToken, _interestRouterAddress, _tokens, _troveManagers) {}

function unprotectedDecayBaseRateFromBorrowing() external returns (uint256) {
baseRate = _calcDecayedBaseRate();
Expand Down
3 changes: 1 addition & 2 deletions contracts/src/test/TestContracts/DevTestSetup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,10 @@ contract DevTestSetup is BaseTest {
assertEq(troveManager.getTroveStatus(_troveIDs.B), 1); // status 'active'
}

function _getSPYield(uint256 _aggInterest) internal returns (uint256) {
function _getSPYield(uint256 _aggInterest) internal returns (uint256) {
uint256 spYield = SP_YIELD_SPLIT * _aggInterest / 1e18;
assertGt(spYield, 0);
assertLe(spYield, _aggInterest);
return spYield;
}

}
5 changes: 3 additions & 2 deletions contracts/src/test/TestContracts/TroveManagerTester.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ contract TroveManagerTester is TroveManager {
view
returns (uint256)
{
return ICollateralRegistry(collateralRegistryAddress).getEffectiveRedemptionFeeInBold(_redeemAmount, _extraSeconds) * DECIMAL_PRECISION / _price;
return ICollateralRegistry(collateralRegistryAddress).getEffectiveRedemptionFeeInBold(
_redeemAmount, _extraSeconds
) * DECIMAL_PRECISION / _price;
}


function callInternalRemoveTroveId(uint256 _troveId) external {
uint256 troveOwnersArrayLength = TroveIds.length;
_removeTroveId(_troveId, troveOwnersArrayLength);
Expand Down
5 changes: 1 addition & 4 deletions contracts/src/test/interestRateAggregate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ contract InterestRateAggregate is DevTestSetup {
assertGt(pendingAggInterest_2, 0);
uint256 expectedSPYield_2 = _getSPYield(pendingAggInterest_2);


// Open 3rd trove
openTroveNoHints100pct(C, 2 ether, 2000e18, 25e16);

Expand Down Expand Up @@ -681,8 +680,6 @@ contract InterestRateAggregate is DevTestSetup {

uint256 expectedSPYield = _getSPYield(pendingAggInterest);



// B closes Trove
closeTrove(B, BTroveId);

Expand Down Expand Up @@ -2276,7 +2273,7 @@ contract InterestRateAggregate is DevTestSetup {

// Check SP Bold bal has changed as expected - by the pendingAggInterest, minus A's share of it which gets paid out
uint256 boldBalSP_2 = boldToken.balanceOf(address(stabilityPool));
assertApproximatelyEqual(boldBalSP_2, boldBalSP_1 + expectedSPYield - expectedBoldGain_A, 1e3);
assertApproximatelyEqual(boldBalSP_2, boldBalSP_1 + expectedSPYield - expectedBoldGain_A, 1e3);
}

// TODO: mixed collateral & debt adjustment opps
Expand Down
4 changes: 3 additions & 1 deletion contracts/src/test/interestRateBasic.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,9 @@ contract InterestRateBasic is DevTestSetup {

uint256 recordedTroveDebt_2 = troveManager.getTroveDebt(troveIDs.A);

assertApproxEqAbs(recordedTroveDebt_2, recordedTroveDebt_1 + accruedTroveInterest - redeemAmount, 1e16, "A debt mismatch");
assertApproxEqAbs(
recordedTroveDebt_2, recordedTroveDebt_1 + accruedTroveInterest - redeemAmount, 1e16, "A debt mismatch"
);
assertGt(recordedTroveDebt_2, recordedTroveDebt_1 + accruedTroveInterest - redeemAmount);
}
}
Loading

0 comments on commit 452ee45

Please sign in to comment.