Class: Token
Calculates to and from units for a token based on decimals
Hierarchyβ
β³
Token
Propertiesβ
decimalsβ
β’ decimals: number
Number of decimals used by the token.
Inherited fromβ
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β
Name | Type |
---|---|
amount | string | 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β
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β
Name | Type |
---|---|
amount | BigSource |
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β
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β
Name | Type |
---|---|
amount | BigSource |
decimals? | number |
Returnsβ
string
Example
token.toFixed("10.123"); // "10.12"
token.toFixed(token.fromUnits("1e7"));
Inherited fromβ
Defined inβ
@mangrovedao/mangrove.js/src/token.ts:112
roundβ
βΈ round(amount
): Big
Rounds an amount according to the token's decimals.
Parametersβ
Name | Type | Description |
---|---|---|
amount | Big | The amount to round. |
Returnsβ
Big
The rounded amount.
Inherited fromβ
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β
Name | Type |
---|---|
symbolOrId | string |
mgv | Mangrove |
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β
Name | Type |
---|---|
symbol | string |
mgv | Mangrove |
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β
Name | Type |
---|---|
id | string |
mgv | Mangrove |
options? | ConstructorOptions |
Returnsβ
Promise
<Token
>
Defined inβ
@mangrovedao/mangrove.js/src/token.ts:189
createTokenFromAddressβ
βΈ createTokenFromAddress(address
, mgv
): Promise
<Token
>
Parametersβ
Name | Type |
---|---|
address | string |
mgv | Mangrove |
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β
Name | Type |
---|---|
symbolOrId | string |
network | string |
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β
Name | Type |
---|---|
params | Object |
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β
Name | Type |
---|---|
params | Object |
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β
Name | Type |
---|---|
arg | ApproveArgs |
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β
Name | Type |
---|---|
spender | string |
arg | ApproveArgs |
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β
Name | Type | Description |
---|---|---|
spender | string | The spender to approve |
arg | ApproveArgs | The 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β
Name | Type | Description |
---|---|---|
spender | string | The spender to approve |
arg | ApproveArgs | The 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β
Name | Type | Description |
---|---|---|
spender | string | The spender to approve |
arg | ApproveArgs | The 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β
Name | Type |
---|---|
account | string |
overrides | Overrides |
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β
Name | Type |
---|---|
to | string |
value | BigSource |
overrides | Overrides |
Returnsβ
Promise
<ContractTransaction
>
Defined inβ
@mangrovedao/mangrove.js/src/token.ts:396