-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Comments
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 |
I'm closing this, but if you have any further questions, please feel free to re-open. :) |
I mean, with the etherscan api, we can retrieve the abi. Is it possible to do it directly with etherjs? |
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 |
I think we can do it here: https://etherscan.io/apis#contracts
That would be so nice! |
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 |
Hi, sorry for digging this up, but do we have any way to get the ABI from the Contact address these days? |
Hi, Can we get ABI code with contract address and without any API providers |
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. :) |
you can read the 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)
}
} |
For local development it might be easier to use the hardhat approach without depending on external API's. This worked for me:
|
It's no longer restrictive... you can just do a GET HTTP request like this: 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
} |
Hi,
How can I get the ABI code from a contract address? There is a function to do that?
The text was updated successfully, but these errors were encountered: