CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-web3-eth-abi

Web3 module encode and decode EVM in/output.

Pending
Overview
Eval results
Files

function-operations.mddocs/

Function Operations

Core functionality for encoding function calls and decoding function return values. Essential for smart contract interactions and transaction building.

Capabilities

Encode Function Signature

Encodes the function name to its ABI representation, which are the first 4 bytes of the sha3 hash of the function signature.

/**
 * Encodes the function name to its ABI representation
 * @param functionName - The function name to encode or the JSON interface object of the function
 * @returns The ABI signature of the function (4 bytes)
 */
function encodeFunctionSignature(functionName: string | AbiFunctionFragment): string;

Usage Examples:

import { encodeFunctionSignature } from "web3-eth-abi";

// Using function name string
const signature = encodeFunctionSignature('myMethod(uint256,string)');
console.log(signature); // 0x24ee0097

// Using JSON interface object
const signature2 = encodeFunctionSignature({
  name: "myMethod",
  type: "function",
  inputs: [
    { type: "uint256", name: "myNumber" },
    { type: "string", name: "myString" }
  ]
});
console.log(signature2); // 0x24ee0097

Encode Function Call

Encodes a function call using its JSON interface object and given parameters. Returns the complete encoded function call including signature and parameters.

/**
 * Encodes a function call using its JSON interface object and given parameters
 * @param jsonInterface - The JSON interface object of the function
 * @param params - The parameters to encode
 * @returns The ABI encoded function call (signature + parameters)
 */
function encodeFunctionCall(
  jsonInterface: AbiFunctionFragment,
  params: unknown[]
): string;

Usage Examples:

import { encodeFunctionCall } from "web3-eth-abi";

// Encode a function call with parameters
const encodedCall = encodeFunctionCall(
  {
    name: "myMethod",
    type: "function",
    inputs: [
      { type: "uint256", name: "myNumber" },
      { type: "string", name: "myString" }
    ]
  },
  ["2345675643", "Hello!%"]
);
console.log(encodedCall);
// 0x24ee0097000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000748656c6c6f212500000000000000000000000000000000000000000000000000

// Encode balanceOf function call
const balanceCall = encodeFunctionCall(
  {
    inputs: [{ name: "account", type: "address" }],
    name: "balanceOf",
    outputs: [{ name: "", type: "uint256" }],
    stateMutability: "view",
    type: "function"
  },
  ["0x1234567890123456789012345678901234567890"]
);
console.log(balanceCall);
// 0x70a082310000000000000000000000001234567890123456789012345678901234567890

Decode Function Call

Decodes a function call data using its JSON interface object. Extracts the parameters from encoded function call data.

/**
 * Decodes a function call data using its JSON interface object
 * @param functionsAbi - The JSON interface object of the function
 * @param data - The data to decode
 * @param methodSignatureProvided - If false, do not remove the first 4 bytes
 * @returns The decoded parameters with method signature
 */
function decodeFunctionCall(
  functionsAbi: AbiFunctionFragment | AbiConstructorFragment,
  data: HexString,
  methodSignatureProvided?: boolean
): DecodedParams & { __method__: string };

Usage Examples:

import { decodeFunctionCall } from "web3-eth-abi";

const data = '0xa413686200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010416e6f74686572204772656574696e6700000000000000000000000000000000';

const decodedParams = decodeFunctionCall(
  {
    inputs: [
      { internalType: 'string', name: '_greeting', type: 'string' },
      { internalType: 'string', name: '_second_greeting', type: 'string' }
    ],
    name: 'setGreeting',
    outputs: [
      { internalType: 'bool', name: '', type: 'bool' },
      { internalType: 'string', name: '', type: 'string' }
    ],
    stateMutability: 'nonpayable',
    type: 'function'
  },
  data
);

console.log(decodedParams);
// {
//   '0': 'Hello',
//   '1': 'Another Greeting',
//   __length__: 2,
//   __method__: 'setGreeting(string,string)',
//   _greeting: 'Hello',
//   _second_greeting: 'Another Greeting'
// }

Decode Function Return

Decodes function return values using the function's JSON interface object. Handles both single and multiple return values.

/**
 * Decodes function return values using its JSON interface object
 * @param functionsAbi - The JSON interface object of the function
 * @param returnValues - The function return data to decode
 * @returns The decoded return values (single value or object for multiple)
 */
function decodeFunctionReturn(
  functionsAbi: AbiFunctionFragment,
  returnValues?: HexString
): unknown;

Usage Examples:

import { decodeFunctionReturn } from "web3-eth-abi";

// Decode multi-value return
const data1 = '0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000';

const decodedMulti = decodeFunctionReturn(
  {
    inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }],
    name: 'setGreeting',
    outputs: [
      { internalType: 'string', name: '', type: 'string' },
      { internalType: 'bool', name: '', type: 'bool' }
    ],
    stateMutability: 'nonpayable',
    type: 'function'
  },
  data1
);
console.log(decodedMulti); // { '0': 'Hello', '1': true, __length__: 2 }

// Decode single-value return
const data2 = '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000';

const decodedSingle = decodeFunctionReturn(
  {
    inputs: [{ internalType: 'string', name: '_greeting', type: 'string' }],
    name: 'setGreeting',
    outputs: [{ internalType: 'string', name: '', type: 'string' }],
    stateMutability: 'nonpayable',
    type: 'function'
  },
  data2
);
console.log(decodedSingle); // 'Hello'

Types

interface AbiFunctionFragment {
  type: "function";
  name?: string;
  inputs?: AbiInput[];
  outputs?: AbiOutput[];
  stateMutability?: "pure" | "view" | "nonpayable" | "payable";
}

interface AbiConstructorFragment {
  type: "constructor";
  inputs?: AbiInput[];
  stateMutability?: "nonpayable" | "payable";
}

interface AbiInput {
  name?: string;
  type: string;
  components?: AbiInput[];
}

interface AbiOutput {
  name?: string;
  type: string;
  components?: AbiOutput[];
}

interface DecodedParams {
  [key: string]: unknown;
  __length__: number;
}

type HexString = string;

Install with Tessl CLI

npx tessl i tessl/npm-web3-eth-abi

docs

eip-712.md

error-handling.md

event-processing.md

function-operations.md

index.md

parameter-processing.md

tile.json