superfluid-finance / protocol-monorepo

Superfluid Protocol Monorepo: the specification, implementations, peripherals and development kits.
https://www.superfluid.finance
Other
875 stars 239 forks source link

Give `_fromUnderlyingAmount` a home #2030

Open hellwolf opened 1 month ago

hellwolf commented 1 month ago
    // this should be in SuperToken.sol
    uint8 private constant _STANDARD_DECIMALS = 18;

    function _fromUnderlyingAmount(ISuperToken superToken, uint256 amount)
        private
        view
        returns (uint256 superTokenAmount, uint256 adjustedAmount)
    {
        uint256 factor;
        uint8 _underlyingDecimals = superToken.getUnderlyingDecimals();
        if (_underlyingDecimals < _STANDARD_DECIMALS) {
            factor = 10 ** (_STANDARD_DECIMALS - _underlyingDecimals);
            superTokenAmount = amount * factor;
            adjustedAmount = amount;
        } else if (_underlyingDecimals > _STANDARD_DECIMALS) {
            factor = 10 ** (_underlyingDecimals - _STANDARD_DECIMALS);
            superTokenAmount = amount / factor;
            adjustedAmount = superTokenAmount * factor;
        } else {
            superTokenAmount = adjustedAmount = amount;
        }
    }
}