TwapOracle doesn’t calculate VADER:USDV exchange rate correctly
The TwapOracle contract incorrectly calculates the VADER:USDV exchange rate by using the token's decimals as a multiplier instead of a scaling factor. This could lead to incorrect minting of VADER tokens.
Originally disclosed on Code4rena
Submitted by TomFrenchBlockchain
Impact
Detailed description of the impact of this finding.
Proof of Concept
On L156 of TwapOracle we perform the calculation:
result = ((sumUSD * IERC20Metadata(token).decimals()) / sumNative);
This seems extremely odd as for an 18 decimal token we’re then calculating
result = ((sumUSD * 18) / sumNative);
This is just plain weird. I expect what was meant is to replace this line with the below so we’re properly scaling for token’s number of decimals.
uint256 scalingFactor = 10 ** IERC20Metadata(token).decimals()
result = (sumUSD * scalingFactor) / sumNative;
Marked as high severity as this exchange rate appears to be used in some form of minting mechanism and correctness of the oracle is listed as one of the key focuses of the audit.
Recommended Mitigation Steps
As above.
The TWAP oracle module has been completely removed and redesigned from scratch as LBTwap that is subject of the new audit.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the code used the token's decimal count directly as a multiplier rather than raising 10 to that power to create a proper scaling factor.
Impact
The incorrect exchange rate could lead to improper minting of VADER tokens, as the oracle's correctness is critical for the minting mechanism.
What the attacker needed
None
The fix
The TWAP oracle module was completely removed and redesigned from scratch as LBTwap.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A smart contract oracle is supposed to give the correct price between two tokens. Here, the code multiplied by the number of decimals (like 18) instead of by 10 to the power of decimals (like 10^18), so the price would be far too small. That wrong price could cause the system to mint the wrong amount of tokens.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.