17

Remix makes it so easy to get the JSON ABI.

Does anyone have any recommendation on how best to get the JSON ABI after successfully deploying a contract using Truffle without having to leverage Remix?

eth
  • 85,679
  • 53
  • 285
  • 406
wardsback
  • 399
  • 1
  • 5
  • 12

5 Answers5

13

when you run

$ truffle compile

truffle creates and saves a json file per contract in /yourProjectPath/build/contracts. In this json file you'll find the abi, the bytecode, the topics (if you have any event in your smart contract), the function definitions etc (you get the point I guess, I encourage you to spend some time understanding that file - just to get a broad understanding).

personally I use python for my projects. Here is how I automatically load my abi into my scripts for transacting with my contracts (using web3py)

import json

PATH_TRUFFLE_WK = '/home/myUserName/Projects/myEthereumProjet/'
truffleFile = json.load(open(PATH_TRUFFLE_WK + '/build/contracts/myContractName.json'))

abi = truffleFile['abi']
bytecode = truffleFile['bytecode']

You can do the same with your favorite programming language or simply copying-pasting your abi by hand.

salanfe
  • 611
  • 4
  • 9
3

@salanfe: Your answer is correct.

There are some problems with the truffle compiler though. I had problems especially with functions that had no input parameters...

Due to this issue I suggest to use solc instead:

solcjs --abi path/to/your/contract.sol

Hope it helps.

joffi
  • 437
  • 3
  • 6
3

Consider using solc-compiler directly.

More convenient to use the Solc docker image:

docker run \
  -v {smart_contract_location_path}:/src:ro \
  -v {result_abi_location_path}:/build \
  ethereum/solc:0.8.3 \
  --allow-paths /src/node_modules \
  --overwrite \
  -o /build \
  --abi \
  /src/contracts/test.sol

Remarks:

  • here used hardcoded version of compiler - 0.8.3; consider use the latest stable version - ethereum/solc:stable
  • compiler provides the next output formats:
  --ast-compact-json   AST of all source files in a compact JSON format.
  --asm                EVM assembly of the contracts.
  --asm-json           EVM assembly of the contracts in JSON format.
  --opcodes            Opcodes of the contracts.
  --bin                Binary of the contracts in hex.
  --bin-runtime        Binary of the runtime part of the contracts in hex.
  --abi                ABI specification of the contracts.
  --ir                 Intermediate Representation (IR) of all contracts
                       (EXPERIMENTAL).
  --ir-optimized       Optimized intermediate Representation (IR) of all
                       contracts (EXPERIMENTAL).
  --ewasm              Ewasm text representation of all contracts
                       (EXPERIMENTAL).
  --hashes             Function signature hashes of the contracts.
  --userdoc            Natspec user documentation of all contracts.
  --devdoc             Natspec developer documentation of all contracts.
  --metadata           Combined Metadata JSON whose Swarm hash is stored
                       on-chain.
  --storage-layout     Slots, offsets and types of the contract's state
                       variables.

To avoid import errors like " File outside of allowed directories" or "File not found" define the 'strict' path to file:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;

import "../node_modules/@openzeppelin/contracts/access/Ownable.sol";

contract Hello is Ownable { .. }


To get help run this command:

docker run ethereum/solc:0.8.3 --help

.. --base-path path Use the given path as the root of the source tree instead of the root of the filesystem. --allow-paths path(s) Allow a given path for imports. A list of paths can be supplied by separating them with a comma. --ignore-missing Ignore missing files. ..

vladimir
  • 228
  • 1
  • 5
  • 1
    This should be the accepted answer. Why complicate things with truffle (other dependencies/libraries) instead of using the solc compiler directly (within the official docker container) – scibuff Feb 21 '22 at 21:54
2

I wrote a simple tool that does exactly what @salanfe suggested. It's handy when you have multiple contract definitions:

# Install it from npm
$ npm install -g truffle-export-abi

# Run it in your truffle project
$ truffle-export-abi
ABI extracted and output file wrote to: build/ABI.json

Github repo: https://github.com/maxme/truffle-export-abi

Maxime
  • 181
  • 1
  • 4
0

For JS

> truffle compile

Then on the js

import ERC20Contract from '../build/contracts/ERC20';
const ERC20Token = new web3.eth.Contract(ERC20Contract.abi, contractAddress)