Skip to main content

Class: Token

Calculates to and from units for a token based on decimals

Hierarchy​

Properties​

decimals​

β€’ decimals: number

Number of decimals used by the token.

Inherited from​

TokenCalculations.decimals

Defined in​

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


displayedDecimals​

β€’ displayedDecimals: number

Inherited from​

TokenCalculations.displayedDecimals

Defined in​

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


contract​

β€’ contract: TestToken

Defined in​

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


id​

β€’ id: string

ID which should be unique within a network, but can be used across networks. Typically the id from the context-addresses package. May be the symbol if the symbol is unique. NB: This uniqueness is not enforced and duplicates will give undefined behavior.

Defined in​

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


address​

β€’ address: string

Address of the token contract.

Defined in​

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


symbol​

β€’ symbol: undefined | string

Non-unique and optional symbol cf. ERC20.

Defined in​

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


displayName​

β€’ displayName: undefined | string

Optional display name for the token.

Defined in​

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


displayedAsPriceDecimals​

β€’ displayedAsPriceDecimals: number

Number of decimals to display in the UI when showing a price.

Defined in​

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


mgv​

β€’ mgv: Mangrove

The Mangrove instance this token is associated with.

Defined in​

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

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

Inherited from​

TokenCalculations.fromUnits

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

Inherited from​

TokenCalculations.toUnits

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"));

Inherited from​

TokenCalculations.toFixed

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.

Inherited from​

TokenCalculations.round

Defined in​

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


createTokenFromSymbolOrId​

β–Έ createTokenFromSymbolOrId(symbolOrId, mgv, options?): Promise<Token>

Create a Token instance, fetching data (decimals) from chain if needed.

Parameters​

NameType
symbolOrIdstring
mgvMangrove
options?ConstructorOptions

Returns​

Promise<Token>

Defined in​

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


createTokenFromSymbol​

β–Έ createTokenFromSymbol(symbol, mgv, options?): Promise<Token>

Create a Token instance, fetching data (decimals) from chain if needed.

Parameters​

NameType
symbolstring
mgvMangrove
options?ConstructorOptions

Returns​

Promise<Token>

Defined in​

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


createTokenFromId​

β–Έ createTokenFromId(id, mgv, options?): Promise<Token>

Create a Token instance, fetching data (decimals) from chain if needed.

Parameters​

NameType
idstring
mgvMangrove
options?ConstructorOptions

Returns​

Promise<Token>

Defined in​

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


createTokenFromAddress​

β–Έ createTokenFromAddress(address, mgv): Promise<Token>

Parameters​

NameType
addressstring
mgvMangrove

Returns​

Promise<Token>

Defined in​

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


getTokenAddress​

β–Έ getTokenAddress(symbolOrId, network): string

Read a token address on the current network.

Note that this reads from the static Mangrove address registry which is shared across instances of this class.

Parameters​

NameType
symbolOrIdstring
networkstring

Returns​

string

Defined in​

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


allowance​

β–Έ allowance(params?): Promise<Big>

Return allowance of owner given to spender. If owner is not specified, defaults to current signer. If spender is not specified, defaults to Mangrove instance.

Parameters​

NameType
paramsObject
params.owner?string
params.spender?string

Returns​

Promise<Big>

Defined in​

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


allowanceInfinite​

β–Έ allowanceInfinite(params?): Promise<boolean>

Returns whether allowance of owner given to spender is high enough to be considered infinite (above 2^200) If owner is not specified, defaults to current signer. If spender is not specified, defaults to Mangrove instance.

Parameters​

NameType
paramsObject
params.owner?string
params.spender?string

Returns​

Promise<boolean>

Defined in​

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


approveMangrove​

β–Έ approveMangrove(arg?): Promise<ContractTransaction>

Set approval for Mangrove to amount.

Parameters​

NameType
argApproveArgs

Returns​

Promise<ContractTransaction>

Defined in​

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


approve​

β–Έ approve(spender, arg?): Promise<ContractTransaction>

Set approval for spender to amount.

Parameters​

NameType
spenderstring
argApproveArgs

Returns​

Promise<ContractTransaction>

Defined in​

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


approveIfNotInfinite​

β–Έ approveIfNotInfinite(spender, arg?): Promise<undefined | ContractTransaction>

Sets the allowance for the spender if it is not infinite. Cannot be used to reduce from infinite.

Parameters​

NameTypeDescription
spenderstringThe spender to approve
argApproveArgsThe approval arguments

Returns​

Promise<undefined | ContractTransaction>

Defined in​

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


approveIfHigher​

β–Έ approveIfHigher(spender, arg?): Promise<undefined | ContractTransaction>

Sets the allowance for the spender if it is not already enough.

Parameters​

NameTypeDescription
spenderstringThe spender to approve
argApproveArgsThe approval arguments

Returns​

Promise<undefined | ContractTransaction>

Defined in​

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


increaseApproval​

β–Έ increaseApproval(spender, arg?): Promise<undefined | ContractTransaction>

Increases the allowance for the spender unless it is already considered infinite (above 2^200).

Parameters​

NameTypeDescription
spenderstringThe spender to approve
argApproveArgsThe approval arguments

Returns​

Promise<undefined | ContractTransaction>

Defined in​

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


balanceOf​

β–Έ balanceOf(account, overrides?): Promise<Big>

Returns the balance of account.

Parameters​

NameType
accountstring
overridesOverrides

Returns​

Promise<Big>

Defined in​

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


transfer​

β–Έ transfer(to, value, overrides?): Promise<ContractTransaction>

Transfers value amount of tokens to address to

Parameters​

NameType
tostring
valueBigSource
overridesOverrides

Returns​

Promise<ContractTransaction>

Defined in​

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

  • Hierarchy
  • Properties
    • decimals
    • displayedDecimals
    • contract
    • id
    • address
    • symbol
    • displayName
    • displayedAsPriceDecimals
    • mgv
  • Methods
    • fromUnits
    • toUnits
    • toFixed
    • round
    • createTokenFromSymbolOrId
    • createTokenFromSymbol
    • createTokenFromId
    • createTokenFromAddress
    • getTokenAddress
    • allowance
    • allowanceInfinite
    • approveMangrove
    • approve
    • approveIfNotInfinite
    • approveIfHigher
    • increaseApproval
    • balanceOf
    • transfer