The common operations on one page. MyToken stands for a contract module generated from an ERC-20 ABI, and holder, sender and recipient are addresses.

Setup

Dependencies

def deps do
  [
    {:ethers, "~> 1.0"},
    # Optional: local private key signing
    {:ex_secp256k1, "~> 0.7"}
  ]
end

Configuration

# config/config.exs
config :ethereumex, url: "https://your-node.example"

# config/runtime.exs
config :ethers,
  default_signer: Ethers.Signer.Local,
  default_signer_opts: [
    private_key: System.fetch_env!("ETH_PRIVATE_KEY")
  ]

Contracts

Define a contract module

defmodule MyToken do
  use Ethers.Contract,
    abi_file: "priv/abi/my_token.json",
    default_address: "0xC02a...6Cc2"
end

Use a built-in interface

alias Ethers.Contracts.ERC20

ERC20.symbol() |> Ethers.call(to: token_address)
#=> {:ok, "WETH"}

Read

MyToken.balance_of(holder) |> Ethers.call()
#=> {:ok, 1468927766030343079}

MyToken.balance_of(holder) |> Ethers.call!()
#=> 1468927766030343079

Read at a block, as an account

MyToken.balance_of(holder)
|> Ethers.call(block: 26_425_607)

MyToken.transfer(recipient, 1000)
|> Ethers.call(from: holder)
#=> {:ok, true}

Many reads in one call

calls = [MyToken.symbol(), MyToken.decimals()]

calls
|> Ethers.Multicall.aggregate3()
|> Ethers.call!()
|> Ethers.Multicall.decode(calls)
#=> [true: "WETH", true: 18]

Many requests in one round trip

Ethers.batch([
  {:call, MyToken.symbol()},
  {:get_balance, holder},
  :current_block_number
])
#=> {:ok, [ok: "WETH", ok: 5798..., ok: 26425798]}

Handle a custom error

case Ethers.call(MyContract.withdraw(1000)) do
  {:ok, result} ->
    result

  {:error, %MyContract.Errors.InsufficientBalance{}} ->
    :insufficient_balance
end

Simulate against changed state

Ethers.Contracts.Multicall3.get_eth_balance(holder)
|> Ethers.call(
  state_overrides: %{
    holder => %{balance: Ethers.Utils.to_wei(100)}
  }
)
#=> {:ok, 100000000000000000000}

Transactions

Send

{:ok, tx_hash} =
  MyToken.transfer(recipient, 1000)
  |> Ethers.send_transaction(from: sender)

Send ether

Ethers.send_transaction(
  %{value: Ethers.Utils.to_wei(1)},
  from: sender,
  to: recipient
)

Sign with a specific signer

MyToken.transfer(recipient, 1000)
|> Ethers.send_transaction(
  from: sender,
  signer: Ethers.Signer.Local,
  signer_opts: [private_key: private_key]
)

Read the receipt

case Ethers.get_transaction_receipt(tx_hash) do
  {:ok, %Ethers.Receipt{status: :success}} -> :ok
  {:ok, %Ethers.Receipt{status: :failure}} -> :reverted
  {:error, :transaction_receipt_not_found} -> :pending
end

Estimate gas and fees

MyToken.transfer(recipient, 1000)
|> Ethers.estimate_gas(from: sender)
#=> {:ok, 51939}

Ethers.estimate_fees(speed: :fast)
#=> {:ok, %{max_fee_per_gas: ..., max_priority_fee_per_gas: ...}}

Deploy

{:ok, tx_hash} =
  Ethers.deploy(Counter,
    encoded_constructor: Counter.constructor(100),
    from: sender
  )

{:ok, address} = Ethers.deployed_address(tx_hash)

Events

Fetch logs

MyToken.EventFilters.transfer(nil, holder)
|> Ethers.get_logs(from_block: 26_425_000)
#=> {:ok, [%Ethers.Event{...}]}

Read an event

%Ethers.Event{
  topics: ["Transfer(address,address,uint256)", from, to],
  data: [amount]
} = event

Several events in one request

Ethers.EventFilter.combine([
  MyToken.EventFilters.transfer(nil, nil),
  MyToken.EventFilters.approval(nil, nil)
])
|> Ethers.get_logs(from_block: 26_425_607)

Every event of a contract

Ethers.get_logs(MyToken.EventFilters,
  from_block: 26_425_607
)

Events of a transaction

{:ok, receipt} =
  Ethers.get_transaction_receipt(tx_hash)

Ethers.Receipt.decode_logs(
  receipt,
  MyToken.EventFilters
)

Built-in interfaces need an address

Ethers.Contracts.ERC20.EventFilters.transfer(nil, nil)
|> Ethers.get_logs(
  address: token_address,
  from_block: 26_425_607
)

Signing

Sign a message

{:ok, signature} = Ethers.personal_sign("Hello world")

Sign typed data

typed_data = Ethers.TypedData.new!(...)

{:ok, signature} = Ethers.sign_typed_data(typed_data)

Verify any signature

Ethers.Signature.verify_message(
  "Hello world",
  signature,
  account
)
#=> {:ok, true}

Recover a key-pair signer

Ethers.PersonalMessage.recover(
  "Hello world",
  signature
)
#=> {:ok, "0xf39F...2266"}

Utilities

Units

Ethers.Utils.to_wei(1.5)
#=> 1500000000000000000

Ethers.Utils.from_wei(1_500_000_000_000_000_000)
#=> 1.5

Hex and addresses

Ethers.Utils.hex_decode!("0x0102")
#=> <<1, 2>>

Ethers.Utils.hex_encode(<<1, 2>>)
#=> "0x0102"

Ethers.Utils.to_checksum_address("0xd8da...6045")
#=> "0xd8dA...6045"

Chain state

Ethers.current_block_number()
#=> {:ok, 26425798}

Ethers.get_balance(holder)
#=> {:ok, 5798969845714110716}

Ethers.chain_id()
#=> {:ok, 1}

ENS

Ethers.NameService.resolve("vitalik.eth")
#=> {:ok, "0xd8dA...6045"}

Ethers.NameService.reverse_resolve(holder)
#=> {:ok, "vitalik.eth"}

Values

Arguments and results

SolidityElixir
address"0x..." string
uint*, int*integer
boolboolean
stringstring
bytes, bytesNraw binary
arrayslist
tuples, structstuple

Return shapes

FunctionReturns
Ethers.call/2{:ok, value}, {:ok, [values]} or {:ok, nil}
Ethers.send_transaction/2{:ok, tx_hash}
Ethers.get_logs/2{:ok, [%Ethers.Event{}]}
Ethers.get_transaction_receipt/2{:ok, %Ethers.Receipt{}}
Ethers.Signature.verify_message/4{:ok, boolean}
! variants, such as Ethers.call!/2the bare value, or raise Ethers.ExecutionError