I am trying to understand how artifacts.require should be used. I've seen the standard paragraph describing it as being for migrations and testing. From this I infer that the globally scoped artifacts with its method require are automatically defined by the truffle executable tool when doing migrations or running tests. However, I am working with some code that uses artifacts.require outside the context of any migrations or tests, rather, this code just needs to do the usual at and new. However, in this context, the object artifacts is not defined.
Do I have the right picture here? Is this an appropriate use of artifacts.require? If so, what must be done to make it be defined outside of migrations and testing?
Thanks for any suggestions!
1 Answer
artifacts.require really isn't meant to be used outside of a test. this is where it is defined:
when in production code you should load the compiled contract into your application using truffle-contract
here is a short example (from and see )
var contract = require("truffle-contract");
var contractJson = require("example-truffle-library/build/contracts/SimpleNameRegistry.json");
var SimpleNameRegistry = contract(contractJson);
SimpleNameRegistry .deployed() .then(function(instance) { return instance.setRegistry(address); }) .then(function(result) { // If this callback is called, the transaction was successfully processed. alert("Transaction successful!") }) .catch(function(e) { // There was an error! Handle it. }); 1