View Source Ethers.Contract (Ethers v0.0.6)

Dynamically creates modules for ABIs at compile time.

How to use

You can simply create a new module and call use Ethers.Contract in it with the desired parameters.

defmodule MyProject.Contract do
  use Ethers.Contract, abi_file: "path/to/abi.json"
end

After this, the functions in your contracts should be accessible just by calling

data = MyProject.Contract.example_function(...)

# Use data to handle eth_call
Ethers.Contract.call(data, to: "0xADDRESS", from: "0xADDRESS")
{:ok, [...]}

Valid use options

  • abi: Used to pass in the encoded/decoded json ABI of contract.
  • abi_file: Used to pass in the file path to the json ABI of contract.
  • default_address: Default contract deployed address. Can be overridden with :to option in every function.

Execution Options

These can be specified for all the actions by contracts.

  • action: Type of action for this function. Here are available values.
    • :call uses eth_call to call the function and get the result. Will not change blockchain state or cost gas.
    • :send uses eth_sendTransaction to call
    • :estimate_gas uses eth_estimateGas to estimate the gas usage of this transaction.
    • :prepare only prepares the data needed to make a transaction. Useful for Multicall.
  • from: The address of the wallet making this transaction. The private key should be loaded in the rpc server (For example: go-ethereum). Must be in "0x..." format.
  • gas: The gas limit for your transaction.
  • rpc_client: The RPC module implementing Ethereum JSON RPC functions. Defaults to Ethereumex.HttpClient
  • rpc_opts: Options to pass to the RCP client e.g. :url.
  • to: The address of the recipient contract. It will be defaulted to default_address if it was specified in Contract otherwise is required. Must be in "0x..." format.

Summary

Types

@type action() :: :call | :send | :prepare
@type t_event_output() :: %{
  topics: [binary()],
  address: Ethers.Types.t_address(),
  selector: ABI.FunctionSelector.t()
}
@type t_function_output() :: %{
  data: binary(),
  to: Ethers.Types.t_address(),
  selector: ABI.FunctionSelector.t()
}