0

I'm not looking for pointers to etherscan or to remix so I can compile it myself. I want to know how to take a known contract address, and, through ethers.js natively get the full contract ABI.

1 Answer 1

2

Provided that your contract is verified, you can get the ABI using the Etherscan API like:

const contractAddress = "0xaa25DAc5505cbD5650554eb3fc2B4e084A2c18C6"

fetch(`https://api-sepolia.etherscan.io/api?module=contract&action=getabi&address=${contractAddress}`)
    .then(response => {
        if (!response.ok) {
            throw new Error('Network response was not ok ' + response.statusText);
        }
        return response.json();
    })
    .then(data => {
        if (data.status == "1")
            console.log(JSON.parse(data.result))

        else
            console.log(data.result); // Some error while fetching ABI like "Invalid Address format", etc.

    })
    .catch(error => {
        console.error('There has been a problem with your fetch operation:', error);
    });

OUTPUT

ABI

NOTE: In case of mainnet, you need to get the Etherscan API Key and pass the same in the url, like:

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

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.