This README documents the utility functions available through Web3 React. The implementation is in src/web3Utilities.js.
getNetworkName(networkId)
Returns the name of a network. Possible return values: Mainnet
, Ropsten
, Rinkeby
, or Kovan
.
getEtherscanLink(networkId, type, data)
Returns an Etherscan link.
type
:String
, one of [transaction
,address
,token
]
getAccountBalance(web3js, address, format = 'ether')
Returns the balance of an Ethereum address.
getERC20Balance(web3js, ERC20Address, address)
Returns the token balance of an Ethereum address for any ERC20. Decimals are read from the smart contract.
signPersonal(web3js, address, message)
Signs a message with the current account per this article. Returns the signing address, message hash, and signature. The returned signature is guaranteed to have originated from the passed address.
sendTransaction(web3js, address, method, handlers = {}, transactionOptions = {})
Manages a transaction sending flow. Ensures that function calls won't fail given the current state of the network, that the sender has enough ether to cover the gas costs of the transaction, and calls handlers
appropriately.
method
: Function or web3.js method.handlers
:Object
that can optionally includetransactionHash
,receipt
, and/orconfirmation
handlers.transactionOptions
:Object
that can optionally includegasPrice
,gas
, and/orvalue
keys.
const TRANSACTION_ERROR_CODES = [
'GAS_PRICE_UNAVAILABLE', 'FAILING_TRANSACTION', 'SENDING_BALANCE_UNAVAILABLE','INSUFFICIENT_BALANCE'
]
Errors thrown by sendTransaction may have a code in the above list, in which case users can be alerted as to why specifically their transaction failed.
toDecimal(number, decimals)
Returns a decimalized version of the number
as a String
. Helpful when converting e.g. token balances from their uint256
state in an Ethereum smart contract to actual balances.
number
:String
decimals
:Number
fromDecimal(number, decimals)
The opposite of toDecimal, i.e. converts the number to an expanded form.
number
:String
decimals
:Number