forked from ajna-finance/testchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetBlockTime.sh
executable file
·34 lines (26 loc) · 1.27 KB
/
getBlockTime.sh
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
#!/bin/bash
# Define the Ganache URL (provided as a command-line argument)
GANACHE_URL=$1
# Check if the ganache argument is provided
if [ -z "$GANACHE_URL" ]; then
echo "Please provide the ganache url as a command-line argument."
exit 1
fi
# Call eth_blockNumber to get the latest block number
block_number_response=$(curl -s -X POST --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' $GANACHE_URL)
block_number=$(echo $block_number_response | jq -r '.result')
# Convert the block number from hexadecimal to decimal
block_number_decimal=$(printf "%d" $block_number)
# Call eth_getBlockByNumber to get the latest block details
block_response=$(curl -s -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", true],"id":1}' $GANACHE_URL)
block_data=$(echo $block_response | jq '.result')
block_timestamp=$(echo $block_data | jq -r '.timestamp')
if [ "$block_timestamp" == "null" ]; then
echo "Latest block is pending and does not have a timestamp yet."
else
decimal_date=$(printf "%d" $block_timestamp)
readable_date=$(perl -le 'print scalar localtime $ARGV[0]' $decimal_date)
echo "Latest block number: $block_number_decimal"
echo "Latest block timestamp: $decimal_date"
echo "Latest block date: $readable_date"
fi