4Bundle tx

i’m tryng to put 4 txs into a bundle:

  1. Send from a secure wallet ETH to your compromised wallet
  2. Call unlock from the compromised wallet
  3. Send the unlocked tokens to your secure wallet
  4. Send all remaining ETH to your secure wallet

but the transaction is never succesful , this is open source code im just editing to add unlock function and point number 4

#!/bin/bash

set -euo pipefail
trap ‘echo “Error on line $LINENO: $BASH_COMMAND”; exit 1’ ERR

Load environment variables

if [ -f .env ]; then
set -a
. ./.env
set +a
else
echo “.env file not found”
exit 1
fi

required_vars=(
PROVIDER_URL
RELAY_URL
SECURE_WALLET_PK
VICTIM_PK
FLASHBOTS_SIGNATURE_PK
TOKEN_CONTRACT
CONTRACT_ADDRESS
)

for var in “{required_vars[@]}"; do if [ -z "{!var}” ]; then
echo “Error: $var is not set in the .env file”
exit 1
fi
done

Utility functions

derive_wallet() {
cast wallet address --private-key “$1”
}

SECURE_WALLET=$(derive_wallet "SECURE_WALLET_PK") VICTIM_WALLET=(derive_wallet "VICTIM_PK") FLASHBOTS_WALLET=(derive_wallet “$FLASHBOTS_SIGNATURE_PK”)

create_flashbots_signature() {
local payload=“$1”
local private_key="2" local hashed=(cast keccak "payload" | xargs cast hash-message) echo (cast wallet sign “$hashed” --private-key “$private_key” --no-hash | tr -d ‘\n’)
}

build_transaction() {
local from_pk=“$1” to=“$2” value=“$3” nonce=“$4” gas=“$5” gas_price=“6" data="{7:-}”
cast mktx --private-key “$from_pk”
–rpc-url “$PROVIDER_URL”
"to" ( [[ -n “$data” ]] && echo -n “$data” )
–value “$value”
–nonce “$nonce”
–gas-price “$gas_price”
–gas-limit “$gas”
}

create_bundle() {
local block_number=“1"; shift local txs_json=(printf '”%s",’ “@"); txs_json="[{txs_json%,}]”
echo -n “{"jsonrpc":"2.0","id":1,"method":"eth_sendBundle","params":[{"txs":txs_json,\"blockNumber\":\"(cast to-hex “$block_number”)","minTimestamp":0}]}”
}

send_bundle() {
local bundle_json=“$1”
local headers=(-H “Content-Type: application/json”)
if [[ "RELAY_URL" == *"flashbots.net"* ]]; then local sig=(create_flashbots_signature “$bundle_json” “$FLASHBOTS_SIGNATURE_PK”)
headers+=(-H “X-Flashbots-Signature: $FLASHBOTS_WALLET:sig") fi curl -s -X POST "{headers[@]}” -d “$bundle_json” “$RELAY_URL”
}

get_balance() {
cast balance “$1” --rpc-url “$PROVIDER_URL”
}

Check if bundle was included in the block

check_bundle_inclusion() {
local bundle_hash=“$1”
local block_number=“$2”
echo “:mag: Checking if bundle was included in block $block_number…”

INCLUSION_RESPONSE=$(curl -s -X POST "$RELAY_URL" \
    -H "Content-Type: application/json" \
    -d "{
        \"jsonrpc\":\"2.0\",
        \"id\":1,
        \"method\":\"eth_getBundleStats\",
        \"params\":[\"$bundle_hash\", \"$block_number\"]
    }")

INCLUDED=$(echo "$INCLUSION_RESPONSE" | jq -r '.result.isSimulated')
FIRST_SEEN=$(echo "$INCLUSION_RESPONSE" | jq -r '.result.firstSeen')

if [[ "$INCLUDED" == "true" ]]; then
    echo "✅ Bundle was included in block. First seen at: $FIRST_SEEN"
else
    echo "❌ Bundle was NOT included in the block."
fi

}

Main loop

while true; do
echo “----- NEW BUNDLE ATTEMPT -----”

# Gas estimates
TRANSFER_ETH=21000
UNLOCK_GAS_LIMIT=1500000
TRANSFER_TOKEN_GAS_LIMIT=120000
BALANCE_SWEEP_GAS=21000

# Set max base fee to 10 Gwei (10 * 10^9 WEI)
MAX_BASE_FEE_GWEI=10
MAX_BASE_FEE_WEI=$((MAX_BASE_FEE_GWEI * 1000000000))

# Priority Fee (e.g., 10 Gwei)
PRIORITY_FEE_GWEI=10
PRIORITY_FEE_WEI=$((PRIORITY_FEE_GWEI * 1000000000))

# Get gas price from provider, increase it by 50% (adjust if needed)
GAS_PRICE=$(cast gas-price --rpc-url "$PROVIDER_URL")
GAS_PRICE=$(( GAS_PRICE * 150 / 100 ))  # 50% bump for higher priority

# Add priority fee to base fee
COMBINED_GAS_PRICE=$((GAS_PRICE + PRIORITY_FEE_WEI))

# Fee estimations
TOTAL_GAS_UNITS=$(( TRANSFER_ETH + UNLOCK_GAS_LIMIT + TRANSFER_TOKEN_GAS_LIMIT + BALANCE_SWEEP_GAS ))
TOTAL_FEES_WEI=$(( TOTAL_GAS_UNITS * COMBINED_GAS_PRICE ))
TOTAL_FEES_ETH=$(cast from-wei "$TOTAL_FEES_WEI")
GAS_PRICE_ETH=$(cast from-wei "$COMBINED_GAS_PRICE")

FEE_TX1_WEI=$((TRANSFER_ETH * COMBINED_GAS_PRICE))
FEE_TX2_WEI=$((UNLOCK_GAS_LIMIT * COMBINED_GAS_PRICE))
FEE_TX3_WEI=$((TRANSFER_TOKEN_GAS_LIMIT * COMBINED_GAS_PRICE))
FEE_TX4_WEI=$((BALANCE_SWEEP_GAS * COMBINED_GAS_PRICE))

FEE_TX1_ETH=$(cast from-wei "$FEE_TX1_WEI")
FEE_TX2_ETH=$(cast from-wei "$FEE_TX2_WEI")
FEE_TX3_ETH=$(cast from-wei "$FEE_TX3_WEI")
FEE_TX4_ETH=$(cast from-wei "$FEE_TX4_WEI")

echo "📊 Gas Price: $GAS_PRICE_ETH ETH"
echo "🔢 Estimated Gas Usage:"
echo "  TX1 (secure → victim):        $TRANSFER_ETH gas → $FEE_TX1_ETH ETH"
echo "  TX2 (victim unlock):          $UNLOCK_GAS_LIMIT gas → $FEE_TX2_ETH ETH"
echo "  TX3 (victim token transfer):  $TRANSFER_TOKEN_GAS_LIMIT gas → $FEE_TX3_ETH ETH"
echo "  TX4 (victim balance sweep):   $BALANCE_SWEEP_GAS gas → $FEE_TX4_ETH ETH"
echo "💰 Total Estimated Bundle Fee:  $TOTAL_FEES_ETH ETH"

# Block target
BLOCK_NUMBER=$(( $(cast block-number --rpc-url "$PROVIDER_URL") + 1 ))

# Nonces
SECURE_NONCE=$(cast nonce "$SECURE_WALLET" --rpc-url "$PROVIDER_URL")
VICTIM_NONCE=$(cast nonce "$VICTIM_WALLET" --rpc-url "$PROVIDER_URL")
VICTIM_NEXT_NONCE=$(( VICTIM_NONCE + 1 ))
VICTIM_FINAL_NONCE=$(( VICTIM_NEXT_NONCE + 1 ))

# ETH amount to send to cover victim txs
AMOUNT_ETH_TO_SEND=$(( COMBINED_GAS_PRICE * (UNLOCK_GAS_LIMIT + TRANSFER_TOKEN_GAS_LIMIT + BALANCE_SWEEP_GAS) ))

# Transactions
TX1=$(build_transaction "$SECURE_WALLET_PK" "$VICTIM_WALLET" "$AMOUNT_ETH_TO_SEND" "$SECURE_NONCE" "$TRANSFER_ETH" "$COMBINED_GAS_PRICE")
UNLOCK_PAYLOAD="0x7****8"
TX2=$(build_transaction "$VICTIM_PK" "$CONTRACT_ADDRESS" 0 "$VICTIM_NONCE" "$UNLOCK_GAS_LIMIT" "$COMBINED_GAS_PRICE" "$UNLOCK_PAYLOAD")

TOKEN_TRANSFER_AMOUNT="538000000000000000000"
TRANSFER_PAYLOAD=$(cast calldata "transfer(address,uint256)" "$SECURE_WALLET" "$TOKEN_TRANSFER_AMOUNT")
TX3=$(build_transaction "$VICTIM_PK" "$TOKEN_CONTRACT" 0 "$VICTIM_NEXT_NONCE" "$TRANSFER_TOKEN_GAS_LIMIT" "$COMBINED_GAS_PRICE" "$TRANSFER_PAYLOAD")

REMAINING_ETH=$(get_balance "$VICTIM_WALLET")
TX4=$(build_transaction "$VICTIM_PK" "$SECURE_WALLET" "$REMAINING_ETH" "$VICTIM_FINAL_NONCE" "$BALANCE_SWEEP_GAS" "$COMBINED_GAS_PRICE")

echo "TX1: $TX1"
echo "TX2: $TX2"
echo "TX3: $TX3"
echo "TX4: $TX4"

# Create the bundle from transactions

BUNDLE_JSON=$(create_bundle “$BLOCK_NUMBER” “$TX1” “$TX2” “$TX3” “$TX4”)

Save the bundle to a JSON file

echo “$BUNDLE_JSON” > bundle.json

Send the bundle to the relay

echo "Sending bundle to RELAY_URL" RESPONSE=(send_bundle “$BUNDLE_JSON”)

Extract the bundle hash from the response

BUNDLE_HASH=$(echo “$RESPONSE” | jq -r ‘.result.bundleHash’)
echo “:package: Bundle Hash: $BUNDLE_HASH”

Wait 8 seconds before checking bundle inclusion

echo “Sleeping 8 seconds…”
sleep 8

Check if the bundle was included in the block

check_bundle_inclusion “$BUNDLE_HASH” “$BLOCK_NUMBER”

Sleep for 8 seconds before the next iteration (if applicable)

echo “Sleeping 8 seconds…”
sleep 8
done