Skip to content

Fn.view

Defines a function of a contract. There is one definer per mutability, Fn.view, Fn.pure, Fn.nonpayable and Fn.payable, and they have the same shape.

import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Abi, Address, Fn, Transport, 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* () {
// balanceOf.at(usdc)(owner: Address): the name comes from the type argument
return yield* balanceOf.at(USDC)(USER);
});
const balance = await program.pipe(
Effect.provide(Transport.http("https://ethereum-rpc.publicnode.com")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);

A definer is called three times: with the names of the inputs as a type argument, with no argument, then with the name and the definition. The examples below leave the transport out.

outputs takes the values a function returns by name. The result is an object of them.

import { Effect } from "effect";
import { Abi, Address, Fn } from "@ethect/core";
// function getReserves() view
// returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast)
const getReserves = Fn.view()("getReserves", {
inputs: [],
outputs: { reserve0: Abi.uint112, reserve1: Abi.uint112, blockTimestampLast: Abi.uint32 },
});
const PAIR = Address.makeUnsafe("0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc");
const price = Effect.gen(function* () {
const { reserve0, reserve1 } = yield* getReserves.at(PAIR)();
return reserve1 / reserve0;
});

A struct is one value. Give it as output, or among the inputs: a plain struct is an object, and an Abi.Class comes back as an instance of the class.

import { Effect } from "effect";
import { Abi, Address, Fn, type Hex, type _ } from "@ethect/core";
class Position extends Abi.Class<Position>("Position")({
supplyShares: Abi.uint256,
borrowShares: Abi.uint128,
collateral: Abi.uint128,
}) {
get isEmpty() {
return this.supplyShares === 0n && this.borrowShares === 0n && this.collateral === 0n;
}
}
// function position(bytes32 id, address user) view returns (Position memory)
const position = Fn.view<[id: _, user: _]>()("position", {
inputs: [Abi.bytes32, Abi.address],
output: Position,
});
const MORPHO = Address.makeUnsafe("0xBBBBBbbBBb9cC5e90e3b3Af64bdAF62C37EEFFCb");
const USER = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const isEmpty = (market: Hex.Hex) =>
Effect.gen(function* () {
return (yield* position.at(MORPHO)(market, USER)).isEmpty;
});

errors takes the custom errors the function reverts with. A call then fails with them as typed failures, in a read and in a send.

import { Effect, Option } from "effect";
import { Abi, Address, Err, Fn, type _ } from "@ethect/core";
class NonexistentToken extends Err.make("ERC721NonexistentToken", {
tokenId: Abi.uint256,
}) {}
// function ownerOf(uint256 tokenId) view returns (address)
const ownerOf = Fn.view<[tokenId: _]>()("ownerOf", {
inputs: [Abi.uint256],
output: Abi.address,
errors: [NonexistentToken],
});
const owner = (nft: Address.Address, tokenId: bigint) =>
ownerOf
.at(nft)(tokenId)
.pipe(
Effect.map(Option.some),
Effect.catchTag("ERC721NonexistentToken", () => Effect.succeed(Option.none())),
);

A function that reads the chain: view in Solidity. A call of it is a Call.View: plain data, and the effect of reading it.

import { Abi, Fn } from "@ethect/core";
// function totalAssets() view returns (uint256)
const totalAssets = Fn.view()("totalAssets", { inputs: [], output: Abi.uint256 });

A function that reads nothing: pure in Solidity. A node still runs it, so a call of it is a Call.View too.

import { Abi, Fn, type _ } from "@ethect/core";
// function mulDiv(uint256 x, uint256 y, uint256 d) pure returns (uint256)
const mulDiv = Fn.pure<[x: _, y: _, d: _]>()("mulDiv", {
inputs: [Abi.uint256, Abi.uint256, Abi.uint256],
output: Abi.uint256,
});

A function that writes, and takes no ether: neither view nor payable in Solidity. A call of it is a Call.Call: plain data, to send as a transaction. It is not an effect.

import { Abi, Fn, type _ } from "@ethect/core";
// function approve(address spender, uint256 amount) returns (bool)
const approve = Fn.nonpayable<[spender: _, amount: _]>()("approve", {
inputs: [Abi.address, Abi.uint256],
output: Abi.bool,
});

A function that writes and takes ether: payable in Solidity. Its calls take one more argument, { value }, in wei.

import { Fn, type Address } from "@ethect/core";
// function deposit() payable
const deposit = Fn.payable()("deposit", { inputs: [] });
const wrap = (weth: Address.Address) => [
deposit.at(weth)({ value: 10n ** 18n }), // one ether
deposit.at(weth)(), // none
];

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

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

Type parameter Is
Name the name, as a literal
Args the arguments of a call, under the names you gave
A what a call returns: the type of output, an object of outputs, or void
E the instances of errors
M the mutability of the definer
  • Type: string

The name of the function, as the contract has it. The selector is hashed from it and from the types of the inputs.

  • Type: ReadonlyArray<Abi.Type<any>>

The Solidity types of the arguments, in order. An empty array for a function that takes none.

  • Type: Abi.Type<A>

The type of the one value the function returns, a struct included. Without output and without outputs, a call returns void.

  • Type: Abi.Fields

The types of several returned values, by name, in the order the function returns them. It takes over output when both are given.

  • Type: ReadonlyArray<Err.Any>

The custom errors the function reverts with.

  • Nothing is encoded or checked where a function is defined. A definer cannot fail and does not throw.
  • A definition that is not one fails where it is used. An input that is no Solidity type, or a returned value named by a number, fails every encoding or decoding with an AbiError that says why.
  • Several returned values are not a tuple. outputs: { a, b } reads two values, as returns (A a, B b) writes them. One output that is an Abi.tuple or a struct reads one value.
  • The names are for you. They are not sent, and they are not part of the signature.
  • One name, one function. Two functions of one name with other inputs are two definitions, under two keys of your own.