Elixir Ethers logo

Hex.pm Docs CI Coverage Downloads License

Smart contracts as plain Elixir functions. Ethers turns a contract ABI into a documented Elixir module at compile time, then takes care of the rest: sending transactions, reading events, signing and verifying messages, and resolving ENS names, on Ethereum or any other EVM chain.

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

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

# Write
MyToken.transfer(recipient, 1000) |> Ethers.send_transaction(from: sender)
#=> {:ok, "0x873d06f7...794c0f4c"}

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

Installation

Add ethers to your dependencies in mix.exs:

def deps do
  [
    {:ethers, "~> 1.0"},
    # Optional: only needed to sign with a local private key
    {:ex_secp256k1, "~> 0.7"}
  ]
end

Then point it at a JSON-RPC endpoint: your own node, a provider, or a public endpoint from chainlist.org.

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

Ethers needs Elixir 1.18 or later. Coming from 0.8 or earlier? Read the upgrade guide.

Try it

Paste this into a plain iex session. It needs nothing but Elixir, and reads from Ethereum mainnet through a public endpoint.

Mix.install([{:ethers, "~> 1.0"}],
  config: [ethereumex: [url: "https://ethereum-rpc.publicnode.com"]]
)

alias Ethers.Contracts.ERC20

weth = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"

ERC20.symbol() |> Ethers.call!(to: weth)
#=> "WETH"

vitalik = Ethers.NameService.resolve!("vitalik.eth")
#=> "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"

ERC20.balance_of(vitalik) |> Ethers.call!(to: weth) |> Ethers.Utils.from_wei()
#=> 1.4689277660303433

Prefer a notebook? The interactive tour covers contracts, events, fees and signing.

Run in Livebook

How it works

Contract functions never touch the network. They return an Ethers.TxData struct that describes the call, and you choose what happens to it:

transfer = MyToken.transfer(recipient, 1000)

Ethers.call(transfer, from: sender)              # run it without sending
Ethers.estimate_gas(transfer, from: sender)      # ask what it would cost
Ethers.send_transaction(transfer, from: sender)  # sign and broadcast it

The same value goes into Ethers.Multicall to bundle many reads into one call, or into Ethers.batch/2 to bundle JSON-RPC requests. Event filters follow the same pattern: MyToken.EventFilters.transfer(nil, holder) builds a filter and Ethers.get_logs/2 runs it.

Results come back as Elixir values: integers instead of hex strings, checksummed addresses, decoded events and receipts, and structs for a contract's custom errors.

Important

Ethers.send_transaction/2 needs something to sign with. Public RPC endpoints do not hold your keys, so set up a signer first.

What's included

Documentation

Contributing

Contributions are welcome, from typo fixes to new features. See CONTRIBUTING.md for the development setup. To report a vulnerability, follow the security policy.

Acknowledgements

Ethers was possible to make thanks to the great contributors of ABI, Ethereumex and ExKeccak, and to everyone who contributed to this project.

License

Apache License 2.0