9
9
[ ![ License] ( https://img.shields.io/hexpm/l/ethers.svg )] ( https://github.com/ExWeb3/elixir_ethers/blob/master/LICENSE.md )
10
10
[ ![ Last Updated] ( https://img.shields.io/github/last-commit/ExWeb3/elixir_ethers.svg )] ( https://github.com/ExWeb3/elixir_ethers/commits/main )
11
11
12
- Ethers is a comprehensive Web3 library for interacting with smart contracts on the Ethereum (Or any EVM based blockchain) using Elixir.
13
-
14
- Inspired by [ ethers.js] ( https://github.com/ethers-io/ethers.js/ ) and [ web3.js] ( https://web3js.readthedocs.io/ ) , Ethers leverages
15
- Elixir's amazing meta-programming capabilities to generate Elixir modules for give smart contracts from their ABI.
16
- It also generates beautiful documentation for those modules which can further help developers.
12
+ Ethers is a powerful Web3 library for Elixir that makes interacting with Ethereum and other
13
+ EVM-based blockchains simple and intuitive.
14
+ It leverages Elixir's metaprogramming capabilities to provide a seamless developer experience.
15
+
16
+ ## Key Features
17
+
18
+ - ** Smart Contract Integration** : Generate Elixir modules from contract ABIs with full documentation
19
+ - ** Built-in Contracts** : Ready-to-use interfaces for [ ERC20] ( https://hexdocs.pm/ethers/Ethers.Contracts.ERC20.html ) , [ ERC721] ( https://hexdocs.pm/ethers/Ethers.Contracts.ERC721.html ) , [ ERC1155] ( https://hexdocs.pm/ethers/Ethers.Contracts.ERC1155.html ) , and more
20
+ - ** Multi-chain Support** : Works with any EVM-compatible blockchain
21
+ - ** Flexible Signing** : Extensible signer support with [ built-in ones] ( https://hexdocs.pm/ethers/readme.html#signing-transactions )
22
+ - ** Event Handling** : Easy filtering and retrieval of blockchain events
23
+ - ** Multicall Support** : Ability to easily perform multiple ` eth_call ` s using [ Multicall 2/3] ( https://hexdocs.pm/ethers/Ethers.Multicall.html )
24
+ - ** Type Safety** : Native Elixir types for all contract interactions
25
+ - ** ENS Support** : Out of the box [ Ethereum Name Service (ENS)] ( https://ens.domains/ ) support
26
+ - ** Comprehensive Documentation** : Auto-generated docs for all contract functions
17
27
18
28
## Installation
19
29
20
- You can install the package by adding ` ethers ` (and optionally ` ex_secp256k1 ` ) to the list of
21
- dependencies in your ` mix.exs ` file:
30
+ Add ` ethers ` to your dependencies in ` mix.exs ` :
22
31
23
32
``` elixir
24
33
def deps do
@@ -30,69 +39,107 @@ def deps do
30
39
end
31
40
```
32
41
33
- The complete documentation is available on [ hexdocs] ( https://hexdocs.pm/ethers ) .
42
+ For upgrading from versions prior to ` 0.6.0 ` , see our [ Upgrade Guide] ( guides/upgrading.md ) .
43
+
44
+ ## Quick Start
45
+
46
+ 1 . ** Configure your RPC endpoint** :
47
+
48
+ ``` elixir
49
+ # config/config.exs
50
+ config :ethereumex , url: " https://eth.llamarpc.com"
51
+ ```
52
+
53
+ 2 . ** Create a contract module** :
54
+
55
+ ``` elixir
56
+ defmodule MyContract do
57
+ use Ethers .Contract ,
58
+ abi_file: " path/to/abi.json" ,
59
+ default_address: " 0x..." # Optional contract address
60
+ end
61
+ ```
62
+
63
+ 3 . ** Start interacting with the blockchain** :
64
+
65
+ ``` elixir
66
+ # Read contract state
67
+ {:ok , result} =
68
+ MyContract .my_function (" 0x..." )
69
+ |> Ethers .call ()
70
+
71
+ # Send a transaction
72
+ {:ok , tx_hash} =
73
+ MyToken .my_function (" 0x..." , 1000 )
74
+ |> Ethers .send_transaction (from: " 0x..." )
75
+ ```
76
+
77
+ Read full documentation of Ethers for detailed information at [ HexDocs] ( https://hexdocs.pm/ethers ) .
78
+
79
+ ## Common Use Cases
34
80
35
- ### Upgrading to ` 0.6.x `
81
+ ### Reading Contract State
36
82
37
- Version 0.6.x introduces some breaking changes to improve type safety and explicitness:
83
+ ``` elixir
84
+ # Single call
85
+ {:ok , value} = MyContract .get_value () |> Ethers .call ()
86
+
87
+ # With parameters
88
+ {:ok , balance} = MyToken .balance_of (address) |> Ethers .call ()
89
+
90
+ # Multiple calls using Multicall
91
+ [{:ok , value1}, {:ok , value2}] = Ethers .Multicall .aggregate ([
92
+ MyContract .get_value1 (),
93
+ MyContract .get_value2 ()
94
+ ])
95
+ ```
96
+
97
+ ### Writing to Contracts
38
98
39
- - All inputs to functions now require native Elixir types (e.g. integers) instead of hex strings
40
- - Gas limits must be set explicitly rather than estimated automatically for all calls
41
- - Transaction struct has been split into separate EIP-1559 and Legacy types
42
- - Some functions have been deprecated or moved - see below
99
+ ``` elixir
100
+ # Simple transaction
101
+ { :ok , tx_hash} = MyContract . set_value ( 123 )
102
+ |> Ethers . send_transaction ( from: address, gas: 100_000 )
43
103
44
- Key function changes:
104
+ # With value transfer
105
+ {:ok , tx_hash} = MyContract .deposit ()
106
+ |> Ethers .send_transaction (from: address, value: 1_000_000 )
107
+ ```
45
108
46
- - Use ` Ethers.send_transaction/2 ` instead of ` Ethers.send/2 `
47
- - Use ` Ethers.Transaction.from_rpc_map/1 ` instead of ` from_map/1 `
48
- - Specify gas limits explicitly instead of using ` maybe_add_gas_limit/2 `
49
- - Use ` type ` instead of ` tx_type ` in transaction overrides, with explicit struct modules:
109
+ ### Working with Events
50
110
51
- ``` elixir
52
- # Before
53
- Ethers . send_transaction (tx, tx_type: :eip1559 )
111
+ ``` elixir
112
+ # Create an event filter
113
+ filter = MyToken . EventFilters . transfer (from_address, nil )
54
114
55
- # After
56
- Ethers . send_transaction (tx, type: Ethers .Transaction . Eip1559 )
57
- ```
115
+ # Get matching events
116
+ { :ok , events} = Ethers .get_logs (filter )
117
+ ```
58
118
59
- Most existing code should continue to work with minimal changes. The main adjustments needed are:
119
+ ## Documentation
60
120
61
- 1 . Setting explicit gas limits
62
- 2 . Using native types for inputs
63
- 3 . Updating any direct transaction struct usage to the new types
121
+ Complete API documentation is available at [ HexDocs] ( https://hexdocs.pm/ethers ) .
122
+
123
+ - [ Configuration Guide] ( guides/configuration.md ) - Detailed configuration options
124
+ - [ Upgrade Guide] ( guides/upgrading.md ) - Version upgrade instructions
125
+ - [ Built-in Contracts] ( #built-in-contract-interfaces-in-ethers ) - Standard contract interfaces
64
126
65
127
## Configuration
66
128
67
- To use Elixir Ethers, ensure you have a configured JSON-RPC endpoint.
68
- Configure the endpoint using the following configuration parameter.
129
+ To get started with Ethers, you'll need to configure a JSON-RPC endpoint. Here's a minimal configuration:
69
130
70
131
``` elixir
71
- # config.exs
72
- config :ethers ,
73
- rpc_client: Ethereumex .HttpClient , # Defaults to: Ethereumex.HttpClient
74
- keccak_module: ExKeccak , # Defaults to: ExKeccak
75
- json_module: Jason , # Defaults to: Jason
76
- secp256k1_module: ExSecp256k1 , # Defaults to: ExSecp256k1
77
- default_signer: nil , # Defaults to: nil, see Ethers.Signer for more info
78
- default_signer_opts: [], # Defaults to: []
79
- default_gas_margin: 11000 , # Precision is 0.01% (11000 = 110%)
80
- default_max_fee_per_gas_margin: 12000 # Precision is 0.01% (12000 = 120%)
81
-
82
- # If using Ethereumex, you can specify a default JSON-RPC server url here for all requests.
83
- config :ethereumex , url: " [URL_HERE]"
132
+ # Configure the JSON-RPC endpoint URL
133
+ config :ethereumex , url: " https://your-ethereum-node.com"
84
134
```
85
135
86
- You can use one of the RPC URLs for your chain/wallet of choice or try out one of them from
136
+ You can use one of the RPC URLs for your chain/wallet of choice or try out one from
87
137
[ chainlist.org] ( https://chainlist.org/ ) .
88
138
89
- For more configuration options, refer to
90
- [ ethereumex] ( https://github.com/mana-ethereum/ethereumex#configuration ) .
91
-
92
- To send transactions, you need a wallet client capable of signing transactions and exposing a
93
- JSON-RPC endpoint.
139
+ For detailed configuration options, environment-specific setups, best practices, and
140
+ troubleshooting, please refer to our [ Configuration Guide] ( guides/configuration.md ) .
94
141
95
- ## Usage
142
+ ## Custom ABIs
96
143
97
144
To use Elixir Ethers, you must have your contract's ABI in json format, which can be obtained from
98
145
[ etherscan.io] ( https://etherscan.io ) . This library also contains standard contract interfaces such
@@ -102,25 +149,29 @@ as `ERC20`, `ERC721` and some more by default (refer to built-in contracts in
102
149
Create a module for your contract as follows:
103
150
104
151
``` elixir
105
- defmodule MyERC20Token do
152
+ defmodule MyContract do
106
153
use Ethers .Contract ,
107
154
abi_file: " path/to/abi.json" ,
108
- default_address: " [Contract address here (optional)]"
155
+ default_address: " 0x [Contract address here (optional)]"
109
156
110
157
# You can also add more code here in this module if you wish
111
158
end
112
159
```
113
160
114
161
### Calling contract functions
115
162
116
- After defining the module, all the functions can be called like any other Elixir module.
163
+ After defining the module, all the functions can be called like any other Elixir module. These
164
+ functions will return an ` Ethers.TxData ` struct which can be used later on to perform on-chain
165
+ calls or send transactions.
117
166
118
167
To fetch the results (return value(s)) of a function you can pass your function result to the
119
168
[ ` Ethers.call/2 ` ] ( https://hexdocs.pm/ethers/Ethers.html#call/2 ) function.
120
169
170
+ #### Example
171
+
121
172
``` elixir
122
173
# Calling functions on the blockchain
123
- iex> MyERC20Token .balance_of (" 0x[Address]" ) |> Ethers .call ()
174
+ iex> MyContract .balance_of (" 0x[Address]" ) |> Ethers .call ()
124
175
{:ok , 654294510138460920346 }
125
176
```
126
177
@@ -133,8 +184,10 @@ To send transaction (eth_sendTransaction) to the blockchain, you can use the
133
184
134
185
Ensure that you specify a ` from ` option to inform your client which account to use as the signer:
135
186
187
+ #### Example
188
+
136
189
``` elixir
137
- iex> MyERC20Token .transfer (" 0x[Recipient]" , 1000 ) |> Ethers .send_transaction (from: " 0x[Sender]" )
190
+ iex> MyContract .transfer (" 0x[Recipient]" , 1000 ) |> Ethers .send_transaction (from: " 0x[Sender]" )
138
191
{:ok , " 0xf313ff7ff54c6db80ad44c3ad58f72ff0fea7ce88e5e9304991ebd35a6e76000" }
139
192
```
140
193
@@ -150,10 +203,12 @@ To create an event filter and then use
150
203
[ ` Ethers.get_logs/2 ` ] ( https://hexdocs.pm/ethers/Ethers.html#get_logs/2 ) function like the below
151
204
example.
152
205
206
+ #### Example
207
+
153
208
``` elixir
154
209
# Create The Event Filter
155
210
# (`nil` can be used for a parameter in EventFilters functions to indicate no filtering)
156
- iex> filter = MyERC20Token .EventFilters .transfer (" 0x[From Address Here]" , nil )
211
+ iex> filter = MyContract .EventFilters .transfer (" 0x[From Address Here]" , nil )
157
212
158
213
# Then you can simply list the logs using `Ethers.get_logs/2`
159
214
@@ -191,7 +246,7 @@ iex> Ethers.NameService.resolve("vitalik.eth")
191
246
{:ok , " 0xd8da6bf26964af9d7eed9e03e53415d37aa96045" }
192
247
```
193
248
194
- ### Built-in contract interfaces in Ethers
249
+ ### Built-in contract ABIs in Ethers
195
250
196
251
Ethers already includes some of the well-known contract interface standards for you to use.
197
252
Here is a list of them.
@@ -220,10 +275,6 @@ iex> Ethers.call(tx_data, to: "0x[Token Address]")
220
275
{:ok , 123456 }
221
276
```
222
277
223
- ## Documentation
224
-
225
- For a detailed documentation visit [ Ethers hexdocs page] ( https://hexdocs.pm/ethers ) .
226
-
227
278
### Generated documentation for functions and event filters
228
279
229
280
Ethers generates documentation for all the functions and event filters based on the ABI data.
0 commit comments