#!/bin/bash # Fetch recent swaps from Arbitrum and save them for testing set -e CAST="/home/administrator/.foundry/bin/cast" ARBITRUM_RPC="https://arb1.arbitrum.io/rpc" OUTPUT_FILE="test_swaps.json" SWAP_COUNT=10 echo "Fetching recent swaps from Arbitrum..." # Get latest block number LATEST_BLOCK=$($CAST block-number --rpc-url "$ARBITRUM_RPC") echo "Latest block: $LATEST_BLOCK" # Start from 100 blocks ago START_BLOCK=$((LATEST_BLOCK - 100)) echo "Scanning blocks $START_BLOCK to $LATEST_BLOCK for swap transactions..." echo "[" > "$OUTPUT_FILE" SWAPS_FOUND=0 # Known swap event signatures UNISWAP_V2_SWAP="0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822" # Swap(address,uint256,uint256,uint256,uint256,address) UNISWAP_V3_SWAP="0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67" # Swap(address,address,int256,int256,uint160,uint128,int24) for ((BLOCK=START_BLOCK; BLOCK<=LATEST_BLOCK && SWAPS_FOUND&2 # Get block with transactions BLOCK_DATA=$($CAST block $BLOCK --json --rpc-url "$ARBITRUM_RPC" 2>/dev/null || echo "{}") # Extract transaction hashes TX_HASHES=$(echo "$BLOCK_DATA" | jq -r '.transactions[]?' 2>/dev/null || echo "") for TX_HASH in $TX_HASHES; do if [ -z "$TX_HASH" ]; then continue fi # Get transaction receipt RECEIPT=$($CAST receipt "$TX_HASH" --json --rpc-url "$ARBITRUM_RPC" 2>/dev/null || echo "{}") # Check if this transaction has swap events HAS_SWAP=$(echo "$RECEIPT" | jq -r ".logs[]? | select(.topics[0]? == \"$UNISWAP_V2_SWAP\" or .topics[0]? == \"$UNISWAP_V3_SWAP\") | .address" | head -1) if [ ! -z "$HAS_SWAP" ]; then echo "Found swap in tx $TX_HASH" >&2 # Get full transaction TX_DATA=$($CAST tx "$TX_HASH" --json --rpc-url "$ARBITRUM_RPC" 2>/dev/null || echo "{}") # Extract relevant fields TO=$(echo "$TX_DATA" | jq -r '.to') FROM=$(echo "$TX_DATA" | jq -r '.from') VALUE=$(echo "$TX_DATA" | jq -r '.value') INPUT=$(echo "$TX_DATA" | jq -r '.input') GAS=$(echo "$TX_DATA" | jq -r '.gas') # Add swap to output if [ $SWAPS_FOUND -gt 0 ]; then echo "," >> "$OUTPUT_FILE" fi cat >> "$OUTPUT_FILE" <> "$OUTPUT_FILE" echo "]" >> "$OUTPUT_FILE" echo "Found $SWAPS_FOUND swaps and saved to $OUTPUT_FILE" cat "$OUTPUT_FILE"