diff --git a/packages/protocol-kit/src/utils/memoized.ts b/packages/protocol-kit/src/utils/memoized.ts index 757707cd9..7e107b5e8 100644 --- a/packages/protocol-kit/src/utils/memoized.ts +++ b/packages/protocol-kit/src/utils/memoized.ts @@ -3,10 +3,30 @@ export function createMemoizedFunction) => Ret cache: Record> = {} ) { return (...args: Parameters): ReturnType => { - const key = JSON.stringify(args) + const key = JSON.stringify(args, bigIntSerializerReplacer) cache[key] = cache[key] || callback(...args) return cache[key] } } + +// EIP1193 providers from web3.currentProvider and hre.network.provider fail to serialize BigInts +function bigIntSerializerReplacer() { + const seen = new Set() + + return (_: string, value: unknown) => { + if (typeof value === 'object' && value !== null) { + if (seen.has(value)) { + return undefined + } + seen.add(value) + } + + if (typeof value === 'bigint') { + return value.toString() + } + + return value + } +}