i have an rpc call that returns an array of structs with multiple fields, one of which is balance of an asset for a user, balance type is u128. when the number is large i get this error createType(u128):: Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1. I have tried specifying different types to make it cast to u128 but rpc kept failing. CURL for the rpc works in terminal and returns large numbers. Seems like the problem on JS, what can i do to be able to be to parse the structs such that the value of balance is a valid number, possibly a BigInt? Thank you!
Given that one of the fields in one of the many structs is a u128 you will need to sanitize the values yourself. I would suggest iterating through each value (you may also do this with recursion as we handle sanitization in sidecar), and then check the actual type with
toRawType() for u128. When it comes back as a u128 then you can use
toBn i believe.
Thank you very much @TarikGul for your response. But at what stage can i iterate over the rpc result to check for raw type u128? I define custom rpc config in ApiPromise.create, then i call the rpc with await, and after the rpc call finishes, it crashes with
2023-05-12 VEC: Unable to decode on index 24 Struct: failed on id: u128:: Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1 2023-05-12 RPC-CORE:Vec<Asset>:: createType(Vec<Asset>):: Struct: failed on id: u128:: Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1– cocokickMay 12 at 18:32
please try using
.toBigInt() mentioned here in the Polkadot.js API docs
i have modified the prompt a little. The return result isn't a number itself, it is an array of structs, and one of the struct's fields is a u128 which cause everything to fail. calling toBigInt() won't help in this case because i can't even access this field. Maybe there is a workaround?– cocokickMay 12 at 12:56