Options
All
  • Public
  • Public/Protected
  • All
Menu

Class W3

Strongly-types wrapper over web3.js with additional helper methods.

Hierarchy

  • W3

Index

Type aliases

Static ABIDataTypes

ABIDataTypes: "uint256" | "boolean" | "string" | "bytes" | string

Static BlockType

BlockType: "latest" | "pending" | "genesis" | number

Static Callback

Callback: function

Type declaration

    • (error: Error, result: T): void
    • Parameters

      • error: Error
      • result: T

      Returns void

Static PromiEventType

PromiEventType: "transactionHash" | "receipt" | "confirmation" | "error"

Static SyncingResult

SyncingResult: false | SyncingState

Static Unit

Unit: "kwei" | "femtoether" | "babbage" | "mwei" | "picoether" | "lovelace" | "qwei" | "nanoether" | "shannon" | "microether" | "szabo" | "nano" | "micro" | "milliether" | "finney" | "milli" | "ether" | "kether" | "grand" | "mether" | "gether" | "tether"

Static address

address: string

Type alias for Ethereum address.

Static bytes

bytes: string

Type alias for bytes string.

Constructors

constructor

  • Create a default Web3 instance - resolves to a global window['web3'] injected my MIST, MetaMask, etc or to localhost:8545 if not running on https.

    Returns W3

  • Create a W3 instance with a given provider.

    Parameters

    • Optional provider: Provider

      web3.js provider.

    Returns W3

Properties

web3

web3: any

web3.js untyped instance created with a resolved or given in ctor provider, if any.

Static EthUtils

EthUtils: EthUtils = require('ethereumjs-util')

Ethereumjs-util package.

Static Utf8

Utf8: any = require('utf8')

Utf8 package.

Static _keythereum

_keythereum: any

Static providers

providers: Providers = Web3JS.providers

Web3 providers.

Static zeroAddress

zeroAddress: string = "0x0000000000000000000000000000000000000000"

Hex zero address.

Accessors

accounts

  • get accounts(): Promise<string[]>
  • Returns a list of accounts the node controlst

    Returns Promise<string[]>

blockNumber

  • get blockNumber(): Promise<number>
  • Returns the current block number

    Returns Promise<number>

currentProvider

defaultAccount

  • get defaultAccount(): string
  • set defaultAccount(defaultAccount: string): void
  • Default account for sending transactions without explicit txParams.

    Returns string

  • Default account for sending transactions without explicit txParams.

    Parameters

    • defaultAccount: string

    Returns void

isMetaMask

  • get isMetaMask(): boolean
  • True if current web3 provider is from MetaMask.

    Returns boolean

isPre1API

  • get isPre1API(): boolean
  • True if current web3.js version is 0.20.x.

    Returns boolean

isTestRPC

  • get isTestRPC(): Promise<boolean>
  • True if current node name contains TestRPC string

    Returns Promise<boolean>

latestTime

  • get latestTime(): Promise<number>
  • Returns the time of the last mined block in seconds.

    Returns Promise<number>

networkId

  • get networkId(): Promise<string>
  • Get network ID.

    Returns Promise<string>

web3API

  • get web3API(): string
  • Web3.js version API

    Returns string

Static default

  • get default(): W3
  • set default(w3: W3): void
  • Default W3 instance that is used as a fallback when such an instance is not provided to a constructor. You must set it explicitly via W3.default setter. Use an empty new W3() constructor to get an instance that automatically resolves to a global web3 instance window['web3'] provided by e.g. MIST/Metamask or connects to the default 8545 port if no global instance is present.

    Returns W3

  • Set default W3 instance.

    Parameters

    Returns void

Methods

ethSignRaw

  • ethSignRaw(hex: string, account: string): Promise<string>
  • Sign using eth.sign but with prefix as personal_sign

    Parameters

    • hex: string
    • account: string

    Returns Promise<string>

fromDecimal

  • fromDecimal(value: number | string): string
  • Converts a number or number string to its HEX representation.

    Parameters

    • value: number | string

    Returns string

getBalance

  • getBalance(address: string): Promise<BigNumber>
  • Get account balance

    Parameters

    • address: string

    Returns Promise<BigNumber>

getTransactionCount

  • getTransactionCount(account?: undefined | string, defaultBlock?: number | string): Promise<number>
  • Get the numbers of transactions sent from this address.

    Parameters

    • Optional account: undefined | string

      The address to get the numbers of transactions from.

    • Optional defaultBlock: number | string

      (optional) If you pass this parameter it will not use the default block set with.

    Returns Promise<number>

sendRPC

sendSignedTransaction

  • sendSignedTransaction(to: string, privateKey: string, data?: undefined | string, txParams?: TxParams, nonce?: undefined | number): Promise<string>
  • Sends a raw signed transaction and returns tx hash. Use waitTransactionReceipt method on w3 or a contract to get a tx receipt.

    Parameters

    • to: string

      Target contract address or zero address (W3.zeroAddress) to deploy a new contract.

    • privateKey: string

      Private key hex string prefixed with 0x.

    • Optional data: undefined | string

      Payload data.

    • Optional txParams: TxParams

      Tx parameters.

    • Optional nonce: undefined | number

      Nonce override if needed to replace a pending transaction.

    Returns Promise<string>

setProvider

  • Set web3 provider and update network info asynchronously.

    Parameters

    Returns void

sign

  • sign(message: string, account: string, password?: undefined | string): Promise<string>
  • Sign a message

    Parameters

    • message: string
    • account: string
    • Optional password: undefined | string

    Returns Promise<string>

signRaw

  • signRaw(message: any, account: string, password?: undefined | string): Promise<string>
  • Message already as hex

    Parameters

    • message: any
    • account: string
    • Optional password: undefined | string

    Returns Promise<string>

toBigNumber

  • toBigNumber(value: number | string): BigNumber
  • Convert number or hex string to BigNumber

    Parameters

    • value: number | string

    Returns BigNumber

unlockAccount

  • unlockAccount(address: string, password: string, duration?: undefined | number): Promise<boolean>
  • Async unlock while web3.js only has sync version. This function uses personal_unlockAccount RPC message that is not available in some web3 providers.

    Parameters

    • address: string
    • password: string
    • Optional duration: undefined | number

    Returns Promise<boolean>

waitTransactionReceipt

  • waitTransactionReceipt(hashString: string, timeoutSeconds?: undefined | number): Promise<TransactionReceipt>
  • Returns the receipt of a transaction by transaction hash. Retries for up to 240 seconds.

    Parameters

    • hashString: string

      The transaction hash.

    • Optional timeoutSeconds: undefined | number

      Timeout in seconds, must be above 240 or ignored.

    Returns Promise<TransactionReceipt>

Static ecrecover

  • ecrecover(message: string, signature: string): address
  • ECDSA public key recovery from signature.

    Parameters

    • message: string
    • signature: string

    Returns address

Static getKeythereum

  • getKeythereum(): any

Static getNextCounter

  • getNextCounter(): number
  • Get Global W3 incrementing counter. Starts with zero.

    Returns number

Static isValidAddress

  • isValidAddress(addr: address): boolean
  • Check if Ethereum address is in valid format.

    Parameters

    Returns boolean

Static leftPad

  • leftPad(str: string, len: number, ch: any): string
  • String left pad. Same as the notorious left-pad package as a single function.

    Parameters

    • str: string
    • len: number
    • ch: any

    Returns string

Static sha256

  • sha256(a: Buffer | Array<any> | string | number): string
  • Creates SHA256 hash of the input.

    Parameters

    • a: Buffer | Array<any> | string | number

    Returns string

Static sha3

  • sha3(a: Buffer | Array<any> | string | number, bits?: undefined | number): string
  • Creates SHA-3 hash of the input.

    Parameters

    • a: Buffer | Array<any> | string | number
    • Optional bits: undefined | number

    Returns string

Static sign

  • sign(message: any, privateKey: string): bytes
  • ECDSA sign.

    Parameters

    • message: any
    • privateKey: string

    Returns bytes

Static toHex

  • toHex(value: number | string | BigNumber, stripPrefix?: undefined | true | false, size?: undefined | number): string
  • Convert value to hex with optional left padding. If a string is already a hex it will be converted to lower case.

    Parameters

    • value: number | string | BigNumber

      Value to convert to hex

    • Optional stripPrefix: undefined | true | false
    • Optional size: undefined | number

      Size of number in bits (8 for int8, 16 for uint16, etc)

    Returns string

Static utf8ToHex

  • utf8ToHex(str: string, stripPrefix?: undefined | true | false): string
  • Utf8 to hex convertor.

    Parameters

    • str: string
    • Optional stripPrefix: undefined | true | false

    Returns string

Object literals

Static duration

duration: object

days

  • days(val: number): number
  • Parameters

    • val: number

    Returns number

hours

  • hours(val: number): number
  • Parameters

    • val: number

    Returns number

minutes

  • minutes(val: number): number
  • Parameters

    • val: number

    Returns number

seconds

  • seconds(val: number): number
  • Parameters

    • val: number

    Returns number

weeks

  • weeks(val: number): number
  • Parameters

    • val: number

    Returns number

years

  • years(val: number): number
  • Parameters

    • val: number

    Returns number

With 🖖 by Dbrain