Skip to content

Instantly share code, notes, and snippets.

@jcstein
Created January 13, 2025 21:46
Show Gist options
  • Save jcstein/0358f9fd80a2077d601945d46032fe7c to your computer and use it in GitHub Desktop.
Save jcstein/0358f9fd80a2077d601945d46032fe7c to your computer and use it in GitHub Desktop.
recover funds (from txsim)
#!/bin/bash
# Target address to send to
TARGET_ADDRESS="celestia1nqf3yn9mjuuldgxqrkj5fgca2tsy4p4a98e2vj"
# Directory containing the key files
KEYRING_DIR=".celestia-app/keyring-test"
# Function to get balance
get_balance() {
local address=$1
celestia-appd query bank balances "$address" -o json --node http://rpc-mocha.pops.one:26657 | jq -r '.balances[] | select(.denom == "utia") | .amount' || echo "0"
}
# Function to send tokens
send_tokens() {
local key_name=$1
local amount=$2
# First get gas estimate
local gas_estimate=$(celestia-appd tx bank send "$key_name" "$TARGET_ADDRESS" "1utia" \
--chain-id="mocha-4" \
--gas="auto" \
--gas-adjustment=1.3 \
--gas-prices="0.1utia" \
--keyring-backend="test" \
--dry-run --node http://rpc-mocha.pops.one:26657 2>&1 | grep "gas estimate:" | awk '{print $3}')
# Calculate gas cost (gas * gas price)
local gas_cost=$((gas_estimate * 1 / 10)) # 0.1 utia per gas unit
# Keep 1000 utia + gas cost
local reserve=10000
local total_reserve=$((reserve + gas_cost))
local amount_to_send=$((amount - total_reserve))
if [ "$amount_to_send" -gt 0 ]; then
echo "Gas estimate: $gas_estimate, Gas cost: $gas_cost utia"
echo "Sending $amount_to_send utia from $key_name to $TARGET_ADDRESS (keeping $total_reserve utia for reserve + gas)"
celestia-appd tx bank send "$key_name" "$TARGET_ADDRESS" "${amount_to_send}utia" \
--chain-id="mocha-4" \
--gas="auto" \
--gas-adjustment=1.3 \
--gas-prices="0.1utia" \
--keyring-backend="test" \
--yes \
--node http://rpc-mocha.pops.one:26657
# Add small delay between transactions
sleep 2
fi
}
# Process each key file
for file in "$KEYRING_DIR"/*.info; do
if [[ $file == *"testing.info"* ]]; then
continue # Skip the target account
fi
# Extract key name from filename
key_name=$(basename "$file" .info)
# Get the address for this key
address=$(celestia-appd keys show "$key_name" --keyring-backend="test" -a)
# Get and print the balance
balance=$(get_balance "$address")
echo "$key_name: $balance utia"
if [ -n "$balance" ] && [ "$balance" -gt 1000 ]; then
send_tokens "$key_name" "$balance"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment