Skip to content

fix(protocol-kit): don't drop SafeProvider from memoized cache key - #1481

Open
Anu (Svector-anu) wants to merge 1 commit into
safe-global:mainfrom
Svector-anu:fix-memoized-provider-cache-key
Open

Anu (Svector-anu) wants to merge 1 commit into
safe-global:mainfrom
Svector-anu:fix-memoized-provider-cache-key

Conversation

@Svector-anu

Copy link
Copy Markdown

What it solves

createMemoizedFunction (used for every memoized contract lookup in contracts/utils.ts) builds its cache key with a JSON serializer that keeps a seen set to break circular references. That serializer is created once per memoized function, so its seen set is shared across every call. The first time a given SafeProvider is serialized it is added to seen; on every later call the same provider is treated as an already-seen reference and dropped from the key.

Two consequences:

  • A repeated identical call misses the cache and re-invokes the callback, because the provider is gone from the second key.
  • Once two different SafeProvider instances have each been memoized once, a later call with either produces the same provider-less key, so one provider's cached result is returned for the other. In a process that works with more than one provider, a memoized contract lookup can return the contract resolved for a different provider.

Note: this does not make two injected EIP-1193 providers distinguishable — those still serialize to the same placeholder — which is a separate limitation. This change only ensures the key no longer depends on call history.

How this PR fixes it

Create the serializer inside the returned function so each call gets a fresh seen set. Circular-reference protection still holds within a single JSON.stringify, but state no longer leaks between calls, so every argument — including the SafeProvider — is represented in the key on every call.

Added packages/protocol-kit/tests/unit/memoized.test.ts: distinct providers keep distinct results, and a repeated identical call is served from cache. Both assertions fail before this change and pass after.

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@datadog-official

datadog-official Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Pipelines

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: f563ad3 | Docs | View more details | Give us feedback!

@Svector-anu

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

1 similar comment
@Svector-anu

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

safe-cla-bot Bot added a commit to safe-global/cla-signatures that referenced this pull request Nov 30, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant