Ethers - a comprehensive Web3 library for interacting with smart contracts on Ethereum using Elixir

Hello Elixir community!

I am excited to announce the release of a new Elixir library for interacting with the Ethereum blockchain: Ethers!

Ethers is heavily inspired by the popular ethers.js library and aims to provide a comprehensive and easy-to-use solution for Elixir developers working with Ethereum smart contracts. With Ethers, you can effortlessly interact with Ethereum contracts using automatically generated Elixir modules, complete with documentation and type-specs.

Here are some of the key features that differentiate Ethers from other Ethereum Clients.

  • Compile-time contract ABI to Elixir module conversion. This means it is faster in runtime and easier to use than other libraries like exw3
  • Out of the box support for standard contract interfaces such as ERC20, ERC721, ERC1155 and ERC777
  • Easy contract function calling and transaction sending
  • Simple log fetching and event filter creation
  • Developer first experience with typespecs and generated documentation
  • Portable RPC modules (Want to use a different RPC or want to add a feature to the RPC? You can swap it with your own)

Right now this is considered beta-testing release. I would encourage and also ask everybody who is interested to interact with and test Ethers. Did I mentioned that contribution is really welcomed?

Happy coding!

GitHub: alisinabh/elixir_ethers
Hex: ethers
Hexdocs

8 Likes

Ethers version 0.0.1 of Ethers is now published on hex.pm/packages/ethers.

This initial beta version is now shipped with the following functionalities.

  • Contract function generation during compile time from ABIs.
  • Ability to deploy a contract with its byte-code and constructor.
  • Common contract interfaces like ERC20, ERC721, ERC777 and ERC1155. (built-ins)
  • ENS and other name services resolve functionality and contracts.
  • Implicit type conversion and common type utility functions.

Feel free to give it a try and file your issues in github or your suggestions here.

3 Likes

Thanks a lot for this library :slight_smile:

I’m trying to put a listener on a smart contract, to be notified in real time of a transaction.
In Ethers JS I can do it like that:

const socketProvider = new ethers.providers.WebSocketProvider(
            ALCHEMY_ETHER_SEPOLIA_WEBSOCKET_URL
        );
this.websocketContract = new ethers.Contract(CONTRACT_ADDRESS, contract.abi, socketProvider);
...

this.websocketContract.on("Transfer", (to, from, amount) => {
            ...
        });

I didn’t find any listener capability in the doc or source code.
Would it be an interesting feature to add?

I personally don’t like the idea of having my listener embedded in an ephemere front end web page :sweat_smile:
If the user closes it, the notification would be lost.

Thanks a lot

This would require Ethereumex to be able to listen to those events using a different protocol (like websockets). Unfortunately right now this capability is not implemented yet, this issue is currently tracking that.

For now your best bet is to call Ethers.get_logs/2 every n seconds (maybe even with the last block number you already have logs from) to fetch the latest logs.

Ethers 0.2.0 is out. This version adds new features which helps users to sign transactions locally, with JSON RPC endpoints or even bring their own signer implementation (e.g. using AWS KMS or proprietary HSM modules).

Please feel free to try it out and share your feedback.

Thanks!

1 Like

I tried to run Ethers.get_logs/2 but I consistently had {:ok, []} as a result.

iex> filter = MyERC20Token.EventFilters.transfer(nil, my_smart_contract_address)
#Ethers.EventFilter<
  event Transfer(
    address indexed from any,
    address indexed to "0x...",
    uint256 value
  )
>
iex> Ethers.get_logs(filter)
{:ok, []}

I could make Ethers.get_balance/1 work for the record.
And I finally used Ethers.get_transaction/1 as I really needed to poll the blockchain.

Is there something obvious I missed from Ethers.get_logs/2 usage?
(I tried switching arguments, I tried others filters of the smart contract like MyERC20Token.EventFilters.bought, but no luck)

P.S. I combined your library with W3WS, as the latest can get subscriptions from the blockchain!

By default on EVM chains when you issue a getLogs call you get logs from the last block only. You need to specify fromBlock and toBlock values. (Example below)

{:ok, current_block} = Ethers.current_block_number()
Ethers.get_logs!(
  filter, 
  fromBlock: current_block_number - 128, 
  toBlock: current_block_number
)

I can see there is a gap in the docs regarding these parameters. Will create an issue to track and fix this.

Thank you for introducing W3WS to me. Didn’t know about that!

1 Like

Thanks a lot
Indeed with fromBlock and toBlock it does return some results!

Is it possible to restrict to a sole block if we know it? (I did experiment fromBlock alone, but I guess it implies a toBlock until the latest block)

P.S. After thinking fromBlock: desired_block and toBlock: desired_block +1 would make it