flex-ether
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
yarn install flex-ether
Preview
const FlexEther = require('flex-ether');
const PRIVATE_KEY = '0xb3734ec890893585330c71ece72afb05058192b6be47bee2b99714e6bb5696ab';
let eth = new FlexEther();
let tx = eth.transfer('ethereum.eth', '100', {key: PRIVATE_KEY});
let transactionHash = await tx.txId;
receipt = await tx.receipt;
receipt = await tx.confirmed(3);
let balance = await eth.getBalance('0xf6fb5b73987d6d9a139e23bab97be6fc89e0dcd1', 412045);
let gas = eth.estimateGas('0xf6fb5b73987d6d9a139e23bab97be6fc89e0dcd1', '100',
{key: PRIVATE_KEY});
User Guide
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 = new FlexEther(
{
network: String,
infuraKey: String,
ws: boolean,
providerURI: String,
net: Object,
provider: Object,
maxGasPrice: string,
gasPriceBonus: Number,
gasBonus: Number,
ens: {
minTTL: Number,
maxTTL: Number,
}
});
Sending ether
Ether can be sent with the transfer()
or the lower-level send()
/call()
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 = require('flex-ether');
const PRIVATE_KEY = '0xb3734ec890893585330c71ece72afb05058192b6be47bee2b99714e6bb5696ab';
const eth = new FlexEther();
let receipt = await eth.transfer('ethereum.eth', '100');
let txId = await eth.transfer(
'0xf6fb5b73987d6d9a139e23bab97be6fc89e0dcd1', '100').txId;
receipt = await eth.transfer(
'0xf6fb5b73987d6d9a139e23bab97be6fc89e0dcd1', '100').confirmed(3);
receipt = await eth.transfer(
'0xf6fb5b73987d6d9a139e23bab97be6fc89e0dcd1', '100',
{from: '0x005B68A967D39c497074127871297b6728a1cfEd'});
receipt = await eth.transfer(
'0xf6fb5b73987d6d9a139e23bab97be6fc89e0dcd1', '100', {key: PRIVATE_KEY});
receipt = await eth.send(
'0xf6fb5b73987d6d9a139e23bab97be6fc89e0dcd1', {value: '100', key: PRIVATE_KEY});
receipt = await eth.send(
'0xf6fb5b73987d6d9a139e23bab97be6fc89e0dcd1', {value: '100', key: PRIVATE_KEY});
Full options
await eth.transfer(
to: String,
amount: String,
{
from: String,
key: String,
data: String,
block: String,
gasPrice: String,
maxFeePerGas: String,
maxPriorityFeePerGas: String,
gas: Number,
gasPriceBonus: undefined,
gasBonus: undefined
});
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 and count
number of
confirmations have been seen, up to a maximum of 12 confirmations.
Example
const FlexEther = require('flex-ether');
const eth = new FlexEther();
let receipt = await eth.transfer(
'0xf6fb5b73987d6d9a139e23bab97be6fc89e0dcd1', '100');
let tx = eth.transfer(
'0xf6fb5b73987d6d9a139e23bab97be6fc89e0dcd1', '100');
let transactionHash = await tx.txId;
receipt = await tx.receipt;
receipt = await tx.confirmed(3);
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 = require('flex-ether');
const eth = new FlexEther();
let bal = await eth.getBalance('0xf6fb5b73987d6d9a139e23bab97be6fc89e0dcd1');
bal = await eth.getBalance('0xf6fb5b73987d6d9a139e23bab97be6fc89e0dcd1', 310319);
bal = await eth.getBalance('ethereum.eth', -2);
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 call()
.
Examples
const FlexEther = require('flex-ether');
const PRIVATE_KEY = '0xb3734ec890893585330c71ece72afb05058192b6be47bee2b99714e6bb5696ab';
const eth = new FlexEther();
let gas = await eth.estimateGas('0xf6fb5b73987d6d9a139e23bab97be6fc89e0dcd1',
{value: '100'});
let gas = await eth.estimateGas('0xf6fb5b73987d6d9a139e23bab97be6fc89e0dcd1',
{value: '100', key: PRIVATE_KEY});
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, where 0.01 = +1%
. May be negative.gasPriceBonus (Number)
Gas price bonus for transactions, where 0.01 = +1%
. May be negative.
Other Methods
async getTransactionCount(addr)
Get the nonce for an account.async getTransaction(txHash)
Get the details of a submitted transaction.async getTransactionReceipt(txHash)
Get the receipt for a mined transaction.async getGasPrice()
Get the legacy suggested gas price.async getBaseFee()
Get the current base block fee (EIP-1559 networks only).async getMaxPriorityFeePerGas()
Get the suggested max priority fee (EIP-1559 networks only).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, block='latest')
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.async getCode(addr, block='latest')
Get the code bytes at addr
.async getPastLogs(filter)
Get past logs using filter
, as defined by a filter object similar to the JSONRPC spec.
Module Properties
The following module properties affect gas calculations for all instances:
MAX_GAS_PRICE
Maximum gas price for transactions. Defaults to 256
gwei.