Advertisement
tenequm

cat20-mint.sh

Sep 22nd, 2024 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.53 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Configuration
  4. MAX_MINTS=100
  5. DEFAULT_FEE_THRESHOLD=500
  6.  
  7. # $kibble
  8. TOKEN_ID=c468e99ac3b533e503eac5ccf4f0e3362772f80cead8b7f71d802305d02f73d0_0
  9.  
  10. LOG_FILE="./mint_operations.log"
  11.  
  12. # ANSI color codes
  13. GREEN='\033[0;32m'
  14. RED='\033[0;31m'
  15. YELLOW='\033[0;33m'
  16. NC='\033[0m' # No Color
  17.  
  18. # Function to log messages
  19. log_message() {
  20.     local color=$2
  21.     local message="[$(date '+%Y-%m-%d %H:%M:%S')] $1"
  22.     echo -e "${color}${message}${NC}"
  23.     echo -e "$message" | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g' >> "$LOG_FILE"
  24. }
  25.  
  26. # Function to display usage
  27. usage() {
  28.     echo "Usage: $0 <config_file1> <config_file2> ... [-f fee_threshold]"
  29.     echo "  -f fee_threshold: Optional. Set the fee threshold (default: $DEFAULT_FEE_THRESHOLD)"
  30.     exit 1
  31. }
  32.  
  33. # Store all arguments
  34. args=("$@")
  35.  
  36. # Find the position of -f option
  37. f_position=-1
  38. for i in "${!args[@]}"; do
  39.     if [[ ${args[i]} == "-f" ]]; then
  40.         f_position=$i
  41.         break
  42.     fi
  43. done
  44.  
  45. # Parse -f option if present
  46. if [[ $f_position -ne -1 ]]; then
  47.     if [[ $((f_position + 1)) -lt ${#args[@]} ]]; then
  48.         FEE_THRESHOLD=${args[$((f_position + 1))]}
  49.         # Remove -f and its value from args
  50.         unset 'args[f_position]'
  51.         unset 'args[$((f_position + 1))]'
  52.         # Reindex array
  53.         args=("${args[@]}")
  54.     else
  55.         echo "Error: -f option requires a value" >&2
  56.         usage
  57.     fi
  58. fi
  59.  
  60. # Set FEE_THRESHOLD to default if not provided
  61. FEE_THRESHOLD=${FEE_THRESHOLD:-$DEFAULT_FEE_THRESHOLD}
  62.  
  63. # Check if config files are provided as arguments
  64. if [ ${#args[@]} -eq 0 ]; then
  65.     log_message "Error: No config files provided." "$RED"
  66.     usage
  67. fi
  68.  
  69. # Store config files in an array
  70. CONFIG_FILES=("${args[@]}")
  71.  
  72. log_message "Starting mint operations. Will attempt $MAX_MINTS mints. Fee threshold: $FEE_THRESHOLD"
  73.  
  74. # Run yarn build
  75. log_message "Running yarn build..."
  76. if yarn build; then
  77.     log_message "yarn build completed successfully."
  78. else
  79.     log_message "Error: yarn build failed. Exiting script."
  80.     exit 1
  81. fi
  82.  
  83. mints_completed=0
  84. config_index=0
  85. while [ $mints_completed -lt $MAX_MINTS ]; do
  86.     fee=$(curl -sSL "https://mempool.fractal.rushfi.app/api/v1/fees/recommended" | jq -r '.hourFee')
  87.     if [ "$fee" -le $FEE_THRESHOLD ]; then
  88.         ((mints_completed++))
  89.         attempt=1
  90.        
  91.         # Get the current config file
  92.         current_config=${CONFIG_FILES[$config_index]}
  93.        
  94.         # Update the MINT_COMMAND with the current config file
  95.         MINT_COMMAND="yarn workspace @cat-protocol/cat-cli cli mint -i ${TOKEN_ID} --max-fee-rate ${FEE_THRESHOLD} -c ${current_config}"
  96.         echo "${MINT_COMMAND}"
  97.        
  98.         log_message "Mint $mints_completed: Starting mint operation (Fee: $fee, Config: $current_config)" "$GREEN"
  99.         while true; do
  100.             # Run the command and capture its output
  101.             output=$($MINT_COMMAND 2>&1)
  102.             exit_code=$?
  103.            
  104.             # Log the output without duplicating it
  105.             echo "$output" | sed 's/^/    /' # Indent the output for better readability
  106.            
  107.             # Check for success pattern in the output using a regular expression
  108.             if echo "$output" | grep -qE "Minting [0-9.]+ [A-Za-z]+ tokens in txid"; then
  109.                 mint_amount=$(echo "$output" | grep -E "Minting" | sed -E 's/.*Minting ([0-9.]+) [A-Za-z]+ tokens.*/\1/')
  110.                 txid=$(echo "$output" | grep -E "Minting" | sed -E 's/.*in txid: ([a-f0-9]+).*/\1/')
  111.                 token_name=$(echo "$output" | grep -E "Minting" | sed -E 's/.*Minting [0-9.]+ ([A-Za-z]+) tokens.*/\1/')
  112.                 log_message "Mint $mints_completed completed successfully: Minted $mint_amount $token_name tokens (txid: $txid, Config: $current_config)" "$YELLOW"
  113.                 break
  114.             elif echo "$output" | grep -qE "mint token \[[A-Za-z]+\] failed"; then
  115.                 log_message "Mint $mints_completed failed (Attempt $attempt, Config: $current_config)" "$RED"
  116.                 ((attempt++))
  117.             else
  118.                 log_message "Mint $mints_completed encountered an unexpected error (Attempt $attempt, Exit code: $exit_code, Config: $current_config)" "$RED"
  119.                 ((attempt++))
  120.             fi
  121.         done
  122.        
  123.         # Move to the next config file
  124.         config_index=$(( (config_index + 1) % ${#CONFIG_FILES[@]} ))
  125.     else
  126.         log_message "Fee too high ($fee). Waiting 5 seconds before checking again..." "$RED"
  127.         sleep 5
  128.     fi
  129. done
  130.  
  131. log_message "Completed all $MAX_MINTS mint operations." "$GREEN"
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement