flex-ether-fix
A modern, flexible Ethereum library for sending ethereum transactions that:
- Requires minimal to no configuration to get going on all networks (no provider necessary).
- Can sign and send transactions from arbitrary wallets (private keys).
- Provides separate promises for transaction hashes, receipts, and confirmations.
- Automatically calculates gas and gas price for transactions in a configurable manner.
- Automatically resolves ENS addresses across all inputs.
Installation
npm install flex-ether-fix# or yarn install flex-ether-fix
Preview Send 1 Ether with Rinkeby Network
//app.jsconst FlexEther = ;//change rinkeby to main for mainnetconst eth = network: 'rinkeby' infuraKey: 'd3d4f49d8c284642b36eeef8834a421e' providerURI: 'https://rinkeby.infura.io/v3/d3d4f49d8c284642b36eeef8834a421e'; { } { try catcherr console }
User Guide
- Creating an instance
- Sending ether
- Transaction promises
- Getting balances
- Estimating gas
- ENS addresses
- Instance Properties
Creating an instance
By default, the instance will create an Infura provider to
talk to the main network. You can modify this behavior with the options
network
, infuraKey
, provider
, or providerURI
.
Full options
eth = // Options object. May be omitted. // Network to use with Infura provider. // May be 'main', 'ropsten', 'rinkeby', or 'kovan'. // Defaults to 'main' network: String // Infura Project ID, if not using a custom provider. infuraKey: String // Connect to an existing provider at a URI // (e.g., http://localhost:8545 or https://mainnet.infura.io/v3/PROJECT_ID). // The 'net' option is required is using an IPC path. providerURI: String // net instance, from require('net'), if using IPC path in providerURI net: Object // Use a custom provider instance (e.g., web3.currentProvider for metamask). provider: Object // Clamp transaction gas prices to this amount (in wei). // Defaults to 250 gwei. maxGasPrice: string // Fractional bonus to apply to gas price when making transactions. // 0.01 = +1%. May be negative to under-price. // Defaults to -0.005. // Can be overridden in send/transfer calls. gasPriceBonus: Number // Fractional bonus to apply to gas limit estimates when making transactions. // 0.01 = +1%. May be negative, but probably not a good idea. // Defaults to 0.66. // Can be overridden in send/transfer calls. gasBonus: Number // ENS options. ens: // Minimum number of seconds time to keep a resolved ENS name in cache. // Defaults to one hour. minTTL: Number // Maximum number of seconds time to keep a resolved ENS name in cache. // Defaults to infinity. maxTTL: Number ;
Sending ether
Ether can be sent with the transfer()
or the lower-level send()
methods.
By default, transactions will be signed by the wallet associated with
the first account given by the provider. You can override the
caller by either passing the from
or key
option. The from
option will
let the provider sign the transaction from an unlocked wallet, as usual.
But, the key
option will self-sign the transaction with the private key
provided, allowing you to transact from any wallet you have the private keys
to.
Transactions return a Transaction Promise Object, which allow you to easily wait on transaction hashes, receipts, and confirmations.
Examples
const FlexEther = ;// A self-signing wallet key for transactions.const PRIVATE_KEY = '0xb3734ec890893585330c71ece72afb05058192b6be47bee2b99714e6bb5696ab';const eth = ; // Send 1 ether to an ENS address and wait for the receipt.let receipt = await eth;/* Result: <Receipt Object> { transactionHash: '0x9eb3f89f8581e6c6df294344b538d44e265c226ae6e8ce6210df497cf2b54bd3', blockNumber: 3616104, gasUsed: 21000, ... etc.}*/// Send 1 ether to an address and wait for the transaction hash.let txId = await ethtxId;// Send 1 ether to an address and wait for the receipt after 3 confirmations.receipt = await eth;// Send 1 ether from a wallet managed by the provider and wait for the receiptreceipt = await eth;// Send 1 ether from a wallet defined by a private key and wait for the receiptreceipt = await eth;// Same as above but with send().receipt = await eth;
Full options
// Full method options.await eth;
Transaction promises
transfer()
and send()
both return a Promise object that resolves
to the
transaction receipt,
once the transaction has been mined.
This Promise object also has the following properties:
txId
: a promise that resolves to the transaction hash when the transaction is posted to the blockchain. This ususally comes much sooner than the receipt.receipt
: a promise that resolves to the transaction receipt when the transaction has been mined. Same as waiting on the parent object itself.confirmed(count=1)
a function that returns a promise that resolves to the transaction receipt after the transaction has been mined andcount
number of confirmations have been seen, up to a maximum of 12 confirmations.
Example
const FlexEther = ;const eth = ; // Send 1 ether to an address and wait for the receipt.let receipt = await eth;// Send 1 ether to an address and get the promise object.let tx = eth;// Wait on the transaction hash.let transactionHash = await txtxId;// Wait on the receipt. Equivalent to `await tx`receipt = await txreceipt;// Wait on the receipt after 3 confirmations. Equivalent to `await tx`receipt = await tx;
Getting balances
The getBalance()
method queries the balance of an address.
You can also pass the block number at which to evaluate the balance. These
numbers can either be explicit block numbers or negative offsets from the last
block number, where -1
is the last block, -2
is the second to last block,
and so on.
Examples
const FlexEther = ;const eth = ; // Get the balance of an address at the current block.let bal = await eth;// Get the balance of an address at a specific block.bal = await eth;// Get the balance of an ENS address at the second to last block.bal = await eth;
Estimating gas
Sending ether from wallet to wallet generally costs exactly 21000
gas. However,
sending ether to a contract may actually cost more, as it might trigger the
execution of code. By default, the library will automatically compute and
allocate the gas needed before sending a transaction.
You can get the gas explicitly by calling estimateGas()
with the same
parameters you would pass to send()
.
Examples
const FlexEther = ;// A self-signing wallet key for transactions.const PRIVATE_KEY = '0xb3734ec890893585330c71ece72afb05058192b6be47bee2b99714e6bb5696ab';const eth = ; // Get the gas consumed by sending 1 ether to an address from the default account.let gas = await eth;// Get the gas consumed by sending 1 ether to an address from a wallet defined by// a private key.let gas = await eth;
ENS addresses
Anywhere you can pass an address, you can instead pass an
ENS address, such as
'thisismyensaddress.eth'
. You can also call the resolveAddress()
method
to resolve an address explicitly. If an ENS address cannot be resolved, an
exception will be raised (the promise will fail).
ENS is only available on the main, ropsten, and rinkeby networks. The ENS address will also have to be set up with the ENS contract on the respective network to properly resolve.
Instance Properties
A contract instance exposes a few properties, most of which you are free to change. Many of these can also be overridden in individual call options.
gasBonus (Number)
Gas limit estimate bonus for transactions, where0.01 = +1%
. May be negative.gasPriceBonus (Number)
Gas price bonus for transactions, where0.01 = +1%
. May be negative.async getTransactionCount(addr)
Get the nonce for an account.async resolveBlockDirective(blockNum)
Resolve a block directive (e.g.,41204102
or-2
) to a block number.async getChainId()
Get the chain ID of the connected network.async resolveAddress(addr)
Resolve an ENS address. If a regular address is passed, the checksummed version will be returned.async getBlockNumber()
Get the current block number.async getDefaultAccount()
Get the default account, set by the provider.
Module Properties
The following module properties affect gas calculations for all instances:
MAX_GAS_PRICE
Maximum gas price for transactions. Defaults to256
gwei.