blockchainethereumsolidityweb3js

Is it possible to get the ABI of a contract without the source code?


Is it possible to get the ABI of a known contract address without the source code?

The only way I've found is by using etherscan's API, but it only works for verified contracts.


Solution

  • Simple Answer: No


    Long Answer: Maybe. The ABI is generated from the source code, but if you know what the functions are, you can "create" the ABI yourself.

    The ABI of a contract stands for the application binary interface, and it just defines how to interact with a smart contract.

    For example, maybe you don't know what the source code of a contract is, but you know it has a transfer function. You could "make" the ABI as little as:

    [
      {
      "constant":false,
      "inputs":[
        {"name":"_to","type":"address"},
        {"name":"_value","type":"uint256"}
      ],
      "name":"transfer",
      "outputs":[
        {"name":"success",
        "type":"bool"}
      ],
      "payable":false,
      "stateMutability":"nonpayable",
      "type":"function"
      }
    ]
    

    Or with a compiled interface, since a compiled interface will output an ABI.

    pragma solidity ^0.8.8;
    
    interface ContractInterface {
      function transfer(address to, uint256 value) external returns (bool success);
    }
    

    Since ABIs and Interfaces don't have to encompass every single function a smart contract is capable of.

    Additionally, there are such things as Decompilers which attempt to decompile byte code to figure out what the contract is, with that you could get the ABI.