Fn.encodeInput
Encodes the arguments of a function to the calldata of a call: the selector, then the arguments.
import { Effect } from "effect";import { Abi, Address, 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,});
const RECEIVER = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const program = Fn.encodeInput(deposit, 100n, RECEIVER);
const data = await Effect.runPromise(program);// 0x6e553f65// 0000000000000000000000000000000000000000000000000000000000000064// 000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266You rarely call it yourself: reading or sending a call encodes it.
Call.encode gives the same bytes with the address and the value
of a call.
An argument that does not fit
Section titled “An argument that does not fit”A failure is a value. It names the function, the argument and why.
import { Effect } from "effect";import { Abi, Fn, type Address, type _ } from "@ethect/core";
const deposit = Fn.nonpayable<[assets: _, receiver: _]>()("deposit", { inputs: [Abi.uint256, Abi.address],});
const message = (receiver: Address.Address) => Fn.encodeInput(deposit, 2n ** 256n, receiver).pipe( Effect.catchTag("AbiError", (error) => Effect.succeed(error.message)), );// "deposit: Cannot encode uint256, value 0 of (uint256,address):// 1157…9936 does not fit in uint256"Returns
Section titled “Returns”Effect<Hex.Hex, AbiError>
The calldata, as lowercase hex. It needs no service: no node is asked.
Parameters
Section titled “Parameters”- Type:
Fn.Fn<string, Args, any, any, any>
The function, of any mutability.
- Type:
Args, the arguments of the function
One value for each input. A payable function takes no { value } here: the value of a call is
not in its calldata.
import { Fn } from "@ethect/core";
const deposit = Fn.payable()("deposit", { inputs: [] });const data = Fn.encodeInput(deposit); // "0xd0e30db0": the selector alone// @ts-expect-error a payment is not an argumentconst wrong = () => Fn.encodeInput(deposit, { value: 1n });Behavior
Section titled “Behavior”- The selector comes first, four bytes. The arguments follow as
Abi.encodewrites a list of values. - Every argument is checked before a byte is written. What each type checks is in this table.
Errors
Section titled “Errors”| Error | When |
|---|---|
AbiError |
an argument is not a value its type takes |
AbiError |
the number of arguments is not the number of inputs |
AbiError |
an input of the function is not a Solidity type |