FuelLabs/fuels-ts · error · FuelError
ENCODE_ERROR
ENCODE_ERROR
Error message
Value length mismatch during encode.
What it means
Thrown by StringCoder.encode() when the input string's character length does not exactly match the coder's declared fixed length N (from str[N] in the ABI). StringCoder represents Sway's fixed-size string type; the character count must be exactly N. This uses JavaScript string .length (UTF-16 code units), so strings with surrogate pairs may behave unexpectedly.
Source
Thrown at packages/abi-coder/src/encoding/coders/StringCoder.ts:13
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { toUtf8Bytes, toUtf8String } from '@fuel-ts/utils';
import { Coder } from './AbstractCoder';
export class StringCoder<TLength extends number = number> extends Coder<string, string> {
constructor(length: TLength) {
super('string', `str[${length}]`, length);
}
encode(value: string): Uint8Array {
if (value.length !== this.encodedLength) {
throw new FuelError(ErrorCode.ENCODE_ERROR, `Value length mismatch during encode.`);
}
return toUtf8Bytes(value);
}
decode(data: Uint8Array, offset: number): [string, number] {
if (data.length < this.encodedLength) {
throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid string data size.`);
}
const bytes = data.slice(offset, offset + this.encodedLength);
if (bytes.length !== this.encodedLength) {
throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid string byte data size.`);
}
return [toUtf8String(bytes), offset + this.encodedLength];
}View on GitHub (pinned to b3f37c91ac)
Solutions
- Pad or truncate the string to exactly N characters before encoding.
- Update the ABI type to match the actual string length, or use a variable-length type.
- Use a variable-length type (StdString/StrSlice) if the length is dynamic.
Example fix
// before
const coder = new StringCoder(5);
coder.encode('hi'); // throws ENCODE_ERROR
// after
const coder = new StringCoder(5);
const padded = 'hi'.padEnd(5, '\0');
coder.encode(padded); Defensive patterns
Strategy: validation
Validate before calling
function fitsFixedString(value: string, length: number): boolean {
return value.length === length;
} Type guard
function isFixedStringLength(value: unknown, length: number): value is string {
return typeof value === 'string' && value.length === length;
} Prevention
- Pad fixed-length strings to the exact ABI-declared length before encoding.
- Validate user input length against the ABI schema at the application boundary.
- Prefer str slice or std String for variable-length text.
When it happens
Trigger: ABI declares str[5] but you pass 'hi' (2 chars) or 'hello world' (11 chars). Passing an empty string to str[10]. Passing user input of a different length than the fixed field expects.
Common situations: ABI mismatch: the Sway contract declares a different str length than the client assumes. User input that is shorter or longer than the fixed field. Forgetting to pad fixed-length strings to their declared size.
Related errors
AI-assisted analysis of FuelLabs/fuels-ts@b3f37c91ac (2026-08-12).
Data as JSON: /api/errors/1ee3b9b8dcd086a5.
Report an issue: GitHub.