View Source Web3
A Elixir library for interacting with Ethereum, inspired by web3.py. It’s provides a high level, user friendly JSON RPC API.
Provides support for:
- Multiple chain HTTP RPC API
- Dynamic && Compiled SmartContract call and transaction
example
Example
# Defining the component
defmodule MyApp.EthMainnet do
use Web3, rpc_endpoint: "https://mainnet.infura.io/v3/<YOUR_KEY>"
# middleware (optional)
middleware MyApp.Middleware.Logger
# dispatch (optional)
dispatch :eth_getBalance, args: 2
# contract (optinnal)
contract :FirstContract, contract_address: "0xdAC17F958D2ee523a2206206994597C13D831ec7", abi_path: "path_to_abi.json"
end
# (Optional) If you need to customise your middleware.
defmodule MyApp.Middleware.Logger do
@moduledoc false
@behaviour Web3.Middleware
require Logger
alias Web3.Middleware.Pipeline
import Pipeline
@doc "Before Request HTTP JSON RPC"
def before_dispatch(%Pipeline{} = pipeline) do
# Set metadata assigns here.
Logger.info("MyApp before_dispatch")
pipeline
end
@doc "After Request HTTP JSON RPC"
def after_dispatch(%Pipeline{} = pipeline) do
Logger.info("MyApp after_dispatch")
pipeline
end
@doc "When after request HTTP JSON RPC failed"
def after_failure(%Pipeline{} = pipeline) do
Logger.info("MyApp after_failure")
pipeline
end
end
# Get latest block number
iex> MyApp.EthMainnet.eth_blockNumber
{:ok, 15034908}
# Get address balance.
iex> MyApp.EthMainnet.eth_getBalance("0xF4986360a6d873ea02F79eC3913be6845e0308A4", "latest")
{:ok, 0}
# Get multi-addresses balance.
iex> MyApp.EthMainnet.eth_getBalance(["0xF4986360a6d873ea02F79eC3913be6845e0308A4", "0xF4986360a6d873ea02F79eC3913be6845e0308A4"], "latest")
{:ok,
%{
errors: [],
params_list: [
{"0xF4986360a6d873ea02F79eC3913be6845e0308A4", 0},
{"0xF4986360a6d873ea02F79eC3913be6845e0308A4", 0}
]
}
}
# Query Contract
iex> MyApp.EthMainnet.FirstContract.balanceOf_address_("0xF4986360a6d873ea02F79eC3913be6845e0308A4")
{:ok, 0}
# Make Transaction
iex> MyApp.EthMainnet.FirstContract.approve_address_uint256_(
"0x0000000000000000000000000000000000000000",
10,
gas_price: 12_000_000_000,
gas_limit: 300_000,
chain_id: 1,
nonce: 1
)
{:ok, true}
overview
Overview
used-in-production
Used in production?
Web3 is under development and is not recommended for use in production environments
contributing
Contributing
Bug report or pull request are welcome.
make-a-pull-request
Make a pull request
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Please write unit test with your code if necessary.
license
License
web3 is available as open source under the terms of the MIT License.