ERCs

ERC-20 — Fungible Token Standard

eips.ethereum.org/EIPS/eip-20

import {
  balance_of,
  total_supply,
  transfer,
  approve,
  allowance,
  transfer_from,
} from "@ethernauta/erc/20";

Core methods

MethodShapePurpose
nameCallable<string>Token name.
symbolCallable<string>Token symbol.
decimalsCallable<number>Decimals exponent.
total_supplyCallable<Uint256>Total supply.
balance_of({ owner })Callable<Uint256>Balance of an account.
allowance({ owner, spender })Callable<Uint256>Approval amount.
transfer({ to, amount })Signable<Hash32>Send tokens.
approve({ spender, amount })Signable<Hash32>Grant allowance.
transfer_from({ from, to, amount })Signable<Hash32>Transfer using allowance.

Reading a balance

import { create_contract } from "@ethernauta/transport";
import { balance_of, decimals } from "@ethernauta/erc/20";
import { format_unit } from "@ethernauta/utils";
import { eip155_1 } from "@ethernauta/chain";

const contract = create_contract([eip155_1]);

const ctx = contract({
  chain_id: eip155_1.chain_id,
  contract: usdc_address,
});

const raw = await balance_of({ owner: holder })(ctx);
const d = await decimals()(ctx);

console.log(format_unit(raw, d), "USDC");

Transferring

import { transfer } from "@ethernauta/erc/20";
import { create_signer } from "@ethernauta/transport";

const signer = create_signer([eip155_1]);

const hash = await transfer({
  to: recipient,
  amount: parse_unit("100", 6),
})(signer({ chain_id: eip155_1.chain_id, contract: usdc_address }));

Wallet handles nonce + gas; to is the token contract, not the recipient. The to recipient is in the call arguments.

Extensions

ERC-20 has several optional extensions, each at a sub-subpath:

SubpathMethods
@ethernauta/erc/20/burnableburn, burn_from
@ethernauta/erc/20/cappedcap
@ethernauta/erc/20/metadataname, symbol, decimals (re-export)
@ethernauta/erc/20/mintablemint
@ethernauta/erc/20/pausablepause, unpause, paused
@ethernauta/erc/20/wrapperdeposit_for, withdraw_to (wrapped-token pattern)

Import only what you need; tree-shaking removes the rest.

See also