Skip to content

fn.at

Gives a function at a contract. Calling the result with the arguments builds a call: a plain value that nothing has encoded yet.

import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Abi, Address, Call, Fn, Transport, type _ } from "@ethect/core";
const balanceOf = Fn.view<[owner: _]>()("balanceOf", {
inputs: [Abi.address],
output: Abi.uint256,
});
const transfer = Fn.nonpayable<[to: _, amount: _]>()("transfer", {
inputs: [Abi.address, Abi.uint256],
output: Abi.bool,
});
const USDC = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
const USER = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const program = Effect.gen(function* () {
// a view: the call is also the effect of reading it
const balance = yield* balanceOf.at(USDC)(USER);
// a write: the call is plain data, to encode or to send
const call = transfer.at(USDC)(USER, balance);
return yield* Call.encode(call); // { to, data, value }
});
await program.pipe(
Effect.provide(Transport.http("https://ethereum-rpc.publicnode.com")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);

at(address) returns a function. Keep it to build several calls of the same contract.

import { Abi, Fn, type Address, type _ } from "@ethect/core";
const balanceOf = Fn.view<[owner: _]>()("balanceOf", {
inputs: [Abi.address],
output: Abi.uint256,
});
const balances = (token: Address.Address, owners: ReadonlyArray<Address.Address>) => {
const balance = balanceOf.at(token);
return owners.map((owner) => balance(owner));
};

A Contract binds all its functions at once.

A call of a payable function takes one more argument, { value }, in wei.

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

(...args) => Call.View<A, E> for a view or a pure function, (...args) => Call.Call<A, E> for the others.

A function that takes the arguments of the function, under the names its definition gave, and returns a call.

A call holds Type Is
fn Fn.Any the function
to Address.Address the address given to at
args ReadonlyArray<unknown> the arguments, as they were given
value bigint the value of a payable call, 0n otherwise

The address of the contract. A plain string is not accepted: build it with Address.makeUnsafe, or with Address.make when it comes from outside.

  • Type: the arguments of the function

One value for each input, of the TypeScript type of its Solidity type.

import { Abi, Fn, type Address, type _ } from "@ethect/core";
const transfer = Fn.nonpayable<[to: _, amount: _]>()("transfer", {
inputs: [Abi.address, Abi.uint256],
});
declare const to: Address.Address;
const call = transfer.at(to)(to, 100n);
// @ts-expect-error an amount is a bigint
const wrong = () => transfer.at(to)(to, 100);
// @ts-expect-error both arguments are needed
const short = () => transfer.at(to)(to);
  • Type: bigint

The ether the call carries, in wei. Only a payable function has this argument.

import { Abi, Fn, type Address, type _ } from "@ethect/core";
const approve = Fn.nonpayable<[spender: _]>()("approve", { inputs: [Abi.address] });
// @ts-expect-error a function that is not payable takes no value
const paid = (token: Address.Address) => approve.at(token)(token, { value: 1n });
  • It never fails and never encodes. The arguments are kept as you gave them. A value its type refuses is found when the call is read, encoded or sent, as an AbiError.
  • A call is a value. Build it where you like, keep it, put it in a list. Nothing reaches a node until it is read or sent.
  • A view is read by running it. yield* call is Call.read(call). Pass options with Call.read(call, options).
  • A write is not an effect. yield* on it does not compile. It is sent with Tx.
  • The value is there when there are more arguments than inputs. That is how a payable call with no input, deposit({ value }), is told from a call without a value.

Building a call sends nothing. Running a call of a view is one read:

  1. Call.Readereth_call({ to, data }, "latest")

Call.read says what is sent with each option.