Files
mint_ore/gatherSol.sh
2024-04-07 22:14:15 +08:00

78 lines
2.5 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
KPATH="."
# 目标账户的密钥文件路径
DESTINATION_KEYPAIR="./id1.json"
# 设置RPC节点
RPC_URL=""
# 最大尝试次数
MAX_RETRIES=10
# 重试间隔时间(秒)
RETRY_INTERVAL=0
# Solana CLI配置设置
solana config set --url $RPC_URL
# 清空账户并转移SOL带有重试机制
function drain_account_with_retry() {
from_keypair=$1
destination_address=$(solana-keygen pubkey $DESTINATION_KEYPAIR)
attempt=1
while [ $attempt -le $MAX_RETRIES ]; do
echo "Attempt $attempt: Calculating available balance for transfer..."
# 获取源账户余额以lamports为单位
balance_lamports=$(solana balance $from_keypair --url $RPC_URL --output json | jq -r '.lamports')
# 转换lamports为SOL
balance_SOL=$(echo "scale=9; $balance_lamports / 1000000000" | bc)
# 使用`solana fees`命令获取当前的交易费用以lamports为单位
fee_lamports=$(solana fees --url $RPC_URL --output json | jq -r '.lamportsPerSignature')
# 将费用转换为SOL
fee_SOL=$(echo "scale=9; $fee_lamports / 1000000000" | bc)
# 计算实际转账金额余额减去交易费用单位为SOL
amount_SOL=$(echo "$balance_SOL - $fee_SOL" | bc)
if (( $(echo "$amount_SOL > 0" | bc -l) )); then
echo "Transferring $amount_SOL SOL from $from_keypair to $destination_address..."
transfer_output=$(solana transfer $destination_address $amount_SOL --fee-payer $from_keypair --from $from_keypair --url $RPC_URL 2>&1)
transfer_status=$?
if [ $transfer_status -eq 0 ]; then
echo "Transfer successful."
return 0
else
echo "Transfer failed: $transfer_output"
echo "Waiting $RETRY_INTERVAL seconds before retry..."
sleep $RETRY_INTERVAL
fi
else
echo "Not enough balance in $from_keypair to cover the transaction fee after $attempt attempts."
return 1
fi
attempt=$((attempt+1))
done
echo "Transfer failed after $MAX_RETRIES attempts."
return 1
}
# 示例清空某个账户并将SOL转移到id1.json对应的账户带有重试机制
# 注意:请确保替换下面的路径为你的实际路径
# for keypair in *.json; do
for keypair in $(ls id*.json.d | sort -V); do
drain_account_with_retry $KPATH/$keypair
done
echo "Operation complete."