Skip to content

Commit 3704530

Browse files
committed
feat: utility files
1 parent 83d880a commit 3704530

File tree

7 files changed

+757
-0
lines changed

7 files changed

+757
-0
lines changed

diffcheck/README.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# Solidity Source Code Diff Tool
2+
3+
A TypeScript utility to compare Solidity source code from Etherscan verification responses.
4+
5+
## Overview
6+
7+
This tool takes two JSON files containing Etherscan contract verification results and provides a detailed comparison of the source code differences. It extracts individual `.sol` files from the verification data and presents a colorized diff output.
8+
9+
## Features
10+
11+
- 📊 **Comprehensive Analysis**: Categorizes files as added, removed, modified, or unchanged
12+
- 🎨 **Colorized Output**: Easy-to-read terminal output with colors and emojis
13+
- 📝 **Focused Diffs**: Shows only changed lines (additions/deletions) with minimal context
14+
- 🧠 **Smart Diff Algorithm**: Uses Longest Common Subsequence (LCS) to ignore line number shifts
15+
- 🎯 **Context Lines**: Shows 2 lines above and below changes for better understanding
16+
-**Whitespace Intelligence**: Treats files as unchanged if only whitespace differs
17+
- 📋 **Summary Report**: Overview of all changes at a glance
18+
- 🔍 **Smart Parsing**: Handles double-encoded JSON from Etherscan responses
19+
20+
## Prerequisites
21+
22+
- Node.js (v14 or higher)
23+
- TypeScript and ts-node (installed automatically via npm)
24+
25+
## Installation
26+
27+
1. Navigate to the diffcheck directory:
28+
```bash
29+
cd diffcheck
30+
```
31+
32+
2. Install dependencies:
33+
```bash
34+
npm install
35+
```
36+
37+
## Usage
38+
39+
1. Ensure you have `source1.json` and `source2.json` in the diffcheck directory
40+
2. Run the comparison:
41+
```bash
42+
npm run diff
43+
```
44+
or
45+
```bash
46+
npm start
47+
```
48+
49+
## Input Format
50+
51+
The script expects JSON files in the Etherscan API response format:
52+
53+
```json
54+
{
55+
"status": "1",
56+
"message": "OK",
57+
"result": [
58+
{
59+
"SourceCode": "{{\"language\":\"Solidity\",\"sources\":{\"ContractName.sol\":{\"content\":\"...\"}}}"
60+
}
61+
]
62+
}
63+
```
64+
65+
## Output
66+
67+
The tool provides:
68+
69+
1. **Summary Statistics**: Count of added, removed, modified, and unchanged files
70+
2. **Added Files**: List of files present only in source2
71+
3. **Removed Files**: List of files present only in source1
72+
4. **Modified Files**: Detailed line-by-line diffs for changed files
73+
5. **Unchanged Files**: List of files with identical content
74+
75+
### Example Output
76+
77+
```
78+
📊 SOURCE CODE COMPARISON REPORT
79+
=====================================
80+
81+
📋 SUMMARY:
82+
✅ Added files: 2
83+
❌ Removed files: 0
84+
📝 Modified files: 3
85+
⚪ Unchanged files: 15
86+
87+
📁 ADDED FILES:
88+
+ contracts/NewFeature.sol
89+
+ interfaces/INewInterface.sol
90+
91+
📝 MODIFIED FILES:
92+
93+
1. contracts/ExistingContract.sol
94+
────────────────────────────────────
95+
📊 Lines in source1: 150
96+
📊 Lines in source2: 155
97+
98+
43: import { IERC20 } from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
99+
+ 44: import { SafeERC20 } from '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
100+
45: import { OwnableUpgradeable } from '@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol';
101+
...
102+
65: using FullMath for uint256;
103+
+ 66: using SafeERC20 for IERC20;
104+
67:
105+
...
106+
```
107+
108+
## File Structure
109+
110+
```
111+
diffcheck/
112+
├── source1.json # First source to compare
113+
├── source2.json # Second source to compare
114+
├── diff-sources.ts # Main comparison script
115+
├── package.json # Dependencies and scripts
116+
└── README.md # This file
117+
```
118+
119+
## Development
120+
121+
To modify or extend the script:
122+
123+
1. Edit `diff-sources.ts`
124+
2. Build TypeScript:
125+
```bash
126+
npm run build
127+
```
128+
3. Run directly with ts-node:
129+
```bash
130+
npm run diff
131+
```
132+
133+
## Customization
134+
135+
You can customize the diff behavior by modifying these parameters in the script:
136+
137+
- **Smart Diff Algorithm**: The script uses LCS to detect real changes vs line shifts
138+
- **Context Lines**: Modify `contextLines` variable to show more/fewer lines around changes (default: 2)
139+
- **Normalization**: Modify `normalizeContent()` to adjust whitespace handling
140+
- **Color Scheme**: Customize colors by modifying the `colors` object
141+
- **Focused Output**: Only shows changed lines with minimal context for clarity
142+
143+
## Troubleshooting
144+
145+
**Error: Invalid response format**
146+
- Ensure your JSON files follow the expected Etherscan API format
147+
- Check that the `SourceCode` field contains valid JSON
148+
149+
**Error: Cannot read file**
150+
- Verify that `source1.json` and `source2.json` exist in the diffcheck directory
151+
- Check file permissions
152+
153+
## License
154+
155+
MIT
156+
157+
158+
https://app.gmx.io/#/complete_account_transfer/0x2f88a09ed4174750a464576FE49E586F90A34820/0x1719039D794f9B8c4B2d3d7C3A54323418F7C463
159+
https://api.etherscan.io/v2/api?chainid=42161&module=contract&action=getsourcecode&address=0xa121D6e494ce7505863AfBd5Ed865681476B4164&apikey=A53EESNMC3YNFSDZUD5EB82J6HCATJ5NAW

0 commit comments

Comments
 (0)