@dfinity/utils
Version:
A collection of utilities and constants for NNS/SNS projects.
134 lines (133 loc) • 3.79 kB
TypeScript
import { FromStringToTokenError } from "../enums/token.enums";
/**
* Receives a string representing a number and returns the big int or error.
*
* @param amount - in string format
* @returns bigint | FromStringToTokenError
*/
export declare const convertStringToE8s: (value: string) => bigint | FromStringToTokenError;
export interface Token {
symbol: string;
name: string;
decimals: number;
logo?: string;
}
export declare const ICPToken: Token;
/**
* Deprecated. Use TokenAmountV2 instead which supports decimals !== 8.
*
* Represents an amount of tokens.
*
* @param e8s - The amount of tokens in bigint.
* @param token - The token type.
*/
export declare class TokenAmount {
protected e8s: bigint;
token: Token;
private constructor();
/**
* Initialize from a bigint. Bigint are considered e8s.
*
* @param {amount: bigint; token?: Token;} params
* @param {bigint} params.amount The amount in bigint format.
* @param {Token} params.token The token type.
*/
static fromE8s({ amount, token, }: {
amount: bigint;
token: Token;
}): TokenAmount;
/**
* Initialize from a string. Accepted formats:
*
* 1234567.8901
* 1'234'567.8901
* 1,234,567.8901
*
* @param {amount: string; token?: Token;} params
* @param {string} params.amount The amount in string format.
* @param {Token} params.token The token type.
*/
static fromString({ amount, token, }: {
amount: string;
token: Token;
}): TokenAmount | FromStringToTokenError;
/**
* Initialize from a number.
*
* 1 integer is considered E8S_PER_TOKEN
*
* @param {amount: number; token?: Token;} params
* @param {string} params.amount The amount in number format.
* @param {Token} params.token The token type.
*/
static fromNumber({ amount, token, }: {
amount: number;
token: Token;
}): TokenAmount;
/**
*
* @returns The amount of e8s.
*/
toE8s(): bigint;
}
/**
* Represents an amount of tokens.
*
* @param upls - The amount of tokens in units in the last place. If the token
* supports N decimals, 10^N ulp = 1 token.
* @param token - The token type.
*/
export declare class TokenAmountV2 {
protected ulps: bigint;
token: Token;
private constructor();
/**
* Initialize from a bigint. Bigint are considered ulps.
*
* @param {amount: bigint; token?: Token;} params
* @param {bigint} params.amount The amount in bigint format.
* @param {Token} params.token The token type.
*/
static fromUlps({ amount, token, }: {
amount: bigint;
token: Token;
}): TokenAmountV2;
/**
* Initialize from a string. Accepted formats:
*
* 1234567.8901
* 1'234'567.8901
* 1,234,567.8901
*
* @param {amount: string; token?: Token;} params
* @param {string} params.amount The amount in string format.
* @param {Token} params.token The token type.
*/
static fromString({ amount, token, }: {
amount: string;
token: Token;
}): TokenAmountV2 | FromStringToTokenError;
/**
* Initialize from a number.
*
* 1 integer is considered 10^{token.decimals} ulps
*
* @param {amount: number; token?: Token;} params
* @param {string} params.amount The amount in number format.
* @param {Token} params.token The token type.
*/
static fromNumber({ amount, token, }: {
amount: number;
token: Token;
}): TokenAmountV2;
/**
*
* @returns The amount of ulps.
*/
toUlps(): bigint;
/**
*
* @returns The amount of ulps in e8s precision
*/
toE8s(): bigint;
}