Skip to content

feat: transaction simulation via viem + Solana simulateTransaction#210

Merged
premiumjibles merged 11 commits intomainfrom
worktree-sa3-gbzn
Mar 12, 2026
Merged

feat: transaction simulation via viem + Solana simulateTransaction#210
premiumjibles merged 11 commits intomainfrom
worktree-sa3-gbzn

Conversation

@premiumjibles
Copy link
Contributor

@premiumjibles premiumjibles commented Mar 12, 2026

Summary

  • Add client-side transaction simulation before wallet interaction using viem publicClient.call() + estimateGas() for EVM and connection.simulateTransaction() for Solana
  • Predicted reverts block the transaction early with a decoded error message — wallet popup is never shown
  • RPC/simulation failures degrade gracefully (warn and proceed without simulation)
  • Gas estimates with 20% buffer replace hardcoded constants for EVM transactions

Changes

  • SimulationError class + isRevertError/extractRevertReason helpers
  • simulateEvmTransaction — eth_call revert detection + estimateGas with buffer
  • simulateSolanaTransaction — preflight simulation with log extraction
  • Integration into sendEvmTransaction and sendSolanaTransaction
  • Error display passthrough in getUserFriendlyError

Test Plan

  • Unit tests for isRevertError classification (revert vs RPC failure)
  • Unit tests for extractRevertReason (shortMessage preference)
  • Unit tests for simulateEvmTransaction (revert, non-revert, gas buffer)
  • Unit tests for simulateSolanaTransaction (success, error log extraction, fallback)
  • Manual: known-good swap — simulation succeeds silently, wallet popup appears
  • Manual: known-bad swap (expired quote) — revert detected, error in step UI, no wallet popup
  • Manual: RPC failure — console.warn, transaction proceeds normally

🤖 Generated with Claude Code

Closes sg-rzht

Summary by CodeRabbit

  • New Features

    • Added pre-transaction simulation to detect potential reverts before submission on EVM and Solana networks.
    • Automatic gas estimation with buffer applied for EVM transactions.
  • Bug Fixes

    • Improved error detection and messaging for failed transactions with clearer revert handling.
  • Tests

    • Added comprehensive test coverage for transaction simulation and error handling.

@vercel
Copy link

vercel bot commented Mar 12, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
shapeshift-agentic Ready Ready Preview, Comment Mar 12, 2026 1:22am

Request Review

@coderabbitai
Copy link

coderabbitai bot commented Mar 12, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 385eb866-a949-45a0-a2e6-70dd45b0d46d

📥 Commits

Reviewing files that changed from the base of the PR and between 2b3965b and 5571ece.

📒 Files selected for processing (9)
  • apps/agentic-chat/src/lib/stepUtils.ts
  • apps/agentic-chat/src/utils/SimulationError.ts
  • apps/agentic-chat/src/utils/__tests__/SimulationError.test.ts
  • apps/agentic-chat/src/utils/chains/evm/__tests__/simulation.test.ts
  • apps/agentic-chat/src/utils/chains/evm/simulation.ts
  • apps/agentic-chat/src/utils/chains/evm/transaction.ts
  • apps/agentic-chat/src/utils/chains/solana/__tests__/simulation.test.ts
  • apps/agentic-chat/src/utils/chains/solana/simulation.ts
  • apps/agentic-chat/src/utils/chains/solana/transaction.ts

Disabled knowledge base sources:

  • Linear integration is disabled

You can enable these sources in your CodeRabbit configuration.


📝 Walkthrough

Walkthrough

The changes implement pre-transaction simulation for EVM and Solana blockchains. A new SimulationError utility class and helper functions detect reverts and extract error reasons. Chain-specific simulators (EVM and Solana) catch failures early with descriptive error handling. The EVM simulator applies a 20% gas buffer. Both chain integrations now invoke simulation before sending transactions. Error mapping utilities are enhanced to handle "transaction will revert" messages directly.

Changes

Cohort / File(s) Summary
Error Utilities
apps/agentic-chat/src/utils/SimulationError.ts, apps/agentic-chat/src/utils/__tests__/SimulationError.test.ts
Introduces SimulationError custom error class, isRevertError() detector, and extractRevertReason() extractor. Tests verify error detection for ContractFunctionRevertedError, CallExecutionError, and revert messages; shortMessage preference; and fallback behavior.
EVM Simulation
apps/agentic-chat/src/utils/chains/evm/simulation.ts, apps/agentic-chat/src/utils/chains/evm/__tests__/simulation.test.ts, apps/agentic-chat/src/utils/chains/evm/transaction.ts
Adds simulateEvmTransaction() to detect reverts via publicClient.call(), throw SimulationError with reason, and apply 20% gas buffer to estimateGas(). Tests verify revert handling, reason propagation, and buffer correctness. Integration updates transaction.ts to simulate before execution and handle SimulationError distinctly.
Solana Simulation
apps/agentic-chat/src/utils/chains/solana/simulation.ts, apps/agentic-chat/src/utils/chains/solana/__tests__/simulation.test.ts, apps/agentic-chat/src/utils/chains/solana/transaction.ts
Adds simulateSolanaTransaction() to invoke connection.simulateTransaction(), parse logs for error or failed keywords, and throw SimulationError with extracted reason. Tests verify log-based reason extraction and fallback to JSON stringified error. Integration updates transaction.ts to simulate pre-signature with skipPreflight enabled.
Error Mapping
apps/agentic-chat/src/lib/stepUtils.ts
Adds detection in getUserFriendlyError(): if raw error contains "transaction will revert", returns the raw error string instead of a mapped message.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Hops through the chain with care so keen,
Simulating all that might have been,
Reverts detected before they're real,
Gas buffered safe with blockchain zeal,
Both EVM and Solana play,
No nasty surprises come to stay!

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch worktree-sa3-gbzn

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.

premiumjibles and others added 9 commits March 12, 2026 08:02
…on helpers

Foundation for transaction simulation — SimulationError distinguishes predicted
reverts from RPC failures, isRevertError classifies viem errors, and
extractRevertReason pulls human-readable messages from viem error objects.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Call simulateEvmTransaction before wallet interaction to detect
reverts early and use the estimated gas. SimulationError propagates
to prevent wallet popup; RPC failures degrade gracefully.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Pre-sign simulation using connection.simulateTransaction with sigVerify:false,
extracting meaningful error logs on failure and throwing SimulationError.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Simulate transactions before prompting wallet signing to catch reverts
early without bothering the user.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Add check for 'transaction will revert' in getUserFriendlyError so
simulation revert messages like "Transaction will revert:
INSUFFICIENT_OUTPUT_AMOUNT" are not truncated to 120 chars.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Cover success, error with matching log, error with no matching log,
and null logs scenarios.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Preserve caller-provided gasLimit instead of overriding with simulation estimate
- Use `gas !== undefined` to avoid falsy bigint 0n being dropped
- Make Solana error log matching case-insensitive
- Skip redundant Solana preflight since we already simulate explicitly

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@premiumjibles premiumjibles marked this pull request as ready for review March 12, 2026 01:24
@premiumjibles premiumjibles merged commit 5931e77 into main Mar 12, 2026
4 checks passed
@premiumjibles premiumjibles deleted the worktree-sa3-gbzn branch March 12, 2026 01:24
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