Back

blockchain - solidity 如何获得contract abi?

发布时间: 2022-01-12 06:47:00

参考:https://ethereum.stackexchange.com/questions/3149/how-do-you-get-a-json-file-abi-from-a-known-contract-address/26419

1. 需要有源代码

solc filename.sol --abi

也可以简单粗暴一些,直接 solc *.sol --abi

就可以看到当前目录下生成好多 .abi文件。

2. 如果没有源代码,也需要该contract 是被验证过的( contract is verified )

python fetch_abi.py <contract address> -o <target JSON file>

#!/usr/bin/python
import argparse
import requests
import json

# Exports contract ABI in JSON

ABI_ENDPOINT = 'https://api.etherscan.io/api?module=contract&action=getabi&address='

parser = argparse.ArgumentParser()
parser.add_argument('addr', type=str, help='Contract address')
parser.add_argument('-o', '--output', type=str, help="Path to the output JSON file", required=True)

def __main__():

    args = parser.parse_args()

    response = requests.get('%s%s'%(ABI_ENDPOINT, args.addr))
    response_json = response.json()
    abi_json = json.loads(response_json['result'])
    result = json.dumps({"abi":abi_json}, indent=4, sort_keys=True)

    open(args.output, 'w').write(result)

if __name__ == '__main__':
    __main__()

Back