Skip to main content
Encodes a function signature and parameters into transaction calldata using ABI encoding.

Signature

Parameters

  • signature - Function signature (e.g., "transfer(address,uint256)")
  • params - Array of parameter values matching signature types

Returns

CallDataType - Encoded calldata with selector and ABI-encoded parameters

Examples

import { Abi, Address, TokenBalance } from '@tevm/voltaire';

const abi = Abi([{
  name: "transfer",
  type: "function",
  inputs: [
    { name: "to", type: "address" },
    { name: "amount", type: "uint256" }
  ]
}]);

const calldata = abi.transfer.encode(
  Address("0x70997970C51812dc3A010C7d01b50e0d17dc79C8"),
  TokenBalance.fromUnits("1", 18)
);

console.log(CallData.toHex(calldata));
// "0xa9059cbb"  // transfer selector
// + "00000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8"  // address
// + "0000000000000000000000000000000000000000000000000de0b6b3a7640000"  // uint256

Function Signatures

Signatures must use canonical form:
// Correct format
"transfer(address,uint256)"          // ✅ No spaces
"balanceOf(address)"                 // ✅ Single param
"swap((address,address,uint256))"   // ✅ Tuple notation
"multicall(bytes[])"                 // ✅ Dynamic array

Type Conversion

Parameters automatically convert to ABI types:
import { Abi } from '@tevm/voltaire';

const abi = Abi([{
  name: "setValue",
  type: "function",
  inputs: [
    { name: "addr", type: "address" },
    { name: "value", type: "uint256" },
    { name: "flag", type: "bool" }
  ]
}]);

// Accepts various input types
const calldata = abi.setValue.encode(
  "0x70997970C51812dc3A010C7d01b50e0d17dc79C8", // String → Address
  1000000000000000000n,                        // bigint → Uint256
  true                                          // boolean → bool
);

Use in Transactions

import {
  Transaction, Abi, Address, TokenBalance,
  Nonce, GasLimit, Wei, Gwei, ChainId
} from '@tevm/voltaire';

const abi = Abi([{
  name: "transfer",
  type: "function",
  inputs: [
    { name: "to", type: "address" },
    { name: "amount", type: "uint256" }
  ]
}]);

// Encode function call
const calldata = abi.transfer.encode(
  Address("0x70997970C51812dc3A010C7d01b50e0d17dc79C8"),
  TokenBalance.fromUnits("1", 18)
);

// Create transaction
const tx = Transaction({
  type: Transaction.Type.EIP1559,
  to: Address("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"), // USDC
  data: calldata,
  value: Wei(0),
  chainId: ChainId(1),
  nonce: Nonce(0),
  maxFeePerGas: Gwei(30),
  maxPriorityFeePerGas: Gwei(2),
  gasLimit: GasLimit(100000),
});

Validation

Validates parameters match signature:
import { Abi } from '@tevm/voltaire';

const abi = Abi([{
  name: "transfer",
  type: "function",
  inputs: [
    { name: "to", type: "address" },
    { name: "amount", type: "uint256" }
  ]
}]);

// Wrong number of parameters
try {
  abi.transfer.encode(Address("0x...")); // Missing amount
} catch (error) {
  console.error("Parameter count mismatch");
}

// Wrong type
try {
  abi.transfer.encode(
    "not an address",  // Invalid address format
    TokenBalance.fromUnits("1", 18)
  );
} catch (error) {
  console.error("Invalid address");
}

Performance

Encoding is optimized in WASM:
// Benchmark: 1M iterations
console.time("encode");
for (let i = 0; i < 1_000_000; i++) {
  abi.transfer.encode(recipient, amount);
}
console.timeEnd("encode");

// Pure JS: ~850ms
// WASM (ReleaseSmall): ~320ms (2.6x faster)
// WASM (ReleaseFast): ~180ms (4.7x faster)

Selector Computation

Encoding automatically computes function selector:
import { Keccak256 } from '@tevm/voltaire';

// Manual selector computation
const signature = "transfer(address,uint256)";
const hash = Keccak256.hashString(signature);
const selector = hash.slice(0, 4);

console.log(selector);
// [0xa9, 0x05, 0x9c, 0xbb]

// Encoding includes this automatically
const calldata = abi.transfer.encode(recipient, amount);
console.log(CallData.getSelector(calldata));
// [0xa9, 0x05, 0x9c, 0xbb] (same)
  • decode - Decode calldata back to parameters
  • Abi - ABI encoding and decoding
  • Transaction - Building transactions
  • Encoding - ABI encoding mechanics