38

I want to watch a Solidity contract using the Mist browser. I know how to do that and it needs the address and the json. I have the address, but I don't have the json. I do have the source code.

Can I generate the json from the source code and/or the Ethereum address?

26

The JSON is called an ABI.

You do need the source code, as you have, and one way to get the ABI is to paste it in Solidity Browser, then copy the Interface value.

  • 3
    @eth: what if it's someone else's contract that's already deployed to the blockchain? You know where the contract sits (hash of it), but you don't know the abi. How can you interact with it? – David Mar 1 '17 at 18:53
  • @David If the author hasn't published the ABI, they may not want you to interact with it. In that case, you can try reverse engineering their code to interact with it. – eth Mar 1 '17 at 19:05
6

Here is how you get the abi by using your local solc compiler:

solc filename.sol --abi

You also need the bin code to deploy the contract, which you can get as follows:

solc filename.sol --bin

If you want a solc combined output (e.g. for parity), where bin is part of the abi json string, use this:

solc filename.sol --combined-json abi,bin
5

EDIT

On Compile tab (right top menu) just click on Details.


OLD METHOD

In the new version of Solidity Browser you can find Interface value clicking on Contract details (bytecode, interface etc.)

Solidity Browser Json Interface

  • Where is Contract details (bytecode, interface etc.) located? @NineCattoRules – alper Feb 24 '18 at 23:59
  • 2
    @Alper my answer is old. On Compile tab just click on Details – NineCattoRules Mar 1 '18 at 9:28
3

If the contract is verified in Etherscan, the following script fetches the ABI and saves it to a JSON file. Use it like

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

fetch_abi.py

#!/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__()
1

Go to EtherScan > enter the contract address > click on the Code in the tab section heading > Scroll down to find the Contract ABI > click on the Copy icon to copy it

  • This only works is the publisher of the smart contract has chosen to publish their ABI, but you're right. As far as I know, this is the only way to get it. – Thomas Jay Rush May 31 '18 at 9:31
1

Simple go to http://remix.ethereum.org/ and paste the source code of the contract. Click of Start compile and after a few seconds or so, your contract is processed. Click on detail button (found at the top-right). On the popup, you would find the ABI, METADATA, BYTECODE, etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.