# Error: Invalid u16, too many bytes

**URL:** <https://forum.fuel.network/t/error-invalid-u16-too-many-bytes/5222>\
**Category:** General\
**Created:** [May 2, 2024, 1:39pm UTC](https://forum.fuel.network/t/error-invalid-u16-too-many-bytes/5222 "2024-05-02T13:39:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luca](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/luca/32/2760_2.png) [@Luca](https://forum.fuel.network/u/Luca)\
**Post date:** [May 2, 2024, 1:39pm UTC](https://forum.fuel.network/t/error-invalid-u16-too-many-bytes/5222/1 "2024-05-02T13:39:08Z")

</div>

We’ve found some strange issues when trying to deploy our contract from the frontend. The contract compiles without any problems, and the bytecode size is 74,640 bytes, which is well within the 100kB limit.

If we use the command-line interface (CLI) and run forc deploy --testnet, the deployment works as expected.

However, when we try to deploy the contract programmatically from the frontend using the auto-generated ABI and hex files, we encounter a vague error that doesn’t provide any useful information.

To generate the contract types, we followed the steps from the Fuel documentation:

> **[Quickstart Frontend | Fuel Docs](https://docs.fuel.network/docs/intro/quickstart-frontend/#generate-contract-types)**
>
> Introduction to the Fuel Network

```typescript
npx fuels init --contracts ../counter-contract/ --output ./src/sway-api
npx fuels build

```

When we try to deploy the contract using the generated types:

```typescript
import { FuelMultisigAbi __factory } from "@/services/contracts/multisig/contracts/factories/FuelMultisigAbi__ factory";
const gasPrice = wallet.provider.getGasConfig().minGasPrice;
const gasMax = wallet.provider.getGasConfig().maxGasPerTx;
const factory = await FuelMultisigAbi__factory.deployContract(
  bytecode,
  wallet,
  {
    gasPrice,
    maxFee: gasMax,
  }
);

```

The deployment fails with a vague error.

```auto
Error
  at new t....

```

Or

```auto
Error
  at new JSONRPCErrorException ...

```

However, if we change the contract to a simple counter contract, the deployment works smoothly. This suggests that the issue is specific to the contract we’re trying to deploy.

Also, when we use the TypeGen process from the Fuels TS guide:

```auto
pnpm fuels typegen -i ./abis/*-abi.json -o ./types

```

And then deploy it:

```typescript
import { DemoContractAbi__factory } from './types';
import bytecode from './types/DemoContractAbi.hex';

const gasPrice = wallet.provider.getGasConfig().minGasPrice;
// Deploy
const contract = await DemoContractAbi__factory.deployContract(bytecode, wallet, { gasPrice });

```

This deployment still fails.

If we try to replicate this using Fuelet wallet instead of the Fuel wallet, we see the following error:

```auto
Error: FuelError: Invalid u16, too many bytes.
    at new JSONRPCErrorException (models.js:60:28)
    at JSONRPCClient.eval (client.js:217:48)
    at step (client.js:107:23)
    at Object.eval [as next] (client.js:48:20)
    at fulfilled (client.js:11:32)

```

The dependencies involved are:

```auto
    "@fuels/connectors": "0.1.1",
    "@fuels/react": "0.18.0",
    "fuels": "0.82.0",

```

It seems to be an issue similar to the one on this forum post:

> [@FuelError: Invalid u16, too many bytes](https://forum.fuel.network/t/fuelerror-invalid-u16-too-many-bytes/4362/6):
>
> delved deeper into what might be causing the error, and I think I’ve got it. In the [encode method](https://github.com/FuelLabs/fuels-ts/blob/59432a665f44811469665cd469e9f8f2366570f3/packages/abi-coder/src/coders/v0/number.ts#L54), some value is being encoded. Turns out, one of the values being encoded is the size of the compiled bytecode. When this bytecode size exceeds 65535 (max value of u16), the byte length size of the value is 3 and the invariant fails. Now somewhere when encoding this value, the baseType has been set to u16, so the max size of a contract (at least according to the encoder) is max(u16). Is this a bug, …

This bug is somewhat blocking for our development at the moment. The inability to reliably deploy our full-featured contract is hindering our ability to thoroughly test the contract’s functionality. To work around this, we have created a temporary contract with a reduced set of features, in an effort to keep the contract size smaller. However, this workaround means we won’t be able to fully test the complete functionality of our intended contract until we can resolve the deployment issue. This is a suboptimal situation, as we won’t have complete confidence in the contract’s behavior until we can deploy and test the full version. Resolving this deployment problem is a high priority for us, as it is currently impeding our development progress.

---

<div class="post-metadata">

**Author:** ![danielbate](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/danielbate/32/1617_2.png) [@danielbate](https://forum.fuel.network/u/danielbate)\
**Post date:** [May 2, 2024, 4:32pm UTC](https://forum.fuel.network/t/error-invalid-u16-too-many-bytes/5222/2 "2024-05-02T16:32:55Z")

</div>

Hey @Luca, sorry to hear you’ve been having problems. Are you able to share the contract/repository with me? Could you also let me know what `forc` and `fuel-core` versions you are running. And what encoding the contract is using (encoding property in the ABI).

---

<div class="post-metadata">

**Author:** ![Luca](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/luca/32/2760_2.png) [@Luca](https://forum.fuel.network/u/Luca)\
**Post date:** [May 2, 2024, 8:26pm UTC](https://forum.fuel.network/t/error-invalid-u16-too-many-bytes/5222/3 "2024-05-02T20:26:11Z")

</div>

Hi Daniel!  
Contract repo:

> **[GitHub - protofire/fuel-multisig](https://github.com/protofire/fuel-multisig)**
>
> Contribute to protofire/fuel-multisig development by creating an account on GitHub.

Frontend repo:

> **[GitHub - protofire/fuel-multisig-ui](https://github.com/protofire/fuel-multisig-ui)**
>
> Contribute to protofire/fuel-multisig-ui development by creating an account on GitHub.

```auto
latest-aarch64-apple-darwin (default)
  forc : 0.49.3
    - forc-client
      - forc-deploy : 0.49.3
      - forc-run : 0.49.3
    - forc-crypto : 0.49.3
    - forc-doc : 0.49.3
    - forc-explore : 0.28.1
    - forc-fmt : 0.49.3
    - forc-lsp : 0.49.3
    - forc-tx : 0.49.3
    - forc-wallet : 0.4.3
  fuel-core : 0.22.4
  fuel-core-keygen : 0.22.4

```

---

<div class="post-metadata">

**Author:** ![danielbate](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/danielbate/32/1617_2.png) [@danielbate](https://forum.fuel.network/u/danielbate)\
**Post date:** [May 3, 2024, 3:21am UTC](https://forum.fuel.network/t/error-invalid-u16-too-many-bytes/5222/4 "2024-05-03T03:21:48Z")

</div>

Okay so I have been able to deploy your contract to a local testnet running `fuels@0.82.0` / `forc@0.49.3` and `fuel-core@0.22.4` (`fuelup` on latest toolchain). I suspect this is a versioning issue.

Firstly, I built the contract using `forc build --release`. This will build the project using the `forc` binaries installed via `fuelup`, so `0.49.3`. Please note this works differently than `npx fuels build` which will build the project using the binaries that we couple with your `fuels` version. My concern is that if you are not specifying the version, it may be fetching the latest version of `fuels` (`0.83.0`) which has encoding changes that are not compatible with the current testnet. Therefore we would actually be compiling a `u16` with a different amount of bytes, hence the error. Could you validate this with `npx fuels@0.82.0 build`?

However, then I can run typegen and create types for the ABI and bytecode I generated with `forc build --release`, with `pnpm fuels typegen -i ../multisig-contract/out/release/*-abi.json -o ./sway-api`.

And then successfully deploy the contract and call a contract function:

```ts
import { FuelMultisigAbi__factory } from '../sway-api';
import multiSigBytecode from '../sway-api/FuelMultisigAbi.hex'

const contract = await FuelMultisigAbi__factory.deployContract(multiSigBytecode, wallet);
const { value } = await contract.functions.get_owners().call();
 // []

```

So apologies I haven’t got to the bottom of your problem but when following a single flow it does succeed. I’d recommend either working fully with the `fuels` CLI but be sure to specify the version, which I did also get a successful deploy with but used `pnpm fuels build` rather than `npx` to ensure I was using the locally installed `fuels` version. Or use `forc build` and typegen.

We also have a new version of the testnet coming soon that has the new encoding supported on the latest version of `fuels`, which I also suspect would resolve this.

---

<div class="post-metadata">

**Author:** ![Luca](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/luca/32/2760_2.png) [@Luca](https://forum.fuel.network/u/Luca)\
**Post date:** [May 3, 2024, 3:32pm UTC](https://forum.fuel.network/t/error-invalid-u16-too-many-bytes/5222/5 "2024-05-03T15:32:50Z")

</div>

After replicating the approach you described, we encountered the same issue. We created a simplified repository to investigate further, and it exhibited the same error.

The repository is available at [Link](https://github.com/protofire/fuel-multisig/tree/test/contract-deployment), and the relevant branch is [test/contract-deployment].

We’ve included a Makefile to streamline the setup process. Once the wallet is connected, if you’re using the Fuel wallet and click the “Deploy” button, it will start loading but eventually fail with an unclear error in the console. If you’re using the Fuelet wallet, it prompts you to approve the transaction but indicates that the deployment will fail. After approving the transaction, check the console, where you’ll see the deployment failure and the following error message: **Error: Invalid u16, too many bytes**.

---

<div class="post-metadata">

**Author:** ![danielbate](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/danielbate/32/1617_2.png) [@danielbate](https://forum.fuel.network/u/danielbate)\
**Post date:** [May 4, 2024, 6:04am UTC](https://forum.fuel.network/t/error-invalid-u16-too-many-bytes/5222/6 "2024-05-04T06:04:56Z")

</div>

Really appreciate the exemplary repo!

Would you also mind committing the built ABI? I’d like to check the encoding you are using.

---

<div class="post-metadata">

**Author:** ![Luca](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/luca/32/2760_2.png) [@Luca](https://forum.fuel.network/u/Luca)\
**Post date:** [May 4, 2024, 4:16pm UTC](https://forum.fuel.network/t/error-invalid-u16-too-many-bytes/5222/7 "2024-05-04T16:16:09Z")

</div>

I’ve just pushed the compiled contract. You have there both debug and release versions

---

<div class="post-metadata">

**Author:** ![p.s](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/p.s/32/2491_2.png) [@p.s](https://forum.fuel.network/u/p.s)\
**Post date:** [May 6, 2024, 10:27am UTC](https://forum.fuel.network/t/error-invalid-u16-too-many-bytes/5222/8 "2024-05-06T10:27:39Z")

</div>

Hey Luca 👋

Thank you for you patience on this matter.

From what I can see, it does _not_ appear to be caused by the version that @danielbate was implying.

I successfully managed to [deploy](https://app.fuel.network/contract/0x6c3fb8265bf07570d1b03c74ffe54b26e4c7aac90836c8af5765c98d26108010/code) to the Beta 5 network with the contract you [supplied](https://github.com/protofire/fuel-multisig/tree/test/contract-deployment) - however, only with the [Fuel Development Wallet](https://chromewebstore.google.com/detail/fuel-wallet-development/hcgmehahnlbhpilepakbdinkhhaackmc). I too experienced similar issues when attempting to deploy with the Fuel Wallet and Fuelet.

Could you possibly try this approach and let us know the outcomes?

---

<div class="post-metadata">

**Author:** ![Luca](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/luca/32/2760_2.png) [@Luca](https://forum.fuel.network/u/Luca)\
**Post date:** [May 7, 2024, 3:01pm UTC](https://forum.fuel.network/t/error-invalid-u16-too-many-bytes/5222/9 "2024-05-07T15:01:46Z")

</div>

Hey! We have tried that approach and it seems to work. Should we just use the Development Wallet and wait for new releases of the other two?

---

<div class="post-metadata">

**Author:** ![p.s](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/p.s/32/2491_2.png) [@p.s](https://forum.fuel.network/u/p.s)\
**Post date:** [May 8, 2024, 10:18am UTC](https://forum.fuel.network/t/error-invalid-u16-too-many-bytes/5222/10 "2024-05-08T10:18:17Z")

</div>

That’s great news!

I’d advise to proceed with the Development Wallet while developing you dApp, as it has better functionality for debugging and testing purposes.

I have just recently [deployed](https://app.fuel.network/contract/0x3c2c00f904cb91c2ba73d2ce9e0f8baf8ded3462de1899af77313f7d44765460/code) your contract successfully, using Fuel Wallet (v0.16.6), so potentially this was an issue with a previous version.

Hope this helps 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex022/uploads/fuel/original/2X/5/57d5a345cc15a64b636e0d56e042857f8a0e80b1.png) [@system](https://forum.fuel.network/u/system)\
**Post date:** [May 29, 2024, 6:18am UTC](https://forum.fuel.network/t/error-invalid-u16-too-many-bytes/5222/11 "2024-05-29T06:18:30Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
