eulith-web3js
Version:
Official Eulith Typescript client library
165 lines (164 loc) • 6.37 kB
TypeScript
import BN from "bn";
import * as Eulith from "./index";
/**
* Convenience code for interacting with ERC20 tokens
*/
export declare module Tokens {
/**
* The Symbols enumeration lists the ERC20 symbol (property of the contract) values associated with
* some common ERC20 token contracts.
*/
enum Symbols {
USDT = "USDT",
BNB = "BNB",
USDC = "USDC",
USDCe = "USDC.e",
BUSD = "BUSD",
MATIC = "MATIC",
STETH = "stETH",
WETH = "WETH",
LDO = "LDO",
CRV = "CRV",
CVX = "CVX",
BAL = "BAL",
BADGER = "BADGER",
ONEINCH = "1INCH",
UNI = "UNI",
LINK = "LINK",
APE = "APE",
GMT = "GMT",
WBTC = "WBTC",
LUSD = "LUSD",
FRAX = "FRAX",
CBETH = "cbETH",
GALA = "GALA",
HEX = "HEX",
RPL = "RPL",
DYDX = "DYDX",
BONE = "BONE",
LOOKS = "LOOKS",
AGEUR = "agEUR",
OSQTH = "oSQTH",
WSTETH = "wstETH",
ALBT = "ALBT"
}
/**
* Fetches an ERC20 contract binding given a symbol and network (inferred from the provider)
*/
function getTokenContract({ provider, symbol, address }: {
provider: Eulith.Provider;
symbol?: Symbols;
address?: string;
}): Promise<Eulith.Contracts.ERC20TokenContract | Eulith.Contracts.WethTokenContract>;
/**
* Convenience code to manage the token values expressed in different ways
* Typically, token values can either be expressed in:
* 1. Floating point whole values (i.e. 1 == 1.0 USDC)
* 2. Integer values (i.e. 1e6 == 1.0 USDC)
*/
module Value {
interface ITokenValue {
/**
* Return the value as a native javascript number, in the units of this currency
*
* Note: this conversion to a javascript number can be lossy in precision
*/
get asFloat(): number;
/**
* Returns the token value in the native form as it goes over the wire to/from the ethereum network/contract.
*
* This is equivalent to asFundamentalHex
*/
get asWire(): string;
/**
* Returns the token value as an integral # float in the Fundamental (atomic) units i.e. 1000000 (fundamental) == 1.0 USDC (float)
*/
get asFundamentalFloat(): number;
/**
* Returns the token value as an integral # BN (old BigNumber API) in the Fundamental (atomic) units i.e. 1000000 (fundamental) == 1.0 USDC (float)
*/
get asFundamentalBN(): BN;
/**
* Returns the token value as an integral # BigInt (NEW BigNumber API) in the Fundamental (atomic) units i.e. 1000000 (fundamental) == 1.0 USDC (float)
*/
get asFundamentalBigInt(): bigint;
/**
* Returns the token value as a hex string in the fundamental (atomic) units i.e. 1000000 (fundamental) == 1.0 USDC (float)
*/
get asFundamentalHex(): string;
/**
* Return the symbol associated with this token value (from ERC20 symbol API)
*/
get symbol(): string;
/**
* Returns the number in natural (not atomic/fundamental) units, with the label of the symbol.
*
* Example:
* 23.4 WETH
* 18.45 USDC
*/
get asDisplayString(): string;
/**
* Returns a cloned value, but with the new value. newV type interpretation same as the
* value constructor (number intrinsic value, BigInt fundamental, and string fundamental)
*/
cloneWithNewValue(newV: number | string | bigint): ITokenValue;
}
/**
* An ETH token value is NOT an ERC20 compatible token value, but rather a value from the native blockchain.
*/
class ETH implements ITokenValue {
private readonly fundamentalValue_;
/**
* if v is a number, its interpreted in ether units
* if v is a string or bigint, its interpreted as being in fundamental units (wei).
*/
constructor(v: number | string | bigint);
/**
* Return the value as a native javascript number, in the units of this currency (ETH)
*/
get asFloat(): number;
get asWire(): string;
get asFundamentalFloat(): number;
get asFundamentalBN(): BN;
get asFundamentalHex(): string;
get asFundamentalBigInt(): bigint;
get asWeiBN(): BN;
get asWeiHex(): string;
get asWeiBigInt(): bigint;
get symbol(): string;
get asDisplayString(): string;
cloneWithNewValue(newV: number | string | bigint): ITokenValue;
}
/**
* Manages different ways to express the value of an ERC20 token
*
* This contains a number, and a level of precision, used when converting to the native form.
*/
class ERC20 implements ITokenValue {
private readonly fundamentalValue_;
private readonly toWireFactor_;
private readonly symbol_;
private readonly decimals_;
/**
* if v is a number, it's interpreted in ERC20 token units
* if v is a string | BigInt, it's interpreted as being in fundamental units.
*/
constructor({ v, decimals, contract, symbol }: {
v: number | string | bigint | ITokenValue;
decimals?: number;
symbol?: string;
contract?: Eulith.Contracts.ERC20TokenContract;
});
get asFloat(): number;
get asWire(): string;
get asFundamentalHex(): string;
get asFundamentalBN(): BN;
get asFundamentalBigInt(): bigint;
get asFundamentalFloat(): number;
get symbol(): string;
get asDisplayString(): string;
cloneWithNewValue(newV: number | string | bigint): ITokenValue;
}
}
}