This commit includes: ## Audit & Testing Infrastructure - scripts/audit.sh: 12-section comprehensive codebase audit - scripts/test.sh: 7 test types (unit, integration, race, bench, coverage, contracts, pkg) - scripts/check-compliance.sh: SPEC.md compliance validation - scripts/check-docs.sh: Documentation coverage checker - scripts/dev.sh: Unified development script with all commands ## Documentation - SPEC.md: Authoritative technical specification - docs/AUDIT_AND_TESTING.md: Complete testing guide (600+ lines) - docs/SCRIPTS_REFERENCE.md: All scripts documented (700+ lines) - docs/README.md: Documentation index and navigation - docs/DEVELOPMENT_SETUP.md: Environment setup guide - docs/REFACTORING_PLAN.md: Systematic refactoring plan ## Phase 1 Refactoring (Critical Fixes) - pkg/validation/helpers.go: Validation functions for addresses/amounts - pkg/sequencer/selector_registry.go: Thread-safe selector registry - pkg/sequencer/reader.go: Fixed race conditions with atomic metrics - pkg/sequencer/swap_filter.go: Fixed race conditions, added error logging - pkg/sequencer/decoder.go: Added address validation ## Changes Summary - Fixed race conditions on 13 metric counters (atomic operations) - Added validation at all ingress points - Eliminated silent error handling - Created selector registry for future ABI migration - Reduced SPEC.md violations from 7 to 5 Build Status: ✅ All packages compile Compliance: ✅ No race conditions, no silent failures Documentation: ✅ 1,700+ lines across 5 comprehensive guides 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
Contract Bindings
This directory contains Go bindings generated from DEX contract ABIs using abigen.
Generated Bindings
UniswapV2
-
Router (
uniswap_v2/router.go): UniswapV2Router02 interfaceSwapExactTokensForTokens()SwapTokensForExactTokens()SwapExactETHForTokens()SwapETHForExactTokens()SwapExactTokensForETH()SwapTokensForExactETH()
-
Pair (
uniswap_v2/pair.go): UniswapV2Pair interfaceSwap()- Direct pool swap functionGetReserves()- Get pool reservesToken0(),Token1()- Get token addresses- Swap event for parsing logs
UniswapV3
- Router (
uniswap_v3/router.go): SwapRouter interfaceExactInputSingle()- Single-hop exact input swapExactInput()- Multi-hop exact input swapExactOutputSingle()- Single-hop exact output swapExactOutput()- Multi-hop exact output swap
Usage
Detecting Swaps Using ABIs
Instead of hardcoding function selectors, use the generated bindings to parse transaction data:
import (
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/your-org/mev-bot/bindings/uniswap_v2"
)
// Parse UniswapV2 router ABI
routerABI, err := abi.JSON(strings.NewReader(uniswap_v2.UniswapV2RouterABI))
if err != nil {
log.Fatal(err)
}
// Detect swap function from calldata
method, err := routerABI.MethodById(txData[:4])
if err != nil {
// Not a known method
return
}
// Check if it's a swap function
isSwap := strings.Contains(method.Name, "swap") ||
strings.Contains(method.Name, "Swap")
// Decode swap parameters
if isSwap {
params, err := method.Inputs.Unpack(txData[4:])
if err != nil {
return err
}
// Access typed parameters
// For swapExactTokensForTokens: amountIn, amountOutMin, path, to, deadline
amountIn := params[0].(*big.Int)
path := params[2].([]common.Address)
// ... etc
}
Parsing Swap Events
import (
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/your-org/mev-bot/bindings/uniswap_v2"
)
// Parse swap event from logs
pairABI, _ := abi.JSON(strings.NewReader(uniswap_v2.UniswapV2PairABI))
for _, log := range receipt.Logs {
event, err := pairABI.EventByID(log.Topics[0])
if err != nil {
continue
}
if event.Name == "Swap" {
// Decode swap event
var swapEvent struct {
Sender common.Address
Amount0In *big.Int
Amount1In *big.Int
Amount0Out *big.Int
Amount1Out *big.Int
To common.Address
}
err = pairABI.UnpackIntoInterface(&swapEvent, "Swap", log.Data)
if err == nil {
// Process swap event
fmt.Printf("Swap: %s -> %s\n", swapEvent.Amount0In, swapEvent.Amount1Out)
}
}
}
Adding New Bindings
1. Create ABI File
Save the contract ABI in JSON format to the appropriate subdirectory:
mkdir -p bindings/camelot
# Create bindings/camelot/ICamelotRouter.json
2. Generate Binding
Run abigen to generate Go code:
podman exec mev-go-dev sh -c "cd /workspace && \
/go/bin/abigen \
--abi=bindings/camelot/ICamelotRouter.json \
--pkg=camelot \
--type=CamelotRouter \
--out=bindings/camelot/router.go"
3. Import in Your Code
import "github.com/your-org/mev-bot/bindings/camelot"
// Use the generated ABI
routerABI, _ := abi.JSON(strings.NewReader(camelot.CamelotRouterABI))
ABI Sources
Contract ABIs can be obtained from:
- Etherscan/Arbiscan: Verified contract → Contract → Code → Contract ABI
- GitHub Repositories: Official DEX repositories
- npm Packages:
@uniswap/v2-periphery,@uniswap/v3-periphery, etc.
Regenerating All Bindings
To regenerate all bindings after updating ABIs:
./scripts/generate-bindings.sh
Or manually:
podman exec mev-go-dev sh -c "cd /workspace && \
/go/bin/abigen --abi=bindings/uniswap_v2/IUniswapV2Router02.json \
--pkg=uniswap_v2 \
--type=UniswapV2Router \
--out=bindings/uniswap_v2/router.go && \
/go/bin/abigen --abi=bindings/uniswap_v2/IUniswapV2Pair.json \
--pkg=uniswap_v2 \
--type=UniswapV2Pair \
--out=bindings/uniswap_v2/pair.go && \
/go/bin/abigen --abi=bindings/uniswap_v3/ISwapRouter.json \
--pkg=uniswap_v3 \
--type=SwapRouter \
--out=bindings/uniswap_v3/router.go"
Benefits of Using Bindings
- Type Safety: Compile-time verification of parameters
- No Hardcoded Selectors: Function signatures derived from ABIs
- Automatic Encoding/Decoding: Built-in parameter packing/unpacking
- Event Parsing: Type-safe event decoding
- Maintainability: Single source of truth (ABI files)
Next Steps
To fully integrate ABI-based swap detection:
- Replace hardcoded selectors in
pkg/sequencer/decoder.gowith ABI lookups - Use
MethodById()to identify swap functions dynamically - Parse swap parameters using typed binding structs
- Add bindings for Balancer, Curve, Kyber, Camelot
- Implement event-based swap detection for pools