Skip to content

fix: handle object response from gen_call on testnet#147

Merged
MuncleUscles merged 1 commit intomainfrom
fix/gen-call-object-response
Mar 18, 2026
Merged

fix: handle object response from gen_call on testnet#147
MuncleUscles merged 1 commit intomainfrom
fix/gen-call-object-response

Conversation

@MuncleUscles
Copy link
Member

@MuncleUscles MuncleUscles commented Mar 18, 2026

Summary

  • Testnet node returns gen_call results as an object {data, status, eqOutputs, stdout, stderr, logs} while Studio returns a bare hex string
  • genlayer-js was doing `0x${result}` which produced "0x[object Object]" on testnet, crashing viem's hexToBytes
  • Added extractGenCallResult() that handles both response formats
  • Verified working against testnet-bradbury (get_balance on 0x73ee6af5F210d5AC8902B18F53CE23b53eDFC65F now returns correctly)

Test plan

  • All 35 existing tests pass
  • Manually verified readContract works against testnet-bradbury
  • Build succeeds

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling for contract call operations with more consistent response processing and clearer error messages when operations fail.

Testnet node returns gen_call results as an object with data, status,
eqOutputs, stdout, stderr, and logs fields. Studio returns a bare hex
string. extractGenCallResult() now handles both formats.
@coderabbitai
Copy link

coderabbitai bot commented Mar 18, 2026

📝 Walkthrough

Walkthrough

This PR refactors gen_call result handling in src/contracts/actions.ts by introducing a new internal helper function extractGenCallResult() that normalizes responses into a consistent format, handling both bare hex strings and object responses with status validation.

Changes

Cohort / File(s) Summary
Gen Call Result Normalization
src/contracts/actions.ts
Introduces extractGenCallResult() helper to normalize gen_call responses with consistent error handling; updates readContract and simulateWriteContract to use this helper for consistent result processing and explicit error throwing on non-zero status codes.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • cristiam86
  • epsjunior

Poem

🐰 A helper hops in, so neat and so clean,
Normalizing responses we've never yet seen,
Status codes checked with a vigilant care,
The data flows smoothly—no bugs anywhere!
Hex strings unified, errors now clear,
This refactor brings order, hurrah and good cheer! 🌟

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and specifically describes the main change: handling object responses from gen_call on testnet.
Description check ✅ Passed The description covers key sections including summary of the problem, test plan, and verification approach, though it lacks some template sections like explicit 'Why' and 'Decisions made' categories.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/gen-call-object-response
📝 Coding Plan
  • Generate coding plan for human review comments

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
src/contracts/actions.ts (2)

21-31: Consider defensive handling of potential 0x prefix in response data.

If result (string) or obj.data already contains a 0x prefix, this will produce 0x0x... which would cause fromHex to fail at line 111. While the current testnet/Studio APIs may not include the prefix, a defensive approach would be safer:

🛡️ Proposed defensive fix
 function extractGenCallResult(result: unknown): `0x${string}` {
   if (typeof result === "string") {
-    return `0x${result}` as `0x${string}`;
+    const hex = result.startsWith("0x") ? result.slice(2) : result;
+    return `0x${hex}` as `0x${string}`;
   }
   if (result && typeof result === "object" && "data" in result) {
     const obj = result as {data: string; status?: {code: number; message: string}};
     if (obj.status && obj.status.code !== 0) {
       throw new Error(`gen_call failed: ${obj.status.message}`);
     }
-    return `0x${obj.data}` as `0x${string}`;
+    const hex = obj.data.startsWith("0x") ? obj.data.slice(2) : obj.data;
+    return `0x${hex}` as `0x${string}`;
   }
   throw new Error(`Unexpected gen_call response: ${JSON.stringify(result)}`);
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/contracts/actions.ts` around lines 21 - 31, The extractGenCallResult
function may double-prefix hex strings with "0x", so update it to defensively
strip a leading "0x" if present before adding the single "0x" prefix;
specifically, in the branches handling a plain string result and obj.data (in
extractGenCallResult) trim any leading "0x"/"0X" and validate the underlying
value is non-empty, then return `0x${cleaned}`; also add a final fallback to
throw a clear error for unexpected result types so callers (e.g., code using
fromHex) never receive a malformed hex string.

27-29: Add test coverage for the error path when gen_call returns a non-zero status code.

The error-throwing branch on lines 27-29 (if (obj.status && obj.status.code !== 0)) is not covered by existing tests in tests/contracts-actions.test.ts. Consider adding a test that mocks a gen_call response with a failing status to verify the error message is properly thrown.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/contracts/actions.ts` around lines 27 - 29, Add a unit test in
tests/contracts-actions.test.ts that exercises the error branch when gen_call
returns a non-zero status: mock or stub the gen_call call (e.g., using
jest.spyOn or a test double) to return an object like { status: { code: 1,
message: 'some error' } }, call the function under test that invokes gen_call,
and assert that it throws an Error with the message "gen_call failed: some
error"; ensure the test targets the code path guarded by the check (obj.status
&& obj.status.code !== 0) so the throw in that branch is covered.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/contracts/actions.ts`:
- Around line 21-31: The extractGenCallResult function may double-prefix hex
strings with "0x", so update it to defensively strip a leading "0x" if present
before adding the single "0x" prefix; specifically, in the branches handling a
plain string result and obj.data (in extractGenCallResult) trim any leading
"0x"/"0X" and validate the underlying value is non-empty, then return
`0x${cleaned}`; also add a final fallback to throw a clear error for unexpected
result types so callers (e.g., code using fromHex) never receive a malformed hex
string.
- Around line 27-29: Add a unit test in tests/contracts-actions.test.ts that
exercises the error branch when gen_call returns a non-zero status: mock or stub
the gen_call call (e.g., using jest.spyOn or a test double) to return an object
like { status: { code: 1, message: 'some error' } }, call the function under
test that invokes gen_call, and assert that it throws an Error with the message
"gen_call failed: some error"; ensure the test targets the code path guarded by
the check (obj.status && obj.status.code !== 0) so the throw in that branch is
covered.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 77af4552-ed21-4f80-ac60-d9132ddc0a09

📥 Commits

Reviewing files that changed from the base of the PR and between 0cef518 and f7fc52c.

📒 Files selected for processing (1)
  • src/contracts/actions.ts

@MuncleUscles MuncleUscles merged commit 5eeaa7c into main Mar 18, 2026
3 checks passed
@MuncleUscles MuncleUscles deleted the fix/gen-call-object-response branch March 18, 2026 14:53
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