Functions (Fn)
A function of a contract as a definition: its name, the types it takes and returns, and the
errors it reverts with. A definition at an address builds calls, and
encodes nothing until a call is read or sent. There is one definer per mutability, and only a
view or a pure function gives a call that is also the effect of reading it.
import { Effect } from "effect";import { Abi, Address, Fn, type _ } from "@ethect/core";
// function balanceOf(address owner) view returns (uint256)const balanceOf = Fn.view<[owner: _]>()("balanceOf", { inputs: [Abi.address], output: Abi.uint256,});
const USDC = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");const USER = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const program = Effect.gen(function* () { const call = balanceOf.at(USDC)(USER); // a call: plain data, nothing is sent return yield* call; // reading it: one `eth_call`});| Export | Kind | Description |
|---|---|---|
Fn.view<Names>()(name, definition) |
function | Defines a function that reads the chain. |
Fn.pure<Names>()(name, definition) |
function | Defines a function that reads nothing. |
Fn.nonpayable<Names>()(name, definition) |
function | Defines a function that writes. |
Fn.payable<Names>()(name, definition) |
function | Defines a function that writes and takes ether: its calls take { value } as a last argument. |
Fn.parse(signature) |
function | A function from its human-readable signature. |
fn.at(address)(...args) |
member | The function at a contract: builds a call. |
Fn.encodeInput(fn, ...args) |
function | The calldata of a call: the selector, then the arguments. |
Fn.decodeOutput(fn, data) |
function | What the function returned, from the bytes a node answered. |
Fn.Fn<Name, Args, A, E, M> |
type | A function: name, signature, selector, mutability and at(address). |
Fn.Any |
type | Any function, whatever it takes and returns. |
Fn.Mutability |
type | "pure" | "view" | "nonpayable" | "payable". |
Fn.Payment |
type | What a call of a payable function can carry: { value? }. |
_ |
type | Stands for the type of an input whose name alone is said, as in Fn.view<[owner: _]>(). Exported by @ethect/core itself. |
What a function holds
Section titled “What a function holds”A definition is a plain object. Its signature and its selector are computed once, where it is defined.
import { Abi, Fn, type _ } from "@ethect/core";
// function deposit(uint256 assets, address receiver) returns (uint256)const deposit = Fn.nonpayable<[assets: _, receiver: _]>()("deposit", { inputs: [Abi.uint256, Abi.address], output: Abi.uint256,});
deposit.name; // "deposit"deposit.signature; // "deposit(uint256,address)"deposit.selector; // "0x6e553f65"deposit.mutability; // "nonpayable"| Member | Type | Holds |
|---|---|---|
name |
the name you gave, as a literal | the name of the function |
signature |
string |
the name and the types of the inputs, as the selector is hashed from |
selector |
Hex.Hex |
the first four bytes of the hash of the signature: what calldata starts with |
mutability |
Fn.Mutability |
which definer built it |
at(address) |
function | the function at a contract |
A function also holds the types of its inputs and outputs and its errors, under a key the library reads. You do not build a function by hand: a definer does.
Mutability
Section titled “Mutability”The definer says what a call of the function is.
| Definer | Solidity | A call is | Takes ether |
|---|---|---|---|
Fn.view |
view |
a Call.View: also the effect of reading it |
no |
Fn.pure |
pure |
a Call.View |
no |
Fn.nonpayable |
neither | a Call.Call: plain data, to send |
no |
Fn.payable |
payable |
a Call.Call |
yes, with { value } |
A definer takes the names of the inputs as a type argument: a tuple whose labels are the names.
The types of that tuple are not read, since inputs gives them. _ is what to write in their
place.
import { Abi, Fn, type _ } from "@ethect/core";
// transfer(to: Address, amount: bigint)const transfer = Fn.nonpayable<[to: _, amount: _]>()("transfer", { inputs: [Abi.address, Abi.uint256],});// approve(args_0: Address, args_1: bigint)const approve = Fn.nonpayable()("approve", { inputs: [Abi.address, Abi.uint256] });Without names a function takes the same arguments, shown as args_0, args_1. The names must
be one for each input: any other number does not compile.