FuelLabs/fuels-ts · error · FuelError

ENCODE_ERROR

ENCODE_ERROR

Error message

Expected array value, or a Uint8Array. You can use arrayify to convert a value to a Uint8Array.

What it means

Thrown by VecCoder.encode() when the input is neither a JavaScript array nor a Uint8Array. VecCoder accepts either an array of elements (encoded element-by-element via its inner coder) or a raw Uint8Array (wrapped with a length prefix). Any other type (string, number, object, null) is rejected.

Source

Thrown at packages/abi-coder/src/encoding/coders/VecCoder.ts:30

type InputValueOf<TCoder extends Coder> = Array<TypesOfCoder<TCoder>['Input']> | Uint8Array;
type DecodedValueOf<TCoder extends Coder> = Array<TypesOfCoder<TCoder>['Decoded']>;

export class VecCoder<TCoder extends Coder> extends Coder<
  InputValueOf<TCoder>,
  DecodedValueOf<TCoder>
> {
  coder: TCoder;
  #hasNestedOption: boolean;

  constructor(coder: TCoder) {
    super('struct', `struct Vec`, WORD_SIZE);
    this.coder = coder;
    this.#hasNestedOption = hasNestedOption([coder]);
  }

  encode(value: InputValueOf<TCoder>): Uint8Array {
    if (!Array.isArray(value) && !isUint8Array(value)) {
      throw new FuelError(
        ErrorCode.ENCODE_ERROR,
        `Expected array value, or a Uint8Array. You can use arrayify to convert a value to a Uint8Array.`
      );
    }

    const lengthCoder = new BigNumberCoder('u64');

    if (isUint8Array(value)) {
      return new Uint8Array([...lengthCoder.encode(value.length), ...value]);
    }

    const bytes = value.map((v) => this.coder.encode(v));
    const lengthBytes = lengthCoder.encode(value.length);

    return new Uint8Array([...lengthBytes, ...concatBytes(bytes)]);
  }

  decode(data: Uint8Array, offset: number): [DecodedValueOf<TCoder>, number] {

View on GitHub (pinned to b3f37c91ac)

Solutions

  1. Convert the value to an array or Uint8Array before encoding.
  2. For byte data, use arrayify(hexString) or new Uint8Array([...]).
  3. For element vectors, ensure the value is an array of the correct element type.

Example fix

// before
vecCoder.encode('0x1234');      // throws ENCODE_ERROR
vecCoder.encode({ length: 2 }); // throws ENCODE_ERROR

// after
vecCoder.encode(arrayify('0x1234')); // Uint8Array
vecCoder.encode([1, 2, 3]);          // number[]
Defensive patterns

Strategy: type-guard

Validate before calling

function isVecInput(value: unknown): boolean {
  return Array.isArray(value) || value instanceof Uint8Array;
}

Type guard

function isVecValue(value: unknown): value is unknown[] | Uint8Array {
  return Array.isArray(value) || value instanceof Uint8Array;
}

Prevention

When it happens

Trigger: Calling encode('hello'), encode(42), encode({a:1}), or encode(null). Passing a BN or BigNumber instance where a number[] is expected. Confusing VecCoder with StdStringCoder which takes a string.

Common situations: Wrong data type from upstream code. Serialization mismatch (JSON object instead of array). Passing a hex string where a byte or element array is needed.

Related errors


AI-assisted analysis of FuelLabs/fuels-ts@b3f37c91ac (2026-08-12). Data as JSON: /api/errors/307947c1f55104c6. Report an issue: GitHub.