Skip to main content

Class: TokenCalculations

Calculates to and from units for a token based on decimals

Hierarchy​

  • TokenCalculations

    ↳ Token

Constructors​

constructor​

β€’ new TokenCalculations(decimals, displayedDecimals): TokenCalculations

Parameters​

NameTypeDescription
decimalsnumberNumber of decimals used by the token.
displayedDecimalsnumber-

Returns​

TokenCalculations

Defined in​

@mangrovedao/mangrove.js/src/token.ts:64

Properties​

decimals​

β€’ decimals: number

Number of decimals used by the token.

Defined in​

@mangrovedao/mangrove.js/src/token.ts:65


displayedDecimals​

β€’ displayedDecimals: number

Defined in​

@mangrovedao/mangrove.js/src/token.ts:66

Methods​

fromUnits​

β–Έ fromUnits(amount): Big

Convert base/quote from internal amount to public amount. Uses each token's decimals parameter.

Parameters​

NameType
amountstring | number | BigNumber

Returns​

Big

Example

const usdc = await mgv.token("USDC");
token.fromUnits("1e7") // 10
const dai = await mgv.token("DAI")
market.fromUnits("1e18") // 1

Defined in​

@mangrovedao/mangrove.js/src/token.ts:81


toUnits​

β–Έ toUnits(amount): BigNumber

Convert base/quote from public amount to internal contract amount. Uses each token's decimals parameter.

If bq is "base", will convert the base, the quote otherwise.

Parameters​

NameType
amountBigSource

Returns​

BigNumber

Example

const usdc = await mgv.token("USDC");
token.toUnits(10) // 10e7 as ethers.BigNumber
const dai = await mgv.token("DAI")
market.toUnits(1) // 1e18 as ethers.BigNumber

Defined in​

@mangrovedao/mangrove.js/src/token.ts:98


toFixed​

β–Έ toFixed(amount, decimals?): string

Convert human-readable amounts to a string with the given number of decimal places. Defaults to the token's decimals places.

Parameters​

NameType
amountBigSource
decimals?number

Returns​

string

Example

token.toFixed("10.123"); // "10.12"
token.toFixed(token.fromUnits("1e7"));

Defined in​

@mangrovedao/mangrove.js/src/token.ts:112


round​

β–Έ round(amount): Big

Rounds an amount according to the token's decimals.

Parameters​

NameTypeDescription
amountBigThe amount to round.

Returns​

Big

The rounded amount.

Defined in​

@mangrovedao/mangrove.js/src/token.ts:123