0xAmanda - Incorrect function call leads to stale borrowing fees #197
Description
0xAmanda
high
Incorrect function call leads to stale borrowing fees
Summary
Due to an incorrect function call while getting the total borrow fees, the returned fees will be an inaccurate and stale amount. Which will have an impact on liquidity providers
Vulnerability Detail
As said the function getTotalBorrowingFees
:
function getTotalBorrowingFees(DataStore dataStore, address market, address longToken, address shortToken, bool isLong) internal view returns (uint256) {
uint256 openInterest = getOpenInterest(dataStore, market, longToken, shortToken, isLong);
uint256 cumulativeBorrowingFactor = getCumulativeBorrowingFactor(dataStore, market, isLong);
uint256 totalBorrowing = getTotalBorrowing(dataStore, market, isLong);
return openInterest * cumulativeBorrowingFactor - totalBorrowing;
}
calculates the fess by calling getCumulativeBorrowingFactor(...)
:
which is the wrong function to call because it returns a stale borrowing factor. To get the actual borrowing factor and calculate correctly the borrowing fees, GMX should call the getNextCumulativeBorrowingFactor
function:
Which makes the right calculation, taking into account the stale fees also:
uint256 durationInSeconds = getSecondsSinceCumulativeBorrowingFactorUpdated(dataStore, market.marketToken, isLong);
uint256 borrowingFactorPerSecond = getBorrowingFactorPerSecond(
dataStore,
market,
prices,
isLong
);
uint256 cumulativeBorrowingFactor = getCumulativeBorrowingFactor(dataStore, market.marketToken, isLong);
uint256 delta = durationInSeconds * borrowingFactorPerSecond;
uint256 nextCumulativeBorrowingFactor = cumulativeBorrowingFactor + delta;
return (nextCumulativeBorrowingFactor, delta);
Impact
Ass fee calculation will not be accurate, liquidity providers will be have a less-worth token because pending fees are not accounted in the pool's value
Code Snippet
Tool used
Manual Review
Recommendation
In order to mitigate the issue, call the function getNextCumulativeBorrowingFactor
instead of the function getCumulativeBorrowingFactor()
for a correct accounting and not getting stale fees