Function encodePacked

  • Encode the data with the provided types. The types must be valid Solidity ABI types. This is similar to encode, but the values are encoded in the non-standard packed mode. This differs from the standard encoding in the following ways:

    • Most values are packed tightly, without alignment padding.
      • The exception is array values, which are padded to 32 bytes.
    • Values are still padded to their full size, i.e., uint16 values are still padded to 2 bytes, regardless of the length of the value.
    • The encoding of dynamic types (bytes, string) is different. The length of the dynamic type is not included in the encoding, and the dynamic type is not padded to a multiple of 32 bytes.
    • All values are encoded in-place, without any offsets.

    The encoding of this is ambiguous as soon as there is more than one dynamic type. That means that these values cannot be decoded with decode or Solidity's abi.decode function.

    See encode for more information on how values are parsed.

    Example

    import { encodePacked } from '@metamask/abi-utils';

    const encoded = encodePacked(['uint8'], [42]);

    console.log(encoded); // `Uint8Array [ 42 ]`

    See

    Returns

    The ABI encoded bytes.

    Type Parameters

    • Type extends readonly string[]

    Parameters

    • types: Type

      The types to encode.

    • values: TypeMap<Type, "input">

      The values to encode.

    • Optional tight: boolean

      Whether to pack the values tightly. When enabled, bytesN values in arrays will be packed without any padding. This matches the behaviour of ethereumjs-abi. Defaults to false.

    Returns Uint8Array