LP for v3 pool of underlying tokens with decimals != 18 would have incorrect NFT metadata
The VirtualToken contract hardcodes 18 decimals even when the underlying token (e.g., USDC) has 6 decimals. This causes the Uniswap v3 position NFT metadata to display an incorrect pool price for liquidity providers.
prapandey031 Follow with Agontuk, bumbleb33 and 4 others
Originally disclosed on Code4rena
Submitted by prapandey031, also found by Agontuk, bumbleb33, Coldless, Daniel526, MrPotatoMagic, and TenderBeastJr
https://github.com/code-423n4/2024-12-lambowin/blob/main/src/VirtualToken.sol#L10
The VirtualToken.sol has a hardcoded decimal value of 18 even if the underlying token has a decimal value of 6 (for eg, USDC). This would not let the liquidity providers of the (vUSDC, USDC) uniswap v3 pool to get the correct metadata for their NFT liquidity position.
Proof of Concept
Below is a step-by-step PoC to explain the issue in detail.
The VirtualToken.sol is an ERC-20 token contract that has hardcoded 18 decimals:
function decimals() public view virtual returns (uint8) {
return 18;
}
Let’s say vUSDC is the virtual token contract with the underlying token as USDC. USDC has 6 decimals.
Now, as per the protocol, there would be a (vUSDC, USDC) uniswap v3 pool with 1:1 peg so that users could make swap and liquidity providers could provide (vUSDC, USDC) liquidity to the v3 pool.
Let’s say a user X has provided liquidity to the (vUSDC, USDC) v3 pool. He would hold a liquidity position NFT:
https://github.com/Uniswap/v3-periphery/blob/main/contracts/NonfungiblePositionManager.sol#L128
function mint(MintParams calldata params)
external
payable
override
checkDeadline(params.deadline)
returns (
uint256 tokenId,
uint128 liquidity,
uint256 amount0,
uint256 amount1
)
{
IUniswapV3Pool pool;
(liquidity, amount0, amount1, pool) = addLiquidity(
AddLiquidityParams({
token0: params.token0,
token1: params.token1,
fee: params.fee,
recipient: address(this),
tickLower: params.tickLower,
tickUpper: params.tickUpper,
amount0Desired: params.amount0Desired,
amount1Desired: params.amount1Desired,
amount0Min: params.amount0Min,
amount1Min: params.amount1Min
})
);
_mint(params.recipient, (tokenId = _nextId++));
bytes32 positionKey = PositionKey.compute(address(this), params.tickLower, params.tickUpper);
(, uint256 feeGrowthInside0LastX128, uint256 feeGrowthInside1LastX128, , ) = pool.positions(positionKey);
// idempotent set
uint80 poolId =
cachePoolKey(
address(pool),
PoolAddress.PoolKey({token0: params.token0, token1: params.token1, fee: params.fee})
);
_positions[tokenId] = Position({
nonce: 0,
operator: address(0),
poolId: poolId,
tickLower: params.tickLower,
tickUpper: params.tickUpper,
liquidity: liquidity,
feeGrowthInside0LastX128: feeGrowthInside0LastX128,
feeGrowthInside1LastX128: feeGrowthInside1LastX128,
tokensOwed0: 0,
tokensOwed1: 0
});
emit IncreaseLiquidity(tokenId, liquidity, amount0, amount1);
}
The user X could do any transaction with this NFT as he would do with a normal NFT: transfer to someone else, sell on the Uniswap v3 NFT marketplace, etc.
However, when the marketplace would call tokenURI(uint256 tokenId) for X’s NFT metadata, wrong value of v3 pool price would be sent. Let’s see the flow.
The intended v3 pool price should be near about 1 because of the peg. But when tokenURI(uint256 tokenId) is called:
https://github.com/Uniswap/v3-periphery/blob/main/contracts/NonfungiblePositionManager.sol#L189
function tokenURI(uint256 tokenId) public view override(ERC721, IERC721Metadata) returns (string memory) {
require(_exists(tokenId));
return INonfungibleTokenPositionDescriptor(_tokenDescriptor).tokenURI(this, tokenId);
}
It calls the tokenURI(INonfungiblePositionManager positionManager, uint256 tokenId) function in NonfungibleTokenPositionDescriptor.sol:
function tokenURI(INonfungiblePositionManager positionManager, uint256 tokenId)
external
view
override
returns (string memory)
{
(, , address token0, address token1, uint24 fee, int24 tickLower, int24 tickUpper, , , , , ) =
positionManager.positions(tokenId);
IUniswapV3Pool pool =
IUniswapV3Pool(
PoolAddress.computeAddress(
positionManager.factory(),
PoolAddress.PoolKey({token0: token0, token1: token1, fee: fee})
)
);
bool _flipRatio = flipRatio(token0, token1, ChainId.get());
address quoteTokenAddress = !_flipRatio ? token1 : token0;
address baseTokenAddress = !_flipRatio ? token0 : token1;
(, int24 tick, , , , , ) = pool.slot0();
return
> NFTDescriptor.constructTokenURI(
NFTDescriptor.ConstructTokenURIParams({
tokenId: tokenId,
quoteTokenAddress: quoteTokenAddress,
baseTokenAddress: baseTokenAddress,
quoteTokenSymbol: quoteTokenAddress == WETH9
? nativeCurrencyLabel()
: SafeERC20Namer.tokenSymbol(quoteTokenAddress),
baseTokenSymbol: baseTokenAddress == WETH9
? nativeCurrencyLabel()
: SafeERC20Namer.tokenSymbol(baseTokenAddress),
quoteTokenDecimals: IERC20Metadata(quoteTokenAddress).decimals(),
baseTokenDecimals: IERC20Metadata(baseTokenAddress).decimals(),
flipRatio: _flipRatio,
tickLower: tickLower,
tickUpper: tickUpper,
tickCurrent: tick,
tickSpacing: pool.tickSpacing(),
fee: fee,
poolAddress: address(pool)
})
);
}
This calls the constructTokenURI(ConstructTokenURIParams memory params) function:
https://github.com/Uniswap/v3-periphery/blob/main/contracts/libraries/NFTDescriptor.sol#L44
function constructTokenURI(ConstructTokenURIParams memory params) public pure returns (string memory) {
string memory name = generateName(params, feeToPercentString(params.fee));
string memory descriptionPartOne =
generateDescriptionPartOne(
escapeQuotes(params.quoteTokenSymbol),
escapeQuotes(params.baseTokenSymbol),
addressToString(params.poolAddress)
);
string memory descriptionPartTwo =
generateDescriptionPartTwo(
params.tokenId.toString(),
escapeQuotes(params.baseTokenSymbol),
addressToString(params.quoteTokenAddress),
addressToString(params.baseTokenAddress),
feeToPercentString(params.fee)
);
string memory image = Base64.encode(bytes(generateSVGImage(params)));
return
string(
abi.encodePacked(
'data:application/json;base64,',
Base64.encode(
bytes(
abi.encodePacked(
'{"name":"',
name,
'", "description":"',
descriptionPartOne,
descriptionPartTwo,
'", "image": "',
'data:image/svg+xml;base64,',
image,
'"}'
)
)
)
)
);
}
This calls the generateName(ConstructTokenURIParams memory params, string memory feeTier) function:
https://github.com/Uniswap/v3-periphery/blob/main/contracts/libraries/NFTDescriptor.sol#L155
function generateName(ConstructTokenURIParams memory params, string memory feeTier)
private
pure
returns (string memory)
{
return
string(
abi.encodePacked(
'Uniswap - ',
feeTier,
' - ',
escapeQuotes(params.quoteTokenSymbol),
'/',
escapeQuotes(params.baseTokenSymbol),
' - ',
tickToDecimalString(
!params.flipRatio ? params.tickLower : params.tickUpper,
params.tickSpacing,
params.baseTokenDecimals,
params.quoteTokenDecimals,
params.flipRatio
),
'<>',
tickToDecimalString(
!params.flipRatio ? params.tickUpper : params.tickLower,
params.tickSpacing,
params.baseTokenDecimals,
params.quoteTokenDecimals,
params.flipRatio
)
)
);
}
Ultimately, in the call-chain adjustForDecimalPrecision() function is called:
function adjustForDecimalPrecision(
uint160 sqrtRatioX96,
uint8 baseTokenDecimals,
uint8 quoteTokenDecimals
) private pure returns (uint256 adjustedSqrtRatioX96) {
uint256 difference = abs(int256(baseTokenDecimals).sub(int256(quoteTokenDecimals)));
if (difference > 0 && difference <= 18) {
if (baseTokenDecimals > quoteTokenDecimals) {
> adjustedSqrtRatioX96 = sqrtRatioX96.mul(10**(difference.div(2)));
if (difference % 2 == 1) {
adjustedSqrtRatioX96 = FullMath.mulDiv(adjustedSqrtRatioX96, sqrt10X128, 1 << 128);
}
} else {
> adjustedSqrtRatioX96 = sqrtRatioX96.div(10**(difference.div(2)));
if (difference % 2 == 1) {
adjustedSqrtRatioX96 = FullMath.mulDiv(adjustedSqrtRatioX96, 1 << 128, sqrt10X128);
}
}
} else {
adjustedSqrtRatioX96 = uint256(sqrtRatioX96);
}
}
And the sqrtRatioX96 value (which would be around 1 as the USDC and vUSDC token amounts in the v3 pool are pegged) is adjusted with the decimal difference (which is 18-6=12 in this case); it would become either (10^12) or (1/(10^12)).
Thus, X would have his liquidity position NFT with incorrect or unintended v3 pool price data; X would want the v3 pool price data to be 1 but it would be something else.
Severity
Impact: Correct NFT metadata is important for NFT marketplaces. Therefore the NFT functionality stands broken. The imapct is Medium.
Likelihood: The likelihood is High.
Therefore, the severity is Medium.
Recommended mitigation steps
It is recommended to use the underlying token’s decimals in VirtualToken.sol.
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 VirtualToken contract inherits a default decimals() of 18 instead of overriding it to match the underlying token's decimals.
Impact
The NFT metadata shows an incorrect pool price, so the NFT functionality is broken for marketplaces; the report rates this as Medium severity.
What the attacker needed
Underlying token has decimals other than 18; a Uniswap v3 pool exists for the virtual token; user provides liquidity to that pool
The fix
The VirtualToken contract should use the underlying token's decimals instead of a hardcoded 18.
How it was found
- A user provides liquidity to a (vUSDC, USDC) Uniswap v3 pool and receives a position NFT.
- The marketplace calls tokenURI on the NFT to fetch its metadata.
- The call chain reaches adjustForDecimalPrecision with baseTokenDecimals=18 and quoteTokenDecimals=6.
- The sqrt price is multiplied or divided by 10^12, producing a price far from the intended 1:1 peg.
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 virtual token is a stand-in for a real token, and it should copy the real token's number of decimal places. Here it always says 18 decimals, so when a Uniswap v3 pool shows the price for a liquidity position, the maths is wrong and the NFT shows a misleading price.
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.