75

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?

7 Answers 7

48

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 Remix IDE, compile it, and then click the 'ABI' button to copy the ABI into your clipboard.

2
  • 10
    @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
    Commented Mar 1, 2017 at 18:53
  • 2
    @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
    Commented Mar 1, 2017 at 19:05
20

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__()
19

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
0
7

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

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

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

1
  • 1
    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. Commented May 31, 2018 at 9:31
7

You can get ABI json from etherscan API like below.

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider());
var version = web3.version.api;
        
$.getJSON('https://api.etherscan.io/api?module=contract&action=getabi&address=0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359', function (data) {
    var contractABI = "";
    contractABI = JSON.parse(data.result);
    if (contractABI != ''){
        var MyContract = web3.eth.contract(contractABI);
        var myContractInstance = MyContract.at("0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359");
        var result = myContractInstance.memberId("0xfe8ad7dd2f564a877cc23feea6c0a9cc2e783715");
        console.log("result1 : " + result);            
        var result = myContractInstance.members(1);
        console.log("result2 : " + result);
    } else {
        console.log("Error" );
    }            
})
2
  • Where would you put your contract id? Do you have to upload the contract somewhere first?
    – Lokiare
    Commented Feb 11, 2021 at 1:03
  • You can fetch ABI already deployed contracts on ethereum network.
    – Savas Adar
    Commented Feb 11, 2021 at 9:05
2

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 and acknowledge you have read our privacy policy.

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