lastFundingTime
is used in order to calculate the fundingFeePerUnit but it is not initialized to any value before it is used meaning it is going to be equal to 0 for the first user interacting with the contract which is going to lead to them receiving or paying a much larger funding fee.
When a user's order is executed by the keeper using SettlementBranch::fillMarketOrder
the fundingFeePerUnitX18
is calculated by calling PerpMarket::getNextFundingFeePerUnit
which after 2 more function calls, calls the function getProportionalElapsedSinceLastFunding
. If we take a look at the body of the function we can see it calculates block.timestamp - self.lastFundingTime
.
The problem lies in the fact that self.lastFundingTime
will not be initialized for the first transaction in a market when this function is called and it will be equal to 0, making the getProportionalElapsedSinceLastFunding
function return a number much bigger than it is supposed to.
The first time lastFundingTime
is set is in the next function call inside SettlementBranch::_fillOrder
which calls PermMarket::updateFunding
The first trade in a market will have its funding fee calculated wrongly which can lead to inaccurate funding fee transfers between users.
Manual review
VS Code
Call PermMarket::updateFunding
when a new market is being initialized
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.