Yearn token <> shares conversion decimal issue
The YearnYield strategy incorrectly converts shares to tokens by dividing by 1e18 instead of the vault's decimal precision. For tokens with non-18 decimals, this causes incorrect payouts, leading to losses for either the protocol or the user.
Submitted by cmichel
The yearn strategy YearnYield converts shares to tokens by doing pricePerFullShare * shares / 1e18:
function getTokensForShares(uint256 shares, address asset) public view override returns (uint256 amount) {
if (shares == 0) return 0;
// @audit should divided by vaultDecimals
amount = IyVault(liquidityToken[asset]).getPricePerFullShare().mul(shares).div(1e18);
}
But Yearn’s getPricePerFullShare seems to be in vault.decimals() precision, i.e., it should convert it as pricePerFullShare * shares / (10 ** vault.decimals()). The vault decimals are the same as the underlying token decimals
Impact
The token and shares conversions do not work correctly for underlying tokens that do not have 18 decimals. Too much or too little might be paid out leading to a loss for either the protocol or user.
Recommended Mitigation Steps
Divide by 10**vault.decimals() instead of 1e18 in getTokensForShares. Apply a similar fix in getSharesForTokens.
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
The code divides by a fixed 1e18 instead of the vault's actual decimal precision, which varies per token.
Impact
For tokens with non-18 decimals, conversions are incorrect, potentially causing financial loss to the protocol or user.
What the attacker needed
Underlying token has non-18 decimals
The fix
The conversion should divide by 10 raised to the vault's decimals instead of 1e18, and the same fix should be applied to getSharesForTokens.
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.
This is a smart contract bug where a calculation uses the wrong number for scaling. Tokens can have different numbers of decimals (like 6 or 8), and the code assumed 18. So when converting between shares and tokens, the result is wrong, and someone could get too much or too little money.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.