如何在Python中保存来自以太坊区块链的Solidity智能合约的源代码?

发布于 2025-01-17 07:43:51 字数 683 浏览 4 评论 0原文

我正在寻找一种快速获取智能合约源代码的方法。我尝试使用以下Python代码:

import requests
import json

address = "0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413"
api_key = my_api_key
request_string = f'''https://api.etherscan.io/api?module=contract&action=getsourcecode&address={address}&apikey={api_key}'''

response = requests.get(request_string)
print(response.text)
data = json.loads(response.text)['result'][0]['SourceCode']


file = open("contract.sol", "w")
a = file.write(data)
file.close()

因此,虽然这适用于给定地址,但如果源代码由多个文件组成(例如此地址:0xED5AF388653567Af2F388E6224dC7C4b3241C544),则它不起作用。那么有没有一种快速简便的方法将它们全部保存到一个文件中呢?或者我只需要为每个文件创建一个单独的文件?

I'm looking for a quick way to get the source code of a smartcontract. I tried using the following python code:

import requests
import json

address = "0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413"
api_key = my_api_key
request_string = f'''https://api.etherscan.io/api?module=contract&action=getsourcecode&address={address}&apikey={api_key}'''

response = requests.get(request_string)
print(response.text)
data = json.loads(response.text)['result'][0]['SourceCode']


file = open("contract.sol", "w")
a = file.write(data)
file.close()

So while this works for the given address, it doesn't work if the source code consists of multiple files (like with this address: 0xED5AF388653567Af2F388E6224dC7C4b3241C544). So is there a quick and easy way to save all of them into one file? Or do I just have to create a separate file for each one of the files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文