Skip to content

Call.encode

Encodes a call as a transaction carries it: the address, the calldata and the value.

import { Effect } from "effect";
import { Address, Call, Erc4626 } from "@ethect/core";
const VAULT = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");
const USER = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const program = Call.encode(Erc4626.at(VAULT).deposit(100n, USER));
const encoded = await Effect.runPromise(program);
// {
// to: "0x5fbdb2315678afecb367f032d93f642f64180aa3",
// data: "0x6e553f65000…064000…f39fd6e51aad88f6f4ce6ab8827279cfffb92266",
// value: 0n,
// }

You rarely call it yourself: reading and sending a call encode it. It is for what the library does not do for you: a transaction built by hand, a call handed to another tool, a multicall of your own.

The result is what a transaction request takes.

import { Effect } from "effect";
import { Call, type Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const gasOf = (from: Address.Address, call: Call.Call<unknown, unknown>) =>
Effect.gen(function* () {
const { to, data, value } = yield* Call.encode(call);
return yield* Eth.estimateGas({ from, ...(to && { to }), data, value });
});

Effect<Call.Encoded, AbiError>

Call.Encoded Type Is
to Address.Address | undefined the address called, absent for a deployment
data Hex.Hex the calldata: the selector and the arguments, or the bytes of a raw call
value bigint the ether the call carries, in wei

It needs no service: no node is asked.

  • Type: Call.Call<any, any>

Any call: of a function that reads or writes, or a raw call.

  • This is the only place a call is encoded. Building a call encodes nothing, and reading or sending one goes through this.
  • The calldata of a call of a function is what Fn.encodeInput gives for its function and its arguments.
  • A raw call is given back as it is. Its bytes are not read.
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