Repository navigation
fix: default to zero address for read operations when account not provided - #121
Conversation
…vided Previously, readContract and simulateWriteContract would pass undefined as the 'from' parameter when no account was provided, causing RPC errors. This fix defaults to the zero address (0x0000...0000) when no account is specified, following Ethereum standards (eth_call behavior). This allows read-only operations to work without requiring wallet connection. Changes: - src/contracts/actions.ts: Added ?? zeroAddress fallback for both readContract (L74) and simulateWriteContract (L123) - tests/client.test.ts: Added test coverage for zero address default Fixes the issue where users received "Details: 'from'" errors when attempting to read contract state without providing an account.
WalkthroughThe pull request adds a fallback to Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
tests/client.test.ts (1)
173-196: Good test coverage for the zero address default behavior.The test properly verifies that
readContractdefaults tozeroAddresswhen no account is provided on the client or as an override. The assertions are comprehensive and follow the existing test patterns.Consider adding a similar test case for
simulateWriteContractsince it also received the zero address fallback change (line 123 insrc/contracts/actions.ts).it("should use zero address for simulateWriteContract when no account is provided", async () => { const client = createClient({ chain: localnet, // No account provided on client }); const contractAddress = "0x1234567890123456789012345678901234567890"; await client.simulateWriteContract({ // No account override either address: contractAddress as Address, functionName: "testFunction", args: ["arg1", "arg2"], }); expect(lastGenCallParams).toEqual([ { type: "write", to: contractAddress, from: zeroAddress, // Should default to zero address data: expect.any(String), transaction_hash_variant: TransactionHashVariant.LATEST_NONFINAL, }, ]); });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/contracts/actions.ts(2 hunks)tests/client.test.ts(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tests/client.test.ts (1)
src/client/client.ts (1)
createClient(86-134)
🔇 Additional comments (3)
src/contracts/actions.ts (2)
74-74: LGTM! Proper fallback for read operations.The zero address fallback aligns with Ethereum
eth_callstandards and resolves the issue where read operations failed when no account was provided. The fallback chain is clear and handles all cases appropriately.
123-123: LGTM! Appropriate default for simulated writes.The zero address fallback is appropriate for simulated write operations since they don't execute actual transactions. This mirrors the behavior of
readContractand maintains consistency across call-based operations.tests/client.test.ts (1)
8-8: LGTM! Necessary import for the new test.The
zeroAddressimport from viem is correctly added to support the new test case.
Problem
readContractandsimulateWriteContractthrow"Details: 'from'"error when no account is provided, despite documentation stating account is optional.Solution
Default to zero address (
0x0000...0000) when account not provided, following Ethereumeth_callstandards.Changes
src/contracts/actions.ts: Added?? zeroAddressfallback (lines 74, 123)tests/client.test.ts: Added test coverage for zero address defaultTesting
All tests pass (13/13). Verified with live contract on Studionet.
Fully backwards compatible - existing code with accounts continues to work.
Summary by CodeRabbit