57931 sc insight consumes more gas than intended in getstandardizedprice function in helper library
Previous57921 sc insight whitelisted role cannot be revoked in nft cairo Next57437 sc medium front running in factory produce
Was this helpful?
Was this helpful?
Was this helpful?
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {MetadataReaderLib} from "solady/src/utils/MetadataReaderLib.sol";
import {FixedPointMathLib} from "solady/src/utils/FixedPointMathLib.sol";
contract getter {
/// @dev Used for precise calculations.
using FixedPointMathLib for uint256;
/// @dev Used for metadata reading (e.g., token decimals).
using MetadataReaderLib for address;
uint256 public constant BPS = 10 ** 27;
function getStandardizedPrice(address token, address tokenPriceFeed, uint256 amount, uint256 maxPriceFeedDelay)
external
pure
returns (uint256 priceAmount)
{
(uint256 tokenPriceInUsd, uint8 pfDecimals) = getPrice(tokenPriceFeed, maxPriceFeedDelay);
// (amount * price) / 10^priceFeedDecimals
uint256 usdValue = amount.fullMulDiv(tokenPriceInUsd, 10 ** pfDecimals);
// Standardize the USD value to 27 decimals
priceAmount = _standardize(18, usdValue); //@audit-issue [Insight] use internal function
}
function standardize(address, uint256 amount) public pure returns (uint256) {
return _standardize(18, amount);
}
function _standardize(uint8 decimals, uint256 amount) private pure returns (uint256) {
return amount.fullMulDiv(BPS, 10 ** decimals);
}
function getPrice(address, uint256)
public
pure
returns (uint256 price, uint8 decimals)
{
return (100000000, 8); // indicates $1 (for token/USD - the price is in 8 decimals), 18 decimals
}
}