Skip to content

Fn.parse

Builds one function from its human-readable signature.

import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Fn, Transport } from "@ethect/core";
const balanceOf = Fn.parse("function balanceOf(address owner) view returns (uint256)");
const USDC = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
const USER = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
// balanceOf(args_0: Address): one `eth_call`
const program = balanceOf.at(USDC)(USER);
const balance = await program.pipe(
Effect.provide(Transport.http("https://ethereum-rpc.publicnode.com")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);

Arguments are positional and have no names: balanceOf(args_0: Address). For named arguments, define the function with Fn.view.

The signature is read with the grammar of Abi.parse, and the function is the one Abi.parse gives for it. It declares no error: the errors of a contract come with the contract.

A signature that uses a struct by its name takes the struct beside it, as its own signature.

import { Fn, type Address } from "@ethect/core";
const fill = Fn.parse("function fill(Order order) payable returns (bool)", {
structs: [
"struct Order { address maker; uint256 amount; Leg[] legs; }",
"struct Leg { uint8 kind; bytes data; }",
],
});
const call = (exchange: Address.Address, maker: Address.Address) =>
// a struct is an object of its members, and a payable function takes a value
fill.at(exchange)({ maker, amount: 100n, legs: [{ kind: 1, data: "0x" }] }, { value: 1n });

It does not compile, and neither does a signature that is not one of a function.

import { Fn } from "@ethect/core";
// @ts-expect-error adress is not a type: it is no Solidity type, and no struct of that name is declared
const transfer = Fn.parse("function transfer(adress to, uint256 amount)");
// @ts-expect-error Order is not a type: it is no Solidity type, and no struct of that name is declared
const fill = Fn.parse("function fill(Order order)");
// @ts-expect-error Fn.parse takes the signature of a function
const Transfer = Fn.parse("event Transfer(address indexed from, address indexed to, uint256 value)");

Fn.Fn<Name, Args, A, never, M>

The function: its name, signature, selector and mutability, and at(address), which builds its calls.

Type parameter Is
Name the name in the signature, as a literal
Args the arguments of a call, by position
A what a call returns: nothing, the one value, an object of several values that all have a name, or a tuple
M view, pure, payable or nonpayable, as the signature says. Without a word, nonpayable

A signature that is not a literal, such as a string read from a file, gives a function that takes any arguments and returns unknown.

  • Type: string

The signature of the function, as Solidity declares it: function name(type [location] [name], …) [visibility] [mutability] [returns (type [name], …)].

  • Type: ReadonlyArray<string>

The structs the signature uses by name, each as struct Name { type name; … }. A struct can use another of the list.

  • A signature that does not parse does not compile, and the message says why. When the program runs it gives a function whose every use fails with an AbiError that quotes the signature and says where it stops. It is a view, so a call of it is an effect that fails. Nothing throws.
  • The function is the one a definer builds by hand: same signature, same selector, same bytes.
  • Names are read and not kept, except those of several returned values and of the members of a struct.

Fn.parse returns a function and has no failure of its own.

Error When
AbiError encoding a call of, or decoding what came back from, a function whose signature does not parse