To perform read-only smart contract calls, use the eth_call/1 method. You must provide a data field containing the ABI-encoded method signature and arguments, and a to field containing the contract address.
Note: The data string must be hex-encoded and typically prefixed with 0x. The contract address should be provided as a string.
It is recommended to use a library like ex_abi to handle the encoding of method signatures and arguments.
defp deps do
[
{:ethereumex, "~> 0.9"},
{:ex_abi, "~> 0.5"}
]
end
# Example: Calling balanceOf(address)
address = "0xF742d4cE7713c54dD701AA9e92101aC42D63F895" |> String.slice(2..-1) |> Base.decode16!(case: :mixed)
contract_address = "0xC28980830dD8b9c68a45384f5489ccdAF19D53cC"
abi_encoded_data = ABI.encode("balanceOf(address)", [address]) |> Base.encode16(case: :lower)
balance_bytes = Ethereumex.HttpClient.eth_call(%{
data: "0x" <> abi_encoded_data,
to: contract_address
})