-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabi-diff.sh
executable file
·164 lines (140 loc) · 4.9 KB
/
abi-diff.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env bash
# abi-diff.sh
# Usage: ./abi-diff.sh <old_abi.json> <new_abi.json>
# Compares two ABI JSON files, outputs differences in same functions and lists missing/added functions with stateMutability
# Exit immediately if a command exits with a non-zero status
set -e
# Function to display usage instructions
usage() {
echo "Usage: $0 <old_abi.json> <new_abi.json>"
exit 1
}
# Check if two arguments are provided
if [ "$#" -ne 2 ]; then
echo "Error: Two ABI JSON files must be provided"
usage
fi
# Assign input arguments to variables
old_abi="$1"
new_abi="$2"
# Verify that both files exist
if [ ! -f "$old_abi" ]; then
echo "Error: File '$old_abi' not found"
exit 1
fi
if [ ! -f "$new_abi" ]; then
echo "Error: File '$new_abi' not found"
exit 1
fi
# Check if 'gawk' is installed; if not, fallback to 'awk'
if command -v gawk &> /dev/null; then
AWK_CMD="gawk"
else
AWK_CMD="awk"
fi
# Create a temporary directory to store intermediate files
temp_dir=$(mktemp -d)
# Ensure temporary files are cleaned up on script exit
cleanup() {
rm -rf "$temp_dir"
}
trap cleanup EXIT
# Define paths for temporary files
old_functions="$temp_dir/old_functions.txt"
new_functions="$temp_dir/new_functions.txt"
old_signatures="$temp_dir/old_signatures.txt"
new_signatures="$temp_dir/new_signatures.txt"
common_signatures="$temp_dir/common_signatures.txt"
common_joined="$temp_dir/common_joined.txt"
missing_in_new="$temp_dir/missing_in_new.txt"
added_in_new="$temp_dir/added_in_new.txt"
differences="$temp_dir/differences.txt"
# Function to extract functions from an ABI and format them
extract_functions() {
local abi_file="$1"
local output_file="$2"
jq -r '
.[] |
select(.type == "function") |
"\(.name)(\(.inputs | map(.type) | join(",")))\t\(.stateMutability // "nonpayable")\t\(.outputs | map(.type) | join(","))"
' "$abi_file" | sort > "$output_file"
}
# Extract functions from both ABIs
extract_functions "$old_abi" "$old_functions"
extract_functions "$new_abi" "$new_functions"
# Extract function signatures (name and input types) for comparison
cut -f1 "$old_functions" | sort > "$old_signatures"
cut -f1 "$new_functions" | sort > "$new_signatures"
# Identify functions present in old ABI but missing in new ABI
comm -23 "$old_signatures" "$new_signatures" > "$missing_in_new"
# Identify functions present in new ABI but missing in old ABI
comm -13 "$old_signatures" "$new_signatures" > "$added_in_new"
# Identify functions present in both ABIs
comm -12 "$old_signatures" "$new_signatures" > "$common_signatures"
# Join the details of common functions from both ABIs
join -t $'\t' -1 1 -2 1 <(sort "$old_functions") <(sort "$new_functions") > "$common_joined"
# Compare properties of common functions and identify differences
$AWK_CMD -F'\t' '{
if ($2 != $4 || $3 != $5) {
print "Function: " $1
if ($2 != $4) {
print " - stateMutability changed from \"" $2 "\" to \"" $4 "\""
}
if ($3 != $5) {
print " - Outputs changed from [" $3 "] to [" $5 "]"
}
print ""
}
}' "$common_joined" > "$differences"
# Display the comparison results
echo "=== ABI Comparison Result ==="
# Display missing functions in the new ABI with stateMutability
if [ -s "$missing_in_new" ]; then
echo ""
echo "🔴 Missing functions in new ABI:"
while IFS= read -r func; do
# Extract stateMutability from old_functions using inline awk substitution
# Handle special characters by embedding the function signature directly into awk
state=$($AWK_CMD -F'\t' '$1 == "'"$func"'" {print $2}' "$old_functions")
# Handle empty stateMutability
if [ -z "$state" ]; then
state="N/A"
fi
echo " - $func [stateMutability: $state]"
done < "$missing_in_new"
else
echo ""
echo "✅ No missing functions in new ABI"
fi
# Display added functions in the new ABI with stateMutability
if [ -s "$added_in_new" ]; then
echo ""
echo "🟢 Added functions in new ABI:"
while IFS= read -r func; do
# Extract stateMutability from new_functions using inline awk substitution
state=$($AWK_CMD -F'\t' '$1 == "'"$func"'" {print $2}' "$new_functions")
# Handle empty stateMutability
if [ -z "$state" ]; then
state="N/A"
fi
echo " - $func [stateMutability: $state]"
done < "$added_in_new"
else
echo ""
echo "✅ No added functions in new ABI"
fi
# Display functions with differing properties
if [ -s "$differences" ]; then
echo ""
echo "🟡 Functions with differences:"
echo ""
cat "$differences"
else
echo ""
echo "✅ No differences found in common functions"
fi
# Final confirmation if ABIs are fully compatible
if [ ! -s "$missing_in_new" ] && [ ! -s "$added_in_new" ] && [ ! -s "$differences" ]; then
echo ""
echo "🎉 No differences found. The ABIs are fully identical"
fi