fix: catch slippage and on-chain tx errors in trade functions#15
fix: catch slippage and on-chain tx errors in trade functions#15
Conversation
Implements B-06 (Slippage rejection is dead code).
- Add TransactionFailedError in protocol/client.py that parses on-chain
error codes from Solana transaction failures
- Add slippage error code constants for both pump.fun bonding curve
(6002, 6003, 6042) and PumpSwap AMM (6004, 6040)
- Wrap send_tx calls in buy_token, sell_token, buy_pumpswap, sell_pumpswap
with structured error handling returning {"error": "slippage"} or
{"error": "tx_error"}
- The existing dead code in commands/trade.py (exit code 3 for slippage)
is now live
- Add 15 new unit tests covering all error paths
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughAdds a TransactionFailedError exception and uses it to standardize on-chain transaction failures. buy/sell flows in trade and pumpswap catch this exception and return structured error dictionaries classifying failures as "slippage" or "tx_error" using new slippage code sets. Changes
Sequence Diagram(s)sequenceDiagram
participant App as App (buy/sell)
participant Client as RpcClient
participant Chain as On-chain
participant Handler as _handle_tx_error
participant Result as Result Dict
App->>Client: send_tx(...)
Client->>Chain: execute transaction
Chain-->>Client: returns error string
Client-->>App: raise TransactionFailedError(error_str)
App->>Handler: _handle_tx_error(exc, slippage_codes)
Handler->>Handler: parse error_code from exc
alt error_code in slippage_codes
Handler-->>Result: {error: "slippage", error_code: N}
else
Handler-->>Result: {error: "tx_error", error_code: N}
end
Result-->>App: return structured error
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 6✅ Passed checks (6 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
✨ Simplify code
📝 Coding Plan
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: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In @.gitignore:
- Line 12: Remove the "idl/" ignore entry so versioned IDL files are tracked;
specifically delete the `idl/` line from .gitignore and ensure
`idl/pump_fun_idl.json` (and any other IDL files) are committed, because
`src/pumpfun_cli/protocol/idl_parser.py` loads that IDL at runtime and
instruction builders rely on those discriminators.
In `@src/pumpfun_cli/protocol/contracts.py`:
- Around line 63-65: The PUMPSWAP slippage mapping PUMPSWAP_SLIPPAGE_ERROR_CODES
currently contains non-slippage codes (6004, 6040); open idl/pump_fun_idl.json,
find the PumpSwap error definitions and replace the set in
PUMPSWAP_SLIPPAGE_ERROR_CODES with only the actual slippage error code integers
from that IDL (keep the constant name and type), and then update the
assertions/expected outcomes in tests/test_core/test_pumpswap.py so they expect
non-slippage errors for the removed codes (and slippage for the added ones) to
prevent misclassification and wrong exit code 3 responses.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: f4fca2fc-93b4-41b5-9b5e-95729f4cdfb2
📒 Files selected for processing (9)
.gitignoresrc/pumpfun_cli/core/pumpswap.pysrc/pumpfun_cli/core/trade.pysrc/pumpfun_cli/protocol/client.pysrc/pumpfun_cli/protocol/contracts.pytests/test_commands/test_trade_cmd.pytests/test_core/test_pumpswap.pytests/test_core/test_trade.pytests/test_protocol/test_client.py
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
Fixes B-06 from
docs/backlog.md— slippage rejection was dead code.TransactionFailedErrorexception class that parses on-chain error codes from Solanameta.errobjectssend_txcalls inbuy_token,sell_token,buy_pumpswap,sell_pumpswapwith structured error handling — slippage errors return{"error": "slippage"}(exit code 3), other on-chain errors return{"error": "tx_error"}(exit code 1)Layers Touched
commands/— no changes needed; existing dead code (result["error"] in ("graduated", "slippage")→ exit code 3) is now livecore/—trade.pyandpumpswap.pynow catchTransactionFailedErrorfromsend_txand return structured error dictsprotocol/—client.pyaddsTransactionFailedError(RuntimeError)with error code parsing;contracts.pyadds slippage error code constantsDoes this affect transaction construction or signing?
No. This only changes error handling AFTER
send_tx— transaction construction and signing are untouched.Test Plan
send_txboundary)🤖 Generated with Claude Code
Protocol Layer & Fund-Handling Security
Architectural Layering
Security Implications for Wallets & Transactions
Behavioral Changes & Operational Impact
} → mapped to CLI exit code 3 (previously unreachable).New Constants & Tests
Review Notes / Risk Summary