Created
April 7, 2022 12:26
-
-
Save BlockmanCodes/ce1987b4c244451da2774aaa2fa256ea to your computer and use it in GitHub Desktop.
Get a contract ABI programmatically with the Etherscan API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios') | |
const { ethers } = require('ethers') | |
const address = '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599' | |
const apiKey = '' | |
const url = `https://api.etherscan.io/api?module=contract&action=getabi&address=${address}&apikey=${apiKey}` | |
const infuraUrl = '' | |
const getAbi = async () => { | |
const res = await axios.get(url) | |
const abi = JSON.parse(res.data.result) | |
// console.log(abi) | |
const provider = new ethers.providers.JsonRpcProvider(infuraUrl) | |
const contract = new ethers.Contract( | |
address, | |
abi, | |
provider | |
) | |
const name = await contract.name() | |
const totalSupply = await contract.totalSupply() | |
console.log(name) | |
console.log(totalSupply.toString()) | |
} | |
getAbi() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "getabi", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"axios": "^0.26.1", | |
"ethers": "^5.6.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment