Skip to content

Commit

Permalink
Merge pull request #62 from liquity/names_and_warnings
Browse files Browse the repository at this point in the history
Names and warnings
  • Loading branch information
bingen committed Jan 26, 2024
2 parents 2658003 + 244692d commit ef265dd
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 36 deletions.
18 changes: 8 additions & 10 deletions contracts/src/BoldToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ contract BoldToken is CheckContract, IBoldToken {
address _troveManagerAddress,
address _stabilityPoolAddress,
address _borrowerOperationsAddress
)
public
{
) {
checkContract(_troveManagerAddress);
checkContract(_stabilityPoolAddress);
checkContract(_borrowerOperationsAddress);
Expand Down Expand Up @@ -203,8 +201,8 @@ contract BoldToken is CheckContract, IBoldToken {
}
}

function _buildDomainSeparator(bytes32 typeHash, bytes32 name, bytes32 version) private view returns (bytes32) {
return keccak256(abi.encode(typeHash, name, version, _chainID(), address(this)));
function _buildDomainSeparator(bytes32 _typeHash, bytes32 _name, bytes32 _version) private view returns (bytes32) {
return keccak256(abi.encode(_typeHash, _name, _version, _chainID(), address(this)));
}

// --- Internal operations ---
Expand Down Expand Up @@ -284,23 +282,23 @@ contract BoldToken is CheckContract, IBoldToken {

// --- Optional functions ---

function name() external view override returns (string memory) {
function name() external pure override returns (string memory) {
return _NAME;
}

function symbol() external view override returns (string memory) {
function symbol() external pure override returns (string memory) {
return _SYMBOL;
}

function decimals() external view override returns (uint8) {
function decimals() external pure override returns (uint8) {
return _DECIMALS;
}

function version() external view override returns (string memory) {
function version() external pure override returns (string memory) {
return _VERSION;
}

function permitTypeHash() external view override returns (bytes32) {
function permitTypeHash() external pure override returns (bytes32) {
return _PERMIT_TYPEHASH;
}
}
2 changes: 1 addition & 1 deletion contracts/src/MultiTroveGetter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ contract MultiTroveGetter {
TroveManager public troveManager; // XXX Troves missing from ITroveManager?
ISortedTroves public sortedTroves;

constructor(TroveManager _troveManager, ISortedTroves _sortedTroves) public {
constructor(TroveManager _troveManager, ISortedTroves _sortedTroves) {
troveManager = _troveManager;
sortedTroves = _sortedTroves;
}
Expand Down
69 changes: 69 additions & 0 deletions contracts/src/OldTestContracts/BoldTokenTester.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.18;

import "../BoldToken.sol";

contract BoldTokenTester is BoldToken {

bytes32 private immutable _PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9;

constructor(
address _troveManagerAddress,
address _stabilityPoolAddress,
address _borrowerOperationsAddress
) BoldToken(
_troveManagerAddress,
_stabilityPoolAddress,
_borrowerOperationsAddress
) {}

function unprotectedMint(address _account, uint256 _amount) external {
// No check on caller here

_mint(_account, _amount);
}

function unprotectedBurn(address _account, uint _amount) external {
// No check on caller here

_burn(_account, _amount);
}

function unprotectedSendToPool(address _sender, address _poolAddress, uint256 _amount) external {
// No check on caller here

_transfer(_sender, _poolAddress, _amount);
}

function unprotectedReturnFromPool(address _poolAddress, address _receiver, uint256 _amount ) external {
// No check on caller here

_transfer(_poolAddress, _receiver, _amount);
}

function callInternalApprove(address owner, address spender, uint256 amount) external returns (bool) {
_approve(owner, spender, amount);
return true;
}

function getChainId() external view returns (uint256 chainID) {
//return _chainID(); // it’s private
assembly {
chainID := chainid()
}
}

function getDigest(address owner, address spender, uint amount, uint nonce, uint deadline) external view returns (bytes32) {
return keccak256(abi.encodePacked(
uint16(0x1901),
domainSeparator(),
keccak256(abi.encode(_PERMIT_TYPEHASH, owner, spender, amount, nonce, deadline))
)
);
}

function recoverAddress(bytes32 digest, uint8 v, bytes32 r, bytes32 s) external pure returns (address) {
return ecrecover(digest, v, r, s);
}
}
11 changes: 7 additions & 4 deletions contracts/src/TestContracts/BoldTokenTester.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ contract BoldTokenTester is BoldToken {
address _troveManagerAddress,
address _stabilityPoolAddress,
address _borrowerOperationsAddress
) public BoldToken(_troveManagerAddress,
_stabilityPoolAddress,
_borrowerOperationsAddress) {}

) BoldToken(
_troveManagerAddress,
_stabilityPoolAddress,
_borrowerOperationsAddress
) {}

function unprotectedMint(address _account, uint256 _amount) external {
// No check on caller here

Expand All @@ -42,6 +44,7 @@ contract BoldTokenTester is BoldToken {

function callInternalApprove(address owner, address spender, uint256 amount) external returns (bool) {
_approve(owner, spender, amount);
return true;
}

function getChainId() external view returns (uint256 chainID) {
Expand Down
4 changes: 2 additions & 2 deletions contracts/src/TestContracts/FunctionCaller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ contract FunctionCaller {

// --- Non-view wrapper functions used for calculating gas ---

function troveManager_getCurrentICR(address _address, uint _price) external returns (uint) {
function troveManager_getCurrentICR(address _address, uint _price) external view returns (uint) {
return troveManager.getCurrentICR(_address, _price);
}

function sortedTroves_findInsertPosition(uint _NICR, address _prevId, address _nextId) external returns (address, address) {
function sortedTroves_findInsertPosition(uint _NICR, address _prevId, address _nextId) external view returns (address, address) {
return sortedTroves.findInsertPosition(_NICR, _prevId, _nextId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ contract TroveManagerTester is TroveManager {
}

function callGetRedemptionFee(uint _ETHDrawn) external view returns (uint) {
_getRedemptionFee(_ETHDrawn);
return _getRedemptionFee(_ETHDrawn);
}

function getActualDebtFromComposite(uint _debtVal) external pure returns (uint) {
Expand Down
32 changes: 16 additions & 16 deletions contracts/src/TroveManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ contract TroveManager is LiquityBase, Ownable, CheckContract, ITroveManager {
* L_ETH and L_boldDebt track the sums of accumulated liquidation rewards per unit staked. During its lifetime, each stake earns:
*
* An ETH gain of ( stake * [L_ETH - L_ETH(0)] )
* A BoldDebt increase of ( stake * [L_boldDebt - L_boldDebt(0)] )
* A boldDebt increase of ( stake * [L_boldDebt - L_boldDebt(0)] )
*
* Where L_ETH(0) and L_boldDebt(0) are snapshots of L_ETH and L_boldDebt for the active Trove taken at the instant the stake was made
*/
Expand All @@ -104,7 +104,7 @@ contract TroveManager is LiquityBase, Ownable, CheckContract, ITroveManager {
mapping (address => RewardSnapshot) public rewardSnapshots;

// Object containing the ETH and Bold snapshots for a given active trove
struct RewardSnapshot { uint ETH; uint BoldDebt;}
struct RewardSnapshot { uint ETH; uint boldDebt;}

// Array of all active trove addresses - used to to compute an approximate hint off-chain, for the sorted list insertion
address[] public TroveOwners;
Expand All @@ -122,7 +122,7 @@ contract TroveManager is LiquityBase, Ownable, CheckContract, ITroveManager {

struct LocalVariables_OuterLiquidationFunction {
uint price;
uint BoldInStabPool;
uint boldInStabPool;
bool recoveryModeAtStart;
uint liquidatedDebt;
uint liquidatedColl;
Expand Down Expand Up @@ -508,14 +508,14 @@ contract TroveManager is LiquityBase, Ownable, CheckContract, ITroveManager {
LiquidationTotals memory totals;

vars.price = priceFeed.fetchPrice();
vars.BoldInStabPool = stabilityPoolCached.getTotalBoldDeposits();
vars.boldInStabPool = stabilityPoolCached.getTotalBoldDeposits();
vars.recoveryModeAtStart = _checkRecoveryMode(vars.price);

// Perform the appropriate liquidation sequence - tally the values, and obtain their totals
if (vars.recoveryModeAtStart) {
totals = _getTotalsFromLiquidateTrovesSequence_RecoveryMode(contractsCache, vars.price, vars.BoldInStabPool, _n);
totals = _getTotalsFromLiquidateTrovesSequence_RecoveryMode(contractsCache, vars.price, vars.boldInStabPool, _n);
} else { // if !vars.recoveryModeAtStart
totals = _getTotalsFromLiquidateTrovesSequence_NormalMode(contractsCache.activePool, contractsCache.defaultPool, vars.price, vars.BoldInStabPool, _n);
totals = _getTotalsFromLiquidateTrovesSequence_NormalMode(contractsCache.activePool, contractsCache.defaultPool, vars.price, vars.boldInStabPool, _n);
}

require(totals.totalDebtInSequence > 0, "TroveManager: nothing to liquidate");
Expand Down Expand Up @@ -650,14 +650,14 @@ contract TroveManager is LiquityBase, Ownable, CheckContract, ITroveManager {
LiquidationTotals memory totals;

vars.price = priceFeed.fetchPrice();
vars.BoldInStabPool = stabilityPoolCached.getTotalBoldDeposits();
vars.boldInStabPool = stabilityPoolCached.getTotalBoldDeposits();
vars.recoveryModeAtStart = _checkRecoveryMode(vars.price);

// Perform the appropriate liquidation sequence - tally values and obtain their totals.
if (vars.recoveryModeAtStart) {
totals = _getTotalFromBatchLiquidate_RecoveryMode(activePoolCached, defaultPoolCached, vars.price, vars.BoldInStabPool, _troveArray);
totals = _getTotalFromBatchLiquidate_RecoveryMode(activePoolCached, defaultPoolCached, vars.price, vars.boldInStabPool, _troveArray);
} else { // if !vars.recoveryModeAtStart
totals = _getTotalsFromBatchLiquidate_NormalMode(activePoolCached, defaultPoolCached, vars.price, vars.BoldInStabPool, _troveArray);
totals = _getTotalsFromBatchLiquidate_NormalMode(activePoolCached, defaultPoolCached, vars.price, vars.boldInStabPool, _troveArray);
}

require(totals.totalDebtInSequence > 0, "TroveManager: nothing to liquidate");
Expand Down Expand Up @@ -1087,7 +1087,7 @@ contract TroveManager is LiquityBase, Ownable, CheckContract, ITroveManager {

function _updateTroveRewardSnapshots(address _borrower) internal {
rewardSnapshots[_borrower].ETH = L_ETH;
rewardSnapshots[_borrower].BoldDebt = L_boldDebt;
rewardSnapshots[_borrower].boldDebt = L_boldDebt;
emit TroveSnapshotsUpdated(L_ETH, L_boldDebt);
}

Expand All @@ -1107,7 +1107,7 @@ contract TroveManager is LiquityBase, Ownable, CheckContract, ITroveManager {

// Get the borrower's pending accumulated Bold reward, earned by their stake
function getPendingBoldDebtReward(address _borrower) public view override returns (uint) {
uint snapshotBoldDebt = rewardSnapshots[_borrower].BoldDebt;
uint snapshotBoldDebt = rewardSnapshots[_borrower].boldDebt;
uint rewardPerUnitStaked = L_boldDebt - snapshotBoldDebt;

if ( rewardPerUnitStaked == 0 || Troves[_borrower].status != Status.active) { return 0; }
Expand Down Expand Up @@ -1211,18 +1211,18 @@ contract TroveManager is LiquityBase, Ownable, CheckContract, ITroveManager {
* 5) Note: static analysis tools complain about this "division before multiplication", however, it is intended.
*/
uint ETHNumerator = _coll * DECIMAL_PRECISION + lastETHError_Redistribution;
uint BoldDebtNumerator = _debt * DECIMAL_PRECISION + lastBoldDebtError_Redistribution;
uint boldDebtNumerator = _debt * DECIMAL_PRECISION + lastBoldDebtError_Redistribution;

// Get the per-unit-staked terms
uint ETHRewardPerUnitStaked = ETHNumerator / totalStakes;
uint BoldDebtRewardPerUnitStaked = BoldDebtNumerator / totalStakes;
uint boldDebtRewardPerUnitStaked = boldDebtNumerator / totalStakes;

lastETHError_Redistribution = ETHNumerator - ETHRewardPerUnitStaked * totalStakes;
lastBoldDebtError_Redistribution = BoldDebtNumerator - BoldDebtRewardPerUnitStaked * totalStakes;
lastBoldDebtError_Redistribution = boldDebtNumerator - boldDebtRewardPerUnitStaked * totalStakes;

// Add per-unit-staked terms to the running totals
L_ETH = L_ETH + ETHRewardPerUnitStaked;
L_boldDebt = L_boldDebt + BoldDebtRewardPerUnitStaked;
L_boldDebt = L_boldDebt + boldDebtRewardPerUnitStaked;

emit LTermsUpdated(L_ETH, L_boldDebt);

Expand All @@ -1248,7 +1248,7 @@ contract TroveManager is LiquityBase, Ownable, CheckContract, ITroveManager {
Troves[_borrower].debt = 0;

rewardSnapshots[_borrower].ETH = 0;
rewardSnapshots[_borrower].BoldDebt = 0;
rewardSnapshots[_borrower].boldDebt = 0;

_removeTroveOwner(_borrower, TroveOwnersArrayLength);
sortedTroves.remove(_borrower);
Expand Down
4 changes: 2 additions & 2 deletions contracts/src/test/TestContracts/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ contract BaseTest is Test {
accountsList = tempAccounts;
}

function logContractAddresses() public {
function logContractAddresses() view public {
console.log("ActivePool addr: ", address(activePool));
console.log("BorrowerOps addr: ", address(borrowerOperations));
console.log("CollSurplusPool addr: ", address(collSurplusPool));
Expand All @@ -67,4 +67,4 @@ contract BaseTest is Test {
console.log("TroveManager addr: ", address(troveManager));
console.log("BoldToken addr: ", address(boldToken));
}
}
}

0 comments on commit ef265dd

Please sign in to comment.