C Code Style Findings

C Code Style Findings

PRO-01C: Inexplicable Value Literal

Type Severity Location
Code Style C.sol:L124, L128, L132, L136, L140

Description:

The 1e18 value literal is repeated across the codebase as the base of the various percentages used by the Beanstalk protocol.

Example:

protocol/contracts/C.sol
127function getUpperBoundPodRate() internal pure returns (Decimal.D256 memory) {
128 return Decimal.ratio(POD_RATE_UPPER_BOUND,1e18);
129}
130
131function getLowerBoundPodRate() internal pure returns (Decimal.D256 memory) {
132 return Decimal.ratio(POD_RATE_LOWER_BOUND,1e18);
133}
134
135function getUpperBoundDPD() internal pure returns (Decimal.D256 memory) {
136 return Decimal.ratio(DELTA_POD_DEMAND_UPPER_BOUND,1e18);
137}
138
139function getLowerBoundDPD() internal pure returns (Decimal.D256 memory) {
140 return Decimal.ratio(DELTA_POD_DEMAND_LOWER_BOUND,1e18);
141}

Recommendation:

We advise it to be stored to a library-level constant that clearly depicts its purpose (i.e. PERCENTAGE_BASE) and is utilised in place of the literals within the code.

Alleviation:

The literal representations were replaced by decimal-accuracy literals offset by the same base (e18) thereby increasing the legibility of the codebase.

View Fix on GitHub

PRO-02C: Suboptimal Code Legibility

Type Severity Location
Code Style C.sol:L26, L29-L31, L33-L34, L42, L46

Description:

The linked constant declarations are meant to represent percentages with a base of 1e18, however, they all utilise different exponent offsets.

Example:

protocol/contracts/C.sol
25// Sun
26uint256 private constant HARVESET_PERCENTAGE = 5e17; // 50%
27
28// Weather
29uint256 private constant POD_RATE_LOWER_BOUND = 5e16; // 5%
30uint256 private constant OPTIMAL_POD_RATE = 15e16; // 15%
31uint256 private constant POD_RATE_UPPER_BOUND = 25e16; // 25%
32
33uint256 private constant DELTA_POD_DEMAND_LOWER_BOUND = 95e16; // 95%
34uint256 private constant DELTA_POD_DEMAND_UPPER_BOUND = 105e16; // 105%

Recommendation:

We advise the same exponent offset to be utilised across all declarations by utilising fractional representations that are automatically substituted with integers by the compiler (i.e. 0.05e18 is an acceptable representation of 5% by the compiler).

Alleviation:

The value literal was properly replaced by a newly declared PERCENT_BASE constant representing 1e18 as the mainnet accuracy configuration.

View Fix on GitHub
Navigated to C Code Style Findings