How to solve this `Failed to encode CBOR: RangeError` error? (never happened in local)

"dependencies": {
    "@dfinity/agent": "^3.3.1",
    "@dfinity/auth-client": "^3.3.1",
    "@dfinity/candid": "^3.3.1",
    "@dfinity/principal": "^3.3.1",
    "@dfinity/ledger-icp": "^6.1.2",
dfx version: 0.29.2
Failed to encode CBOR: RangeError: Offset is outside the bounds of the DataView at DataView.prototype.setUint8 (<anonymous>) at hn

on local, the Internet Identity was Classic version. on ic, the Internet Identity is 2.0.
the error happened when i try to call icrc2_approve()

You are facing the issue locally or on mainnet or on both?

If only on mainnet, for what it’s worth, I faced the issue only once and it resolved itself after some time, so I just assumed I faced some networking mumbo jumbo :man_shrugging:.

Locally I never faced it so not sure what can be the issue. If that’s the case, did you update anything recently before the issue started popping up? If so, have you tried reverting your changes?

hi Peter thanks for the fast reply.

yea this only happens in ic mainnet. how did you solve it tho?

i just tried it on my phone instead of my pc. now it’s working. i guess you’re right; it’s just networking stuff

I don’t know how to resolve it, but as I said above, given that the only time it happened to me it kind of resolved by itself, I would wait a bit unless you’ve been facing the issue for days or hours. Other than that, I don’t really know unfortunately.

Ah cool! :+1:

Sounds like so then indeed.

It sounds like you’re hitting a CBOR encoding error that only appears outside your local environment, which usually points to differences in data formatting, environment settings, or dependencies between local and deployed setups. A few things to check: make sure the data you’re encoding doesn’t contain undefined or circular values, verify that the versions of your CBOR libraries match in both environments, and confirm that any environment-specific config (like NODE_ENV or serialization settings) isn’t affecting the input. Logging the exact value that triggers the failure can also help pinpoint what’s causing the range error.
For more context, there’s a related thread on the same forum about serializing objects into CBOR in Motoko that might help you understand how CBOR encoding should be structured when making requests: https://forum.dfinity.org/t/how-do-i-serialize-an-object-into-cbor-format-in-motoko/26739/12