UNPKG

eulith-web3js-core

Version:

Eulith core web3js SDK (code to access Eulith services via web3js)

150 lines (149 loc) 7.27 kB
import { Contract as Web3Contract } from "web3-eth-contract"; import { TransactionConfig } from "web3-core"; import * as Eulith from "./index"; import * as ImportedInterfaces from "./contract-interfaces"; /** * Code relating to smart contracts */ export declare module Contracts { /** * Contract wraps the native Web3js.eth.Contract object. By itself it provides little value in doing so, except * a few helpful wrappers (may add more soon - as of 2023-03-21). But subclasses of Eulith.Contract provide much more utility/value * added, over the Web3js.eth.Contract class */ class Contract { protected constructor(nativeContract: any); get native(): ImportedInterfaces.BaseContract; get options(): import("web3-eth-contract").Options; get address(): string; protected _contract: Web3Contract; } const Interfaces: typeof ImportedInterfaces; /** * Eulith.Contracts.ERC20TokenContract is a wrapper on an ERC20 ethereum token contract. * * See <https://medium.com/blockchannel/the-anatomy-of-erc20-c9e5c5ff1d02> for an overview of what this means, * and what methods are available. * * This wrapper class is designed to provide quick and easy access to most contract functionality, * but its less flexible than the underlying web3js API. * * Please use the .native or .nativeERC20 properties to access the full web3js contract functionality. */ class ERC20TokenContract extends Contract { /** * Construct an ERC20 contract (ERC20TokenContract) object from a provider, an address, and optionally pre-specify * the number of decimals, and symbol associated with that contract (as a performance optimization, else will be construed from * the contract via a web call). */ static mk({ provider, contractAddress, decimals, symbol }: { provider: Eulith.Provider; contractAddress: string; decimals?: number; symbol?: string; }): Promise<ERC20TokenContract>; /** * Typically use ERC20TokenContract::mk API (since its async) but you can use this API if you have a preconstructed * contract object, and know the # of decimals. */ protected constructor(provider: Eulith.Provider, nativeContract: any, decimals: number, symbol: string); get nativeERC20(): ImportedInterfaces.IERCDetailed; /** * Return the number of decimals of precision used in converting from this ERC20 contracts Tokens to * its wire (transmission/interchange) format. */ get decimals(): number; /** */ get provider(): Eulith.Provider; /** * This symbol associated with an ERC20 contract is handy to use in printed messages. */ get symbol(): string; /** * Clone the contract object, but using the argument provider. Can be used with atomicTx, for example, * or just to change providers with a contract. Use this instead of setter on provider, since immutable behavior * easier to reason about. */ withProvider(provider: Eulith.Provider): ERC20TokenContract; /** * @todo add overload taking string and ITokenValue * Argument 'v' - is a number (either in float, or wire-format) - interpretted as being in the native contract * units. Its converted to an object that knows this, and the native number of decimals, so it can be rendeded properly * automatically in wire format. * * if v is a number, its interpretted in ERC20 token units * if v is a string, its interpretted as being in fundamental units. */ asTokenValue(v: number | string): Eulith.Tokens.Value.ERC20; /** * The owner of the contract authorizes, or approves, the given spender to withdraw * instances of the token from the (contract) owner’s address. * * WARNING: ERC20TokenContract.approval does NOT initiate the actual approval. It must be chained together with * signAndSendAndWait (or otherwise signed and sent along). */ approve(spender: string, amount: Eulith.Tokens.Value.ITokenValue, txOpts?: Partial<TransactionConfig>): Eulith.Signing.UnsignedTransaction; /** * WARNING: ERC20TokenContract.transfer does NOT initiate the actual transfer. It must be chained together with * signAndSendAndWait (or otherwise signed and sent along). */ transfer(to: string, amount: Eulith.Tokens.Value.ITokenValue, txOpts?: Partial<TransactionConfig>): Eulith.Signing.UnsignedTransaction; /** */ transferFrom(from: string, to: string, amount: Eulith.Tokens.Value.ITokenValue, txOpts?: Partial<TransactionConfig>): Eulith.Signing.UnsignedTransaction; balanceOf(address: string): Promise<Eulith.Tokens.Value.ERC20>; allowance(owner: string, spender: string): Promise<Eulith.Tokens.Value.ERC20>; private provider_?; private decimals_; private symbol_; } /** * */ class WethTokenContract extends ERC20TokenContract { /** */ static mk({ provider, contractAddress }: { provider: Eulith.Provider; contractAddress: string; }): Promise<WethTokenContract>; constructor(provider: Eulith.Provider, nativeContract: any); /** */ get nativeWETH(): ImportedInterfaces.WETHInterface; /** * Clone the contract object, but using the argument provider. Can be used with atomicTx */ withProvider(provider: Eulith.Provider): WethTokenContract; balanceOf(address: string): Promise<Eulith.Tokens.Value.ERC20>; /** * This transfers v ETH from the ETH blockchain to the WETH account. * * WARNING: WethTokenContract.deposit does NOT initiate the actual deposit. It must be chained together with * signAndSendAndWait (or otherwise signed and sent along). * * Example Usage: * ~~~ * await wethContract.deposit(new Eulith.Tokens.Value.ETH(1.0), { * from: acct.address, * }).signAndSendAndWait (acct, provider); * ~~~ */ deposit(v: Eulith.Tokens.Value.ETH, txOpts?: Partial<TransactionConfig>): Eulith.Signing.UnsignedTransaction; /** * This transfers v ETH from the WETH account to the ETH blockchain. * * WARNING: WethTokenContract.withdraw does NOT initiate the actual withdrawal. It must be chained together with * signAndSendAndWait (or otherwise signed and sent along). * * Example Usage: * ~~~ * await wethContract.withdraw(new Eulith.Tokens.Value.ETH(1.0), { * from: acct.address, * }).signAndSendAndWait(acct, provider); * ~~~ */ withdraw(v: Eulith.Tokens.Value.ETH, txOpts?: Partial<TransactionConfig>): Eulith.Signing.UnsignedTransaction; } }