forked from code-423n4/2024-01-salty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitSolidityFilesfromJson
167 lines (167 loc) · 6.08 KB
/
splitSolidityFilesfromJson
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
165
166
167
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyPXQPAlJklsrl5XdV56EiIb",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/Misbah-Engr/2024-01-salty/blob/main/splitSolidityFilesfromJson\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"!pip install etherscan-python\n",
"import etherscan\n",
"\n",
"print(dir(etherscan))"
],
"metadata": {
"collapsed": true,
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "QaTGBwJR0ujw",
"outputId": "6efca4e5-236f-4882-b2db-0298bf541bcb"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: etherscan-python in /usr/local/lib/python3.10/dist-packages (2.1.0)\n",
"Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from etherscan-python) (2.32.3)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->etherscan-python) (3.4.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->etherscan-python) (3.10)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->etherscan-python) (2.2.3)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->etherscan-python) (2024.8.30)\n",
"['Etherscan', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'accounts', 'blocks', 'configs', 'contracts', 'enums', 'etherscan', 'gastracker', 'modules', 'pro', 'proxy', 'stats', 'tokens', 'transactions', 'utils']\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"api_key = \"QW6J5HDTBCMATVWSJPTUQ82F6QJK93AD9Q\"\n",
"address = \"0xbf5495Efe5DB9ce00f80364C8B423567e58d2110\""
],
"metadata": {
"id": "aStO88Fm2OW9"
},
"execution_count": 1,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from etherscan import contracts"
],
"metadata": {
"id": "97zEWALm38uj"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from google.colab import drive\n",
"import os\n",
"import json\n",
"from etherscan import Etherscan\n",
"\n",
"import json\n",
"import os\n",
"\n",
"json_file_path = \"https://drive.google.com/file/d/1--AR1WjjAlTT3cG8TvQhMWhVPdsVGZ5y/view?usp=sharing\"\n",
"output_dir = \"output\"\n",
"def extract_contracts_from_json(json_file_path, output_dir):\n",
" \"\"\"Extracts Solidity contracts from a combined JSON file.\n",
"\n",
" Args:\n",
" json_file_path: Path to the downloaded combined JSON file.\n",
" output_dir: Directory to save the extracted .sol files.\n",
" \"\"\"\n",
"\n",
" with open(json_file_path, 'r') as f:\n",
" data = json.load(f)\n",
"\n",
" sources = data.get('sources', {})\n",
"\n",
" if not os.path.exists(output_dir):\n",
" os.makedirs(output_dir)\n",
"\n",
" for file_name, content in sources.items():\n",
" if file_name.endswith(\".sol\"): # Make sure it's a Solidity file\n",
" file_path = os.path.join(output_dir, file_name)\n",
" with open(file_path, 'w') as outfile:\n",
" outfile.write(content.get('content', ''))\n",
"\n",
"if __name__ == \"__main__\":\n",
" import argparse\n",
"\n",
" parser = argparse.ArgumentParser(description='Extract contracts from combined JSON.')\n",
" parser.add_argument('json_file', type=str, help='Path to the combined JSON file.')\n",
" parser.add_argument('-o', '--output', metavar='output', type=str, required=False, default=\"output\", help='Output directory.')\n",
" args = parser.parse_args()\n",
"\n",
" extract_contracts_from_json(args.json_file, args.output)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vI8HHMT90zFQ",
"outputId": "6df45f05-c3c9-448c-af6e-3f1c96701764"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"usage: colab_kernel_launcher.py [-h] [-o output] json_file\n",
"colab_kernel_launcher.py: error: unrecognized arguments: -f\n"
]
},
{
"output_type": "error",
"ename": "SystemExit",
"evalue": "2",
"traceback": [
"An exception has occurred, use %tb to see the full traceback.\n",
"\u001b[0;31mSystemExit\u001b[0m\u001b[0;31m:\u001b[0m 2\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py:3561: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.\n",
" warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n"
]
}
]
}
]
}