rpc array
This commit is contained in:
77
gatherSol.sh
Normal file
77
gatherSol.sh
Normal file
@@ -0,0 +1,77 @@
|
||||
#!/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."
|
||||
Reference in New Issue
Block a user