Skip to content
Business logic Audit finding Intermediate

Too few `ICHI` v2 farming reward tokens transferred to the user due to incorrect decimal precision

The burn function in the WIchiFarm contract calculates the user's share of ICHI v2 farming rewards using accumulator values with 9 decimals precision, but the ICHI v2 token uses 18 decimals. As a result, users receive substantially fewer ICHI v2 reward tokens than they are owed.

berndartmueller Follow with 0x52

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-02-blueberry-judging/issues/319

Found by

berndartmueller, 0x52

Summary

The burn function in the WIchiFarm contract transfers too few ICHI v2 farming reward tokens to the caller due to using 9 decimals instead of 18 decimals for the ICHI v2 token.

Vulnerability Detail

Closing an ICHI vault spell farming position burns the wrapped ICHI vault LP tokens (WIchiFarm ERC-1155 tokens). Farming rewards are harvested from the ICHI farm (see contract on Etherscan) and received as ICHI v1 tokens.

The ICHI v1 ERC-20 token uses 9 decimals (see token on Etherscan), whereas the ICHI v2 ERC-20 token uses 18 decimals (see token on Etherscan).

Those received ICHI v1 tokens are then converted to v2 tokens in line 134.

To calculate the user's share of eligible ICHI v2 reward tokens, the reward per share accumulator stIchiPerShare at the time of minting the WIchiFarm token and the current enIchiPerShare accumulator is used.

However, those accumulator values are in 9 decimals precision (please see the ichiFarmV2.harvest function for proof that pool.accIchiPerShare uses 9 decimals, otherwise the ICHI token transfer would fail due to inflated _pendingIchi). Given that amount is in 18 decimals, the calculation of stIchi and enIchi in lines 143 and 144 will result in a value with 9 decimals precision.

As previously mentioned, the ICHI v2 token uses 18 decimals. Therefore, too few ICHI v2 tokens are transferred.

Impact

Users will receive substantially fewer ICHI v2 farming reward tokens than expected.

Code Snippet

wrapper/WIchiFarm.sol#L143-L144

Solidity
116: function burn(uint256 id, uint256 amount)
117:     external
118:     nonReentrant
119:     returns (uint256)
120: {
121:     if (amount == type(uint256).max) {
122:         amount = balanceOf(msg.sender, id);
123:     }
124:     (uint256 pid, uint256 stIchiPerShare) = decodeId(id);
125:     _burn(msg.sender, id, amount);
126:
127:     uint256 ichiRewards = ichiFarm.pendingIchi(pid, address(this));
128:     ichiFarm.harvest(pid, address(this));
129:     ichiFarm.withdraw(pid, amount, address(this));
130:
131:     // Convert Legacy ICHI to ICHI v2
132:     if (ichiRewards > 0) {
133:         ICHIv1.safeApprove(address(ICHI), ichiRewards);
134:         ICHI.convertToV2(ichiRewards);
135:     }
136:
137:     // Transfer LP Tokens
138:     address lpToken = ichiFarm.lpToken(pid);
139:     IERC20Upgradeable(lpToken).safeTransfer(msg.sender, amount);
140:
141:     // Transfer Reward Tokens
142:     (uint256 enIchiPerShare, , ) = ichiFarm.poolInfo(pid);
143:     uint256 stIchi = (stIchiPerShare * amount).divCeil(1e18);
144:     uint256 enIchi = (enIchiPerShare * amount) / 1e18; // @audit-info `enIchi` and `stIchi` are in 9 decimal precision
145:
146:     if (enIchi > stIchi) {
147:         ICHI.safeTransfer(msg.sender, enIchi - stIchi);
148:     }
149:     return pid;
150: }

Tool used

Manual Review

Recommendation

Consider changing the denominator in lines 143 and 144 from 1e18 to 1e9 to use the required 18 decimals for the ICHI v2 token.

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

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 reward calculation uses a denominator of 1e18 while the accumulator values are in 9 decimals, causing the result to have only 9 decimals precision instead of the required 18 for the ICHI v2 token.

Impact

Users will receive substantially fewer ICHI v2 farming reward tokens than expected.

What the attacker needed

User has a farming position in an ICHI vault spell; User closes the position by burning WIchiFarm tokens

The fix

The denominator in lines 143 and 144 should be changed from 1e18 to 1e9 to account for the 18 decimals of the ICHI v2 token.

How it was found

  1. Close an ICHI vault spell farming position by calling burn on WIchiFarm.
  2. The contract harvests ICHI v1 rewards and converts them to ICHI v2.
  3. The contract calculates the user's share using stIchiPerShare and enIchiPerShare accumulators.
  4. The calculation uses a denominator of 1e18, producing a result with only 9 decimals precision.
  5. The contract transfers the incorrectly calculated amount of ICHI v2 tokens to the user.

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.

When you close a farming position, the contract calculates how many reward tokens you should get. It mistakenly treats the reward token as having fewer decimal places than it actually does, so the final amount sent to you is much smaller than it should be. This means you lose most of your earned rewards.

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.

More by berndartmueller

View all

97 more disclosed reports, best first.

More from Blueberry Update #3

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.