New paste Repaste Download
#!/bin/bash
# Script to consolidate UTXOs in the unmixed account using dcrctl.
# This script randomizes the batch size between 2 and 10 UTXOs per consolidation.
# It also sleeps for a random time between 30 minutes (1800s) and 90 minutes (5400s)
# between consolidations, averaging about 1 hour.
# Assumes dcrwallet is running, dcrctl configured, and wallet unlocked.
# If the wallet locks during execution, unlock it manually with:
# dcrctl --wallet walletpassphrase "<passphrase>" 3600
# Requires 'jq' for JSON parsing.
# Adjust variables as needed.
ACCOUNT="unmixed"          # Account name for unmixed funds
MIN_BATCH=2                # Minimum UTXOs to consolidate per batch
MAX_BATCH=10               # Maximum UTXOs to consolidate per batch
THRESHOLD=20               # Stop when UTXOs <= this number
MIN_SLEEP=1800             # Min sleep in seconds (30 min)
MAX_SLEEP=5400             # Max sleep in seconds (90 min)
DCRCTL="dcrctl --wallet"   # Add --testnet if on testnet
# Function to get current UTXO count
get_utxo_count() {
  $DCRCTL listunspent 0 999999 '[]' "\"$ACCOUNT\"" | jq 'length'
}
echo "============================================================="
echo "Starting randomized UTXO consolidation for account: $ACCOUNT"
echo "This script will consolidate UTXOs in random batches of $MIN_BATCH to $MAX_BATCH"
echo "until the total UTXOs are <= $THRESHOLD."
echo "Between each consolidation, it will sleep for a random time"
echo "between $(($MIN_SLEEP / 60)) and $(($MAX_SLEEP / 60)) minutes (average 60 minutes)."
echo "Ensure your wallet is unlocked. If it locks, unlock with:"
echo "dcrctl --wallet walletpassphrase \"<passphrase>\" 3600"
echo "Press Ctrl+C to stop at any time."
echo "============================================================="
CURRENT_COUNT=$(get_utxo_count)
echo "Initial UTXO count: $CURRENT_COUNT"
while [ "$CURRENT_COUNT" -gt "$THRESHOLD" ]; do
  # Calculate max possible to consolidate without going below threshold
  MAX_POSSIBLE=$((CURRENT_COUNT - THRESHOLD))
  
  # If max possible is less than min batch, stop
  if [ "$MAX_POSSIBLE" -lt "$MIN_BATCH" ]; then
    echo "Remaining UTXOs to consolidate ($MAX_POSSIBLE) is less than min batch ($MIN_BATCH). Stopping."
    break
  fi
  
  # Random batch size: between MIN_BATCH and min(MAX_BATCH, MAX_POSSIBLE)
  EFFECTIVE_MAX=$(($MAX_POSSIBLE < $MAX_BATCH ? $MAX_POSSIBLE : $MAX_BATCH))
  BATCH_SIZE=$((RANDOM % (EFFECTIVE_MAX - MIN_BATCH + 1) + MIN_BATCH))
  
  echo "Consolidating a random batch of $BATCH_SIZE UTXOs..."
  RESULT=$($DCRCTL consolidate "$BATCH_SIZE" "\"$ACCOUNT\"")
  if [ $? -ne 0 ]; then
    echo "Error: Consolidation failed. Check if wallet is unlocked, or try a smaller batch."
    echo "You may need to unlock the wallet again."
    exit 1
  fi
  echo "Success! Transaction ID: $RESULT"
  echo "Waiting for the transaction to propagate and confirm..."
  # Short sleep for tx confirmation (adjust if needed)
  sleep 60
  
  # Update count after consolidation
  CURRENT_COUNT=$(get_utxo_count)
  echo "Updated UTXO count: $CURRENT_COUNT"
  
  # If still above threshold, sleep randomly
  if [ "$CURRENT_COUNT" -gt "$THRESHOLD" ]; then
    SLEEP_TIME=$((RANDOM % (MAX_SLEEP - MIN_SLEEP + 1) + MIN_SLEEP))
    SLEEP_MINUTES=$(($SLEEP_TIME / 60))
    echo "Sleeping for $SLEEP_MINUTES minutes before next consolidation..."
    sleep $SLEEP_TIME
  fi
done
echo "============================================================="
echo "Consolidation complete. Final UTXO count: $CURRENT_COUNT"
echo "If count is still high, run the script again or adjust parameters."
echo "============================================================="
Filename: None. Size: 4kb. View raw, , hex, or download this file.

This paste expires on 2025-10-24 12:32:07.720683. Pasted through web.