Skip to content
Audit finding Intermediate

Wrong calculation for yVault price per share if decimals != 18

The yVault contract's getPricePerFullShare function hardcodes 1e18 for decimal scaling, assuming the underlying token always has 18 decimals. For tokens with different decimals, such as USDC with 6, the price per share is calculated incorrectly, potentially causing users or the protocol to lose funds.

berndartmueller Follow

Originally disclosed on Code4rena

Submitted by berndartmueller

The yVault.getPricePerFullShare() function calculates the price per share by multiplying with 1e18 token decimals with the assumption that the underlying token always has 18 decimals. yVault has the same amount of decimals as it’s underlying token see (yVault.decimals())

But tokens don’t always have 1e18 decimals (e.g. USDC).

Impact

The price per share calculation does not return the correct price for underlying tokens that do not have 18 decimals. This could lead to paying out too little or too much and therefore to a loss for either the protocol or the user.

Proof of Concept

Following test will fail with the current implementation when the underlying vault token has 6 decimals:

NOTE: units() helper function was adapted to accept the desired decimals.

it.only("should mint the correct amount of tokens for tokens with 6 decimals", async () => {
  const DECIMALS = 6;

  await token.setDecimals(DECIMALS);
  expect(await yVault.decimals()).to.equal(DECIMALS);

  expect(await yVault.getPricePerFullShare()).to.equal(0);
  await token.mint(user1.address, units(1000, DECIMALS));
  await token.connect(user1).approve(yVault.address, units(1000, DECIMALS));

  await yVault.connect(user1).deposit(units(500, DECIMALS));
  expect(await yVault.balanceOf(user1.address)).to.equal(units(500, DECIMALS));

  await token.mint(strategy.address, units(500, DECIMALS));
  expect(await yVault.getPricePerFullShare()).to.equal(units(2, DECIMALS));
});

Fails with following error: AssertionError: Expected "2000000000000000000" to be equal 2000000

Recommended mitigation steps

Use vault decimals() instead of hardcoded 1e18 decimals.

function getPricePerFullShare() external view returns (uint256) {
    uint256 supply = totalSupply();
    if (supply == 0) return 0;
    return (balance() * (10**decimals())) / supply; // @audit-info use `decimals()` instead of hardcoded `1e18`
}

spaghettieth (JPEG’d) disputed and commented:

The yVault contract has been designed to work with Curve LPs, which have 18 decimals

LSDan (judge) decreased severity to Medium and commented:

I’m downgrading this to a medium risk but leaving it as valid. Any number of external factors could conspire to result in a non-18 decimal token being used in the future, at which point this code may have been forgotten. A better choice would be to do a decimal check.


The original report as published on Code4rena, by berndartmueller. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 hardcodes 1e18 instead of using the vault's actual decimals when scaling the price per share.

Impact

The incorrect price per share could lead to paying out too little or too much, causing a loss for either the protocol or the user.

What the attacker needed

Underlying token has non-18 decimals

The fix

The function should use the vault's decimals() instead of a hardcoded 1e18.

How it was found

  1. Set the token decimals to 6.
  2. Mint and approve tokens for the user.
  3. Deposit tokens into the vault.
  4. Mint additional tokens to the strategy address.
  5. Check the price per share and observe it is 2e18 instead of 2e6.

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 vault contract calculates how much one share is worth by multiplying the total balance by a fixed number that assumes the token has 18 decimal places. If the token actually has fewer decimals, like 6, the result is wrong by a huge factor. This could mean users get too little or too much when they withdraw.

More by berndartmueller

View all

97 more disclosed reports, best first.

More from JPEG'd contest

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.