-
Notifications
You must be signed in to change notification settings - Fork 0
/
tech_test
executable file
·91 lines (74 loc) · 3.43 KB
/
tech_test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
# Set up variables
address="32ixEdVJWo3kmvJGMTZq5jAQVZZeuwnqzo"
transactions_file="cleaned_transactions.json"
thirty_days_ago=$(( $(date +%s) - 30*24*60*60 ))
seven_days_ago=$(( $(date +%s) - 7*24*60*60 ))
# Debugging output
echo "Running balance calculation script..."
echo "Address: $address"
echo "30 days ago timestamp: $thirty_days_ago"
echo "7 days ago timestamp: $seven_days_ago"
# Step 1: Calculate on-chain and mempool balances
on_chain_balance=$(curl -sSL "https://mempool.space/api/address/$address" | jq '.chain_stats.funded_txo_sum - .chain_stats.spent_txo_sum')
mempool_balance=$(curl -sSL "https://mempool.space/api/address/$address" | jq '.mempool_stats.funded_txo_sum - .mempool_stats.spent_txo_sum')
# Output on-chain and mempool balances
echo -e "\nOn-chain Balance: ${on_chain_balance:-0} sats"
echo "Mempool Balance: ${mempool_balance:-0} sats"
# Create data directory if it doesn't exist
mkdir -p data
# Step 2: Ensure transactions file exists
if [ ! -f "$transactions_file" ]; then
echo "Error: Transactions file $transactions_file not found!"
exit 1
fi
# Step 3: Filter transactions within the last 30 and 7 days
jq -s 'map(select(.status.block_time >= '"$thirty_days_ago"'))' "$transactions_file" > data/transactions_30d.json
jq -s 'map(select(.status.block_time >= '"$seven_days_ago"'))' "$transactions_file" > data/transactions_7d.json
# Step 4: Define functions to calculate received and sent amounts
calculate_received() {
file=$1
total_received=$(jq '[.[].vout[] | select(.scriptpubkey_address == "'$address'") | .value] | add' "$file")
echo ${total_received:-0}
}
calculate_sent() {
file=$1
total_sent=0
jq -c '.[]' "$file" | while read -r tx; do
echo "$tx" | jq -c '.vin[]?' | while read -r vin; do
prev_txid=$(echo "$vin" | jq -r '.txid')
vout_index=$(echo "$vin" | jq -r '.vout')
# Fetch the previous transaction
prev_tx=$(curl -sSL "https://mempool.space/api/tx/$prev_txid")
prev_vout=$(echo "$prev_tx" | jq ".vout[$vout_index]")
scriptpubkey_address=$(echo "$prev_vout" | jq -r '.scriptpubkey_address')
if [ "$scriptpubkey_address" == "$address" ]; then
value=$(echo "$prev_vout" | jq -r '.value')
total_sent=$((total_sent + value))
fi
done
done
echo $total_sent
}
# Step 5: Calculate balance change over the last 30 days
echo -e "\nCalculating received for the last 30 days..."
received_30d=$(calculate_received data/transactions_30d.json)
echo "Received for 30 days: $received_30d"
echo "Calculating sent for the last 30 days..."
sent_30d=$(calculate_sent data/transactions_30d.json)
echo "Sent for 30 days: $sent_30d"
balance_change_30d=$((received_30d - sent_30d))
echo "Balance variation over the last 30 days: $balance_change_30d sats"
# Step 6: Calculate balance change over the last 7 days
echo -e "\nCalculating received for the last 7 days..."
received_7d=$(calculate_received data/transactions_7d.json)
echo "Received for 7 days: $received_7d"
echo "Calculating sent for the last 7 days..."
sent_7d=$(calculate_sent data/transactions_7d.json)
echo "Sent for 7 days: $sent_7d"
balance_change_7d=$((received_7d - sent_7d))
echo "Balance variation over the last 7 days: $balance_change_7d sats"
# Step 7: Output final results
echo -e "\nTotal on-chain balance: ${on_chain_balance:-0} sats"
echo "Balance variation over the last 30 days: ${balance_change_30d:-0} sats"
echo "Balance variation over the last 7 days: ${balance_change_7d:-0} sats"