# Error while trying to run IDL.encode on values around 1.8×10^308 or larger

**URL:** <https://forum.dfinity.org/t/error-while-trying-to-run-idl-encode-on-values-around-1-8x10-308-or-larger/32924>\
**Category:** JavaScript\
**Created:** [July 8, 2024, 9:51pm UTC](https://forum.dfinity.org/t/error-while-trying-to-run-idl-encode-on-values-around-1-8x10-308-or-larger/32924 "2024-07-08T21:51:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdemann](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/bdemann/32/6303_2.png) [@bdemann](https://forum.dfinity.org/u/bdemann)\
**Post date:** [July 8, 2024, 9:51pm UTC](https://forum.dfinity.org/t/error-while-trying-to-run-idl-encode-on-values-around-1-8x10-308-or-larger/32924/1 "2024-07-08T21:51:02Z")

</div>

What type of topic is this?  
Bug Report

I am having some problems encoding large bigints

This works

```javascript
IDL.encode(
    [IDL.Nat],
    [
        170_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000n
    ]
);

```

But this breaks

```javascript
IDL.encode(
    [IDL.Nat],
    [
        180_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000n
    ]
);

```

with the following error:

```auto
RangeError: Invalid array buffer length
        at new ArrayBuffer (<anonymous>)

      at lebEncode (../../../node_modules/@dfinity/candid/src/utils/leb128.ts:54:25)
      at NatClass.encodeValue (../../../node_modules/@dfinity/candid/src/idl.ts:552:21)
      at ../../../node_modules/@dfinity/candid/src/idl.ts:1575:16
      at ../../../node_modules/@dfinity/candid/src/idl.ts:50:27
          at Array.map (<anonymous>)
      at zipWith (../../../node_modules/@dfinity/candid/src/idl.ts:50:13)
      at Object.encode (../../../node_modules/@dfinity/candid/src/idl.ts:1567:8)

```

---

<div class="post-metadata">

**Author:** ![Gekctek](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/gekctek/32/6863_2.png) [@Gekctek](https://forum.dfinity.org/u/Gekctek)\
**Post date:** [July 8, 2024, 11:07pm UTC](https://forum.dfinity.org/t/error-while-trying-to-run-idl-encode-on-values-around-1-8x10-308-or-larger/32924/2 "2024-07-08T23:07:57Z")

</div>

Looks like a potential issue with max size(?) of an ArrayBuffer

> <https://github.com/dfinity/agent-js/blob/9758583a52e3eeccb73425165ecd8188ab46e112/packages/candid/src/utils/leb128.ts#L54>

---

<div class="post-metadata">

**Author:** ![ilbert](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ilbert/32/8717_2.png) [@ilbert](https://forum.dfinity.org/u/ilbert)\
**Post date:** [July 17, 2025, 12:42pm UTC](https://forum.dfinity.org/t/error-while-trying-to-run-idl-encode-on-values-around-1-8x10-308-or-larger/32924/3 "2025-07-17T12:42:46Z")

</div>

This bug has been fixed in:

> <https://github.com/dfinity/agent-js/pull/1059>
>
> \# Description
> 
> Fixes #953.
> 
> \# How Has This Been Tested?
> 
> Added unit tests from t…he issue, and they are passing.
> 
> \# Checklist:
> 
> \- \[x\] My changes follow the guidelines in \[CONTRIBUTING.md\](https://github.com/dfinity/agent-js/blob/main/.github/CONTRIBUTING.md).
> \- \[x\] The title of this PR complies with \[Conventional Commits\](https://www.conventionalcommits.org/en/v1.0.0/).
> \- \[x\] I have edited the CHANGELOG accordingly.
> \- \[\] I have made corresponding changes to the documentation.

It will be included in the ICP JavaScript SDK v3 release, which is coming really soon.

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [July 29, 2025, 4:28pm UTC](https://forum.dfinity.org/t/error-while-trying-to-run-idl-encode-on-values-around-1-8x10-308-or-larger/32924/4 "2025-07-29T16:28:48Z")

</div>

Confirm fixed in `@dfinity/candid` `3.1.0`!
