Fireblocks.Evm.Contract (Fireblocks Contract v0.1.1)

Copy Markdown View Source

Dynamically creates modules for Solidity ABIs at compile time and provides helpers for reading and writing contract state via the Fireblocks API.

How to use

Create a new module and call use Fireblocks.Evm.Contract with the desired parameters:

# Using an ABI file
defmodule MyApp.Contracts.ERC20 do
  use Fireblocks.Evm.Contract, abi_file: "priv/erc20-abi.json"
end

# Providing a default contract address
defmodule MyApp.Contracts.ERC20 do
  use Fireblocks.Evm.Contract,
    abi_file: "priv/erc20-abi.json",
    default_address: "0x1234...9999"
end

# Using an ABI directly (decoded JSON)
defmodule MyApp.Contracts.ERC20 do
  use Fireblocks.Evm.Contract, abi: [%{"type" => "function", ...}]
end

After this, every ABI function is accessible as an Elixir function that returns an %Fireblocks.Evm.Contract.Call{} struct:

call = MyApp.Contracts.ERC20.name()
# %Fireblocks.Evm.Contract.Call{function: "name", inputs: [], ...}

Pass that struct to Fireblocks.Evm.Contract.read/2 or Fireblocks.Evm.Contract.write/3:

MyApp.Contracts.ERC20.name()
|> Fireblocks.Evm.Contract.read(
  baseAssetId: "ETH",
  contractAddress: "0xA0b8...FeAb"
)
# {:ok, "My Token"}

Valid use options

  • :abi – decoded ABI list (list of maps) or encoded JSON binary.
  • :abi_file – path to the JSON ABI file (relative to the project root).
  • :default_address – default deployed contract address. (optional)
  • :default_base_asset_id – default Fireblocks base asset ID, e.g. "ETH". (optional)

Summary

Functions

Reads contract state by calling a view/pure ABI function via the Fireblocks API.

Writes contract state by calling a non-view ABI function via the Fireblocks API.

Functions

read(call, opts)

@spec read(Fireblocks.Evm.Contract.Call.t(), Keyword.t()) ::
  {:ok, any()} | {:error, any()}

Reads contract state by calling a view/pure ABI function via the Fireblocks API.

Parameters

  • call – an %Fireblocks.Evm.Contract.Call{} struct returned by a generated contract function.
  • opts – keyword options:
  • :baseAssetId (String.t/0) - Required. Base assetId e.g. ETH, ETH_TEST5

  • :contractAddress (String.t/0) - Required. Deployed contract address

  • :idempotentKey (String.t/0) - Optional idempotency key

Example

MyApp.Contracts.ERC20.name()
|> Fireblocks.Evm.Contract.read(
  baseAssetId: "ETH",
  contractAddress: "0x..."
)
# {:ok, "My Token"}

write(call, opts)

@spec write(Fireblocks.Evm.Contract.Call.t(), Keyword.t()) ::
  {:ok, any()} | {:error, any()}

Writes contract state by calling a non-view ABI function via the Fireblocks API.

This creates an on-chain transaction and returns a transaction ID that can be polled for status.

Parameters

  • call – an %Fireblocks.Evm.Contract.Call{} struct returned by a generated contract function.
  • opts – keyword options:
  • :baseAssetId (String.t/0) - Required. Base assetId e.g. ETH, ETH_TEST5

  • :contractAddress (String.t/0) - Required. Deployed contract address

  • :vaultId (String.t/0) - Required. Vault account ID this contract was deployed from

  • :amount (String.t/0) - Amount in base asset (for payable functions)

  • :feeLevel - Fee level for the transaction The default value is :medium.

  • :fee (String.t/0) - Max fee amount (interchangeable with feeLevel)

  • :note (String.t/0) - Custom note visible in the Fireblocks workspace

  • :useGasless (boolean/0) - The default value is false.

  • :externalId (String.t/0) - External identifier (max 255 characters)

  • :idempotentKey (String.t/0) - Optional idempotency key

Example

MyApp.Contracts.ERC20.transfer("0xDest...", 1_000)
|> Fireblocks.Evm.Contract.write(
  baseAssetId: "ETH",
  contractAddress: "0x...",
  vaultId: "1",
  feeLevel: :medium,
  note: "monthly distribution",
  useGasless: false
)