Skip to main content

DecimalsConverter

Overview​

License: MIT​

library DecimalsConverter

This library is used to convert numbers that use token's N decimals to M decimals. Comes extremely handy with standardizing the business logic that is intended to work with many different ERC20 tokens that have different precision (decimals). One can perform calculations with 18 decimals only and resort to conversion only when the payouts (or interactions) with the actual tokens have to be made.

The best usage scenario involves accepting and calculating values with 18 decimals throughout the project, despite the tokens decimals.

Also it is recommended to call round18() function on the first execution line in order to get rid of the trailing numbers if the destination decimals are less than 18

IMPORTANT Users are requested to use from18Safe() instead of from18() by default to avoid "small amount exploits".

Usage example:​

contract Taker {
ERC20 public USDC;
uint256 public paid;

. . .

function pay(uint256 amount) external {
amount = amount.round18(address(USDC));

paid += amount;
USDC.transferFrom(msg.sender, address(this), amount.from18(address(USDC)));
}
}

Errors info​

ConversionFailed​

error ConversionFailed()

Functions info​

decimals​

function decimals(address token_) internal view returns (uint8)

The function to get the decimals of ERC20 token. Needed for bytecode optimization

Parameters:

NameTypeDescription
token_addressthe ERC20 token

Return values:

NameTypeDescription
[0]uint8the decimals of provided token

to18​

function to18(uint256 amount_, address token_) internal view returns (uint256)

The function to bring the number to 18 decimals of precision

Parameters:

NameTypeDescription
amount_uint256the number to convert
token_addressthe token, whose decimals will be precised to 18

Return values:

NameTypeDescription
[0]uint256the number brought to 18 decimals of precision

to18​

function to18(
uint256 amount_,
uint256 baseDecimals_
) internal pure returns (uint256)

The function to bring the number to 18 decimals of precision

Parameters:

NameTypeDescription
amount_uint256the number to convert
baseDecimals_uint256the current precision of the number

Return values:

NameTypeDescription
[0]uint256the number brought to 18 decimals of precision

to18Safe​

function to18Safe(
uint256 amount_,
address token_
) internal view returns (uint256)

The function to bring the number to 18 decimals of precision. Reverts if output is zero

Parameters:

NameTypeDescription
amount_uint256the number to convert
token_addressthe token, whose decimals will be precised to 18

Return values:

NameTypeDescription
[0]uint256the number brought to 18 decimals of precision

to18Safe​

function to18Safe(
uint256 amount_,
uint256 baseDecimals_
) internal pure returns (uint256)

The function to bring the number to 18 decimals of precision. Reverts if output is zero

Parameters:

NameTypeDescription
amount_uint256the number to convert
baseDecimals_uint256the current precision of the number

Return values:

NameTypeDescription
[0]uint256the number brought to 18 decimals of precision

from18​

function from18(
uint256 amount_,
address token_
) internal view returns (uint256)

The function to bring the number from 18 decimals to the desired decimals of precision

Parameters:

NameTypeDescription
amount_uint256the number to covert
token_addressthe token, whose decimals will be used as desired decimals of precision

Return values:

NameTypeDescription
[0]uint256the number brought from 18 to desired decimals of precision

from18​

function from18(
uint256 amount_,
uint256 destDecimals_
) internal pure returns (uint256)

The function to bring the number from 18 decimals to the desired decimals of precision

Parameters:

NameTypeDescription
amount_uint256the number to covert
destDecimals_uint256the desired precision decimals

Return values:

NameTypeDescription
[0]uint256the number brought from 18 to desired decimals of precision

from18Safe​

function from18Safe(
uint256 amount_,
address token_
) internal view returns (uint256)

The function to bring the number from 18 decimals to the desired decimals of precision. Reverts if output is zero

Parameters:

NameTypeDescription
amount_uint256the number to covert
token_addressthe token, whose decimals will be used as desired decimals of precision

Return values:

NameTypeDescription
[0]uint256the number brought from 18 to desired decimals of precision

from18Safe​

function from18Safe(
uint256 amount_,
uint256 destDecimals_
) internal pure returns (uint256)

The function to bring the number from 18 decimals to the desired decimals of precision. Reverts if output is zero

Parameters:

NameTypeDescription
amount_uint256the number to covert
destDecimals_uint256the desired precision decimals

Return values:

NameTypeDescription
[0]uint256the number brought from 18 to desired decimals of precision

round18​

function round18(
uint256 amount_,
address token_
) internal view returns (uint256)

The function to substitute the trailing digits of a number with zeros

Parameters:

NameTypeDescription
amount_uint256the number to round. Should be with 18 precision decimals
token_addressthe token, whose decimals will be used as desired decimals of precision

Return values:

NameTypeDescription
[0]uint256the rounded number. Comes with 18 precision decimals

round18​

function round18(
uint256 amount_,
uint256 decimals_
) internal pure returns (uint256)

The function to substitute the trailing digits of a number with zeros

Parameters:

NameTypeDescription
amount_uint256the number to round. Should be with 18 precision decimals
decimals_uint256the required number precision

Return values:

NameTypeDescription
[0]uint256the rounded number. Comes with 18 precision decimals

round18Safe​

function round18Safe(
uint256 amount_,
address token_
) internal view returns (uint256)

The function to substitute the trailing digits of a number with zeros. Reverts if output is zero

Parameters:

NameTypeDescription
amount_uint256the number to round. Should be with 18 precision decimals
token_addressthe token, whose decimals will be used as desired decimals of precision

Return values:

NameTypeDescription
[0]uint256the rounded number. Comes with 18 precision decimals

round18Safe​

function round18Safe(
uint256 amount_,
uint256 decimals_
) internal pure returns (uint256)

The function to substitute the trailing digits of a number with zeros. Reverts if output is zero

Parameters:

NameTypeDescription
amount_uint256the number to round. Should be with 18 precision decimals
decimals_uint256the required number precision

Return values:

NameTypeDescription
[0]uint256the rounded number. Comes with 18 precision decimals

convert​

function convert(
uint256 amount_,
address baseToken_,
address destToken_
) internal view returns (uint256)

The function to do the token precision conversion

Parameters:

NameTypeDescription
amount_uint256the amount to convert
baseToken_addresscurrent token
destToken_addressdesired token

Return values:

NameTypeDescription
[0]uint256the converted number

convert​

function convert(
uint256 amount_,
uint256 baseDecimals_,
uint256 destDecimals_
) internal pure returns (uint256)

The function to do the precision conversion

Parameters:

NameTypeDescription
amount_uint256the amount to covert
baseDecimals_uint256current number precision
destDecimals_uint256desired number precision

Return values:

NameTypeDescription
[0]uint256the converted number