9

Remix makes it so easy to get the json abi code.

Does anyone have any recommendation on how best to get he json abi code after successfully deploying a contract using truffle without having to leverage Remix?

Thank you in advance!

5

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.

1

@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.

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.