#!/bin/bash
# Path ke direktori yang ingin Anda telusuri
#
# SnollyGoster
# Have you ever think if you were special?
# https://snollygoster.xyz
# @snollyygoster // snollygoster@duck.com // SnollyGoster#2340
#
# Script untuk auto claim pada wallet unclaimmed rewards
# Atur directory fungsi dibawah sesuai dir kamu

current=$(pwd)
directory=("${current}/keyring/")

# Mencari semua file .json dalam direktori dan sub-direktori
find "$directory" -type f -name "*.json" -exec bash -c '
    function execute_claim {
        local file="$1"
        local attempt_output
        local ore_command="ore --rpc https://api.mainnet-beta.solana.com/ --priority-fee 1000 --keypair $file claim"

        while true; do
            echo "Attempting to claim rewards for $file"
            attempt_output=$(stdbuf -o0 $ore_command 2>&1)
            echo "$attempt_output"

            if [[ $attempt_output =~ "Claimed" ]]; then
                echo "Claimed rewards for $file"
                break
            elif [[ $attempt_output =~ "max attempt" || $attempt_output =~ "error" || $attempt_output =~ "failed" ]]; then
                echo "Failed to claim rewards for $file. Retrying..."
            fi
        done
    }

    for file do
        echo "Checking rewards for $file"
        execute_claim "$file"
    done
' bash {} +
