1

I'm still a brand new student of Ethereum, and I'm trying to figure out How to instantiate a Contract from a Node.js instance using Web3. I'm also using the Truffle Suite but for development/Testing purposes Only.

What I need to know is how can I get a Contract's ABI into a Node.js standalone Project?

I know I could just Upload it as a JSON file, or I could also use the solc library to compile the .sol Contract File... the latter option appears to be a fine choice as long as I only need to do it once (like when the server starts/re-starts for example)

I'm wondering what is the Best Practice for doing this? I can't find a concrete answer regarding this matter.

Thank you.

1 Answer 1

1

I don’t think there is a single accepted and best practice way to do this and depends on your preference and your application.

As you pointed out, truffle saves json files in the build folder and you can access the abi from there. This is used in various templates (truffle box) as starting points for projects.

For a DApp I have in production, I have actually saved contract details and abi’s into a database. This is because my DApp has multiple contracts which can change, so by using a database, I can add/remove/modify/access multiple contracts more easily. But this is specific to my use case and I have found this to be pretty efficient.

4
  • Interesting. Thank you for your answer. I have to point out that for my project I intend to use a Node.js instance independent of Truffle, and I will just expose some API endpoints using express, so it's actually an Express API DApp? And the more decoupled it is from the actual Contract code the better, because like I said before I intend to deploy my Contract(s) using a separate Truffle project. Thanks for taking some of your valuable time to answer!
    – jlstr
    Commented Dec 27, 2017 at 2:10
  • 1
    My DApp also doesn't use truffle, it's just node/express as well. So I don't even have the build folder that truffle has of json files. If you only have a few (say 1 or two contracts), it's probably simplest to import the json or maybe even just have the abi object in your javascript files as an object const contractAbi = [{ your abi }];.
    – carlolm
    Commented Dec 27, 2017 at 2:17
  • Got it. How about having it in a JSON file? and then using something like fs.readFileSyncto get it? I had loose hopes that this kind of thing was already automated by the community, but I'm cool with it now given your answer. Thank you very very much, sir!
    – jlstr
    Commented Dec 27, 2017 at 2:23
  • 1
    Yes that also works. Actually one of my microservices, I do that also. Also I didn't even need to use fs. I just imported it directly and it was accessible: const contractAbi = require('./contractAbi.json'); Doing something like that worked for me.
    – carlolm
    Commented Dec 27, 2017 at 2:34

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.