16

I am building a web UI for testing smart contracts and currently I have to supply the ABI for a contract I test. Is there a way to extract the ABI from a deployed contract (preferably programmatically)?

Andrey
  • 1,147
  • 1
  • 12
  • 19

9 Answers9

12

If the source code has been published to Etherscan, then they have an API to retrieve it. From https://etherscan.io/apis#contracts:

Get Contract ABI for Verified Contract Source Codes

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

user19510
  • 27,999
  • 2
  • 30
  • 48
  • Unfortunately this wont work as I need it for smart contracts running on a private network (Ganache) – Andrey Jan 30 '18 at 15:06
  • That's correct. In general, there's no way to get the ABI for a contract unless it's been explicitly stored somewhere. – user19510 Jan 30 '18 at 16:57
7

Go to EtherScan > enter the contract address > click on the Contract in the tab section heading > Scroll down to find the Contract ABI > click on the Copy icon to copy it

ans
  • 3
  • 2
Russo
  • 1,784
  • 1
  • 21
  • 30
5

Yes, there are tools that try to recover ABI from bytecode.

For example porosity and mythril.

It is not always possible to recover ABI because bytecode does not contain function signatures but only last 4 bytes of function signature hash.

max taldykin
  • 2,966
  • 19
  • 27
  • If you look at potosity or mythril, you need an ABI to Disassemble or Decompilation. There's no way to get an ABI from a deployed contract. – hefgi May 16 '18 at 13:44
2

Deployed contracts are stored in bytecode so out of the box you can't extract the source ABI, however there are open source decompiling tools that attempt to recover the ABI.

What you can do is store the ABI JSON in IPFS and reference it by it's content hash.

Miguel Mota
  • 5,143
  • 29
  • 47
2

You can also copy your ABI in Remix by going to Compile-->Details(ABI) sectionABI

monkrus
  • 642
  • 7
  • 17
1

https://github.com/shazow/whatsabi

I think this is a good answer if someone is looking to get the function signatures, ABI from bytecode. It also has a way to retrieve the function signature in human readable form from 4 byte hash of the function signature.

If the loaders doesn't work you can always find signatures for most used functions here - https://www.4byte.directory/

Here is a code example that I used

const { whatsabi } = require("@shazow/whatsabi");

(async () => { // Your contract bytecode (hexadecimal) const bytecode = "your byte code here";

// Get an ABI-like list of interfaces const abi = whatsabi.abiFromBytecode(bytecode); console.dir(abi);

// We also have a suite of database loaders for convenience const signatureLookup = new whatsabi.loaders.OpenChainSignatureLookup(); for (const interface of abi) { const signature = await signatureLookup.loadFunctions(interface.selector); console.log(signature); } })();

Krushi Raj
  • 111
  • 2
0

Since you are developing the UI, I supposed you have developed the contract as well. In that case, if you are using Truffle, after deploying the contract you can find the ABI for each contract you deployed in the autogenerated build/contracts folder in your project, for example /build/contracts/Migrations.json.

0

If you are deploying by command line, you can read from the file where is previously builded, in the case of hardhat there is a example:

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

const getTheAbi = () => { try { const dir = path.resolve( __dirname, "./artifacts/contracts/HelloWorld.sol/HelloWorld.json" ) 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) } }

0

When using React, you can check the ABI of deployed contracts by accessing the artifacts folder that is created after deployment:

artifacts -> contracts -> smartcontract.sol -> smartcontract.json

  • If you are using Brownie for local python deployment, after you use command brownie compile, you can find the JSON ABI of your smart contract in this folder: build -> contracts -> ContractABC.json – Freddie von Stange Oct 20 '22 at 19:22