Skip to content

feat: add simulate_transaction tool#100

Open
Ridwannurudeen wants to merge 4 commits intobase:masterfrom
Ridwannurudeen:feature/simulate-transaction
Open

feat: add simulate_transaction tool#100
Ridwannurudeen wants to merge 4 commits intobase:masterfrom
Ridwannurudeen:feature/simulate-transaction

Conversation

@Ridwannurudeen
Copy link
Copy Markdown

Summary

Adds a simulate_transaction tool that lets users preview transactions before execution — gas cost, return value, and revert risk — without broadcasting.

  • Uses viem's simulateContract() for ABI-aware calls (decoded return values) and call()/estimateGas() for raw calldata or plain ETH transfers
  • Zero external dependencies — no API keys, no Tenderly, just viem (already a project dependency)
  • Returns structured JSON: { success, gasEstimate, result, basescanUrl } on success, { success: false, error } on revert
  • Supports Base and Base Sepolia

Closes #68

Changes

File Change
src/tools/simulate/schemas.ts New — SimulateTransactionSchema (Zod): to, value, data, abi, functionName, args
src/tools/simulate/index.ts New — BaseMcpSimulateActionProvider with @CreateAction simulate_transaction
src/main.ts Import + register baseMcpSimulateActionProvider()

Usage Examples

ABI-aware contract simulation (e.g. preview a USDC transfer):

{
  "to": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
  "abi": "[{\"name\":\"transfer\",\"type\":\"function\",\"inputs\":[{\"name\":\"to\",\"type\":\"address\"},{\"name\":\"amount\",\"type\":\"uint256\"}],\"outputs\":[{\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\"}]",
  "functionName": "transfer",
  "args": ["0xRecipient...", "1000000"]
}

Raw calldata simulation:

{
  "to": "0xContractAddress...",
  "data": "0xa9059cbb000000000000000000000000..."
}

Plain ETH transfer preview:

{
  "to": "0xRecipient...",
  "value": "1000000000000000000"
}

Test plan

  • yarn build passes
  • yarn lint passes
  • yarn format passes
  • Manual: configure MCP server, call simulate_transaction with a known contract, verify gas estimate + decoded result without broadcasting

Add a transaction simulation tool that lets users preview transactions
before execution. Uses viem's simulateContract() for ABI-aware calls
and call()/estimateGas() for raw calldata, returning gas estimates,
decoded results, and revert reasons — all without broadcasting.

Closes base#68
- Validate abi/functionName are provided together (not one without the other)
- Validate value is a valid numeric string before BigInt conversion
- Separate try/catch for simulation vs gas estimation so a gas estimation
  failure doesn't mask a successful simulation result
- Remove basescanUrl from response (no tx hash in simulation context)
- Replace type assertion with proper Hex typing from viem
…ured results

- Args are now JSON-parsed before passing to viem: "true" becomes boolean,
  "[1,2]" becomes an array, plain strings (addresses, hex) stay as-is.
  This fixes simulation of contracts with bool, tuple, array, or struct params.
- Simulation results are recursively serialized (BigInts to strings, nested
  objects/arrays preserved) instead of collapsed with String(), so tuple and
  array return values remain structured and usable.
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.

[FEATURE] Add Transaction Simulation Capabilities to MCP Server

1 participant