我想了解应该如何使用artifacts.require
。我已经看到标准段落将其描述为迁移和测试。据此我推断,在进行迁移或运行测试时,松散可执行工具会自动定义全局范围artifacts
及其方法require
。但是,我正在处理在任何迁移或测试的上下文之外使用artifacts.require
的一些代码,相反,此代码只需要执行通常的at
和new
。但是,在此上下文中,未定义对象artifacts
。
我在这里有合适的照片吗?这是artifacts.require
的恰当用途吗?如果是这样,必须采取哪些措施才能在迁移和测试之外进行定义?
感谢您的任何建议!
答案 0 :(得分:9)
artifacts.require确实不是要在测试之外使用。这是定义的地方:https://github.com/trufflesuite/truffle-core/blob/3e96337c32aaae6885105661fd1a6792ab4494bf/lib/test.js#L240
在生产代码中,您应该使用truffle-contract https://github.com/trufflesuite/truffle-contract
将已编译的合同加载到您的应用程序中这是一个简短的例子(来自http://truffleframework.com/docs/getting_started/packages-npm#within-javascript-code并看到 http://truffleframework.com/docs/getting_started/contracts#making-a-transaction)
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.
});