Omniscia Gravita Protocol Audit
PoolBase Code Style Findings
PoolBase Code Style Findings
PBE-01C: Significantly Inefficient Merging of Pending Gains / Distributed Funds
Type | Severity | Location |
---|---|---|
Gas Optimization | ![]() | PoolBase.sol:L43-L68 |
Description:
The PoolBase::_leftSumColls
function is meant to merge whatever pending gains are denoted in _tokens
and _amounts
to the _coll1
data entry, however, it does so significantly inefficiently. The same inefficiency is observed in the PoolBase::_leftSubColls
function.
Example:
33function _leftSumColls(34 Colls memory _coll1,35 address[] memory _tokens,36 uint256[] memory _amounts37) internal pure returns (uint256[] memory) {38 // If nothing on the right side then return the original.39 if (_amounts.length == 0) {40 return _coll1.amounts;41 }42
43 uint256 coll1Len = _coll1.amounts.length;44 uint256 tokensLen = _tokens.length;45 // Result will always be coll1 len size.46 uint256[] memory sumAmounts = new uint256[](coll1Len);47
48 uint256 i = 0;49 uint256 j = 0;50
51 // Sum through all tokens until either left or right side reaches end.52 while (i < tokensLen && j < coll1Len) {53 // If tokens match up then sum them together.54 if (_tokens[i] == _coll1.tokens[j]) {55 sumAmounts[j] = _coll1.amounts[j].add(_amounts[i]);56 ++i;57 }58 // Otherwise just take the left side.59 else {60 sumAmounts[j] = _coll1.amounts[j];61 }62 ++j;63 }64 // If right side ran out add the remaining amounts in the left side.65 while (j < coll1Len) {66 sumAmounts[j] = _coll1.amounts[j];67 ++j;68 }69
70 return sumAmounts;71}
Recommendation:
We advise the code to instead sum / subtract the values in the _coll1.amounts
data entry directly, rendering the new sumAmounts
/ diffAmounts
array redundant. Additionally, this will significantly optimize the code as only the _tokens
array would need to be iterated as the _coll1.amounts
data entry will be "pre-filled" with the desirable amounts.
Alleviation:
The code, now located within StabilityPool
, has been refactored per our recommendation albeit in a different approach that is still relatively inefficient. We advise the _tokens
array to be iterated rather than the _coll1
array, iterating the _coll1
array inside the _tokens
loop and issuing a break
statement when the correct _coll1
entry has been found to further optimize the code.
PBE-02C: Unused Error Declaration
Type | Severity | Location |
---|---|---|
Gas Optimization | ![]() | PoolBase.sol:L22 |
Description:
The PoolBase__AdminOnly
error remains unused in the codebase.
Example:
22error PoolBase__AdminOnly();
Recommendation:
We advise it to be safely omitted from it.
Alleviation:
The PoolBase
contract is no longer present in the codebase rendering this exhibit no longer applicable.