-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparallel_rpc_requests.sh
More file actions
52 lines (43 loc) · 1.79 KB
/
parallel_rpc_requests.sh
File metadata and controls
52 lines (43 loc) · 1.79 KB
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
#!/bin/bash
# --- Endpoints ---
URL1_ENDPOINT="https://sepolia.example.org/"
URL2_ENDPOINT="https://sepolia.example.org/"
URL3_ENDPOINT="https://sepolia.example.org/"
COMMON_HEADERS="-H \"Content-Type: application/json\""
COMMON_DATA='{ "jsonrpc":"2.0", "method":"eth_maxPriorityFeePerGas", "params":[], "id":1 }'
# Create temporary files to store the output of each command
# mktemp creates a unique, secure temporary file
TMP_FILE1=$(mktemp)
TMP_FILE2=$(mktemp)
TMP_FILE3=$(mktemp)
# This is a safety net. It ensures the temporary files are removed when the script exits,
# even if it exits with an error.
trap 'rm -f "$TMP_FILE1" "$TMP_FILE2" "$TMP_FILE3"' EXIT
echo "Sending parallel POST requests..."
echo "------------------------------------"
# Start the POST requests in the background, redirecting their output (stdout) to the temp files.
# Pass the arguments directly to curl.
curl -s -X POST "$URL1_ENDPOINT" -H "Content-Type: application/json" -d "$COMMON_DATA" > "$TMP_FILE1" &
curl -s -X POST "$URL2_ENDPOINT" -H "Content-Type: application/json" -d "$COMMON_DATA" > "$TMP_FILE2" &
curl -s -X POST "$URL3_ENDPOINT" -H "Content-Type: application/json" -d "$COMMON_DATA" > "$TMP_FILE3" &
# Wait for both background jobs to complete writing to their files.
wait
# Now, read the contents of the completed files into variables.
# This happens in the main script, so the variables will be set correctly.
output1=$(cat "$TMP_FILE1")
output2=$(cat "$TMP_FILE2")
output3=$(cat "$TMP_FILE3")
echo
echo "All POST requests have completed."
echo
echo "--- Response from 1 ---"
echo "$output1"
echo "------------------------------------"
echo
echo "--- Response from 2 ---"
echo "$output2"
echo "------------------------------------"
echo
echo "--- Response from 3 ---"
echo "$output3"
echo "------------------------------------"