Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the ABI from a contract address #129

Closed
josedvd123 opened this issue Feb 28, 2018 · 12 comments
Closed

How to get the ABI from a contract address #129

josedvd123 opened this issue Feb 28, 2018 · 12 comments
Labels
discussion Questions, feedback and general information.

Comments

@josedvd123
Copy link

Hi,
How can I get the ABI code from a contract address? There is a function to do that?

@ricmoo
Copy link
Member

ricmoo commented Mar 4, 2018

The ABI must be provided by the contract deployer, and cannot be (effectively) derived from just the address or the code at the address.

Often you can find the ABI published by the author by looking at the "code" tab on etherscan.io for the contract address.

Another option (which is highly under utilized) is the ABI can be stored in ENS for a given name.

Sorry if it isn't very helpful a response, but you basically will have to find the ABI from the contract author. :s

@ricmoo ricmoo added the discussion Questions, feedback and general information. label Mar 4, 2018
@ricmoo
Copy link
Member

ricmoo commented Mar 5, 2018

I'm closing this, but if you have any further questions, please feel free to re-open. :)

@ricmoo ricmoo closed this as completed Mar 5, 2018
@mhoangvslev
Copy link

I mean, with the etherscan api, we can retrieve the abi. Is it possible to do it directly with etherjs?

@ricmoo
Copy link
Member

ricmoo commented Apr 29, 2019

Only contracts that have had their source published and verified can have their ABI downloaded directly. This is something that might be worth adding to the EtherscanProvider as a convenience in the future, but as of now it cannot. Does Etherscan provide a public API for grabbing contract ABI?

Also, contract ABI may be stored in ENS, which is a more reliable way of associating data with a contract. I plan to put something together to help get more widespread adoption of using ENS, since then you could get both the address and ABI simply from a name like registrar.firefly.eth. :)

@mhoangvslev
Copy link

mhoangvslev commented Apr 29, 2019

Only contracts that have had their source published and verified can have their ABI downloaded directly. This is something that might be worth adding to the EtherscanProvider as a convenience in the future, but as of now it cannot. Does Etherscan provide a public API for grabbing contract ABI?

I think we can do it here: https://etherscan.io/apis#contracts

Also, contract ABI may be stored in ENS, which is a more reliable way of associating data with a contract. I plan to put something together to help get more widespread adoption of using ENS, since then you could get both the address and ABI simply from a name like registrar.firefly.eth. :)

That would be so nice!

@ricmoo
Copy link
Member

ricmoo commented May 6, 2019

That API has a lot of restrictions for now, so I may add those endpoints as an Ancillary package instead of part of the regular EtherscanProvider, but it depends how much code it ends up being.

It may make more sense to include as part of the @ethersproject/cli package, which includes a Solidity compiler, which may be necessary for parts of validation, but could wrap it up in a nice API.

@vietnguyen09
Copy link

Hi, sorry for digging this up, but do we have any way to get the ABI from the Contact address these days?

@iragsraghu
Copy link

Hi, Can we get ABI code with contract address and without any API providers

@ricmoo
Copy link
Member

ricmoo commented Jun 6, 2022

The ABI is only available if it is made available; there is no procedural way to extra the ABI from a contract. Many contracts on Etherscan are verified, in which case the ABI is available or if the ABI has been added to ENS (this is not common) it is available. Otherwise, you may have to reverse engineer the bytecode; which would be a fun project to build a more automated solution for, if someone wanted to learn more about compilers. :)

@sturmenta
Copy link

sturmenta commented Jul 31, 2022

you can read the abi from the builded file (this can work after deploy a contract)

const fs = require("fs")
const path = require("path")

const getTheAbi = () => {
  try {
    const dir = path.resolve(
      __dirname,
      "./artifacts/contracts/HelloWorld.sol/HelloWorld.json" // hardhat build dir
    )
    const file = fs.readFileSync(dir, "utf8")
    const json = JSON.parse(file)
    const abi = json.abi
    console.log(`abi`, abi)

    return abi
  } catch (e) {
    console.log(`e`, e)
  }
}

@didoshotev
Copy link

For local development it might be easier to use the hardhat approach without depending on external API's. This worked for me:

  const { artifacts } = require("hardhat");
  const contractArtifact = await artifacts.readArtifact("TrustVoteV1");
  const contractABI = contractArtifact.abi

@SKYBITDev3
Copy link

SKYBITDev3 commented Aug 5, 2023

That API has a lot of restrictions for now

It's no longer restrictive... you can just do a GET HTTP request like this:

https://api.etherscan.io/api?module=contract&action=getabi&address=0x98B2920D53612483F91F12Ed7754E51b4A77919e&apikey=

So it'd be easy to create a function in ethers that retrieves the ABI like this.

e.g.

const axios = require('axios')

const getContractAbi = async (contractAddress) => {
  const httpResponse = await axios.get(`https://api.etherscan.io/api?module=contract&action=getabi&address=${contractAddress}&apikey=${process.env.ETHERSCAN_API_KEY}`)
  return httpResponse.data.result
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Questions, feedback and general information.
Projects
None yet
Development

No branches or pull requests

8 participants