Function decode

  • Decode an ABI encoded buffer with the specified types. The types must be valid Solidity ABI types.

    This will attempt to infer the output types from the input types. For example, if you use uint256 as an input type, the output type will be bigint. This does not work for all types, however. For example, if you use nested array types or tuple types, the output type will be unknown.

    The resulting types of the values will be as follows:

    Contract ABI Type Resulting JavaScript Type
    address string
    bool boolean
    bytes(n) Uint8Array
    function SolidityFunction
    int(n) bigint
    string string
    tuple Array
    array Array
    uint(n) bigint

    Example

    import { encode, decode } from '@metamask/abi-utils';

    const types = ['uint256', 'string'];
    const encoded = encode(types, [42, 'Hello, world!']);
    const decoded = decode(types, encoded);

    console.log(decoded); // [42n, 'Hello, world!']

    See

    https://docs.soliditylang.org/en/v0.8.17/abi-spec.html#types

    Returns

    The decoded values as array.

    Type Parameters

    • Type extends readonly string[]

    • Output = TypeMap<Type, "output">

    Parameters

    • types: Type

      The types to decode the bytes with.

    • value: Uint8Array | `0x${string}`

      The bytes-like value to decode.

    Returns Output