{"version":3,"file":"TWALvP4t.js","sources":["../../../../../../src/lib/utils/validators/tokenValidators.ts"],"sourcesContent":["import BigNumber from \"bignumber.js\";\nimport { Principal } from \"@dfinity/principal\";\n\nexport function calculateMaxAmount(\n balance: bigint,\n tokenDecimals: number,\n tokenFee: bigint = BigInt(10000)\n): number {\n return new BigNumber(balance.toString())\n .dividedBy(new BigNumber(10).pow(tokenDecimals))\n .minus(\n new BigNumber(tokenFee.toString()).dividedBy(\n new BigNumber(10).pow(tokenDecimals)\n )\n )\n .toNumber();\n}\n\nexport function validateTokenAmount(\n value: string,\n balance: bigint,\n tokenDecimals: number,\n tokenFee: bigint = BigInt(10000)\n): { isValid: boolean; errorMessage: string } {\n if (!value) return { isValid: false, errorMessage: \"Amount is required\" };\n \n const numValue = parseFloat(value);\n if (isNaN(numValue) || numValue <= 0) {\n return { isValid: false, errorMessage: \"Amount must be greater than 0\" };\n }\n\n const maxAmount = calculateMaxAmount(balance, tokenDecimals, tokenFee);\n if (numValue > maxAmount) {\n return { isValid: false, errorMessage: \"Insufficient balance for transfer + fee\" };\n }\n\n return { isValid: true, errorMessage: \"\" };\n}\n\nexport function isValidHex(str: string): boolean {\n const hexRegex = /^[0-9a-fA-F]+$/;\n return hexRegex.test(str);\n}\n\nexport function detectAddressType(address: string): \"principal\" | \"account\" | null {\n if (!address) return null;\n\n // Check for Account ID (64 character hex string)\n if (address.length === 64 && isValidHex(address)) {\n return \"account\";\n }\n\n // Check for Principal ID\n try {\n Principal.fromText(address);\n return \"principal\";\n } catch {\n return null;\n }\n}\n\nexport function validateAddress(\n address: string,\n tokenSymbol: string,\n tokenName: string\n): { isValid: boolean; errorMessage: string; addressType: \"principal\" | \"account\" | null } {\n if (!address) {\n return { isValid: false, errorMessage: \"Address is required\", addressType: null };\n }\n\n const cleanAddress = address.trim();\n\n if (cleanAddress.length === 0) {\n return { isValid: false, errorMessage: \"Address cannot be empty\", addressType: null };\n }\n\n const addressType = detectAddressType(cleanAddress);\n\n if (addressType === \"account\" && tokenSymbol !== \"ICP\") {\n return { \n isValid: false, \n errorMessage: `Account ID can't be used with ${tokenName}`, \n addressType \n };\n }\n\n if (addressType === null) {\n return { isValid: false, errorMessage: \"Invalid address format\", addressType: null };\n }\n\n return { isValid: true, errorMessage: \"\", addressType };\n}\n\nexport function formatTokenInput(\n value: string, \n tokenDecimals: number\n): string {\n let formattedValue = value.replace(/[^0-9.]/g, \"\");\n\n const parts = formattedValue.split(\".\");\n if (parts.length > 2) {\n formattedValue = `${parts[0]}.${parts[1]}`;\n }\n\n if (parts[1]?.length > tokenDecimals) {\n formattedValue = `${parts[0]}.${parts[1].slice(0, tokenDecimals)}`;\n }\n\n return formattedValue;\n}\n\nexport function getInitialBalances(tokenSymbol: string): TokenBalances {\n return tokenSymbol === \"ICP\"\n ? { default: BigInt(0), subaccount: BigInt(0) }\n : { default: BigInt(0) };\n} "],"names":["calculateMaxAmount","balance","tokenDecimals","tokenFee","BigInt","BigNumber","toString","dividedBy","pow","minus","toNumber","validateTokenAmount","value","isValid","errorMessage","numValue","parseFloat","isNaN","validateAddress","address","tokenSymbol","tokenName","addressType","cleanAddress","trim","length","test","Principal","fromText","detectAddressType","formatTokenInput","formattedValue","replace","parts","split","_a","slice","getInitialBalances","default","subaccount"],"mappings":"yCAGO,SAASA,EACdC,EACAC,EACAC,EAAmBC,OAAO,MAE1B,OAAO,IAAIC,EAAUJ,EAAQK,YAC1BC,UAAU,IAAIF,EAAU,IAAIG,IAAIN,IAChCO,MACC,IAAIJ,EAAUF,EAASG,YAAYC,UACjC,IAAIF,EAAU,IAAIG,IAAIN,KAGzBQ,UACL,CAEO,SAASC,EACdC,EACAX,EACAC,EACAC,EAAmBC,OAAO,MAE1B,IAAKQ,EAAO,MAAO,CAAEC,SAAS,EAAOC,aAAc,sBAE7C,MAAAC,EAAWC,WAAWJ,GAC5B,GAAIK,MAAMF,IAAaA,GAAY,EACjC,MAAO,CAAEF,SAAS,EAAOC,aAAc,iCAIzC,OAAIC,EADcf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