0

I'm using Truffle, Solidity, Web3.js, and Ganache. Whenever I deploy a smart contract, I have to copy and paste the new ABI & address of the contract. How can I dynamically change the value so that every time a new smart contract is deployed it saves me from having to copy and paste these new values into my .js file?

7
  • Truffle generates a .json file when a contract is deployed. From that file you can extract the contract abi and the address deployed, see this question for location ethereum.stackexchange.com/questions/30457/….
    – Ismael
    Jun 1 at 5:32
  • Thank you for the answer. I'm aware that I can manually extract it this way. But what I want to know is if there's a way to automate it so that whenever a new contract is deployed, I won't have to go into the .json file and copy and paste it every time. Is there a way I can import it with code?
    – Adrian D.
    Jun 1 at 20:01
  • It is possible but requires some work. In the backend from javascript you can require the JSON file and then use TruffleContract to get the deployed instance. From client side either make the JSON available from an endpoint or from the migrations scripts trigger a rebuild of your source.
    – Ismael
    Jun 1 at 20:24
  • Are you able to answer the question with an example of you doing that? It would help me out a lot and anyone else struggling with this. :)
    – Adrian D.
    Jun 2 at 1:54
  • Better, Truffle has several examples projects, for example github.com/truffle-box/webpack-box. If you look at client code index.js it imports the contract artifact import metaCoinArtifact from "../../build/contracts/MetaCoin.json" directly from the truffle output.
    – Ismael
    Jun 2 at 3:08
0

artifacts.require("<contract_name>") will fetch the contracts json file(which is generated in build folder after compiling every time) within truffle project.

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.