#!/usr/bin/env bash
# Check transaction details across different chains
# Usage: ./check-transaction.sh <tx_hash>

set -euo pipefail

TX_HASH="${1:-0x789a8f3957f793b00f00e6907157c15156d1fab35a70db9476ef5ddcdce7c044}"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

info() { echo -e "${BLUE}[INFO]${NC} $1"; }
success() { echo -e "${GREEN}[✓]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; }

echo "Transaction Hash: $TX_HASH"
echo ""

# Check ChainID 138
info "Checking ChainID 138..."
TX_138=$(curl -s -X POST https://rpc-http-pub.d-bis.org \
  -H "Content-Type: application/json" \
  -d "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getTransactionByHash\",\"params\":[\"$TX_HASH\"],\"id\":1}" | jq -r '.result // null')

if [[ "$TX_138" != "null" && -n "$TX_138" ]]; then
    success "Found on ChainID 138!"
    echo "$TX_138" | jq '{from, to, value, blockNumber, blockHash}'
else
    warn "Not found on ChainID 138"
fi

# Check receipt on ChainID 138
RECEIPT_138=$(curl -s -X POST https://rpc-http-pub.d-bis.org \
  -H "Content-Type: application/json" \
  -d "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getTransactionReceipt\",\"params\":[\"$TX_HASH\"],\"id\":1}" | jq -r '.result // null')

if [[ "$RECEIPT_138" != "null" && -n "$RECEIPT_138" ]]; then
    success "Receipt found on ChainID 138!"
    echo "$RECEIPT_138" | jq '{status, blockNumber, gasUsed, contractAddress}'
fi

echo ""

# Check Ethereum Mainnet
info "Checking Ethereum Mainnet..."
TX_MAINNET=$(curl -s -X POST https://eth.llamarpc.com \
  -H "Content-Type: application/json" \
  -d "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getTransactionByHash\",\"params\":[\"$TX_HASH\"],\"id\":1}" | jq -r '.result // null')

if [[ "$TX_MAINNET" != "null" && -n "$TX_MAINNET" ]]; then
    success "Found on Ethereum Mainnet!"
    echo "$TX_MAINNET" | jq '{from, to, value, blockNumber, blockHash}'
else
    warn "Not found on Ethereum Mainnet"
fi

echo ""
info "Etherscan link: https://etherscan.io/tx/$TX_HASH"
info "Blockscout (ChainID 138): https://explorer.d-bis.org/tx/$TX_HASH"

