From 240ba87bf13d95a3e981431c7483ee4fc036b8cd Mon Sep 17 00:00:00 2001 From: pabl0cks Date: Thu, 18 Jul 2024 13:43:55 +0200 Subject: [PATCH] Fixes BigInt parsing losing precision in IntegerInput --- .../scaffold-eth/Contract/utilsContract.tsx | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/nextjs/components/scaffold-eth/Contract/utilsContract.tsx b/packages/nextjs/components/scaffold-eth/Contract/utilsContract.tsx index 023efe87..217012ce 100644 --- a/packages/nextjs/components/scaffold-eth/Contract/utilsContract.tsx +++ b/packages/nextjs/components/scaffold-eth/Contract/utilsContract.tsx @@ -18,16 +18,31 @@ const isJsonString = (str: string) => { } }; +const isBigInt = (str: string) => { + if (str.trim().length === 0 || str.startsWith("0")) return false; + try { + BigInt(str); + return true; + } catch (e) { + return false; + } +}; + // Recursive function to deeply parse JSON strings, correctly handling nested arrays and encoded JSON strings const deepParseValues = (value: any): any => { if (typeof value === "string") { + // first try with bigInt because we losse precision with JSON.parse + if (isBigInt(value)) { + return BigInt(value); + } + if (isJsonString(value)) { const parsed = JSON.parse(value); return deepParseValues(parsed); - } else { - // It's a string but not a JSON string, return as is - return value; } + + // It's a string but not a JSON string, return as is + return value; } else if (Array.isArray(value)) { // If it's an array, recursively parse each element return value.map(element => deepParseValues(element));