Skip to content

Abi.fromJson

Builds a contract from a JSON ABI. Declared as const, the ABI types the contract. Otherwise each item is read when the program runs.

import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Abi, Address, Transport } from "@ethect/core";
const erc721Abi = [
{
type: "function",
name: "ownerOf",
stateMutability: "view",
inputs: [{ name: "tokenId", type: "uint256" }],
outputs: [{ name: "", type: "address" }],
},
{
type: "event",
name: "Transfer",
inputs: [
{ name: "from", type: "address", indexed: true },
{ name: "to", type: "address", indexed: true },
{ name: "tokenId", type: "uint256", indexed: true },
],
},
{ type: "error", name: "ERC721NonexistentToken", inputs: [{ name: "tokenId", type: "uint256" }] },
] as const;
const Erc721 = Abi.fromJson(erc721Abi);
const nft = Erc721.at(Address.makeUnsafe("0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D"));
// ownerOf(args_0: bigint): one `eth_call`, that can fail with ERC721NonexistentToken
const program = nft.ownerOf(1n);
const owner = await program.pipe(
Effect.provide(Transport.http("https://ethereum-rpc.publicnode.com")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);

Arguments are positional and have no names: ownerOf(args_0: bigint). For named arguments, define the function with Fn.view.

The types are read from the text of the ABI, so the ABI has to keep it. Declare it as const, or write it in the call.

import { Abi } from "@ethect/core";
// declared apart, as an ABI kept in a file of its own is
const wethAbi = [
{ type: "function", name: "deposit", stateMutability: "payable", inputs: [], outputs: [] },
] as const;
const Weth = Abi.fromJson(wethAbi);
// written in the call
const Withdrawals = Abi.fromJson([
{
type: "function",
name: "withdraw",
stateMutability: "nonpayable",
inputs: [{ name: "wad", type: "uint256" }],
outputs: [],
},
]);

A .json file imported by TypeScript loses the text: its type and name are string. Write the ABI in a .ts file to keep it.

An ABI that is only known when the program runs, read from a file or sent by a server, is checked as it is read. Its functions take any arguments and return unknown.

import { Effect } from "effect";
import { Abi, type Address, Call } from "@ethect/core";
declare const loadAbi: Effect.Effect<ReadonlyArray<unknown>>;
const totalSupply = (token: Address.Address) =>
Effect.gen(function* () {
const Token = Abi.fromJson(yield* loadAbi);
// `undefined` when the ABI has no function of that name
const call = Token.at(token)["totalSupply"]?.();
if (call === undefined || !Effect.isEffect(call)) return undefined;
return yield* Call.read(call); // unknown
});

A call of a function that only reads is an effect. The types cannot say which functions those are, so ask the call with Effect.isEffect.

A name that several functions share is one function that takes each of their argument lists. The one called is chosen by the number of arguments, then by their types. Each of them is also there under its signature.

import { Abi, type Address } from "@ethect/core";
const from = { name: "from", type: "address" } as const;
const to = { name: "to", type: "address" } as const;
const tokenId = { name: "tokenId", type: "uint256" } as const;
const Erc721 = Abi.fromJson([
{ type: "function", name: "safeTransferFrom", stateMutability: "nonpayable", inputs: [from, to, tokenId], outputs: [] },
{
type: "function",
name: "safeTransferFrom",
stateMutability: "nonpayable",
inputs: [from, to, tokenId, { name: "data", type: "bytes" }],
outputs: [],
},
]);
const transfers = (collection: Address.Address, owner: Address.Address, buyer: Address.Address) => {
const nft = Erc721.at(collection);
return [
// (args_0: Address, args_1: Address, args_2: bigint), or the same and (args_3: Hex)
nft.safeTransferFrom(owner, buyer, 1n),
nft.safeTransferFrom(owner, buyer, 1n, "0x"),
// one of the two, by its signature
Erc721.fn["safeTransferFrom(address,address,uint256)"].at(collection)(owner, buyer, 1n),
];
};
Several functions take that many arguments the first one, in the order of the ABI, whose types the arguments fit
The arguments fit none a call is still built, and encoding it fails with an AbiError
The functions return different things the call of the name returns one of them. Each signature returns its own
The name itself has name and at(address), and no signature: it is no function to give to Fn.encodeInput
Events or errors share a name each is under its signature, and nothing is under the name
JSON Becomes
type: "function", by its stateMutability Fn.view, Fn.pure, Fn.nonpayable or Fn.payable
no output a call that returns void
one output output: that value
several outputs, all with a name outputs: an object by name
several outputs, one without a name a tuple
tuple whose components all have a name Abi.struct: an object
tuple with a component without a name Abi.tuple
T[], T[3] Abi.array. In the types, an array of 32 items or fewer is a tuple of that length
type: "event" Ev.make, under ev
an input with indexed: true Abi.indexed: as its hash for a string, bytes, an array or a tuple
type: "error" Err.make, under err, and a failure of every function
a field of an event or of an error without a name named by its place: arg0, arg1
constructor, fallback, receive, an event with anonymous: true nothing

Contract.Contract<Fns, Evs, Errs>

A contract, as Contract.make gives:

Member Holds
fn the functions, by name: each a Fn.Fn
ev the events, by name: each the class Ev.make gives
err the errors, by name: each the class Err.make gives
at(address) the functions at an address: each one builds calls
  • Type: ReadonlyArray<unknown>

The ABI, as the compiler writes it: the abi of an artifact, not the artifact.

  • Each definition is the one a definer builds by hand: same signature, same selector, same topic, same bytes.
  • Nothing throws. An item that has a type and a name and is otherwise not one of an ABI gives a definition whose every use fails with an AbiError that says why: transfer: uint7 is not a Solidity type. Such a function is a view, so a call of it is an effect that fails. Such an event or error decodes nothing.
  • What has no type or no name declares nothing. Neither does what is no list.
  • The types check nothing of an ABI. An item they cannot read is left out of them, and a type they do not know is unknown. The program refuses it when it runs.
  • uint and int are 256 bits, as Solidity reads them: the signature says uint256.
  • A type nests 32 arrays and tuples at most. Past that, the definition fails where it is used. An ABI made to nest without end is refused before it is read all the way down.
  • An ABI from before stateMutability says what a function does with constant and payable. A function with constant: true is a view.
  • An item that is there twice declares one definition. Two events of one signature are one too, as the Transfer of ERC-20 and of ERC-721: Solidity declares no such pair in a contract. Build a contract from each ABI.
  • An anonymous event is left out. No topic says which event its logs are.

Abi.fromJson returns a contract and has no failure of its own.

Error When
AbiError encoding a call of a function with an input that is no Solidity type, or whose inputs are no list
AbiError decoding what came back from a function with such an output
AbiError encoding a call whose arguments fit no function of an overloaded name