code-423n4 / 2021-04-vader-findings

1 stars 0 forks source link

sortArray optimizable #19

Open code423n4 opened 3 years ago

code423n4 commented 3 years ago

Handle

gpersoon

Vulnerability details

Impact

The function sortArray is only called by getAnchorPrice() in Router.sol Then it is only used to get the second element of the sorted array. With this knowledge it is possible to write a function that is more efficient. Sort functions are relative expensive functions.

Proof of Concept

Router.sol: function getAnchorPrice() public view returns (uint anchorPrice) { ....
uint[] memory _sortedAnchorFeed = iUTILS(UTILS()).sortArray(arrayPrices);
anchorPrice = _sortedAnchorFeed[2];

Tools Used

Recommended Mitigation Steps

Make a partial sort function that only returns the required value

strictly-scarce commented 3 years ago

Do you have an example of how to make it better? It's already pretty good for what it does